jdk/src/java.management/share/classes/sun/management/ManagementFactoryHelper.java
changeset 30355 e37c7eba132f
parent 28775 d786aae24263
child 32834 e1dca5fe4de3
equal deleted inserted replaced
30354:ca83b4cae363 30355:e37c7eba132f
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package sun.management;
    26 package sun.management;
    27 
    27 
    28 import java.lang.management.*;
    28 import java.lang.management.*;
    29 
       
    30 import javax.management.DynamicMBean;
       
    31 import javax.management.InstanceAlreadyExistsException;
    29 import javax.management.InstanceAlreadyExistsException;
    32 import javax.management.InstanceNotFoundException;
    30 import javax.management.InstanceNotFoundException;
    33 import javax.management.MBeanServer;
    31 import javax.management.MBeanServer;
    34 import javax.management.MBeanRegistrationException;
    32 import javax.management.MBeanRegistrationException;
    35 import javax.management.NotCompliantMBeanException;
    33 import javax.management.NotCompliantMBeanException;
    36 import javax.management.ObjectName;
    34 import javax.management.ObjectName;
    37 import javax.management.RuntimeOperationsException;
    35 import javax.management.RuntimeOperationsException;
    38 import java.security.AccessController;
    36 import java.security.AccessController;
    39 import java.security.PrivilegedActionException;
    37 import java.security.PrivilegedActionException;
    40 import java.security.PrivilegedExceptionAction;
    38 import java.security.PrivilegedExceptionAction;
    41 
       
    42 import sun.util.logging.LoggingSupport;
    39 import sun.util.logging.LoggingSupport;
    43 
       
    44 import java.util.ArrayList;
    40 import java.util.ArrayList;
    45 import java.util.HashMap;
       
    46 import java.util.List;
    41 import java.util.List;
    47 import com.sun.management.DiagnosticCommandMBean;
       
    48 import com.sun.management.HotSpotDiagnosticMXBean;
       
    49 
    42 
    50 /**
    43 /**
    51  * ManagementFactoryHelper provides static factory methods to create
    44  * ManagementFactoryHelper provides static factory methods to create
    52  * instances of the management interface.
    45  * instances of the management interface.
    53  */
    46  */
    54 public class ManagementFactoryHelper {
    47 public class ManagementFactoryHelper {
       
    48     static {
       
    49         // make sure that the management lib is loaded within
       
    50         // java.lang.management.ManagementFactory
       
    51         sun.misc.Unsafe.getUnsafe().ensureClassInitialized(ManagementFactory.class);
       
    52     }
       
    53 
       
    54     private static final VMManagement jvm = new VMManagementImpl();
       
    55 
    55     private ManagementFactoryHelper() {};
    56     private ManagementFactoryHelper() {};
    56 
    57 
    57     private static VMManagement jvm;
    58     public static VMManagement getVMManagement() {
       
    59         return jvm;
       
    60     }
    58 
    61 
    59     private static ClassLoadingImpl    classMBean = null;
    62     private static ClassLoadingImpl    classMBean = null;
    60     private static MemoryImpl          memoryMBean = null;
    63     private static MemoryImpl          memoryMBean = null;
    61     private static ThreadImpl          threadMBean = null;
    64     private static ThreadImpl          threadMBean = null;
    62     private static RuntimeImpl         runtimeMBean = null;
    65     private static RuntimeImpl         runtimeMBean = null;
    63     private static CompilationImpl     compileMBean = null;
    66     private static CompilationImpl     compileMBean = null;
    64     private static OperatingSystemImpl osMBean = null;
    67     private static BaseOperatingSystemImpl osMBean = null;
    65 
    68 
    66     public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {
    69     public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {
    67         if (classMBean == null) {
    70         if (classMBean == null) {
    68             classMBean = new ClassLoadingImpl(jvm);
    71             classMBean = new ClassLoadingImpl(jvm);
    69         }
    72         }
    98         return compileMBean;
   101         return compileMBean;
    99     }
   102     }
   100 
   103 
   101     public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {
   104     public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {
   102         if (osMBean == null) {
   105         if (osMBean == null) {
   103             osMBean = new OperatingSystemImpl(jvm);
   106             osMBean = new BaseOperatingSystemImpl(jvm);
   104         }
   107         }
   105         return osMBean;
   108         return osMBean;
   106     }
   109     }
   107 
   110 
   108     public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
   111     public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
   121             result.add(m);
   124             result.add(m);
   122         }
   125         }
   123         return result;
   126         return result;
   124     }
   127     }
   125 
   128 
   126     public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
   129      public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
   127         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
   130         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
   128         List<GarbageCollectorMXBean> result = new ArrayList<>(mgrs.length);
   131         List<GarbageCollectorMXBean> result = new ArrayList<>(mgrs.length);
   129         for (MemoryManagerMXBean m : mgrs) {
   132         for (MemoryManagerMXBean m : mgrs) {
   130             if (GarbageCollectorMXBean.class.isInstance(m)) {
   133             if (GarbageCollectorMXBean.class.isInstance(m)) {
   131                  result.add(GarbageCollectorMXBean.class.cast(m));
   134                  result.add(GarbageCollectorMXBean.class.cast(m));
   255                 return pool.getMemoryUsed();
   258                 return pool.getMemoryUsed();
   256             }
   259             }
   257         };
   260         };
   258     }
   261     }
   259 
   262 
   260     private static HotSpotDiagnostic hsDiagMBean = null;
       
   261     private static HotspotRuntime hsRuntimeMBean = null;
   263     private static HotspotRuntime hsRuntimeMBean = null;
   262     private static HotspotClassLoading hsClassMBean = null;
   264     private static HotspotClassLoading hsClassMBean = null;
   263     private static HotspotThread hsThreadMBean = null;
   265     private static HotspotThread hsThreadMBean = null;
   264     private static HotspotCompilation hsCompileMBean = null;
   266     private static HotspotCompilation hsCompileMBean = null;
   265     private static HotspotMemory hsMemoryMBean = null;
   267     private static HotspotMemory hsMemoryMBean = null;
   266     private static DiagnosticCommandImpl hsDiagCommandMBean = null;
       
   267 
       
   268     public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() {
       
   269         if (hsDiagMBean == null) {
       
   270             hsDiagMBean = new HotSpotDiagnostic();
       
   271         }
       
   272         return hsDiagMBean;
       
   273     }
       
   274 
   268 
   275     /**
   269     /**
   276      * This method is for testing only.
   270      * This method is for testing only.
   277      */
   271      */
   278     public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
   272     public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
   308     public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {
   302     public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {
   309         if (hsMemoryMBean == null) {
   303         if (hsMemoryMBean == null) {
   310             hsMemoryMBean = new HotspotMemory(jvm);
   304             hsMemoryMBean = new HotspotMemory(jvm);
   311         }
   305         }
   312         return hsMemoryMBean;
   306         return hsMemoryMBean;
   313     }
       
   314 
       
   315     public static synchronized DiagnosticCommandMBean getDiagnosticCommandMBean() {
       
   316         // Remote Diagnostic Commands may not be supported
       
   317         if (hsDiagCommandMBean == null && jvm.isRemoteDiagnosticCommandsSupported()) {
       
   318             hsDiagCommandMBean = new DiagnosticCommandImpl(jvm);
       
   319         }
       
   320         return hsDiagCommandMBean;
       
   321     }
   307     }
   322 
   308 
   323     /**
   309     /**
   324      * This method is for testing only.
   310      * This method is for testing only.
   325      */
   311      */
   372         "sun.management:type=HotspotRuntime";
   358         "sun.management:type=HotspotRuntime";
   373 
   359 
   374     private final static String HOTSPOT_THREAD_MBEAN_NAME =
   360     private final static String HOTSPOT_THREAD_MBEAN_NAME =
   375         "sun.management:type=HotspotThreading";
   361         "sun.management:type=HotspotThreading";
   376 
   362 
   377     final static String HOTSPOT_DIAGNOSTIC_COMMAND_MBEAN_NAME =
       
   378         "com.sun.management:type=DiagnosticCommand";
       
   379 
       
   380     public static HashMap<ObjectName, DynamicMBean> getPlatformDynamicMBeans() {
       
   381         HashMap<ObjectName, DynamicMBean> map = new HashMap<>();
       
   382         DiagnosticCommandMBean diagMBean = getDiagnosticCommandMBean();
       
   383         if (diagMBean != null) {
       
   384             map.put(Util.newObjectName(HOTSPOT_DIAGNOSTIC_COMMAND_MBEAN_NAME), diagMBean);
       
   385         }
       
   386         return map;
       
   387     }
       
   388 
       
   389     static void registerInternalMBeans(MBeanServer mbs) {
   363     static void registerInternalMBeans(MBeanServer mbs) {
   390         // register all internal MBeans if not registered
   364         // register all internal MBeans if not registered
   391         // No exception is thrown if a MBean with that object name
   365         // No exception is thrown if a MBean with that object name
   392         // already registered
   366         // already registered
   393         addMBean(mbs, getHotspotClassLoadingMBean(),
   367         addMBean(mbs, getHotspotClassLoadingMBean(),
   439         if (getCompilationMXBean() != null) {
   413         if (getCompilationMXBean() != null) {
   440             unregisterMBean(mbs, HOTSPOT_COMPILATION_MBEAN_NAME);
   414             unregisterMBean(mbs, HOTSPOT_COMPILATION_MBEAN_NAME);
   441         }
   415         }
   442     }
   416     }
   443 
   417 
   444     static {
       
   445         AccessController.doPrivileged(
       
   446             new java.security.PrivilegedAction<Void>() {
       
   447                 public Void run() {
       
   448                     System.loadLibrary("management");
       
   449                     return null;
       
   450                 }
       
   451             });
       
   452         jvm = new VMManagementImpl();
       
   453     }
       
   454 
       
   455     public static boolean isThreadSuspended(int state) {
   418     public static boolean isThreadSuspended(int state) {
   456         return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);
   419         return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);
   457     }
   420     }
   458 
   421 
   459     public static boolean isThreadRunningNative(int state) {
   422     public static boolean isThreadRunningNative(int state) {
   469     // These values are defined in jmm.h
   432     // These values are defined in jmm.h
   470     private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;
   433     private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;
   471     private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
   434     private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
   472     private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;
   435     private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;
   473 
   436 
       
   437     // Invoked by the VM
       
   438     private static MemoryPoolMXBean createMemoryPool
       
   439         (String name, boolean isHeap, long uThreshold, long gcThreshold) {
       
   440         return new MemoryPoolImpl(name, isHeap, uThreshold, gcThreshold);
       
   441     }
       
   442 
       
   443     private static MemoryManagerMXBean createMemoryManager(String name) {
       
   444         return new MemoryManagerImpl(name);
       
   445     }
       
   446 
       
   447     private static GarbageCollectorMXBean
       
   448         createGarbageCollector(String name, String type) {
       
   449 
       
   450         // ignore type parameter which is for future extension
       
   451         return new GarbageCollectorImpl(name);
       
   452     }
   474 }
   453 }