Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way of getting thread CPU time using JMX

Tags:

java

jmx

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.

alt text

like image 755
Robert Munteanu Avatar asked Jun 09 '09 16:06

Robert Munteanu


1 Answers

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.

like image 99
markusk Avatar answered Sep 22 '22 03:09

markusk