1 /*
   2  * Copyright (c) 2003, 2013, 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 sun.misc.Perf;
  29 import sun.management.counter.*;
  30 import sun.management.counter.perf.*;
  31 import java.nio.ByteBuffer;
  32 import java.io.IOException;
  33 import java.net.InetAddress;
  34 import java.net.UnknownHostException;
  35 import java.util.List;
  36 import java.util.Arrays;
  37 import java.util.Collections;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedAction;
  40 import sun.security.action.GetPropertyAction;
  41 
  42 /**
  43  * Implementation of VMManagement interface that accesses the management
  44  * attributes and operations locally within the same Java virtual
  45  * machine.
  46  */
  47 class VMManagementImpl implements VMManagement {
  48 
  49     private static String version;
  50 
  51     private static boolean compTimeMonitoringSupport;
  52     private static boolean threadContentionMonitoringSupport;
  53     private static boolean currentThreadCpuTimeSupport;
  54     private static boolean otherThreadCpuTimeSupport;
  55     private static boolean bootClassPathSupport;
  56     private static boolean objectMonitorUsageSupport;
  57     private static boolean synchronizerUsageSupport;
  58     private static boolean threadAllocatedMemorySupport;
  59     private static boolean gcNotificationSupport;
  60     private static boolean remoteDiagnosticCommandsSupport;
  61 
  62 
  63     static {
  64         version = getVersion0();
  65         if (version == null) {
  66             throw new AssertionError("Invalid Management Version");
  67         }
  68         initOptionalSupportFields();
  69     }
  70     private native static String getVersion0();
  71     private native static void initOptionalSupportFields();
  72 
  73     // Optional supports
  74     public boolean isCompilationTimeMonitoringSupported() {
  75         return compTimeMonitoringSupport;
  76     }
  77 
  78     public boolean isThreadContentionMonitoringSupported() {
  79         return threadContentionMonitoringSupport;
  80     }
  81 
  82     public boolean isCurrentThreadCpuTimeSupported() {
  83         return currentThreadCpuTimeSupport;
  84     }
  85 
  86     public boolean isOtherThreadCpuTimeSupported() {
  87         return otherThreadCpuTimeSupport;
  88     }
  89 
  90     public boolean isBootClassPathSupported() {
  91         return bootClassPathSupport;
  92     }
  93 
  94     public boolean isObjectMonitorUsageSupported() {
  95         return objectMonitorUsageSupport;
  96     }
  97 
  98     public boolean isSynchronizerUsageSupported() {
  99         return synchronizerUsageSupport;
 100     }
 101 
 102     public boolean isThreadAllocatedMemorySupported() {
 103         return threadAllocatedMemorySupport;
 104     }
 105 
 106     public boolean isGcNotificationSupported() {
 107         return gcNotificationSupport;
 108     }
 109 
 110     public boolean isRemoteDiagnosticCommandsSupported() {
 111         return remoteDiagnosticCommandsSupport;
 112     }
 113 
 114     public native boolean isThreadContentionMonitoringEnabled();
 115     public native boolean isThreadCpuTimeEnabled();
 116     public native boolean isThreadAllocatedMemoryEnabled();
 117 
 118     // Class Loading Subsystem
 119     public int    getLoadedClassCount() {
 120         long count = getTotalClassCount() - getUnloadedClassCount();
 121         return (int) count;
 122     }
 123     public native long getTotalClassCount();
 124     public native long getUnloadedClassCount();
 125 
 126     public native boolean getVerboseClass();
 127 
 128     // Memory Subsystem
 129     public native boolean getVerboseGC();
 130 
 131     // Runtime Subsystem
 132     public String   getManagementVersion() {
 133         return version;
 134     }
 135 
 136     public String getVmId() {
 137         int pid = getProcessId();
 138         String hostname = "localhost";
 139         try {
 140             hostname = InetAddress.getLocalHost().getHostName();
 141         } catch (UnknownHostException e) {
 142             // ignore
 143         }
 144 
 145         return pid + "@" + hostname;
 146     }
 147     private native int getProcessId();
 148 
 149     public String   getVmName() {
 150         return System.getProperty("java.vm.name");
 151     }
 152 
 153     public String   getVmVendor() {
 154         return System.getProperty("java.vm.vendor");
 155     }
 156     public String   getVmVersion() {
 157         return System.getProperty("java.vm.version");
 158     }
 159     public String   getVmSpecName()  {
 160         return System.getProperty("java.vm.specification.name");
 161     }
 162     public String   getVmSpecVendor() {
 163         return System.getProperty("java.vm.specification.vendor");
 164     }
 165     public String   getVmSpecVersion() {
 166         return System.getProperty("java.vm.specification.version");
 167     }
 168     public String   getClassPath() {
 169         return System.getProperty("java.class.path");
 170     }
 171     public String   getLibraryPath()  {
 172         return System.getProperty("java.library.path");
 173     }
 174 
 175     public String   getBootClassPath( ) {
 176         PrivilegedAction<String> pa
 177             = new GetPropertyAction("sun.boot.class.path");
 178         String result =  AccessController.doPrivileged(pa);
 179         return result;
 180     }
 181 
 182     public long getUptime() {
 183         return getUptime0();
 184     }
 185 
 186     private List<String> vmArgs = null;
 187     public synchronized List<String> getVmArguments() {
 188         if (vmArgs == null) {
 189             String[] args = getVmArguments0();
 190             List<String> l = ((args != null && args.length != 0) ? Arrays.asList(args) :
 191                                         Collections.<String>emptyList());
 192             vmArgs = Collections.unmodifiableList(l);
 193         }
 194         return vmArgs;
 195     }
 196     public native String[] getVmArguments0();
 197 
 198     public native long getStartupTime();
 199     private native long getUptime0();
 200     public native int getAvailableProcessors();
 201 
 202     // Compilation Subsystem
 203     public String   getCompilerName() {
 204         String name =  AccessController.doPrivileged(
 205             new PrivilegedAction<String>() {
 206                 public String run() {
 207                     return System.getProperty("sun.management.compiler");
 208                 }
 209             });
 210         return name;
 211     }
 212     public native long getTotalCompileTime();
 213 
 214     // Thread Subsystem
 215     public native long getTotalThreadCount();
 216     public native int  getLiveThreadCount();
 217     public native int  getPeakThreadCount();
 218     public native int  getDaemonThreadCount();
 219 
 220     // Operating System
 221     public String getOsName() {
 222         return System.getProperty("os.name");
 223     }
 224     public String getOsArch() {
 225         return System.getProperty("os.arch");
 226     }
 227     public String getOsVersion() {
 228         return System.getProperty("os.version");
 229     }
 230 
 231     // Hotspot-specific runtime support
 232     public native long getSafepointCount();
 233     public native long getTotalSafepointTime();
 234     public native long getSafepointSyncTime();
 235     public native long getTotalApplicationNonStoppedTime();
 236 
 237     public native long getLoadedClassSize();
 238     public native long getUnloadedClassSize();
 239     public native long getClassLoadingTime();
 240     public native long getMethodDataSize();
 241     public native long getInitializedClassCount();
 242     public native long getClassInitializationTime();
 243     public native long getClassVerificationTime();
 244 
 245     // Performance Counter Support
 246     private PerfInstrumentation perfInstr = null;
 247     private boolean noPerfData = false;
 248 
 249     private synchronized PerfInstrumentation getPerfInstrumentation() {
 250         if (noPerfData || perfInstr != null) {
 251              return perfInstr;
 252         }
 253 
 254         // construct PerfInstrumentation object
 255         Perf perf =  AccessController.doPrivileged(new Perf.GetPerfAction());
 256         try {
 257             ByteBuffer bb = perf.attach(0, "r");
 258             if (bb.capacity() == 0) {
 259                 noPerfData = true;
 260                 return null;
 261             }
 262             perfInstr = new PerfInstrumentation(bb);
 263         } catch (IllegalArgumentException e) {
 264             // If the shared memory doesn't exist e.g. if -XX:-UsePerfData
 265             // was set
 266             noPerfData = true;
 267         } catch (IOException e) {
 268             throw new AssertionError(e);
 269         }
 270         return perfInstr;
 271     }
 272 
 273     public List<Counter> getInternalCounters(String pattern) {
 274         PerfInstrumentation perf = getPerfInstrumentation();
 275         if (perf != null) {
 276             return perf.findByPattern(pattern);
 277         } else {
 278             return Collections.emptyList();
 279         }
 280     }
 281 }