2
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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).
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.
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
26
package sun.management;
28
import java.lang.management.*;
30
import javax.management.InstanceAlreadyExistsException;
31
import javax.management.InstanceNotFoundException;
32
import javax.management.MBeanServer;
33
import javax.management.MBeanRegistrationException;
34
import javax.management.NotCompliantMBeanException;
35
import javax.management.ObjectName;
36
import javax.management.RuntimeOperationsException;
37
import java.security.AccessController;
38
import java.security.PrivilegedActionException;
39
import java.security.PrivilegedExceptionAction;
40
import sun.security.action.LoadLibraryAction;
42
import sun.util.logging.LoggingSupport;
44
import java.util.ArrayList;
45
import java.util.Collections;
46
import java.util.List;
47
import com.sun.management.OSMBeanFactory;
49
import static java.lang.management.ManagementFactory.*;
52
* ManagementFactoryHelper provides static factory methods to create
53
* instances of the management interface.
55
public class ManagementFactoryHelper {
56
private ManagementFactoryHelper() {};
58
private static VMManagement jvm;
60
private static ClassLoadingImpl classMBean = null;
61
private static MemoryImpl memoryMBean = null;
62
private static ThreadImpl threadMBean = null;
63
private static RuntimeImpl runtimeMBean = null;
64
private static CompilationImpl compileMBean = null;
65
private static OperatingSystemImpl osMBean = null;
67
public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {
68
if (classMBean == null) {
69
classMBean = new ClassLoadingImpl(jvm);
74
public static synchronized MemoryMXBean getMemoryMXBean() {
75
if (memoryMBean == null) {
76
memoryMBean = new MemoryImpl(jvm);
81
public static synchronized ThreadMXBean getThreadMXBean() {
82
if (threadMBean == null) {
83
threadMBean = new ThreadImpl(jvm);
88
public static synchronized RuntimeMXBean getRuntimeMXBean() {
89
if (runtimeMBean == null) {
90
runtimeMBean = new RuntimeImpl(jvm);
95
public static synchronized CompilationMXBean getCompilationMXBean() {
96
if (compileMBean == null && jvm.getCompilerName() != null) {
97
compileMBean = new CompilationImpl(jvm);
102
public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {
103
if (osMBean == null) {
104
osMBean = (OperatingSystemImpl)
105
OSMBeanFactory.getOperatingSystemMXBean(jvm);
110
public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
111
MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools();
112
List<MemoryPoolMXBean> list = new ArrayList<MemoryPoolMXBean>(pools.length);
113
for (MemoryPoolMXBean p : pools) {
119
public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
120
MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers();
121
List<MemoryManagerMXBean> result = new ArrayList<MemoryManagerMXBean>(mgrs.length);
122
for (MemoryManagerMXBean m : mgrs) {
128
public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
129
MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers();
130
List<GarbageCollectorMXBean> result = new ArrayList<GarbageCollectorMXBean>(mgrs.length);
131
for (MemoryManagerMXBean m : mgrs) {
132
if (GarbageCollectorMXBean.class.isInstance(m)) {
133
result.add(GarbageCollectorMXBean.class.cast(m));
139
public static PlatformLoggingMXBean getPlatformLoggingMXBean() {
140
if (LoggingSupport.isAvailable()) {
141
return PlatformLoggingImpl.instance;
147
// The logging MXBean object is an instance of
148
// PlatformLoggingMXBean and java.util.logging.LoggingMXBean
149
// but it can't directly implement two MXBean interfaces
150
// as a compliant MXBean implements exactly one MXBean interface,
151
// or if it implements one interface that is a subinterface of
152
// all the others; otherwise, it is a non-compliant MXBean
153
// and MBeanServer will throw NotCompliantMBeanException.
154
// See the Definition of an MXBean section in javax.management.MXBean spec.
156
// To create a compliant logging MXBean, define a LoggingMXBean interface
157
// that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean
158
interface LoggingMXBean
159
extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean {
162
static class PlatformLoggingImpl implements LoggingMXBean
164
final static PlatformLoggingMXBean instance = new PlatformLoggingImpl();
165
final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging";
167
private volatile ObjectName objname; // created lazily
169
public ObjectName getObjectName() {
170
ObjectName result = objname;
171
if (result == null) {
172
synchronized (this) {
174
if (result == null) {
175
result = Util.newObjectName(LOGGING_MXBEAN_NAME);
184
public java.util.List<String> getLoggerNames() {
185
return LoggingSupport.getLoggerNames();
189
public String getLoggerLevel(String loggerName) {
190
return LoggingSupport.getLoggerLevel(loggerName);
194
public void setLoggerLevel(String loggerName, String levelName) {
195
LoggingSupport.setLoggerLevel(loggerName, levelName);
199
public String getParentLoggerName(String loggerName) {
200
return LoggingSupport.getParentLoggerName(loggerName);
204
private static List<BufferPoolMXBean> bufferPools = null;
205
public static synchronized List<BufferPoolMXBean> getBufferPoolMXBeans() {
206
if (bufferPools == null) {
207
bufferPools = new ArrayList<>(2);
208
bufferPools.add(createBufferPoolMXBean(sun.misc.SharedSecrets.getJavaNioAccess()
209
.getDirectBufferPool()));
210
bufferPools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl
211
.getMappedBufferPool()));
216
private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";
219
* Creates management interface for the given buffer pool.
221
private static BufferPoolMXBean
222
createBufferPoolMXBean(final sun.misc.JavaNioAccess.BufferPool pool)
224
return new BufferPoolMXBean() {
225
private volatile ObjectName objname; // created lazily
227
public ObjectName getObjectName() {
228
ObjectName result = objname;
229
if (result == null) {
230
synchronized (this) {
232
if (result == null) {
233
result = Util.newObjectName(BUFFER_POOL_MXBEAN_NAME +
234
",name=" + pool.getName());
242
public String getName() {
243
return pool.getName();
246
public long getCount() {
247
return pool.getCount();
250
public long getTotalCapacity() {
251
return pool.getTotalCapacity();
254
public long getMemoryUsed() {
255
return pool.getMemoryUsed();
261
* Registers a given MBean if not registered in the MBeanServer;
262
* otherwise, just return.
264
private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {
266
final ObjectName objName = Util.newObjectName(mbeanName);
268
// inner class requires these fields to be final
269
final MBeanServer mbs0 = mbs;
270
final Object mbean0 = mbean;
271
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
272
public Void run() throws MBeanRegistrationException,
273
NotCompliantMBeanException {
275
mbs0.registerMBean(mbean0, objName);
277
} catch (InstanceAlreadyExistsException e) {
278
// if an instance with the object name exists in
279
// the MBeanServer ignore the exception
284
} catch (PrivilegedActionException e) {
285
throw Util.newException(e.getException());
289
static void registerInternalMBeans(MBeanServer mbs) {
292
static void unregisterInternalMBeans(MBeanServer mbs) {
296
jvm = new VMManagementImpl();
299
public static boolean isThreadSuspended(int state) {
300
return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);
303
public static boolean isThreadRunningNative(int state) {
304
return ((state & JMM_THREAD_STATE_FLAG_NATIVE) != 0);
307
public static Thread.State toThreadState(int state) {
308
// suspended and native bits may be set in state
309
int threadStatus = state & ~JMM_THREAD_STATE_FLAG_MASK;
310
return sun.misc.VM.toThreadState(threadStatus);
313
// These values are defined in jmm.h
314
private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;
315
private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
316
private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;