Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`Monitor cpu usage per thread in java?

I would like to ask whether there is some simple way to determine cpu usage per thread in java. Thanks

like image 306
awk Avatar asked Apr 16 '09 12:04

awk


People also ask

How do I identify high CPU utilization by Java threads on Linux?

Press Shift + h and wait a few seconds. You can see the Show threads on message in the top console. Now, you can see thread-level details like CPU and memory utilization. You'll notice that a java command thread with PID 7087 is utilizing 94% of the CPU.

How do I monitor JVM threads?

The simplest way to see the number of threads in Java is to use a graphical tool like Java VisualVM. Apart from the application threads, Java VisualVM also lists the GC or any other threads used by the application like JMX threads. Monitoring the number of threads is the most basic feature in Java VisualVM.


1 Answers

I believe the JConsole (archived link) does provide this kind of information through a plugin

JConsole Thread CPU usage, after blogs.oracle.com/lmalventosa/resource/thread_cpu_usage.jpg

It uses ThreadMXBean getThreadCpuTime() function.

Something along the line of:

        long upTime = runtimeProxy.getUptime();         List<Long> threadCpuTime = new ArrayList<Long>();         for (int i = 0; i < threadIds.size(); i++) {             long threadId = threadIds.get(i);             if (threadId != -1) {                 threadCpuTime.add(threadProxy.getThreadCpuTime(threadId));             } else {                 threadCpuTime.add(0L);             }         }         int nCPUs = osProxy.getAvailableProcessors();         List<Float> cpuUsageList = new ArrayList<Float>();         if (prevUpTime > 0L && upTime > prevUpTime) {             // elapsedTime is in ms             long elapsedTime = upTime - prevUpTime;             for (int i = 0; i < threadIds.size(); i++) {                 // elapsedCpu is in ns                 long elapsedCpu = threadCpuTime.get(i) - prevThreadCpuTime.get(i);                 // cpuUsage could go higher than 100% because elapsedTime                 // and elapsedCpu are not fetched simultaneously. Limit to                 // 99% to avoid Chart showing a scale from 0% to 200%.                 float cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 1000000F * nCPUs));                 cpuUsageList.add(cpuUsage);             }         } 
like image 56
VonC Avatar answered Sep 22 '22 22:09

VonC