History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/management/ThreadImpl.java (1 of 18352 files). Show all

Unified · Split

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