1 /* 2 * Copyright (c) 2003, 2011, 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 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 native boolean isThreadContentionMonitoringEnabled(); 110 public native boolean isThreadCpuTimeEnabled(); 111 public native boolean isThreadAllocatedMemoryEnabled(); 112 113 // Class Loading Subsystem 114 public int getLoadedClassCount() { 115 long count = getTotalClassCount() - getUnloadedClassCount(); 116 return (int) count; 117 } 118 public native long getTotalClassCount(); 119 public native long getUnloadedClassCount(); 120 121 public native boolean getVerboseClass(); 122 123 // Memory Subsystem 124 public native boolean getVerboseGC(); 125 126 // Runtime Subsystem 127 public String getManagementVersion() { 128 return version; 129 } 130 131 public String getVmId() { 132 int pid = getProcessId(); 133 String hostname = "localhost"; 134 try { 135 hostname = InetAddress.getLocalHost().getHostName(); 136 } catch (UnknownHostException e) { 137 // ignore 138 } 139 140 return pid + "@" + hostname; 141 } 142 private native int getProcessId(); 143 144 public String getVmName() { 145 return System.getProperty("java.vm.name"); 146 } 147 148 public String getVmVendor() { 149 return System.getProperty("java.vm.vendor"); 150 } 151 public String getVmVersion() { 152 return System.getProperty("java.vm.version"); 153 } 154 public String getVmSpecName() { 155 return System.getProperty("java.vm.specification.name"); 156 } 157 public String getVmSpecVendor() { 158 return System.getProperty("java.vm.specification.vendor"); 159 } 160 public String getVmSpecVersion() { 161 return System.getProperty("java.vm.specification.version"); 162 } 163 public String getClassPath() { 164 return System.getProperty("java.class.path"); 165 } 166 public String getLibraryPath() { 167 return System.getProperty("java.library.path"); 168 } 169 170 public String getBootClassPath( ) { 171 PrivilegedAction<String> pa 172 = new GetPropertyAction("sun.boot.class.path"); 173 String result = AccessController.doPrivileged(pa); 174 return result; 175 } 176 177 private List<String> vmArgs = null; 178 public synchronized List<String> getVmArguments() { 179 if (vmArgs == null) { 180 String[] args = getVmArguments0(); 181 List<String> l = ((args != null && args.length != 0) ? Arrays.asList(args) : 182 Collections.<String>emptyList()); 183 vmArgs = Collections.unmodifiableList(l); 184 } 185 return vmArgs; 186 } 187 public native String[] getVmArguments0(); 188 189 public native long getStartupTime(); 190 public native int getAvailableProcessors(); 191 192 // Compilation Subsystem 193 public String getCompilerName() { 194 String name = AccessController.doPrivileged( 195 new PrivilegedAction<String>() { 196 public String run() { 197 return System.getProperty("sun.management.compiler"); 198 } 199 }); 200 return name; 201 } 202 public native long getTotalCompileTime(); 203 204 // Thread Subsystem 205 public native long getTotalThreadCount(); 206 public native int getLiveThreadCount(); 207 public native int getPeakThreadCount(); 208 public native int getDaemonThreadCount(); 209 210 // Operating System 211 public String getOsName() { 212 return System.getProperty("os.name"); 213 } 214 public String getOsArch() { 215 return System.getProperty("os.arch"); 216 } 217 public String getOsVersion() { 218 return System.getProperty("os.version"); 219 } 220 221 // Hotspot-specific runtime support 222 public native long getSafepointCount(); 223 public native long getTotalSafepointTime(); 224 public native long getSafepointSyncTime(); 225 public native long getTotalApplicationNonStoppedTime(); 226 227 public native long getLoadedClassSize(); 228 public native long getUnloadedClassSize(); 229 public native long getClassLoadingTime(); 230 public native long getMethodDataSize(); 231 public native long getInitializedClassCount(); 232 public native long getClassInitializationTime(); 233 public native long getClassVerificationTime(); 234 235 // Performance Counter Support 236 private PerfInstrumentation perfInstr = null; 237 private boolean noPerfData = false; 238 239 private synchronized PerfInstrumentation getPerfInstrumentation() { 240 if (noPerfData || perfInstr != null) { 241 return perfInstr; 242 } 243 244 // construct PerfInstrumentation object 245 Perf perf = AccessController.doPrivileged(new Perf.GetPerfAction()); 246 try { 247 ByteBuffer bb = perf.attach(0, "r"); 248 if (bb.capacity() == 0) { 249 noPerfData = true; 250 return null; 251 } 252 perfInstr = new PerfInstrumentation(bb); 253 } catch (IllegalArgumentException e) { 254 // If the shared memory doesn't exist e.g. if -XX:-UsePerfData 255 // was set 256 noPerfData = true; 257 } catch (IOException e) { 258 throw new AssertionError(e); 259 } 260 return perfInstr; 261 } 262 263 public List<Counter> getInternalCounters(String pattern) { 264 PerfInstrumentation perf = getPerfInstrumentation(); 265 if (perf != null) { 266 return perf.findByPattern(pattern); 267 } else { 268 return Collections.emptyList(); 269 } 270 } 271 }