author | mchung |
Thu, 10 Apr 2008 10:47:13 -0700 | |
changeset 401 | ef01e0dccd63 |
parent 2 | 90ce3da70b43 |
child 715 | f16baef3a20e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2003-2005 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 java.lang.management.ThreadMXBean; |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
29 |
import java.lang.management.ManagementFactory; |
2 | 30 |
|
31 |
import java.lang.management.ThreadInfo; |
|
32 |
import java.lang.management.LockInfo; |
|
33 |
import java.lang.management.MonitorInfo; |
|
34 |
import java.util.Map; |
|
35 |
import java.util.HashMap; |
|
36 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
37 |
import javax.management.ObjectName; |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
38 |
|
2 | 39 |
/** |
40 |
* Implementation class for the thread subsystem. |
|
41 |
* Standard and committed hotspot-specific metrics if any. |
|
42 |
* |
|
43 |
* ManagementFactory.getThreadMXBean() returns an instance |
|
44 |
* of this class. |
|
45 |
*/ |
|
46 |
class ThreadImpl implements ThreadMXBean { |
|
47 |
||
48 |
private final VMManagement jvm; |
|
49 |
||
50 |
// default for thread contention monitoring is disabled. |
|
51 |
private boolean contentionMonitoringEnabled = false; |
|
52 |
private boolean cpuTimeEnabled; |
|
53 |
||
54 |
/** |
|
55 |
* Constructor of ThreadImpl class. |
|
56 |
*/ |
|
57 |
ThreadImpl(VMManagement vm) { |
|
58 |
this.jvm = vm; |
|
59 |
this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled(); |
|
60 |
} |
|
61 |
||
62 |
public int getThreadCount() { |
|
63 |
return jvm.getLiveThreadCount(); |
|
64 |
} |
|
65 |
||
66 |
public int getPeakThreadCount() { |
|
67 |
return jvm.getPeakThreadCount(); |
|
68 |
} |
|
69 |
||
70 |
public long getTotalStartedThreadCount() { |
|
71 |
return jvm.getTotalThreadCount(); |
|
72 |
} |
|
73 |
||
74 |
public int getDaemonThreadCount() { |
|
75 |
return jvm.getDaemonThreadCount(); |
|
76 |
} |
|
77 |
||
78 |
public boolean isThreadContentionMonitoringSupported() { |
|
79 |
return jvm.isThreadContentionMonitoringSupported(); |
|
80 |
} |
|
81 |
||
82 |
public synchronized boolean isThreadContentionMonitoringEnabled() { |
|
83 |
if (!isThreadContentionMonitoringSupported()) { |
|
84 |
throw new UnsupportedOperationException( |
|
85 |
"Thread contention monitoring is not supported."); |
|
86 |
} |
|
87 |
return contentionMonitoringEnabled; |
|
88 |
} |
|
89 |
||
90 |
public boolean isThreadCpuTimeSupported() { |
|
91 |
return jvm.isOtherThreadCpuTimeSupported(); |
|
92 |
} |
|
93 |
||
94 |
public boolean isCurrentThreadCpuTimeSupported() { |
|
95 |
return jvm.isCurrentThreadCpuTimeSupported(); |
|
96 |
} |
|
97 |
||
98 |
public boolean isThreadCpuTimeEnabled() { |
|
99 |
if (!isThreadCpuTimeSupported() && |
|
100 |
!isCurrentThreadCpuTimeSupported()) { |
|
101 |
throw new UnsupportedOperationException( |
|
102 |
"Thread CPU time measurement is not supported"); |
|
103 |
} |
|
104 |
return cpuTimeEnabled; |
|
105 |
} |
|
106 |
||
107 |
public long[] getAllThreadIds() { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
108 |
Util.checkMonitorAccess(); |
2 | 109 |
|
110 |
Thread[] threads = getThreads(); |
|
111 |
int length = threads.length; |
|
112 |
long[] ids = new long[length]; |
|
113 |
for (int i = 0; i < length; i++) { |
|
114 |
Thread t = threads[i]; |
|
115 |
ids[i] = t.getId(); |
|
116 |
} |
|
117 |
return ids; |
|
118 |
} |
|
119 |
||
120 |
public ThreadInfo getThreadInfo(long id) { |
|
121 |
if (id <= 0) { |
|
122 |
throw new IllegalArgumentException( |
|
123 |
"Invalid thread ID parameter: " + id); |
|
124 |
} |
|
125 |
||
126 |
long[] ids = new long[1]; |
|
127 |
ids[0] = id; |
|
128 |
final ThreadInfo[] infos = getThreadInfo(ids, 0); |
|
129 |
return infos[0]; |
|
130 |
} |
|
131 |
||
132 |
public ThreadInfo getThreadInfo(long id, int maxDepth) { |
|
133 |
if (id <= 0) { |
|
134 |
throw new IllegalArgumentException( |
|
135 |
"Invalid thread ID parameter: " + id); |
|
136 |
} |
|
137 |
if (maxDepth < 0) { |
|
138 |
throw new IllegalArgumentException( |
|
139 |
"Invalid maxDepth parameter: " + maxDepth); |
|
140 |
} |
|
141 |
||
142 |
long[] ids = new long[1]; |
|
143 |
ids[0] = id; |
|
144 |
final ThreadInfo[] infos = getThreadInfo(ids, maxDepth); |
|
145 |
return infos[0]; |
|
146 |
} |
|
147 |
||
148 |
public ThreadInfo[] getThreadInfo(long[] ids) { |
|
149 |
return getThreadInfo(ids, 0); |
|
150 |
} |
|
151 |
||
152 |
public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) { |
|
153 |
if (ids == null) { |
|
154 |
throw new NullPointerException("Null ids parameter."); |
|
155 |
} |
|
156 |
||
157 |
if (maxDepth < 0) { |
|
158 |
throw new IllegalArgumentException( |
|
159 |
"Invalid maxDepth parameter: " + maxDepth); |
|
160 |
} |
|
161 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
162 |
Util.checkMonitorAccess(); |
2 | 163 |
|
164 |
ThreadInfo[] infos = new ThreadInfo[ids.length]; |
|
165 |
if (maxDepth == Integer.MAX_VALUE) { |
|
166 |
getThreadInfo0(ids, -1, infos); |
|
167 |
} else { |
|
168 |
getThreadInfo0(ids, maxDepth, infos); |
|
169 |
} |
|
170 |
return infos; |
|
171 |
} |
|
172 |
||
173 |
||
174 |
||
175 |
public void setThreadContentionMonitoringEnabled(boolean enable) { |
|
176 |
if (!isThreadContentionMonitoringSupported()) { |
|
177 |
throw new UnsupportedOperationException( |
|
178 |
"Thread contention monitoring is not supported"); |
|
179 |
} |
|
180 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
181 |
Util.checkControlAccess(); |
2 | 182 |
|
183 |
synchronized (this) { |
|
184 |
if (contentionMonitoringEnabled != enable) { |
|
185 |
if (enable) { |
|
186 |
// if reeabled, reset contention time statistics |
|
187 |
// for all threads |
|
188 |
resetContentionTimes0(0); |
|
189 |
} |
|
190 |
||
191 |
// update the VM of the state change |
|
192 |
setThreadContentionMonitoringEnabled0(enable); |
|
193 |
||
194 |
contentionMonitoringEnabled = enable; |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
public long getCurrentThreadCpuTime() { |
|
200 |
// check if Thread CPU time measurement is supported. |
|
201 |
if (!isCurrentThreadCpuTimeSupported()) { |
|
202 |
throw new UnsupportedOperationException( |
|
203 |
"Current thread CPU time measurement is not supported."); |
|
204 |
} |
|
205 |
||
206 |
if (!isThreadCpuTimeEnabled()) { |
|
207 |
return -1; |
|
208 |
} |
|
209 |
||
210 |
return getThreadTotalCpuTime0(0); |
|
211 |
} |
|
212 |
||
213 |
public long getThreadCpuTime(long id) { |
|
214 |
// check if Thread CPU time measurement is supported. |
|
215 |
if (!isThreadCpuTimeSupported() && |
|
216 |
!isCurrentThreadCpuTimeSupported()) { |
|
217 |
throw new UnsupportedOperationException( |
|
218 |
"Thread CPU Time Measurement is not supported."); |
|
219 |
} |
|
220 |
||
221 |
if (!isThreadCpuTimeSupported()) { |
|
222 |
// support current thread only |
|
223 |
if (id != Thread.currentThread().getId()) { |
|
224 |
throw new UnsupportedOperationException( |
|
225 |
"Thread CPU Time Measurement is only supported" + |
|
226 |
" for the current thread."); |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
if (id <= 0) { |
|
231 |
throw new IllegalArgumentException( |
|
232 |
"Invalid thread ID parameter: " + id); |
|
233 |
} |
|
234 |
||
235 |
if (!isThreadCpuTimeEnabled()) { |
|
236 |
return -1; |
|
237 |
} |
|
238 |
||
239 |
if (id == Thread.currentThread().getId()) { |
|
240 |
// current thread |
|
241 |
return getThreadTotalCpuTime0(0); |
|
242 |
} else { |
|
243 |
return getThreadTotalCpuTime0(id); |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
public long getCurrentThreadUserTime() { |
|
248 |
// check if Thread CPU time measurement is supported. |
|
249 |
if (!isCurrentThreadCpuTimeSupported()) { |
|
250 |
throw new UnsupportedOperationException( |
|
251 |
"Current thread CPU time measurement is not supported."); |
|
252 |
} |
|
253 |
||
254 |
if (!isThreadCpuTimeEnabled()) { |
|
255 |
return -1; |
|
256 |
} |
|
257 |
||
258 |
return getThreadUserCpuTime0(0); |
|
259 |
} |
|
260 |
||
261 |
public long getThreadUserTime(long id) { |
|
262 |
// check if Thread CPU time measurement is supported. |
|
263 |
if (!isThreadCpuTimeSupported() && |
|
264 |
!isCurrentThreadCpuTimeSupported()) { |
|
265 |
throw new UnsupportedOperationException( |
|
266 |
"Thread CPU time measurement is not supported."); |
|
267 |
} |
|
268 |
||
269 |
if (!isThreadCpuTimeSupported()) { |
|
270 |
// support current thread only |
|
271 |
if (id != Thread.currentThread().getId()) { |
|
272 |
throw new UnsupportedOperationException( |
|
273 |
"Thread CPU time measurement is only supported" + |
|
274 |
" for the current thread."); |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
if (id <= 0) { |
|
279 |
throw new IllegalArgumentException( |
|
280 |
"Invalid thread ID parameter: " + id); |
|
281 |
} |
|
282 |
||
283 |
if (!isThreadCpuTimeEnabled()) { |
|
284 |
return -1; |
|
285 |
} |
|
286 |
||
287 |
if (id == Thread.currentThread().getId()) { |
|
288 |
// current thread |
|
289 |
return getThreadUserCpuTime0(0); |
|
290 |
} else { |
|
291 |
return getThreadUserCpuTime0(id); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
||
296 |
public void setThreadCpuTimeEnabled(boolean enable) { |
|
297 |
if (!isThreadCpuTimeSupported() && |
|
298 |
!isCurrentThreadCpuTimeSupported()) { |
|
299 |
throw new UnsupportedOperationException( |
|
300 |
"Thread CPU time measurement is not supported"); |
|
301 |
} |
|
302 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
303 |
Util.checkControlAccess(); |
2 | 304 |
synchronized (this) { |
305 |
if (cpuTimeEnabled != enable) { |
|
306 |
// update VM of the state change |
|
307 |
setThreadCpuTimeEnabled0(enable); |
|
308 |
cpuTimeEnabled = enable; |
|
309 |
} |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
public long[] findMonitorDeadlockedThreads() { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
314 |
Util.checkMonitorAccess(); |
2 | 315 |
|
316 |
Thread[] threads = findMonitorDeadlockedThreads0(); |
|
317 |
if (threads == null) { |
|
318 |
return null; |
|
319 |
} |
|
320 |
||
321 |
long[] ids = new long[threads.length]; |
|
322 |
for (int i = 0; i < threads.length; i++) { |
|
323 |
Thread t = threads[i]; |
|
324 |
ids[i] = t.getId(); |
|
325 |
} |
|
326 |
return ids; |
|
327 |
} |
|
328 |
||
329 |
public long[] findDeadlockedThreads() { |
|
330 |
if (!isSynchronizerUsageSupported()) { |
|
331 |
throw new UnsupportedOperationException( |
|
332 |
"Monitoring of Synchronizer Usage is not supported."); |
|
333 |
} |
|
334 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
335 |
Util.checkMonitorAccess(); |
2 | 336 |
|
337 |
Thread[] threads = findDeadlockedThreads0(); |
|
338 |
if (threads == null) { |
|
339 |
return null; |
|
340 |
} |
|
341 |
||
342 |
long[] ids = new long[threads.length]; |
|
343 |
for (int i = 0; i < threads.length; i++) { |
|
344 |
Thread t = threads[i]; |
|
345 |
ids[i] = t.getId(); |
|
346 |
} |
|
347 |
return ids; |
|
348 |
} |
|
349 |
||
350 |
public void resetPeakThreadCount() { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
351 |
Util.checkControlAccess(); |
2 | 352 |
resetPeakThreadCount0(); |
353 |
} |
|
354 |
||
355 |
public boolean isObjectMonitorUsageSupported() { |
|
356 |
return jvm.isObjectMonitorUsageSupported(); |
|
357 |
} |
|
358 |
||
359 |
public boolean isSynchronizerUsageSupported() { |
|
360 |
return jvm.isSynchronizerUsageSupported(); |
|
361 |
} |
|
362 |
||
363 |
public ThreadInfo[] getThreadInfo(long[] ids, |
|
364 |
boolean lockedMonitors, |
|
365 |
boolean lockedSynchronizers) { |
|
366 |
if (ids == null) { |
|
367 |
throw new NullPointerException("Null ids parameter."); |
|
368 |
} |
|
369 |
||
370 |
if (lockedMonitors && !isObjectMonitorUsageSupported()) { |
|
371 |
throw new UnsupportedOperationException( |
|
372 |
"Monitoring of Object Monitor Usage is not supported."); |
|
373 |
} |
|
374 |
if (lockedSynchronizers && !isSynchronizerUsageSupported()) { |
|
375 |
throw new UnsupportedOperationException( |
|
376 |
"Monitoring of Synchronizer Usage is not supported."); |
|
377 |
} |
|
378 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
379 |
Util.checkMonitorAccess(); |
2 | 380 |
return dumpThreads0(ids, lockedMonitors, lockedSynchronizers); |
381 |
} |
|
382 |
||
383 |
||
384 |
public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers) { |
|
385 |
if (lockedMonitors && !isObjectMonitorUsageSupported()) { |
|
386 |
throw new UnsupportedOperationException( |
|
387 |
"Monitoring of Object Monitor Usage is not supported."); |
|
388 |
} |
|
389 |
if (lockedSynchronizers && !isSynchronizerUsageSupported()) { |
|
390 |
throw new UnsupportedOperationException( |
|
391 |
"Monitoring of Synchronizer Usage is not supported."); |
|
392 |
} |
|
393 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
394 |
Util.checkMonitorAccess(); |
2 | 395 |
return dumpThreads0(null, lockedMonitors, lockedSynchronizers); |
396 |
} |
|
397 |
||
398 |
// VM support where maxDepth == -1 to request entire stack dump |
|
399 |
private static native Thread[] getThreads(); |
|
400 |
private static native void getThreadInfo0(long[] ids, |
|
401 |
int maxDepth, |
|
402 |
ThreadInfo[] result); |
|
403 |
private static native long getThreadTotalCpuTime0(long id); |
|
404 |
private static native long getThreadUserCpuTime0(long id); |
|
405 |
private static native void setThreadCpuTimeEnabled0(boolean enable); |
|
406 |
private static native void setThreadContentionMonitoringEnabled0(boolean enable); |
|
407 |
private static native Thread[] findMonitorDeadlockedThreads0(); |
|
408 |
private static native Thread[] findDeadlockedThreads0(); |
|
409 |
private static native void resetPeakThreadCount0(); |
|
410 |
private static native ThreadInfo[] dumpThreads0(long[] ids, |
|
411 |
boolean lockedMonitors, |
|
412 |
boolean lockedSynchronizers); |
|
413 |
||
414 |
// tid == 0 to reset contention times for all threads |
|
415 |
private static native void resetContentionTimes0(long tid); |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
416 |
|
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
417 |
public ObjectName getObjectName() { |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
418 |
return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME); |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
419 |
} |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
420 |
|
2 | 421 |
} |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
422 |