I'm currently getting the total thread CPU time using JMX in the following manner:
private long calculateTotalThreadCpuTime(ThreadMXBean thread) {
long totalTime = 0l;
for (ThreadInfo threadInfo : thread.dumpAllThreads(false, false))
totalTime += thread.getThreadCpuTime(threadInfo.getThreadId());
return totalTime;
}
As the ThreadMXBean is actually a remote proxy, performance is dreadful, in the order of magnitude of seconds for this actual method call.
Is there a faster way of doing this?
Update: I'm using this for performance monitoring. The measurements were both 'wall clock' time and JProfiler, which showed about 85% of my time spent in this method. I do have some other MXBean calls ( Runtime, Memory, GC ), but they are much cheaper. Most likely because every call to thread.getThreadCpuTime
is a remote one.
Update 2: JProfiler screenshot showing the performance problems.
If you are willing to use non-standard APIs, you can cast OperatingSystemMXBean
to com.sun.management.OperatingSystemMXBean
and invoke getProcessCpuTime()
, as described in Using a Sun internal class to get JVM CPU time on David Robert Nadeau's blog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With