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