J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | |
| 59 | static { |
| 60 | version = getVersion0(); |
| 61 | if (version == null) { |
| 62 | throw new InternalError("Invalid Management Version"); |
| 63 | } |
| 64 | initOptionalSupportFields(); |
| 65 | } |
| 66 | private native static String getVersion0(); |
| 67 | private native static void initOptionalSupportFields(); |
| 68 | |
| 69 | // Optional supports |
| 70 | public boolean isCompilationTimeMonitoringSupported() { |
| 71 | return compTimeMonitoringSupport; |
| 72 | } |
| 73 | |
| 74 | public boolean isThreadContentionMonitoringSupported() { |
| 75 | return threadContentionMonitoringSupport; |
| 76 | } |
| 77 | |
| 78 | public boolean isCurrentThreadCpuTimeSupported() { |
| 79 | return currentThreadCpuTimeSupport; |
| 80 | } |
| 81 | |
| 82 | public boolean isOtherThreadCpuTimeSupported() { |
| 83 | return otherThreadCpuTimeSupport; |
| 84 | } |
| 85 | |
| 86 | public boolean isBootClassPathSupported() { |
| 87 | return bootClassPathSupport; |
| 88 | } |
| 89 | |
| 90 | public boolean isObjectMonitorUsageSupported() { |
| 91 | return objectMonitorUsageSupport; |
| 92 | } |
| 93 | |
| 94 | public boolean isSynchronizerUsageSupported() { |
| 95 | return synchronizerUsageSupport; |
| 96 | } |
| 97 | |
| 98 | public native boolean isThreadContentionMonitoringEnabled(); |
| 99 | public native boolean isThreadCpuTimeEnabled(); |
| 100 | |
| 101 | |
| 102 | // Class Loading Subsystem |
| 103 | public int getLoadedClassCount() { |
| 104 | long count = getTotalClassCount() - getUnloadedClassCount(); |
| 105 | return (int) count; |
| 106 | } |
| 107 | public native long getTotalClassCount(); |
| 108 | public native long getUnloadedClassCount(); |
| 109 | |
| 110 | public native boolean getVerboseClass(); |
| 111 | |
| 112 | // Memory Subsystem |
| 113 | public native boolean getVerboseGC(); |
| 114 | |
| 115 | // Runtime Subsystem |
| 116 | public String getManagementVersion() { |
| 117 | return version; |
| 118 | } |
| 119 | |
| 120 | public String getVmId() { |
| 121 | int pid = getProcessId(); |
| 122 | String hostname = "localhost"; |
| 123 | try { |
| 124 | hostname = InetAddress.getLocalHost().getHostName(); |
| 125 | } catch (UnknownHostException e) { |
| 126 | // ignore |
| 127 | } |
| 128 | |
| 129 | return pid + "@" + hostname; |
| 130 | } |
| 131 | private native int getProcessId(); |
| 132 | |
| 133 | public String getVmName() { |
| 134 | return System.getProperty("java.vm.name"); |
| 135 | } |
| 136 | |
| 137 | public String getVmVendor() { |
| 138 | return System.getProperty("java.vm.vendor"); |
| 139 | } |
| 140 | public String getVmVersion() { |
| 141 | return System.getProperty("java.vm.version"); |
| 142 | } |
| 143 | public String getVmSpecName() { |
| 144 | return System.getProperty("java.vm.specification.name"); |
| 145 | } |
| 146 | public String getVmSpecVendor() { |
| 147 | return System.getProperty("java.vm.specification.vendor"); |
| 148 | } |
| 149 | public String getVmSpecVersion() { |
| 150 | return System.getProperty("java.vm.specification.version"); |
| 151 | } |
| 152 | public String getClassPath() { |
| 153 | return System.getProperty("java.class.path"); |
| 154 | } |
| 155 | public String getLibraryPath() { |
| 156 | return System.getProperty("java.library.path"); |
| 157 | } |
| 158 | |
| 159 | public String getBootClassPath( ) { |
| 160 | PrivilegedAction<String> pa |
| 161 | = new GetPropertyAction("sun.boot.class.path"); |
| 162 | String result = AccessController.doPrivileged(pa); |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | private List<String> vmArgs = null; |
| 167 | public synchronized List<String> getVmArguments() { |
| 168 | if (vmArgs == null) { |
| 169 | String[] args = getVmArguments0(); |
| 170 | List<String> l = ((args != null && args.length != 0) ? Arrays.asList(args) : |
| 171 | Collections.<String>emptyList()); |
| 172 | vmArgs = Collections.unmodifiableList(l); |
| 173 | } |
| 174 | return vmArgs; |
| 175 | } |
| 176 | public native String[] getVmArguments0(); |
| 177 | |
| 178 | public native long getStartupTime(); |
| 179 | public native int getAvailableProcessors(); |
| 180 | |
| 181 | // Compilation Subsystem |
| 182 | public String getCompilerName() { |
| 183 | String name = AccessController.doPrivileged( |
| 184 | new PrivilegedAction<String>() { |
| 185 | public String run() { |
| 186 | return System.getProperty("sun.management.compiler"); |
| 187 | } |
| 188 | }); |
| 189 | return name; |
| 190 | } |
| 191 | public native long getTotalCompileTime(); |
| 192 | |
| 193 | // Thread Subsystem |
| 194 | public native long getTotalThreadCount(); |
| 195 | public native int getLiveThreadCount(); |
| 196 | public native int getPeakThreadCount(); |
| 197 | public native int getDaemonThreadCount(); |
| 198 | |
| 199 | // Operating System |
| 200 | public String getOsName() { |
| 201 | return System.getProperty("os.name"); |
| 202 | } |
| 203 | public String getOsArch() { |
| 204 | return System.getProperty("os.arch"); |
| 205 | } |
| 206 | public String getOsVersion() { |
| 207 | return System.getProperty("os.version"); |
| 208 | } |
| 209 | |
| 210 | // Hotspot-specific runtime support |
| 211 | public native long getSafepointCount(); |
| 212 | public native long getTotalSafepointTime(); |
| 213 | public native long getSafepointSyncTime(); |
| 214 | public native long getTotalApplicationNonStoppedTime(); |
| 215 | |
| 216 | public native long getLoadedClassSize(); |
| 217 | public native long getUnloadedClassSize(); |
| 218 | public native long getClassLoadingTime(); |
| 219 | public native long getMethodDataSize(); |
| 220 | public native long getInitializedClassCount(); |
| 221 | public native long getClassInitializationTime(); |
| 222 | public native long getClassVerificationTime(); |
| 223 | |
| 224 | // Performance Counter Support |
| 225 | private PerfInstrumentation perfInstr = null; |
| 226 | private boolean noPerfData = false; |
| 227 | |
| 228 | private synchronized PerfInstrumentation getPerfInstrumentation() { |
| 229 | if (noPerfData || perfInstr != null) { |
| 230 | return perfInstr; |
| 231 | } |
| 232 | |
| 233 | // construct PerfInstrumentation object |
| 234 | Perf perf = AccessController.doPrivileged(new Perf.GetPerfAction()); |
| 235 | try { |
| 236 | ByteBuffer bb = perf.attach(0, "r"); |
| 237 | if (bb.capacity() == 0) { |
| 238 | noPerfData = true; |
| 239 | return null; |
| 240 | } |
| 241 | perfInstr = new PerfInstrumentation(bb); |
| 242 | } catch (IllegalArgumentException e) { |
| 243 | // If the shared memory doesn't exist e.g. if -XX:-UsePerfData |
| 244 | // was set |
| 245 | noPerfData = true; |
| 246 | } catch (IOException e) { |
| 247 | throw new InternalError(e.getMessage()); |
| 248 | } |
| 249 | return perfInstr; |
| 250 | } |
| 251 | |
| 252 | public List<Counter> getInternalCounters(String pattern) { |
| 253 | PerfInstrumentation perf = getPerfInstrumentation(); |
| 254 | if (perf != null) { |
| 255 | return perf.findByPattern(pattern); |
| 256 | } else { |
| 257 | return Collections.emptyList(); |
| 258 | } |
| 259 | } |
| 260 | } |