Java VisualVM CPU usage and processor affinity
Asked Answered
W

1

8

In my experience today, I find that Oracle's Java VisualVM shows cpu usage as a percent of total machine cores, even when the JVM being monitored has a limited process affinity set in the OS. This is in the "monitor" tab.

Limiting the monitored jvm with taskset (on linux, Ubuntu), when the processors allowed to that jvm are near 100% utilization in htop, the cpu percentage shown in VisualVM is clearly equal to the total number of cpus divided by the number of processors allowed to the monitored jvm. The resolution of the scale is therefore inadequate for this case.

Can you confirm you observed the same on other operating systems or in general?

Is there a way to have VisualVM account for only affinity-assigned cores when showing cpu usage?

Wolfson answered 4/3, 2016 at 17:14 Comment(0)
P
7

According to VisualVM source code, CPU usage is indeed calculated as total CPU time divided by number of processors:

    long processCpuTime = tracksProcessCpuTime ?
        model.getProcessCpuTime() / processorsCount : -1;

where processorsCount is obtained from OperatingSystemMXBean:

    OperatingSystemMXBean osbean = mxbeans.getOperatingSystemMXBean();
    if (osbean != null) processorsCount = osbean.getAvailableProcessors();

There was a long-standing JVM bug JDK-6515172, that the process affinity was not taken into account, i.e. getAvailableProcessors always returned the total number of CPUs regardless of tasksets. This was specific to Linux and BSD; worked normally on Solaris and Windows.

About a month ago this bug has been finally resolved. The fix, however, is only for JDK 9.

Look at this question for possible workarounds. They are somewhat ugly though.

Papandreou answered 12/3, 2016 at 1:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.