author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 401 | ef01e0dccd63 |
child 1226 | eba24d3b9c0c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.management; |
|
27 |
||
28 |
import java.lang.management.*; |
|
29 |
import java.util.logging.LogManager; |
|
30 |
||
31 |
import javax.management.DynamicMBean; |
|
32 |
import javax.management.MBeanServer; |
|
33 |
import javax.management.MBeanServerFactory; |
|
34 |
import javax.management.MBeanInfo; |
|
35 |
import javax.management.NotificationEmitter; |
|
36 |
import javax.management.ObjectName; |
|
37 |
import javax.management.ObjectInstance; |
|
38 |
import javax.management.InstanceAlreadyExistsException; |
|
39 |
import javax.management.InstanceNotFoundException; |
|
40 |
import javax.management.MBeanRegistrationException; |
|
41 |
import javax.management.NotCompliantMBeanException; |
|
42 |
import javax.management.RuntimeOperationsException; |
|
43 |
import javax.management.StandardEmitterMBean; |
|
44 |
import javax.management.StandardMBean; |
|
45 |
import java.security.AccessController; |
|
46 |
import java.security.PrivilegedActionException; |
|
47 |
import java.security.PrivilegedExceptionAction; |
|
48 |
import sun.security.action.LoadLibraryAction; |
|
49 |
||
50 |
import java.util.ArrayList; |
|
51 |
import java.util.List; |
|
52 |
import java.util.HashMap; |
|
53 |
import java.util.Map; |
|
54 |
import java.util.Set; |
|
55 |
import java.util.Iterator; |
|
56 |
import java.util.ListIterator; |
|
57 |
import com.sun.management.OSMBeanFactory; |
|
58 |
import com.sun.management.HotSpotDiagnosticMXBean; |
|
59 |
||
60 |
import static java.lang.management.ManagementFactory.*; |
|
61 |
||
62 |
/** |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
63 |
* ManagementFactoryHelper provides static factory methods to create |
2 | 64 |
* instances of the management interface. |
65 |
*/ |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
66 |
public class ManagementFactoryHelper { |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
67 |
private ManagementFactoryHelper() {}; |
2 | 68 |
|
69 |
private static VMManagement jvm; |
|
70 |
||
71 |
private static boolean mbeansCreated = false; |
|
72 |
private static ClassLoadingImpl classMBean = null; |
|
73 |
private static MemoryImpl memoryMBean = null; |
|
74 |
private static ThreadImpl threadMBean = null; |
|
75 |
private static RuntimeImpl runtimeMBean = null; |
|
76 |
private static CompilationImpl compileMBean = null; |
|
77 |
private static OperatingSystemImpl osMBean = null; |
|
78 |
||
79 |
public static synchronized ClassLoadingMXBean getClassLoadingMXBean() { |
|
80 |
if (classMBean == null) { |
|
81 |
classMBean = new ClassLoadingImpl(jvm); |
|
82 |
} |
|
83 |
return classMBean; |
|
84 |
} |
|
85 |
||
86 |
public static synchronized MemoryMXBean getMemoryMXBean() { |
|
87 |
if (memoryMBean == null) { |
|
88 |
memoryMBean = new MemoryImpl(jvm); |
|
89 |
} |
|
90 |
return memoryMBean; |
|
91 |
} |
|
92 |
||
93 |
public static synchronized ThreadMXBean getThreadMXBean() { |
|
94 |
if (threadMBean == null) { |
|
95 |
threadMBean = new ThreadImpl(jvm); |
|
96 |
} |
|
97 |
return threadMBean; |
|
98 |
} |
|
99 |
||
100 |
public static synchronized RuntimeMXBean getRuntimeMXBean() { |
|
101 |
if (runtimeMBean == null) { |
|
102 |
runtimeMBean = new RuntimeImpl(jvm); |
|
103 |
} |
|
104 |
return runtimeMBean; |
|
105 |
} |
|
106 |
||
107 |
public static synchronized CompilationMXBean getCompilationMXBean() { |
|
108 |
if (compileMBean == null && jvm.getCompilerName() != null) { |
|
109 |
compileMBean = new CompilationImpl(jvm); |
|
110 |
} |
|
111 |
return compileMBean; |
|
112 |
} |
|
113 |
||
114 |
public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() { |
|
115 |
if (osMBean == null) { |
|
116 |
osMBean = (OperatingSystemImpl) |
|
117 |
OSMBeanFactory.getOperatingSystemMXBean(jvm); |
|
118 |
} |
|
119 |
return osMBean; |
|
120 |
} |
|
121 |
||
122 |
public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() { |
|
123 |
MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools(); |
|
124 |
List<MemoryPoolMXBean> list = new ArrayList<MemoryPoolMXBean>(pools.length); |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
125 |
for (MemoryPoolMXBean p : pools) { |
2 | 126 |
list.add(p); |
127 |
} |
|
128 |
return list; |
|
129 |
} |
|
130 |
||
131 |
public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() { |
|
132 |
MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers(); |
|
133 |
List<MemoryManagerMXBean> result = new ArrayList<MemoryManagerMXBean>(mgrs.length); |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
134 |
for (MemoryManagerMXBean m : mgrs) { |
2 | 135 |
result.add(m); |
136 |
} |
|
137 |
return result; |
|
138 |
} |
|
139 |
||
140 |
public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() { |
|
141 |
MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers(); |
|
142 |
List<GarbageCollectorMXBean> result = new ArrayList<GarbageCollectorMXBean>(mgrs.length); |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
143 |
for (MemoryManagerMXBean m : mgrs) { |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
144 |
if (GarbageCollectorMXBean.class.isInstance(m)) { |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
145 |
result.add(GarbageCollectorMXBean.class.cast(m)); |
2 | 146 |
} |
147 |
} |
|
148 |
return result; |
|
149 |
} |
|
150 |
||
151 |
private static HotSpotDiagnostic hsDiagMBean = null; |
|
152 |
private static HotspotRuntime hsRuntimeMBean = null; |
|
153 |
private static HotspotClassLoading hsClassMBean = null; |
|
154 |
private static HotspotThread hsThreadMBean = null; |
|
155 |
private static HotspotCompilation hsCompileMBean = null; |
|
156 |
private static HotspotMemory hsMemoryMBean = null; |
|
157 |
||
158 |
public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() { |
|
159 |
if (hsDiagMBean == null) { |
|
160 |
hsDiagMBean = new HotSpotDiagnostic(); |
|
161 |
} |
|
162 |
return hsDiagMBean; |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
||
167 |
/** |
|
168 |
* This method is for testing only. |
|
169 |
*/ |
|
170 |
public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() { |
|
171 |
if (hsRuntimeMBean == null) { |
|
172 |
hsRuntimeMBean = new HotspotRuntime(jvm); |
|
173 |
} |
|
174 |
return hsRuntimeMBean; |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* This method is for testing only. |
|
179 |
*/ |
|
180 |
public static synchronized HotspotClassLoadingMBean getHotspotClassLoadingMBean() { |
|
181 |
if (hsClassMBean == null) { |
|
182 |
hsClassMBean = new HotspotClassLoading(jvm); |
|
183 |
} |
|
184 |
return hsClassMBean; |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* This method is for testing only. |
|
189 |
*/ |
|
190 |
public static synchronized HotspotThreadMBean getHotspotThreadMBean() { |
|
191 |
if (hsThreadMBean == null) { |
|
192 |
hsThreadMBean = new HotspotThread(jvm); |
|
193 |
} |
|
194 |
return hsThreadMBean; |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* This method is for testing only. |
|
199 |
*/ |
|
200 |
public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() { |
|
201 |
if (hsMemoryMBean == null) { |
|
202 |
hsMemoryMBean = new HotspotMemory(jvm); |
|
203 |
} |
|
204 |
return hsMemoryMBean; |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* This method is for testing only. |
|
209 |
*/ |
|
210 |
public static synchronized HotspotCompilationMBean getHotspotCompilationMBean() { |
|
211 |
if (hsCompileMBean == null) { |
|
212 |
hsCompileMBean = new HotspotCompilation(jvm); |
|
213 |
} |
|
214 |
return hsCompileMBean; |
|
215 |
} |
|
216 |
||
217 |
/** |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
218 |
* Registers a given MBean if not registered in the MBeanServer; |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
219 |
* otherwise, just return. |
2 | 220 |
*/ |
221 |
private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) { |
|
222 |
try { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
223 |
final ObjectName objName = Util.newObjectName(mbeanName); |
2 | 224 |
|
225 |
// inner class requires these fields to be final |
|
226 |
final MBeanServer mbs0 = mbs; |
|
227 |
final Object mbean0 = mbean; |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
228 |
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
229 |
public Void run() throws MBeanRegistrationException, |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
230 |
NotCompliantMBeanException { |
2 | 231 |
try { |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
232 |
mbs0.registerMBean(mbean0, objName); |
2 | 233 |
return null; |
234 |
} catch (InstanceAlreadyExistsException e) { |
|
235 |
// if an instance with the object name exists in |
|
236 |
// the MBeanServer ignore the exception |
|
237 |
} |
|
238 |
return null; |
|
239 |
} |
|
240 |
}); |
|
241 |
} catch (PrivilegedActionException e) { |
|
242 |
throw Util.newException(e.getException()); |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
private final static String HOTSPOT_CLASS_LOADING_MBEAN_NAME = |
|
247 |
"sun.management:type=HotspotClassLoading"; |
|
248 |
||
249 |
private final static String HOTSPOT_COMPILATION_MBEAN_NAME = |
|
250 |
"sun.management:type=HotspotCompilation"; |
|
251 |
||
252 |
private final static String HOTSPOT_MEMORY_MBEAN_NAME = |
|
253 |
"sun.management:type=HotspotMemory"; |
|
254 |
||
255 |
private static final String HOTSPOT_RUNTIME_MBEAN_NAME = |
|
256 |
"sun.management:type=HotspotRuntime"; |
|
257 |
||
258 |
private final static String HOTSPOT_THREAD_MBEAN_NAME = |
|
259 |
"sun.management:type=HotspotThreading"; |
|
260 |
||
261 |
static void registerInternalMBeans(MBeanServer mbs) { |
|
262 |
// register all internal MBeans if not registered |
|
263 |
// No exception is thrown if a MBean with that object name |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
264 |
// already registered |
2 | 265 |
addMBean(mbs, getHotspotClassLoadingMBean(), |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
266 |
HOTSPOT_CLASS_LOADING_MBEAN_NAME); |
2 | 267 |
addMBean(mbs, getHotspotMemoryMBean(), |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
268 |
HOTSPOT_MEMORY_MBEAN_NAME); |
2 | 269 |
addMBean(mbs, getHotspotRuntimeMBean(), |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
270 |
HOTSPOT_RUNTIME_MBEAN_NAME); |
2 | 271 |
addMBean(mbs, getHotspotThreadMBean(), |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
272 |
HOTSPOT_THREAD_MBEAN_NAME); |
2 | 273 |
|
274 |
// CompilationMBean may not exist |
|
275 |
if (getCompilationMXBean() != null) { |
|
276 |
addMBean(mbs, getHotspotCompilationMBean(), |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
277 |
HOTSPOT_COMPILATION_MBEAN_NAME); |
2 | 278 |
} |
279 |
} |
|
280 |
||
281 |
private static void unregisterMBean(MBeanServer mbs, String mbeanName) { |
|
282 |
try { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
283 |
final ObjectName objName = Util.newObjectName(mbeanName); |
2 | 284 |
|
285 |
// inner class requires these fields to be final |
|
286 |
final MBeanServer mbs0 = mbs; |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
287 |
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
288 |
public Void run() throws MBeanRegistrationException, |
2 | 289 |
RuntimeOperationsException { |
290 |
try { |
|
291 |
mbs0.unregisterMBean(objName); |
|
292 |
} catch (InstanceNotFoundException e) { |
|
293 |
// ignore exception if not found |
|
294 |
} |
|
295 |
return null; |
|
296 |
} |
|
297 |
}); |
|
298 |
} catch (PrivilegedActionException e) { |
|
299 |
throw Util.newException(e.getException()); |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
static void unregisterInternalMBeans(MBeanServer mbs) { |
|
304 |
// unregister all internal MBeans |
|
305 |
unregisterMBean(mbs, HOTSPOT_CLASS_LOADING_MBEAN_NAME); |
|
306 |
unregisterMBean(mbs, HOTSPOT_MEMORY_MBEAN_NAME); |
|
307 |
unregisterMBean(mbs, HOTSPOT_RUNTIME_MBEAN_NAME); |
|
308 |
unregisterMBean(mbs, HOTSPOT_THREAD_MBEAN_NAME); |
|
309 |
||
310 |
// CompilationMBean may not exist |
|
311 |
if (getCompilationMXBean() != null) { |
|
312 |
unregisterMBean(mbs, HOTSPOT_COMPILATION_MBEAN_NAME); |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
static { |
|
317 |
AccessController.doPrivileged(new LoadLibraryAction("management")); |
|
318 |
jvm = new VMManagementImpl(); |
|
319 |
} |
|
320 |
||
321 |
public static boolean isThreadSuspended(int state) { |
|
322 |
return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0); |
|
323 |
} |
|
324 |
||
325 |
public static boolean isThreadRunningNative(int state) { |
|
326 |
return ((state & JMM_THREAD_STATE_FLAG_NATIVE) != 0); |
|
327 |
} |
|
328 |
||
329 |
public static Thread.State toThreadState(int state) { |
|
330 |
// suspended and native bits may be set in state |
|
331 |
int threadStatus = state & ~JMM_THREAD_STATE_FLAG_MASK; |
|
332 |
return sun.misc.VM.toThreadState(threadStatus); |
|
333 |
} |
|
334 |
||
335 |
// These values are defined in jmm.h |
|
336 |
private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000; |
|
337 |
private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000; |
|
338 |
private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000; |
|
339 |
||
340 |
} |