Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/ManagementFactoryHelper.java
38827 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import java.lang.management.*;2829import javax.management.DynamicMBean;30import javax.management.InstanceAlreadyExistsException;31import javax.management.InstanceNotFoundException;32import javax.management.MBeanServer;33import javax.management.MBeanRegistrationException;34import javax.management.NotCompliantMBeanException;35import javax.management.ObjectName;36import javax.management.RuntimeOperationsException;37import java.security.AccessController;38import java.security.PrivilegedActionException;39import java.security.PrivilegedExceptionAction;4041import sun.util.logging.LoggingSupport;4243import java.util.ArrayList;44import java.util.Collections;45import java.util.HashMap;46import java.util.List;47import com.sun.management.DiagnosticCommandMBean;48import com.sun.management.HotSpotDiagnosticMXBean;4950import static java.lang.management.ManagementFactory.*;5152/**53* ManagementFactoryHelper provides static factory methods to create54* instances of the management interface.55*/56public class ManagementFactoryHelper {57private ManagementFactoryHelper() {};5859private static VMManagement jvm;6061private static ClassLoadingImpl classMBean = null;62private static MemoryImpl memoryMBean = null;63private static ThreadImpl threadMBean = null;64private static RuntimeImpl runtimeMBean = null;65private static CompilationImpl compileMBean = null;66private static OperatingSystemImpl osMBean = null;6768public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {69if (classMBean == null) {70classMBean = new ClassLoadingImpl(jvm);71}72return classMBean;73}7475public static synchronized MemoryMXBean getMemoryMXBean() {76if (memoryMBean == null) {77memoryMBean = new MemoryImpl(jvm);78}79return memoryMBean;80}8182public static synchronized ThreadMXBean getThreadMXBean() {83if (threadMBean == null) {84threadMBean = new ThreadImpl(jvm);85}86return threadMBean;87}8889public static synchronized RuntimeMXBean getRuntimeMXBean() {90if (runtimeMBean == null) {91runtimeMBean = new RuntimeImpl(jvm);92}93return runtimeMBean;94}9596public static synchronized CompilationMXBean getCompilationMXBean() {97if (compileMBean == null && jvm.getCompilerName() != null) {98compileMBean = new CompilationImpl(jvm);99}100return compileMBean;101}102103public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {104if (osMBean == null) {105osMBean = new OperatingSystemImpl(jvm);106}107return osMBean;108}109110public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {111MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools();112List<MemoryPoolMXBean> list = new ArrayList<>(pools.length);113for (MemoryPoolMXBean p : pools) {114list.add(p);115}116return list;117}118119public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {120MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers();121List<MemoryManagerMXBean> result = new ArrayList<>(mgrs.length);122for (MemoryManagerMXBean m : mgrs) {123result.add(m);124}125return result;126}127128public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {129MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers();130List<GarbageCollectorMXBean> result = new ArrayList<>(mgrs.length);131for (MemoryManagerMXBean m : mgrs) {132if (GarbageCollectorMXBean.class.isInstance(m)) {133result.add(GarbageCollectorMXBean.class.cast(m));134}135}136return result;137}138139public static PlatformLoggingMXBean getPlatformLoggingMXBean() {140if (LoggingSupport.isAvailable()) {141return PlatformLoggingImpl.instance;142} else {143return null;144}145}146147/**148* The logging MXBean object is an instance of149* PlatformLoggingMXBean and java.util.logging.LoggingMXBean150* but it can't directly implement two MXBean interfaces151* as a compliant MXBean implements exactly one MXBean interface,152* or if it implements one interface that is a subinterface of153* all the others; otherwise, it is a non-compliant MXBean154* and MBeanServer will throw NotCompliantMBeanException.155* See the Definition of an MXBean section in javax.management.MXBean spec.156*157* To create a compliant logging MXBean, define a LoggingMXBean interface158* that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean159*/160public interface LoggingMXBean161extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean {162}163164static class PlatformLoggingImpl implements LoggingMXBean165{166final static PlatformLoggingMXBean instance = new PlatformLoggingImpl();167final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging";168169private volatile ObjectName objname; // created lazily170@Override171public ObjectName getObjectName() {172ObjectName result = objname;173if (result == null) {174synchronized (this) {175result = objname;176if (result == null) {177result = Util.newObjectName(LOGGING_MXBEAN_NAME);178objname = result;179}180}181}182return result;183}184185@Override186public java.util.List<String> getLoggerNames() {187return LoggingSupport.getLoggerNames();188}189190@Override191public String getLoggerLevel(String loggerName) {192return LoggingSupport.getLoggerLevel(loggerName);193}194195@Override196public void setLoggerLevel(String loggerName, String levelName) {197LoggingSupport.setLoggerLevel(loggerName, levelName);198}199200@Override201public String getParentLoggerName(String loggerName) {202return LoggingSupport.getParentLoggerName(loggerName);203}204}205206private static List<BufferPoolMXBean> bufferPools = null;207public static synchronized List<BufferPoolMXBean> getBufferPoolMXBeans() {208if (bufferPools == null) {209bufferPools = new ArrayList<>(2);210bufferPools.add(createBufferPoolMXBean(sun.misc.SharedSecrets.getJavaNioAccess()211.getDirectBufferPool()));212bufferPools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl213.getMappedBufferPool()));214}215return bufferPools;216}217218private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";219220/**221* Creates management interface for the given buffer pool.222*/223private static BufferPoolMXBean224createBufferPoolMXBean(final sun.misc.JavaNioAccess.BufferPool pool)225{226return new BufferPoolMXBean() {227private volatile ObjectName objname; // created lazily228@Override229public ObjectName getObjectName() {230ObjectName result = objname;231if (result == null) {232synchronized (this) {233result = objname;234if (result == null) {235result = Util.newObjectName(BUFFER_POOL_MXBEAN_NAME +236",name=" + pool.getName());237objname = result;238}239}240}241return result;242}243@Override244public String getName() {245return pool.getName();246}247@Override248public long getCount() {249return pool.getCount();250}251@Override252public long getTotalCapacity() {253return pool.getTotalCapacity();254}255@Override256public long getMemoryUsed() {257return pool.getMemoryUsed();258}259};260}261262private static HotSpotDiagnostic hsDiagMBean = null;263private static HotspotRuntime hsRuntimeMBean = null;264private static HotspotClassLoading hsClassMBean = null;265private static HotspotThread hsThreadMBean = null;266private static HotspotCompilation hsCompileMBean = null;267private static HotspotMemory hsMemoryMBean = null;268private static DiagnosticCommandImpl hsDiagCommandMBean = null;269270public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() {271if (hsDiagMBean == null) {272hsDiagMBean = new HotSpotDiagnostic();273}274return hsDiagMBean;275}276277/**278* This method is for testing only.279*/280public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {281if (hsRuntimeMBean == null) {282hsRuntimeMBean = new HotspotRuntime(jvm);283}284return hsRuntimeMBean;285}286287/**288* This method is for testing only.289*/290public static synchronized HotspotClassLoadingMBean getHotspotClassLoadingMBean() {291if (hsClassMBean == null) {292hsClassMBean = new HotspotClassLoading(jvm);293}294return hsClassMBean;295}296297/**298* This method is for testing only.299*/300public static synchronized HotspotThreadMBean getHotspotThreadMBean() {301if (hsThreadMBean == null) {302hsThreadMBean = new HotspotThread(jvm);303}304return hsThreadMBean;305}306307/**308* This method is for testing only.309*/310public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {311if (hsMemoryMBean == null) {312hsMemoryMBean = new HotspotMemory(jvm);313}314return hsMemoryMBean;315}316317public static synchronized DiagnosticCommandMBean getDiagnosticCommandMBean() {318// Remote Diagnostic Commands may not be supported319if (hsDiagCommandMBean == null && jvm.isRemoteDiagnosticCommandsSupported()) {320hsDiagCommandMBean = new DiagnosticCommandImpl(jvm);321}322return hsDiagCommandMBean;323}324325/**326* This method is for testing only.327*/328public static synchronized HotspotCompilationMBean getHotspotCompilationMBean() {329if (hsCompileMBean == null) {330hsCompileMBean = new HotspotCompilation(jvm);331}332return hsCompileMBean;333}334335/**336* Registers a given MBean if not registered in the MBeanServer;337* otherwise, just return.338*/339private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {340try {341final ObjectName objName = Util.newObjectName(mbeanName);342343// inner class requires these fields to be final344final MBeanServer mbs0 = mbs;345final Object mbean0 = mbean;346AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {347public Void run() throws MBeanRegistrationException,348NotCompliantMBeanException {349try {350mbs0.registerMBean(mbean0, objName);351return null;352} catch (InstanceAlreadyExistsException e) {353// if an instance with the object name exists in354// the MBeanServer ignore the exception355}356return null;357}358});359} catch (PrivilegedActionException e) {360throw Util.newException(e.getException());361}362}363364private final static String HOTSPOT_CLASS_LOADING_MBEAN_NAME =365"sun.management:type=HotspotClassLoading";366367private final static String HOTSPOT_COMPILATION_MBEAN_NAME =368"sun.management:type=HotspotCompilation";369370private final static String HOTSPOT_MEMORY_MBEAN_NAME =371"sun.management:type=HotspotMemory";372373private static final String HOTSPOT_RUNTIME_MBEAN_NAME =374"sun.management:type=HotspotRuntime";375376private final static String HOTSPOT_THREAD_MBEAN_NAME =377"sun.management:type=HotspotThreading";378379final static String HOTSPOT_DIAGNOSTIC_COMMAND_MBEAN_NAME =380"com.sun.management:type=DiagnosticCommand";381382public static HashMap<ObjectName, DynamicMBean> getPlatformDynamicMBeans() {383HashMap<ObjectName, DynamicMBean> map = new HashMap<>();384DiagnosticCommandMBean diagMBean = getDiagnosticCommandMBean();385if (diagMBean != null) {386map.put(Util.newObjectName(HOTSPOT_DIAGNOSTIC_COMMAND_MBEAN_NAME), diagMBean);387}388return map;389}390391static void registerInternalMBeans(MBeanServer mbs) {392// register all internal MBeans if not registered393// No exception is thrown if a MBean with that object name394// already registered395addMBean(mbs, getHotspotClassLoadingMBean(),396HOTSPOT_CLASS_LOADING_MBEAN_NAME);397addMBean(mbs, getHotspotMemoryMBean(),398HOTSPOT_MEMORY_MBEAN_NAME);399addMBean(mbs, getHotspotRuntimeMBean(),400HOTSPOT_RUNTIME_MBEAN_NAME);401addMBean(mbs, getHotspotThreadMBean(),402HOTSPOT_THREAD_MBEAN_NAME);403404// CompilationMBean may not exist405if (getCompilationMXBean() != null) {406addMBean(mbs, getHotspotCompilationMBean(),407HOTSPOT_COMPILATION_MBEAN_NAME);408}409}410411private static void unregisterMBean(MBeanServer mbs, String mbeanName) {412try {413final ObjectName objName = Util.newObjectName(mbeanName);414415// inner class requires these fields to be final416final MBeanServer mbs0 = mbs;417AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {418public Void run() throws MBeanRegistrationException,419RuntimeOperationsException {420try {421mbs0.unregisterMBean(objName);422} catch (InstanceNotFoundException e) {423// ignore exception if not found424}425return null;426}427});428} catch (PrivilegedActionException e) {429throw Util.newException(e.getException());430}431}432433static void unregisterInternalMBeans(MBeanServer mbs) {434// unregister all internal MBeans435unregisterMBean(mbs, HOTSPOT_CLASS_LOADING_MBEAN_NAME);436unregisterMBean(mbs, HOTSPOT_MEMORY_MBEAN_NAME);437unregisterMBean(mbs, HOTSPOT_RUNTIME_MBEAN_NAME);438unregisterMBean(mbs, HOTSPOT_THREAD_MBEAN_NAME);439440// CompilationMBean may not exist441if (getCompilationMXBean() != null) {442unregisterMBean(mbs, HOTSPOT_COMPILATION_MBEAN_NAME);443}444}445446static {447AccessController.doPrivileged(448new java.security.PrivilegedAction<Void>() {449public Void run() {450System.loadLibrary("management");451return null;452}453});454jvm = new VMManagementImpl();455}456457public static boolean isThreadSuspended(int state) {458return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);459}460461public static boolean isThreadRunningNative(int state) {462return ((state & JMM_THREAD_STATE_FLAG_NATIVE) != 0);463}464465public static Thread.State toThreadState(int state) {466// suspended and native bits may be set in state467int threadStatus = state & ~JMM_THREAD_STATE_FLAG_MASK;468return sun.misc.VM.toThreadState(threadStatus);469}470471// These values are defined in jmm.h472private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;473private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;474private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;475476}477478479