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     private List<String> vmArgs = null;
 183     public synchronized List<String> getVmArguments() {
 184         if (vmArgs == null) {
 185             String[] args = getVmArguments0();
 186             List<String> l = ((args != null && args.length != 0) ? Arrays.asList(args) :
 187                                         Collections.<String>emptyList());
 188             vmArgs = Collections.unmodifiableList(l);
 189         }
 190         return vmArgs;
 191     }
 192     public native String[] getVmArguments0();
 193 
 194     public native long getStartupTime();
 195     public native int getAvailableProcessors();
 196 
 197     // Compilation Subsystem
 198     public String   getCompilerName() {
 199         String name =  AccessController.doPrivileged(
 200             new PrivilegedAction<String>() {
 201                 public String run() {
 202                     return System.getProperty("sun.management.compiler");
 203                 }
 204             });
 205         return name;
 206     }
 207     public native long getTotalCompileTime();
 208 
 209     // Thread Subsystem
 210     public native long getTotalThreadCount();
 211     public native int  getLiveThreadCount();
 212     public native int  getPeakThreadCount();
 213     public native int  getDaemonThreadCount();
 214 
 215     // Operating System
 216     public String getOsName() {
 217         return System.getProperty("os.name");
 218     }
 219     public String getOsArch() {
 220         return System.getProperty("os.arch");
 221     }
 222     public String getOsVersion() {
 223         return System.getProperty("os.version");
 224     }
 225 
 226     // Hotspot-specific runtime support
 227     public native long getSafepointCount();
 228     public native long getTotalSafepointTime();
 229     public native long getSafepointSyncTime();
 230     public native long getTotalApplicationNonStoppedTime();
 231 
 232     public native long getLoadedClassSize();
 233     public native long getUnloadedClassSize();
 234     public native long getClassLoadingTime();
 235     public native long getMethodDataSize();
 236     public native long getInitializedClassCount();
 237     public native long getClassInitializationTime();
 238     public native long getClassVerificationTime();
 239 
 240     // Performance Counter Support
 241     private PerfInstrumentation perfInstr = null;
 242     private boolean noPerfData = false;
 243 
 244     private synchronized PerfInstrumentation getPerfInstrumentation() {
 245         if (noPerfData || perfInstr != null) {
 246              return perfInstr;
 247         }
 248 
 249         // construct PerfInstrumentation object
 250         Perf perf =  AccessController.doPrivileged(new Perf.GetPerfAction());
 251         try {
 252             ByteBuffer bb = perf.attach(0, "r");
 253             if (bb.capacity() == 0) {
 254                 noPerfData = true;
 255                 return null;
 256             }
 257             perfInstr = new PerfInstrumentation(bb);
 258         } catch (IllegalArgumentException e) {
 259             // If the shared memory doesn't exist e.g. if -XX:-UsePerfData
 260             // was set
 261             noPerfData = true;
 262         } catch (IOException e) {
 263             throw new AssertionError(e);
 264         }
 265         return perfInstr;
 266     }
 267 
 268     public List<Counter> getInternalCounters(String pattern) {
 269         PerfInstrumentation perf = getPerfInstrumentation();
 270         if (perf != null) {
 271             return perf.findByPattern(pattern);
 272         } else {
 273             return Collections.emptyList();
 274         }
 275     }
 276 }