Skip to content

Commit c35855d

Browse files
committed
fallback to getThreadCpuTime(long) if getThreadCpuTime(long[]) is not available
1 parent 247378a commit c35855d

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

visualvm/sampler/src/com/sun/tools/visualvm/sampler/cpu/ThreadsCPU.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.lang.management.ManagementFactory;
3030
import java.lang.management.ThreadInfo;
3131
import java.lang.management.ThreadMXBean;
32+
import java.util.logging.Level;
33+
import java.util.logging.Logger;
3234
import javax.management.InstanceNotFoundException;
3335
import javax.management.MBeanException;
3436
import javax.management.MBeanServerConnection;
@@ -42,9 +44,11 @@
4244
*/
4345
public class ThreadsCPU {
4446
private static final ObjectName THREAD_NAME = getThreadName();
47+
private static final Logger LOGGER = Logger.getLogger(ThreadsCPU.class.getName());
4548

4649
private final ThreadMXBean threadBean;
4750
private final MBeanServerConnection connection;
51+
private boolean useBulkOperation = true;
4852

4953
public ThreadsCPU(ThreadMXBean bean, MBeanServerConnection conn) {
5054
threadBean = bean;
@@ -54,9 +58,26 @@ public ThreadsCPU(ThreadMXBean bean, MBeanServerConnection conn) {
5458
public ThreadsCPUInfo getThreadsCPUInfo() throws MBeanException, ReflectionException, IOException, InstanceNotFoundException {
5559
long[] ids = threadBean.getAllThreadIds();
5660
ThreadInfo[] tids = threadBean.getThreadInfo(ids);
57-
Object[] args = new Object[] {ids};
58-
String[] sigs = new String[] {"[J"}; // NOI18N
59-
long[] tinfo = (long[])connection.invoke(THREAD_NAME, "getThreadCpuTime", args, sigs); // NOI18N
61+
long[] tinfo;
62+
63+
if (useBulkOperation) {
64+
Object[] args = new Object[] {ids};
65+
String[] sigs = new String[] {"[J"}; // NOI18N
66+
67+
try {
68+
tinfo = (long[])connection.invoke(THREAD_NAME, "getThreadCpuTime", args, sigs); // NOI18N
69+
} catch (javax.management.ReflectionException ex) {
70+
LOGGER.log(Level.INFO, "getThreadCpuTime failed", ex);
71+
useBulkOperation = false;
72+
return getThreadsCPUInfo();
73+
}
74+
} else {
75+
tinfo = new long[ids.length];
76+
77+
for (int i = 0; i < ids.length; i++) {
78+
tinfo[i] = threadBean.getThreadCpuTime(ids[i]);
79+
}
80+
}
6081
long time = System.currentTimeMillis();
6182

6283
return new ThreadsCPUInfo(time,tids,tinfo);

0 commit comments

Comments
 (0)