jmx-dev RFR: 8368527: JMX: Add an MXBeans method to query GC CPU time
Jonas Norlinder
duke at openjdk.org
Tue Sep 30 09:11:49 UTC 2025
On Tue, 30 Sep 2025 04:21:44 GMT, Bernd <duke at openjdk.org> wrote:
>> Hi all,
>>
>> This PR augments the CPU time sampling measurement capabilities that a user can perform from Java code with the addition of `MemoryMXBean.getGcCpuTime()`. With this patch it will be possible for a user to measure process and GC CPU time during critical section or iterations in benchmarks to name a few. This new method complements the existing `OperatingSystemMXBean.getProcessCpuTime()` for a refined understanding.
>>
>> `CollectedHeap::gc_threads_do` may operate on terminated GC threads during shutdown, but thanks to JDK-8366865 by @walulyai we can piggyback on the new `Universe::is_shutting_down`. I have implemented a stress-test `test/jdk/java/lang/management/MemoryMXBean/GetGcCpuTime.java` that may identify reading CPU time of terminated threads. Synchronizing on `Universe::is_shutting_down` and `Heap_lock` resolves this problem.
>>
>> FWIW; To my understanding we don't want to add a `Universe::is_shutting_down` check in gc_threads_do as this may introduce a performance penalty that is unacceptable, therefore we must be careful about the few places where external users call upon gc_threads_do and may race with a terminating VM.
>>
>> Tested: test/jdk/java/lang/management/MemoryMXBean/GetGcCpuTime.java, jdk/javax/management/mxbean hotspot/jtreg/vmTestbase/nsk/monitoring on Linux x64, Linux aarch64, Windows x64, macOS x64 and macOS aarch64 with release and fastdebug.
>
> src/java.management/share/classes/java/lang/management/MemoryMXBean.java line 273:
>
>> 271: * Returns the CPU time used by all garbage collection threads.
>> 272: *
>> 273: * <p> This include time since genesis, so the value can be
>
> The “so the” is not too obvious to me. Maybe simplify it a bit “This includes time since genesis and counts activities even before the first collection cycle.”?
Thanks @ecki for pointing that out :) I make a simplification whenever the CSR discussion (https://bugs.openjdk.org/browse/JDK-8368529) have reached a consensus where this method should live.
> src/java.management/share/classes/java/lang/management/MemoryMXBean.java line 279:
>
>> 277: * deduplication thread (if enabled). This method returns
>> 278: * {@code -1} if the platform does not support this operation
>> 279: * or if called during shutdown.
>
> Can we explicitly state here that this is concurrent time as well as worker time spent during pauses but not accumulated pause times?
Just want to ensure that I read this correct, please correct any mistake below:
* concurrent time = `CPU time`
* accumulate times = `wall-clock time`
Is your suggestion that we should clarify that the method may only account for `CPU time` and not `wall-clock time` during pauses?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/27537#discussion_r2390466153
PR Review Comment: https://git.openjdk.org/jdk/pull/27537#discussion_r2390453810
More information about the jmx-dev
mailing list