1 /*
   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.
   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.  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.
  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.management;
  27 
  28 import java.lang.management.*;
  29 
  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;
  41 
  42 import sun.util.logging.LoggingSupport;
  43 
  44 import java.util.ArrayList;
  45 import java.util.Collections;
  46 import java.util.List;
  47 import com.sun.management.OSMBeanFactory;
  48 import com.sun.management.HotSpotDiagnosticMXBean;
  49 
  50 import static java.lang.management.ManagementFactory.*;
  51 
  52 /**
  53  * ManagementFactoryHelper provides static factory methods to create
  54  * instances of the management interface.
  55  */
  56 public class ManagementFactoryHelper {
  57     private ManagementFactoryHelper() {};
  58 
  59     private static VMManagement jvm;
  60 
  61     private static ClassLoadingImpl    classMBean = null;
  62     private static MemoryImpl          memoryMBean = null;
  63     private static ThreadImpl          threadMBean = null;
  64     private static RuntimeImpl         runtimeMBean = null;
  65     private static CompilationImpl     compileMBean = null;
  66     private static OperatingSystemImpl osMBean = null;
  67 
  68     public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {
  69         if (classMBean == null) {
  70             classMBean = new ClassLoadingImpl(jvm);
  71         }
  72         return classMBean;
  73     }
  74 
  75     public static synchronized MemoryMXBean getMemoryMXBean() {
  76         if (memoryMBean == null) {
  77             memoryMBean = new MemoryImpl(jvm);
  78         }
  79         return memoryMBean;
  80     }
  81 
  82     public static synchronized ThreadMXBean getThreadMXBean() {
  83         if (threadMBean == null) {
  84             threadMBean = new ThreadImpl(jvm);
  85         }
  86         return threadMBean;
  87     }
  88 
  89     public static synchronized RuntimeMXBean getRuntimeMXBean() {
  90         if (runtimeMBean == null) {
  91             runtimeMBean = new RuntimeImpl(jvm);
  92         }
  93         return runtimeMBean;
  94     }
  95 
  96     public static synchronized CompilationMXBean getCompilationMXBean() {
  97         if (compileMBean == null && jvm.getCompilerName() != null) {
  98             compileMBean = new CompilationImpl(jvm);
  99         }
 100         return compileMBean;
 101     }
 102 
 103     public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {
 104         if (osMBean == null) {
 105             osMBean = (OperatingSystemImpl)
 106                           OSMBeanFactory.getOperatingSystemMXBean(jvm);
 107         }
 108         return osMBean;
 109     }
 110 
 111     public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
 112         MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools();
 113         List<MemoryPoolMXBean> list = new ArrayList<>(pools.length);
 114         for (MemoryPoolMXBean p : pools) {
 115             list.add(p);
 116         }
 117         return list;
 118     }
 119 
 120     public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
 121         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 122         List<MemoryManagerMXBean> result = new ArrayList<>(mgrs.length);
 123         for (MemoryManagerMXBean m : mgrs) {
 124             result.add(m);
 125         }
 126         return result;
 127     }
 128 
 129     public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
 130         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 131         List<GarbageCollectorMXBean> result = new ArrayList<>(mgrs.length);
 132         for (MemoryManagerMXBean m : mgrs) {
 133             if (GarbageCollectorMXBean.class.isInstance(m)) {
 134                  result.add(GarbageCollectorMXBean.class.cast(m));
 135             }
 136         }
 137         return result;
 138     }
 139 
 140     public static PlatformLoggingMXBean getPlatformLoggingMXBean() {
 141         if (LoggingSupport.isAvailable()) {
 142             return PlatformLoggingImpl.instance;
 143         } else {
 144             return null;
 145         }
 146     }
 147 
 148     // The logging MXBean object is an instance of
 149     // PlatformLoggingMXBean and java.util.logging.LoggingMXBean
 150     // but it can't directly implement two MXBean interfaces
 151     // as a compliant MXBean implements exactly one MXBean interface,
 152     // or if it implements one interface that is a subinterface of
 153     // all the others; otherwise, it is a non-compliant MXBean
 154     // 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 interface
 158     // that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean
 159     interface LoggingMXBean
 160         extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean {
 161     }
 162 
 163     static class PlatformLoggingImpl implements LoggingMXBean
 164     {
 165         final static PlatformLoggingMXBean instance = new PlatformLoggingImpl();
 166         final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging";
 167 
 168         private volatile ObjectName objname;  // created lazily
 169         @Override
 170         public ObjectName getObjectName() {
 171             ObjectName result = objname;
 172             if (result == null) {
 173                 synchronized (this) {
 174                     result = objname;
 175                     if (result == null) {
 176                         result = Util.newObjectName(LOGGING_MXBEAN_NAME);
 177                         objname = result;
 178                     }
 179                 }
 180             }
 181             return result;
 182         }
 183 
 184         @Override
 185         public java.util.List<String> getLoggerNames() {
 186             return LoggingSupport.getLoggerNames();
 187         }
 188 
 189         @Override
 190         public String getLoggerLevel(String loggerName) {
 191             return LoggingSupport.getLoggerLevel(loggerName);
 192         }
 193 
 194         @Override
 195         public void setLoggerLevel(String loggerName, String levelName) {
 196             LoggingSupport.setLoggerLevel(loggerName, levelName);
 197         }
 198 
 199         @Override
 200         public String getParentLoggerName(String loggerName) {
 201             return LoggingSupport.getParentLoggerName(loggerName);
 202         }
 203     }
 204 
 205     private static List<BufferPoolMXBean> bufferPools = null;
 206     public static synchronized List<BufferPoolMXBean> getBufferPoolMXBeans() {
 207         if (bufferPools == null) {
 208             bufferPools = new ArrayList<>(2);
 209             bufferPools.add(createBufferPoolMXBean(sun.misc.SharedSecrets.getJavaNioAccess()
 210                 .getDirectBufferPool()));
 211             bufferPools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl
 212                 .getMappedBufferPool()));
 213         }
 214         return bufferPools;
 215     }
 216 
 217     private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";
 218 
 219     /**
 220      * Creates management interface for the given buffer pool.
 221      */
 222     private static BufferPoolMXBean
 223         createBufferPoolMXBean(final sun.misc.JavaNioAccess.BufferPool pool)
 224     {
 225         return new BufferPoolMXBean() {
 226             private volatile ObjectName objname;  // created lazily
 227             @Override
 228             public ObjectName getObjectName() {
 229                 ObjectName result = objname;
 230                 if (result == null) {
 231                     synchronized (this) {
 232                         result = objname;
 233                         if (result == null) {
 234                             result = Util.newObjectName(BUFFER_POOL_MXBEAN_NAME +
 235                                 ",name=" + pool.getName());
 236                             objname = result;
 237                         }
 238                     }
 239                 }
 240                 return result;
 241             }
 242             @Override
 243             public String getName() {
 244                 return pool.getName();
 245             }
 246             @Override
 247             public long getCount() {
 248                 return pool.getCount();
 249             }
 250             @Override
 251             public long getTotalCapacity() {
 252                 return pool.getTotalCapacity();
 253             }
 254             @Override
 255             public long getMemoryUsed() {
 256                 return pool.getMemoryUsed();
 257             }
 258         };
 259     }
 260 
 261     private static HotSpotDiagnostic hsDiagMBean = null;
 262     private static HotspotRuntime hsRuntimeMBean = null;
 263     private static HotspotClassLoading hsClassMBean = null;
 264     private static HotspotThread hsThreadMBean = null;
 265     private static HotspotCompilation hsCompileMBean = null;
 266     private static HotspotMemory hsMemoryMBean = null;
 267 
 268     public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() {
 269         if (hsDiagMBean == null) {
 270             hsDiagMBean = new HotSpotDiagnostic();
 271         }
 272         return hsDiagMBean;
 273     }
 274 
 275     /**
 276      * This method is for testing only.
 277      */
 278     public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
 279         if (hsRuntimeMBean == null) {
 280             hsRuntimeMBean = new HotspotRuntime(jvm);
 281         }
 282         return hsRuntimeMBean;
 283     }
 284 
 285     /**
 286      * This method is for testing only.
 287      */
 288     public static synchronized HotspotClassLoadingMBean getHotspotClassLoadingMBean() {
 289         if (hsClassMBean == null) {
 290             hsClassMBean = new HotspotClassLoading(jvm);
 291         }
 292         return hsClassMBean;
 293     }
 294 
 295     /**
 296      * This method is for testing only.
 297      */
 298     public static synchronized HotspotThreadMBean getHotspotThreadMBean() {
 299         if (hsThreadMBean == null) {
 300             hsThreadMBean = new HotspotThread(jvm);
 301         }
 302         return hsThreadMBean;
 303     }
 304 
 305     /**
 306      * This method is for testing only.
 307      */
 308     public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {
 309         if (hsMemoryMBean == null) {
 310             hsMemoryMBean = new HotspotMemory(jvm);
 311         }
 312         return hsMemoryMBean;
 313     }
 314 
 315     /**
 316      * This method is for testing only.
 317      */
 318     public static synchronized HotspotCompilationMBean getHotspotCompilationMBean() {
 319         if (hsCompileMBean == null) {
 320             hsCompileMBean = new HotspotCompilation(jvm);
 321         }
 322         return hsCompileMBean;
 323     }
 324 
 325     /**
 326      * Registers a given MBean if not registered in the MBeanServer;
 327      * otherwise, just return.
 328      */
 329     private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {
 330         try {
 331             final ObjectName objName = Util.newObjectName(mbeanName);
 332 
 333             // inner class requires these fields to be final
 334             final MBeanServer mbs0 = mbs;
 335             final Object mbean0 = mbean;
 336             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
 337                 public Void run() throws MBeanRegistrationException,
 338                                          NotCompliantMBeanException {
 339                     try {
 340                         mbs0.registerMBean(mbean0, objName);
 341                         return null;
 342                     } catch (InstanceAlreadyExistsException e) {
 343                         // if an instance with the object name exists in
 344                         // the MBeanServer ignore the exception
 345                     }
 346                     return null;
 347                 }
 348             });
 349         } catch (PrivilegedActionException e) {
 350             throw Util.newException(e.getException());
 351         }
 352     }
 353 
 354     private final static String HOTSPOT_CLASS_LOADING_MBEAN_NAME =
 355         "sun.management:type=HotspotClassLoading";
 356 
 357     private final static String HOTSPOT_COMPILATION_MBEAN_NAME =
 358         "sun.management:type=HotspotCompilation";
 359 
 360     private final static String HOTSPOT_MEMORY_MBEAN_NAME =
 361         "sun.management:type=HotspotMemory";
 362 
 363     private static final String HOTSPOT_RUNTIME_MBEAN_NAME =
 364         "sun.management:type=HotspotRuntime";
 365 
 366     private final static String HOTSPOT_THREAD_MBEAN_NAME =
 367         "sun.management:type=HotspotThreading";
 368 
 369     static void registerInternalMBeans(MBeanServer mbs) {
 370         // register all internal MBeans if not registered
 371         // No exception is thrown if a MBean with that object name
 372         // already registered
 373         addMBean(mbs, getHotspotClassLoadingMBean(),
 374             HOTSPOT_CLASS_LOADING_MBEAN_NAME);
 375         addMBean(mbs, getHotspotMemoryMBean(),
 376             HOTSPOT_MEMORY_MBEAN_NAME);
 377         addMBean(mbs, getHotspotRuntimeMBean(),
 378             HOTSPOT_RUNTIME_MBEAN_NAME);
 379         addMBean(mbs, getHotspotThreadMBean(),
 380             HOTSPOT_THREAD_MBEAN_NAME);
 381 
 382         // CompilationMBean may not exist
 383         if (getCompilationMXBean() != null) {
 384             addMBean(mbs, getHotspotCompilationMBean(),
 385                 HOTSPOT_COMPILATION_MBEAN_NAME);
 386         }
 387     }
 388 
 389     private static void unregisterMBean(MBeanServer mbs, String mbeanName) {
 390         try {
 391             final ObjectName objName = Util.newObjectName(mbeanName);
 392 
 393             // inner class requires these fields to be final
 394             final MBeanServer mbs0 = mbs;
 395             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
 396                 public Void run() throws MBeanRegistrationException,
 397                                            RuntimeOperationsException  {
 398                     try {
 399                         mbs0.unregisterMBean(objName);
 400                     } catch (InstanceNotFoundException e) {
 401                         // ignore exception if not found
 402                     }
 403                     return null;
 404                 }
 405             });
 406         } catch (PrivilegedActionException e) {
 407             throw Util.newException(e.getException());
 408         }
 409     }
 410 
 411     static void unregisterInternalMBeans(MBeanServer mbs) {
 412         // unregister all internal MBeans
 413         unregisterMBean(mbs, HOTSPOT_CLASS_LOADING_MBEAN_NAME);
 414         unregisterMBean(mbs, HOTSPOT_MEMORY_MBEAN_NAME);
 415         unregisterMBean(mbs, HOTSPOT_RUNTIME_MBEAN_NAME);
 416         unregisterMBean(mbs, HOTSPOT_THREAD_MBEAN_NAME);
 417 
 418         // CompilationMBean may not exist
 419         if (getCompilationMXBean() != null) {
 420             unregisterMBean(mbs, HOTSPOT_COMPILATION_MBEAN_NAME);
 421         }
 422     }
 423 
 424     static {
 425         AccessController.doPrivileged(new LoadLibraryAction("management"));
 426         jvm = new VMManagementImpl();
 427     }
 428 
 429     public static boolean isThreadSuspended(int state) {
 430         return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);
 431     }
 432 
 433     public static boolean isThreadRunningNative(int state) {
 434         return ((state & JMM_THREAD_STATE_FLAG_NATIVE) != 0);
 435     }
 436 
 437     public static Thread.State toThreadState(int state) {
 438         // suspended and native bits may be set in state
 439         int threadStatus = state & ~JMM_THREAD_STATE_FLAG_MASK;
 440         return sun.misc.VM.toThreadState(threadStatus);
 441     }
 442 
 443     // These values are defined in jmm.h
 444     private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;
 445     private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
 446     private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;
 447 
 448 }