0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.management;
0N/A
0N/Aimport java.lang.management.*;
0N/A
0N/Aimport javax.management.InstanceAlreadyExistsException;
0N/Aimport javax.management.InstanceNotFoundException;
3887N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanRegistrationException;
0N/Aimport javax.management.NotCompliantMBeanException;
3887N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.RuntimeOperationsException;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedActionException;
0N/Aimport java.security.PrivilegedExceptionAction;
0N/Aimport sun.security.action.LoadLibraryAction;
0N/A
1806N/Aimport sun.util.logging.LoggingSupport;
1806N/A
0N/Aimport java.util.ArrayList;
1806N/Aimport java.util.Collections;
0N/Aimport java.util.List;
0N/Aimport com.sun.management.OSMBeanFactory;
0N/Aimport com.sun.management.HotSpotDiagnosticMXBean;
0N/A
0N/Aimport static java.lang.management.ManagementFactory.*;
0N/A
0N/A/**
178N/A * ManagementFactoryHelper provides static factory methods to create
0N/A * instances of the management interface.
0N/A */
178N/Apublic class ManagementFactoryHelper {
178N/A private ManagementFactoryHelper() {};
0N/A
0N/A private static VMManagement jvm;
0N/A
0N/A private static ClassLoadingImpl classMBean = null;
0N/A private static MemoryImpl memoryMBean = null;
0N/A private static ThreadImpl threadMBean = null;
0N/A private static RuntimeImpl runtimeMBean = null;
0N/A private static CompilationImpl compileMBean = null;
0N/A private static OperatingSystemImpl osMBean = null;
0N/A
0N/A public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {
0N/A if (classMBean == null) {
0N/A classMBean = new ClassLoadingImpl(jvm);
0N/A }
0N/A return classMBean;
0N/A }
0N/A
0N/A public static synchronized MemoryMXBean getMemoryMXBean() {
0N/A if (memoryMBean == null) {
0N/A memoryMBean = new MemoryImpl(jvm);
0N/A }
0N/A return memoryMBean;
0N/A }
0N/A
0N/A public static synchronized ThreadMXBean getThreadMXBean() {
0N/A if (threadMBean == null) {
0N/A threadMBean = new ThreadImpl(jvm);
0N/A }
0N/A return threadMBean;
0N/A }
0N/A
0N/A public static synchronized RuntimeMXBean getRuntimeMXBean() {
0N/A if (runtimeMBean == null) {
0N/A runtimeMBean = new RuntimeImpl(jvm);
0N/A }
0N/A return runtimeMBean;
0N/A }
0N/A
0N/A public static synchronized CompilationMXBean getCompilationMXBean() {
0N/A if (compileMBean == null && jvm.getCompilerName() != null) {
0N/A compileMBean = new CompilationImpl(jvm);
0N/A }
0N/A return compileMBean;
0N/A }
0N/A
0N/A public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {
0N/A if (osMBean == null) {
0N/A osMBean = (OperatingSystemImpl)
0N/A OSMBeanFactory.getOperatingSystemMXBean(jvm);
0N/A }
0N/A return osMBean;
0N/A }
0N/A
0N/A public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
0N/A MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools();
0N/A List<MemoryPoolMXBean> list = new ArrayList<MemoryPoolMXBean>(pools.length);
178N/A for (MemoryPoolMXBean p : pools) {
0N/A list.add(p);
0N/A }
0N/A return list;
0N/A }
0N/A
0N/A public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
0N/A MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers();
0N/A List<MemoryManagerMXBean> result = new ArrayList<MemoryManagerMXBean>(mgrs.length);
178N/A for (MemoryManagerMXBean m : mgrs) {
0N/A result.add(m);
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
0N/A MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers();
0N/A List<GarbageCollectorMXBean> result = new ArrayList<GarbageCollectorMXBean>(mgrs.length);
178N/A for (MemoryManagerMXBean m : mgrs) {
178N/A if (GarbageCollectorMXBean.class.isInstance(m)) {
178N/A result.add(GarbageCollectorMXBean.class.cast(m));
0N/A }
0N/A }
0N/A return result;
0N/A }
0N/A
3887N/A public static PlatformLoggingMXBean getPlatformLoggingMXBean() {
1806N/A if (LoggingSupport.isAvailable()) {
3887N/A return PlatformLoggingImpl.instance;
1806N/A } else {
3887N/A return null;
1806N/A }
1806N/A }
1806N/A
3887N/A // The logging MXBean object is an instance of
3887N/A // PlatformLoggingMXBean and java.util.logging.LoggingMXBean
3887N/A // but it can't directly implement two MXBean interfaces
3887N/A // as a compliant MXBean implements exactly one MXBean interface,
3887N/A // or if it implements one interface that is a subinterface of
3887N/A // all the others; otherwise, it is a non-compliant MXBean
3887N/A // and MBeanServer will throw NotCompliantMBeanException.
3887N/A // See the Definition of an MXBean section in javax.management.MXBean spec.
3887N/A //
3887N/A // To create a compliant logging MXBean, define a LoggingMXBean interface
3887N/A // that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean
3887N/A interface LoggingMXBean
3887N/A extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean {
3887N/A }
3887N/A
3887N/A static class PlatformLoggingImpl implements LoggingMXBean
3887N/A {
3887N/A final static PlatformLoggingMXBean instance = new PlatformLoggingImpl();
3887N/A final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging";
3887N/A
3887N/A private volatile ObjectName objname; // created lazily
3887N/A @Override
3887N/A public ObjectName getObjectName() {
3887N/A ObjectName result = objname;
3887N/A if (result == null) {
3887N/A synchronized (this) {
4364N/A result = objname;
4364N/A if (result == null) {
3887N/A result = Util.newObjectName(LOGGING_MXBEAN_NAME);
3887N/A objname = result;
1806N/A }
1806N/A }
1806N/A }
3887N/A return result;
3887N/A }
1806N/A
3887N/A @Override
3887N/A public java.util.List<String> getLoggerNames() {
3887N/A return LoggingSupport.getLoggerNames();
3887N/A }
1806N/A
3887N/A @Override
3887N/A public String getLoggerLevel(String loggerName) {
3887N/A return LoggingSupport.getLoggerLevel(loggerName);
3887N/A }
1806N/A
3887N/A @Override
3887N/A public void setLoggerLevel(String loggerName, String levelName) {
3887N/A LoggingSupport.setLoggerLevel(loggerName, levelName);
3887N/A }
1806N/A
3887N/A @Override
3887N/A public String getParentLoggerName(String loggerName) {
3887N/A return LoggingSupport.getParentLoggerName(loggerName);
3887N/A }
1806N/A }
1806N/A
3887N/A private static List<BufferPoolMXBean> bufferPools = null;
3887N/A public static synchronized List<BufferPoolMXBean> getBufferPoolMXBeans() {
3887N/A if (bufferPools == null) {
3887N/A bufferPools = new ArrayList<>(2);
3887N/A bufferPools.add(createBufferPoolMXBean(sun.misc.SharedSecrets.getJavaNioAccess()
3887N/A .getDirectBufferPool()));
3887N/A bufferPools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl
3887N/A .getMappedBufferPool()));
3887N/A }
3887N/A return bufferPools;
1320N/A }
1320N/A
1320N/A private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";
1320N/A
1320N/A /**
1320N/A * Creates management interface for the given buffer pool.
1320N/A */
1320N/A private static BufferPoolMXBean
1320N/A createBufferPoolMXBean(final sun.misc.JavaNioAccess.BufferPool pool)
1320N/A {
1320N/A return new BufferPoolMXBean() {
1320N/A private volatile ObjectName objname; // created lazily
1320N/A @Override
1320N/A public ObjectName getObjectName() {
1320N/A ObjectName result = objname;
1320N/A if (result == null) {
1320N/A synchronized (this) {
4364N/A result = objname;
4364N/A if (result == null) {
1790N/A result = Util.newObjectName(BUFFER_POOL_MXBEAN_NAME +
1320N/A ",name=" + pool.getName());
1320N/A objname = result;
1320N/A }
1320N/A }
1320N/A }
1320N/A return result;
1320N/A }
1320N/A @Override
1320N/A public String getName() {
1320N/A return pool.getName();
1320N/A }
1320N/A @Override
1320N/A public long getCount() {
1320N/A return pool.getCount();
1320N/A }
1320N/A @Override
1320N/A public long getTotalCapacity() {
1320N/A return pool.getTotalCapacity();
1320N/A }
1320N/A @Override
1320N/A public long getMemoryUsed() {
1320N/A return pool.getMemoryUsed();
1320N/A }
1320N/A };
1320N/A }
1320N/A
0N/A private static HotSpotDiagnostic hsDiagMBean = null;
0N/A private static HotspotRuntime hsRuntimeMBean = null;
0N/A private static HotspotClassLoading hsClassMBean = null;
0N/A private static HotspotThread hsThreadMBean = null;
0N/A private static HotspotCompilation hsCompileMBean = null;
0N/A private static HotspotMemory hsMemoryMBean = null;
0N/A
0N/A public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() {
0N/A if (hsDiagMBean == null) {
0N/A hsDiagMBean = new HotSpotDiagnostic();
0N/A }
0N/A return hsDiagMBean;
0N/A }
0N/A
0N/A /**
0N/A * This method is for testing only.
0N/A */
0N/A public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
0N/A if (hsRuntimeMBean == null) {
0N/A hsRuntimeMBean = new HotspotRuntime(jvm);
0N/A }
0N/A return hsRuntimeMBean;
0N/A }
0N/A
0N/A /**
0N/A * This method is for testing only.
0N/A */
0N/A public static synchronized HotspotClassLoadingMBean getHotspotClassLoadingMBean() {
0N/A if (hsClassMBean == null) {
0N/A hsClassMBean = new HotspotClassLoading(jvm);
0N/A }
0N/A return hsClassMBean;
0N/A }
0N/A
0N/A /**
0N/A * This method is for testing only.
0N/A */
0N/A public static synchronized HotspotThreadMBean getHotspotThreadMBean() {
0N/A if (hsThreadMBean == null) {
0N/A hsThreadMBean = new HotspotThread(jvm);
0N/A }
0N/A return hsThreadMBean;
0N/A }
0N/A
0N/A /**
0N/A * This method is for testing only.
0N/A */
0N/A public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {
0N/A if (hsMemoryMBean == null) {
0N/A hsMemoryMBean = new HotspotMemory(jvm);
0N/A }
0N/A return hsMemoryMBean;
0N/A }
0N/A
0N/A /**
0N/A * This method is for testing only.
0N/A */
0N/A public static synchronized HotspotCompilationMBean getHotspotCompilationMBean() {
0N/A if (hsCompileMBean == null) {
0N/A hsCompileMBean = new HotspotCompilation(jvm);
0N/A }
0N/A return hsCompileMBean;
0N/A }
0N/A
0N/A /**
178N/A * Registers a given MBean if not registered in the MBeanServer;
178N/A * otherwise, just return.
0N/A */
0N/A private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {
0N/A try {
1790N/A final ObjectName objName = Util.newObjectName(mbeanName);
0N/A
0N/A // inner class requires these fields to be final
0N/A final MBeanServer mbs0 = mbs;
0N/A final Object mbean0 = mbean;
178N/A AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
178N/A public Void run() throws MBeanRegistrationException,
178N/A NotCompliantMBeanException {
0N/A try {
178N/A mbs0.registerMBean(mbean0, objName);
0N/A return null;
0N/A } catch (InstanceAlreadyExistsException e) {
0N/A // if an instance with the object name exists in
0N/A // the MBeanServer ignore the exception
0N/A }
0N/A return null;
0N/A }
0N/A });
0N/A } catch (PrivilegedActionException e) {
0N/A throw Util.newException(e.getException());
0N/A }
0N/A }
0N/A
0N/A private final static String HOTSPOT_CLASS_LOADING_MBEAN_NAME =
0N/A "sun.management:type=HotspotClassLoading";
0N/A
0N/A private final static String HOTSPOT_COMPILATION_MBEAN_NAME =
0N/A "sun.management:type=HotspotCompilation";
0N/A
0N/A private final static String HOTSPOT_MEMORY_MBEAN_NAME =
0N/A "sun.management:type=HotspotMemory";
0N/A
0N/A private static final String HOTSPOT_RUNTIME_MBEAN_NAME =
0N/A "sun.management:type=HotspotRuntime";
0N/A
0N/A private final static String HOTSPOT_THREAD_MBEAN_NAME =
0N/A "sun.management:type=HotspotThreading";
0N/A
0N/A static void registerInternalMBeans(MBeanServer mbs) {
0N/A // register all internal MBeans if not registered
0N/A // No exception is thrown if a MBean with that object name
178N/A // already registered
0N/A addMBean(mbs, getHotspotClassLoadingMBean(),
178N/A HOTSPOT_CLASS_LOADING_MBEAN_NAME);
0N/A addMBean(mbs, getHotspotMemoryMBean(),
178N/A HOTSPOT_MEMORY_MBEAN_NAME);
0N/A addMBean(mbs, getHotspotRuntimeMBean(),
178N/A HOTSPOT_RUNTIME_MBEAN_NAME);
0N/A addMBean(mbs, getHotspotThreadMBean(),
178N/A HOTSPOT_THREAD_MBEAN_NAME);
0N/A
0N/A // CompilationMBean may not exist
0N/A if (getCompilationMXBean() != null) {
0N/A addMBean(mbs, getHotspotCompilationMBean(),
178N/A HOTSPOT_COMPILATION_MBEAN_NAME);
0N/A }
0N/A }
0N/A
0N/A private static void unregisterMBean(MBeanServer mbs, String mbeanName) {
0N/A try {
1790N/A final ObjectName objName = Util.newObjectName(mbeanName);
0N/A
0N/A // inner class requires these fields to be final
0N/A final MBeanServer mbs0 = mbs;
178N/A AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
178N/A public Void run() throws MBeanRegistrationException,
0N/A RuntimeOperationsException {
0N/A try {
0N/A mbs0.unregisterMBean(objName);
0N/A } catch (InstanceNotFoundException e) {
0N/A // ignore exception if not found
0N/A }
0N/A return null;
0N/A }
0N/A });
0N/A } catch (PrivilegedActionException e) {
0N/A throw Util.newException(e.getException());
0N/A }
0N/A }
0N/A
0N/A static void unregisterInternalMBeans(MBeanServer mbs) {
0N/A // unregister all internal MBeans
0N/A unregisterMBean(mbs, HOTSPOT_CLASS_LOADING_MBEAN_NAME);
0N/A unregisterMBean(mbs, HOTSPOT_MEMORY_MBEAN_NAME);
0N/A unregisterMBean(mbs, HOTSPOT_RUNTIME_MBEAN_NAME);
0N/A unregisterMBean(mbs, HOTSPOT_THREAD_MBEAN_NAME);
0N/A
0N/A // CompilationMBean may not exist
0N/A if (getCompilationMXBean() != null) {
0N/A unregisterMBean(mbs, HOTSPOT_COMPILATION_MBEAN_NAME);
0N/A }
0N/A }
0N/A
0N/A static {
0N/A AccessController.doPrivileged(new LoadLibraryAction("management"));
0N/A jvm = new VMManagementImpl();
0N/A }
0N/A
0N/A public static boolean isThreadSuspended(int state) {
0N/A return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);
0N/A }
0N/A
0N/A public static boolean isThreadRunningNative(int state) {
0N/A return ((state & JMM_THREAD_STATE_FLAG_NATIVE) != 0);
0N/A }
0N/A
0N/A public static Thread.State toThreadState(int state) {
0N/A // suspended and native bits may be set in state
0N/A int threadStatus = state & ~JMM_THREAD_STATE_FLAG_MASK;
0N/A return sun.misc.VM.toThreadState(threadStatus);
0N/A }
0N/A
0N/A // These values are defined in jmm.h
0N/A private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;
0N/A private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
0N/A private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;
0N/A
0N/A}