1 /*
2 * Copyright (c) 2003, 2024, 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 jdk.internal.perf.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
39 /**
40 * Implementation of VMManagement interface that accesses the management
41 * attributes and operations locally within the same Java virtual
42 * machine.
43 */
44 class VMManagementImpl implements VMManagement {
45
46 private static String version;
47
48 private static boolean compTimeMonitoringSupport;
49 private static boolean threadContentionMonitoringSupport;
50 private static boolean currentThreadCpuTimeSupport;
51 private static boolean otherThreadCpuTimeSupport;
52 private static boolean objectMonitorUsageSupport;
53 private static boolean synchronizerUsageSupport;
54 private static boolean threadAllocatedMemorySupport;
55 private static boolean gcNotificationSupport;
56 private static boolean remoteDiagnosticCommandsSupport;
57
58
59 static {
60 version = getVersion0();
61 if (version == null) {
62 throw new AssertionError("Invalid Management Version");
63 }
64 initOptionalSupportFields();
65 }
66 private static native String getVersion0();
67 private static native 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 false;
88 }
89
90 public boolean isObjectMonitorUsageSupported() {
91 return objectMonitorUsageSupport;
92 }
93
94 public boolean isSynchronizerUsageSupported() {
95 return synchronizerUsageSupport;
96 }
97
98 public boolean isThreadAllocatedMemorySupported() {
99 return threadAllocatedMemorySupport;
100 }
101
102 public boolean isGcNotificationSupported() {
103 boolean isSupported = true;
104 try {
105 Class.forName("com.sun.management.GarbageCollectorMXBean");
106 } catch (ClassNotFoundException x) {
107 isSupported = false;
108 }
109 return isSupported;
110 }
111
112 public boolean isRemoteDiagnosticCommandsSupported() {
113 return remoteDiagnosticCommandsSupport;
114 }
115
116 public native boolean isThreadContentionMonitoringEnabled();
117 public native boolean isThreadCpuTimeEnabled();
118 public native boolean isThreadAllocatedMemoryEnabled();
119
120 // AOT Subsystem
121 public native String getAOTMode();
122 public native boolean isAOTRecording();
123 public native long getAOTRecordingDuration();
124 public native boolean endAOTRecording();
125
126 // Class Loading Subsystem
127 public int getLoadedClassCount() {
128 long count = getTotalClassCount() - getUnloadedClassCount();
129 return (int) count;
130 }
131 public native long getTotalClassCount();
132 public native long getUnloadedClassCount();
133
134 public native boolean getVerboseClass();
135
136 // Memory Subsystem
137 public native boolean getVerboseGC();
138
139 // Runtime Subsystem
140 public String getManagementVersion() {
141 return version;
142 }
143
144 public String getVmId() {
145 int pid = getProcessId();
146 String hostname = "localhost";
147 try {
148 hostname = InetAddress.getLocalHost().getHostName();
149 } catch (UnknownHostException e) {
150 // ignore
151 }
152
153 return pid + "@" + hostname;
154 }
155 private native int getProcessId();
156
157 public String getVmName() {
158 return System.getProperty("java.vm.name");
159 }
160
161 public String getVmVendor() {
162 return System.getProperty("java.vm.vendor");
163 }
164 public String getVmVersion() {
165 return System.getProperty("java.vm.version");
166 }
167 public String getVmSpecName() {
168 return System.getProperty("java.vm.specification.name");
169 }
170 public String getVmSpecVendor() {
171 return System.getProperty("java.vm.specification.vendor");
172 }
173 public String getVmSpecVersion() {
174 return System.getProperty("java.vm.specification.version");
175 }
176 public String getClassPath() {
177 return System.getProperty("java.class.path");
178 }
179 public String getLibraryPath() {
180 return System.getProperty("java.library.path");
181 }
182
183 public String getBootClassPath( ) {
184 throw new UnsupportedOperationException(
185 "Boot class path mechanism is not supported");
186 }
187
188 public long getUptime() {
189 return getUptime0();
190 }
191
192 private List<String> vmArgs = null;
193 public synchronized List<String> getVmArguments() {
194 if (vmArgs == null) {
195 String[] args = getVmArguments0();
196 List<String> l = ((args != null && args.length != 0) ? Arrays.asList(args) :
197 Collections.<String>emptyList());
198 vmArgs = Collections.unmodifiableList(l);
199 }
200 return vmArgs;
201 }
202 public native String[] getVmArguments0();
203
204 public native long getStartupTime();
205 private native long getUptime0();
206 public native int getAvailableProcessors();
207
208 // Compilation Subsystem
209 public String getCompilerName() {
210 return System.getProperty("sun.management.compiler");
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 = Perf.getPerf();
256 try {
257 ByteBuffer bb = perf.attach(0);
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 }