Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine gc-cpu utilization within an application?

We would like to internalize some of the functionality of JConsole/JVisualVM and gather data about cpu-utilization and memory consumption from within the running application. The reason is, that security-constraints prevent us from opening the jmx ports to the outside on a production system.

Most of the data required can be monitored via the MXBeans, however, the cputime used for garbage-collection still eludes us. Monitoring the gc-time via the GarbageCollectorMXBean is not useful, as it only provides walltime of the parallel working collectors.

I assume it would be possible to use the ThreadMXBean to determine the cputime of all gc-threads. I can see no way to securely identify those threads.

Does anyone know, how JVisualVM calculates that number?

like image 365
Jonathan Avatar asked Jun 25 '14 13:06

Jonathan


1 Answers

Some time ago I have written summary about JVM diagnostics API, it is available here.

In short, MBean and "pref counters" are main source of JVM self diagnostic.

JVisualVM is using GarbageCollectorMXBean interpreting wall clock time as CPU time (i.e. showing nonsense).

ThreadMXBean does not include GC thread is thread list so it is useless too. In SJK tool I was subtracting cumulative CPU time of all application thread from process CPU time. This approach is not accurate, but better than nothing.

"Perf counters" have accurate information about CPU usage for GC. "Perf counters" could be accessed via sun.management.counter.perf.PerfInstrumentation.

Here you can find example (JUnit test) dumping "perf counters" of host JVM.

You need sun.gc.collector.0.time and sun.gc.collector.1.time counters which measure cummulative CPU usage for young and old / full GC (take a note that they reported in ticks).

like image 115
Alexey Ragozin Avatar answered Oct 21 '22 11:10

Alexey Ragozin