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