Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining CPU thread usage in Java

I have a question around getting CPU utilization for a given JNI block. I'm making some intensive CPU computation in the underlying C++ JNI native method. I'm in the process of optimizing this computation and want to benchmark it against varying inputs.

I need some guidance on how to go about measuring this. The alternatives I have considered so far are

  • Using JMX ThreadMXBean to measure system CPU usage for the current thread that invokes call into JNI method. However, I am not sure if JNI code is executed within the invoking thread context. What happens when the thread spawns more threads?

  • Using JMX OperatingSystemMXBean to get the CPU usage for the entire JVM. Ideally, this is not want I want as there could be parallel executions in JVM that might tweak the benchmarking.

  • Measure externally using getrusage(..). What I want to know here is that how is it different than using OperatingSystemMXBean.

like image 507
kuriouscoder Avatar asked Apr 28 '15 22:04

kuriouscoder


People also ask

How do I find out how many threads I have in Java?

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.

How do I check CPU and memory usage in Java?

Using VisualVM (jvisualvm) jvisualvm is a tool to analyse the runtime behavior of your Java application. It allows you to trace a running Java program and see its the memory and CPU consumption. You can also use it to create a memory heap dump to analyze the objects in the heap.

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.


1 Answers

You can use ThreadMXBean to get cpu usage statistics from all running threads. In the example below the CPU usage per thread is calculated:

private int sampleTime = 10000;
private ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
private RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
private OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
private Map<Long, Long> threadInitialCPU = new HashMap<Long, Long>();
private Map<Long, Float> threadCPUUsage = new HashMap<Long, Float>();
private long initialUptime = runtimeMxBean.getUptime();

ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(false, false);
for (ThreadInfo info : threadInfos) {
    threadInitialCPU.put(info.getThreadId(), threadMxBean.getThreadCpuTime(info.getThreadId()));
}

try {Thread.sleep(sampleTime);} catch (InterruptedException e) {}

long upTime = runtimeMxBean.getUptime();

Map<Long, Long> threadCurrentCPU = new HashMap<Long, Long>();
ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(false, false);
for (ThreadInfo info : threadInfos) {
    threadCurrentCPU.put(info.getThreadId(), threadMxBean.getThreadCpuTime(info.getThreadId()));
}

// CPU over all processes
//int nrCPUs = osMxBean.getAvailableProcessors();
// total CPU: CPU % can be more than 100% (devided over multiple cpus)
long nrCPUs = 1;
// elapsedTime is in ms.
long elapsedTime = (upTime - initialUptime);
for (ThreadInfo info : threadInfos) {
    // elapsedCpu is in ns
    Long initialCPU = threadInitialCPU.get(info.getThreadId());
    if (initialCPU != null) {
        long elapsedCpu = threadCurrentCPU.get(info.getThreadId()) - initialCPU;
        float cpuUsage = elapsedCpu / (elapsedTime * 1000000F * nrCPUs);
        threadCPUUsage.put(info.getThreadId(), cpuUsage);
    }
}

// threadCPUUsage contains cpu % per thread
System.out.println(threadCPUUsage);
// You can use osMxBean.getThreadInfo(theadId) to get information on every thread reported in threadCPUUsage and analyze the most CPU intentive threads
like image 123
R. Oosterholt Avatar answered Oct 19 '22 16:10

R. Oosterholt