author | phh |
Thu, 20 Jan 2011 19:34:40 -0500 | |
changeset 8001 | 192adf3627b7 |
parent 5506 | 202f599c92aa |
child 8002 | 5d022b92fbef |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.management; |
|
27 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
28 |
import java.lang.management.ManagementFactory; |
2 | 29 |
|
30 |
import java.lang.management.ThreadInfo; |
|
31 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
32 |
import javax.management.ObjectName; |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
33 |
|
2 | 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 |
*/ |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
41 |
class ThreadImpl implements com.sun.management.ThreadMXBean { |
2 | 42 |
|
43 |
private final VMManagement jvm; |
|
44 |
||
45 |
// default for thread contention monitoring is disabled. |
|
46 |
private boolean contentionMonitoringEnabled = false; |
|
47 |
private boolean cpuTimeEnabled; |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
48 |
private boolean allocatedMemoryEnabled; |
2 | 49 |
|
50 |
/** |
|
51 |
* Constructor of ThreadImpl class. |
|
52 |
*/ |
|
53 |
ThreadImpl(VMManagement vm) { |
|
54 |
this.jvm = vm; |
|
55 |
this.cpuTimeEnabled = jvm.isThreadCpuTimeEnabled(); |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
56 |
this.allocatedMemoryEnabled = jvm.isThreadAllocatedMemoryEnabled(); |
2 | 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 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
95 |
public boolean isThreadAllocatedMemorySupported() { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
96 |
return jvm.isThreadAllocatedMemorySupported(); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
97 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
98 |
|
2 | 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 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
108 |
public boolean isThreadAllocatedMemoryEnabled() { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
109 |
if (!isThreadAllocatedMemorySupported()) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
110 |
throw new UnsupportedOperationException( |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
111 |
"Thread allocated memory measurement is not supported"); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
112 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
113 |
return allocatedMemoryEnabled; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
114 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
115 |
|
2 | 116 |
public long[] getAllThreadIds() { |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
117 |
Util.checkMonitorAccess(); |
2 | 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 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
147 |
private void verifyThreadIds(long[] ids) { |
2 | 148 |
if (ids == null) { |
149 |
throw new NullPointerException("Null ids parameter."); |
|
150 |
} |
|
151 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
152 |
for (int i = 0; i < ids.length; i++) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
153 |
if (ids[i] <= 0) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
154 |
throw new IllegalArgumentException( |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
155 |
"Invalid thread ID parameter: " + ids[i]); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
156 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
157 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
158 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
159 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
160 |
public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
161 |
verifyThreadIds(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
162 |
|
2 | 163 |
if (maxDepth < 0) { |
164 |
throw new IllegalArgumentException( |
|
165 |
"Invalid maxDepth parameter: " + maxDepth); |
|
166 |
} |
|
167 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
168 |
Util.checkMonitorAccess(); |
2 | 169 |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
170 |
ThreadInfo[] infos = new ThreadInfo[ids.length]; // nulls |
2 | 171 |
if (maxDepth == Integer.MAX_VALUE) { |
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
172 |
getThreadInfo1(ids, -1, infos); |
2 | 173 |
} else { |
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
174 |
getThreadInfo1(ids, maxDepth, infos); |
2 | 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 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
185 |
Util.checkControlAccess(); |
2 | 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 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
203 |
private boolean verifyCurrentThreadCpuTime() { |
2 | 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 |
} |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
209 |
return isThreadCpuTimeEnabled(); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
210 |
} |
2 | 211 |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
212 |
public long getCurrentThreadCpuTime() { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
213 |
if (verifyCurrentThreadCpuTime()) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
214 |
return getThreadTotalCpuTime0(0); |
2 | 215 |
} |
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
216 |
return -1; |
2 | 217 |
} |
218 |
||
219 |
public long getThreadCpuTime(long id) { |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
220 |
long[] ids = new long[1]; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
221 |
ids[0] = id; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
222 |
final long[] times = getThreadCpuTime(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
223 |
return times[0]; |
2 | 224 |
} |
225 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
226 |
private boolean verifyThreadCpuTime(long[] ids) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
227 |
verifyThreadIds(ids); |
2 | 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 |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
238 |
for (int i = 0; i < ids.length; i++) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
239 |
if (ids[i] != Thread.currentThread().getId()) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
240 |
throw new UnsupportedOperationException( |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
241 |
"Thread CPU time measurement is only supported" + |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
242 |
" for the current thread."); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
243 |
} |
2 | 244 |
} |
245 |
} |
|
246 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
247 |
return isThreadCpuTimeEnabled(); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
248 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
249 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
250 |
public long[] getThreadCpuTime(long[] ids) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
251 |
boolean verified = verifyThreadCpuTime(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
252 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
253 |
int length = ids.length; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
254 |
long[] times = java.util.Arrays.fill(new long[length], -1); |
2 | 255 |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
256 |
if (verified) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
257 |
if (length == 1) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
258 |
long id = ids[0]; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
259 |
if (id == Thread.currentThread().getId()) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
260 |
id = 0; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
261 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
262 |
getThreadTotalCpuTime0(id); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
263 |
} else { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
264 |
getThreadTotalCpuTime1(ids, times); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
265 |
} |
2 | 266 |
} |
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
267 |
return times; |
2 | 268 |
} |
269 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
270 |
public long getCurrentThreadUserTime() { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
271 |
if (verifyCurrentThreadCpuTime()) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
272 |
return getThreadUserCpuTime0(0); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
273 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
274 |
return -1; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
275 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
276 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
277 |
public long getThreadUserTime(long id) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
278 |
long[] ids = new long[1]; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
279 |
ids[0] = id; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
280 |
final long[] times = getThreadUserTime(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
281 |
return times[0]; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
282 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
283 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
284 |
public long[] getThreadUserTime(long[] ids) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
285 |
boolean verified = verifyThreadCpuTime(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
286 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
287 |
int length = ids.length; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
288 |
long[] times = java.util.Arrays.fill(new long[length], -1); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
289 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
290 |
if (verified) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
291 |
if (length == 1) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
292 |
long id = ids[0]; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
293 |
if (id == Thread.currentThread().getId()) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
294 |
id = 0; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
295 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
296 |
times[0] = getThreadUserCpuTime0(id); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
297 |
} else { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
298 |
getThreadUserCpuTime1(ids, times); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
299 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
300 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
301 |
return times; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
302 |
} |
2 | 303 |
|
304 |
public void setThreadCpuTimeEnabled(boolean enable) { |
|
305 |
if (!isThreadCpuTimeSupported() && |
|
306 |
!isCurrentThreadCpuTimeSupported()) { |
|
307 |
throw new UnsupportedOperationException( |
|
308 |
"Thread CPU time measurement is not supported"); |
|
309 |
} |
|
310 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
311 |
Util.checkControlAccess(); |
2 | 312 |
synchronized (this) { |
313 |
if (cpuTimeEnabled != enable) { |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
314 |
// notify VM of the state change |
2 | 315 |
setThreadCpuTimeEnabled0(enable); |
316 |
cpuTimeEnabled = enable; |
|
317 |
} |
|
318 |
} |
|
319 |
} |
|
320 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
321 |
public long getThreadAllocatedBytes(long id) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
322 |
long[] ids = new long[1]; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
323 |
ids[0] = id; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
324 |
final long[] sizes = getThreadAllocatedBytes(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
325 |
return sizes[0]; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
326 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
327 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
328 |
private boolean verifyThreadAllocatedMemory(long[] ids) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
329 |
verifyThreadIds(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
330 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
331 |
// check if Thread allocated memory measurement is supported. |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
332 |
if (!isThreadAllocatedMemorySupported()) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
333 |
throw new UnsupportedOperationException( |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
334 |
"Thread allocated memory measurement is not supported."); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
335 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
336 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
337 |
return isThreadAllocatedMemoryEnabled(); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
338 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
339 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
340 |
public long[] getThreadAllocatedBytes(long[] ids) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
341 |
boolean verified = verifyThreadAllocatedMemory(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
342 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
343 |
long[] times = java.util.Arrays.fill(new long[length], -1); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
344 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
345 |
if (verified) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
346 |
getThreadAllocatedMemory1(ids, sizes); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
347 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
348 |
return sizes; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
349 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
350 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
351 |
public void setThreadAllocatedMemoryEnabled(boolean enable) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
352 |
if (!isThreadAllocatedMemorySupported()) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
353 |
throw new UnsupportedOperationException( |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
354 |
"Thread allocated memory measurement is not supported."); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
355 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
356 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
357 |
Util.checkControlAccess(); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
358 |
synchronized (this) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
359 |
if (allocatedMemoryEnabled != enable) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
360 |
// notify VM of the state change |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
361 |
setThreadAllocatedMemoryEnabled0(enable); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
362 |
allocatedMemoryEnabled = enable; |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
363 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
364 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
365 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
366 |
|
2 | 367 |
public long[] findMonitorDeadlockedThreads() { |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
368 |
Util.checkMonitorAccess(); |
2 | 369 |
|
370 |
Thread[] threads = findMonitorDeadlockedThreads0(); |
|
371 |
if (threads == null) { |
|
372 |
return null; |
|
373 |
} |
|
374 |
||
375 |
long[] ids = new long[threads.length]; |
|
376 |
for (int i = 0; i < threads.length; i++) { |
|
377 |
Thread t = threads[i]; |
|
378 |
ids[i] = t.getId(); |
|
379 |
} |
|
380 |
return ids; |
|
381 |
} |
|
382 |
||
383 |
public long[] findDeadlockedThreads() { |
|
384 |
if (!isSynchronizerUsageSupported()) { |
|
385 |
throw new UnsupportedOperationException( |
|
386 |
"Monitoring of Synchronizer Usage is not supported."); |
|
387 |
} |
|
388 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
389 |
Util.checkMonitorAccess(); |
2 | 390 |
|
391 |
Thread[] threads = findDeadlockedThreads0(); |
|
392 |
if (threads == null) { |
|
393 |
return null; |
|
394 |
} |
|
395 |
||
396 |
long[] ids = new long[threads.length]; |
|
397 |
for (int i = 0; i < threads.length; i++) { |
|
398 |
Thread t = threads[i]; |
|
399 |
ids[i] = t.getId(); |
|
400 |
} |
|
401 |
return ids; |
|
402 |
} |
|
403 |
||
404 |
public void resetPeakThreadCount() { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
405 |
Util.checkControlAccess(); |
2 | 406 |
resetPeakThreadCount0(); |
407 |
} |
|
408 |
||
409 |
public boolean isObjectMonitorUsageSupported() { |
|
410 |
return jvm.isObjectMonitorUsageSupported(); |
|
411 |
} |
|
412 |
||
413 |
public boolean isSynchronizerUsageSupported() { |
|
414 |
return jvm.isSynchronizerUsageSupported(); |
|
415 |
} |
|
416 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
417 |
private void verifyDumpThreads(boolean lockedMonitors, |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
418 |
boolean lockedSynchronizers) { |
2 | 419 |
if (lockedMonitors && !isObjectMonitorUsageSupported()) { |
420 |
throw new UnsupportedOperationException( |
|
421 |
"Monitoring of Object Monitor Usage is not supported."); |
|
422 |
} |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
423 |
|
2 | 424 |
if (lockedSynchronizers && !isSynchronizerUsageSupported()) { |
425 |
throw new UnsupportedOperationException( |
|
426 |
"Monitoring of Synchronizer Usage is not supported."); |
|
427 |
} |
|
428 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
429 |
Util.checkMonitorAccess(); |
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
430 |
} |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
431 |
|
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
432 |
public ThreadInfo[] getThreadInfo(long[] ids, |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
433 |
boolean lockedMonitors, |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
434 |
boolean lockedSynchronizers) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
435 |
verifyThreadIds(ids); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
436 |
verifyDumpThreads(lockedMonitors, lockedSynchronizers); |
2 | 437 |
return dumpThreads0(ids, lockedMonitors, lockedSynchronizers); |
438 |
} |
|
439 |
||
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
440 |
public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
441 |
boolean lockedSynchronizers) { |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
442 |
verifyDumpThreads(lockedMonitors, lockedSynchronizers); |
2 | 443 |
return dumpThreads0(null, lockedMonitors, lockedSynchronizers); |
444 |
} |
|
445 |
||
446 |
// VM support where maxDepth == -1 to request entire stack dump |
|
447 |
private static native Thread[] getThreads(); |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
448 |
private static native void getThreadInfo1(long[] ids, |
2 | 449 |
int maxDepth, |
450 |
ThreadInfo[] result); |
|
451 |
private static native long getThreadTotalCpuTime0(long id); |
|
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
452 |
private static native void getThreadTotalCpuTime1(long[] ids, long[] result); |
2 | 453 |
private static native long getThreadUserCpuTime0(long id); |
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
454 |
private static native void getThreadUserCpuTime1(long[] ids, long[] result); |
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
455 |
private static native void getThreadAllocatedMemory1(long[] ids, long[] result); |
2 | 456 |
private static native void setThreadCpuTimeEnabled0(boolean enable); |
8001
192adf3627b7
6173675: M&M: approximate memory allocation rate/amount per thread
phh
parents:
5506
diff
changeset
|
457 |
private static native void setThreadAllocatedMemoryEnabled0(boolean enable); |
2 | 458 |
private static native void setThreadContentionMonitoringEnabled0(boolean enable); |
459 |
private static native Thread[] findMonitorDeadlockedThreads0(); |
|
460 |
private static native Thread[] findDeadlockedThreads0(); |
|
461 |
private static native void resetPeakThreadCount0(); |
|
462 |
private static native ThreadInfo[] dumpThreads0(long[] ids, |
|
463 |
boolean lockedMonitors, |
|
464 |
boolean lockedSynchronizers); |
|
465 |
||
466 |
// tid == 0 to reset contention times for all threads |
|
467 |
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
|
468 |
|
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
469 |
public ObjectName getObjectName() { |
4156 | 470 |
return Util.newObjectName(ManagementFactory.THREAD_MXBEAN_NAME); |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
471 |
} |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
472 |
|
2 | 473 |
} |