jmx-dev RFR: 8368527: JMX: Add an MXBeans method to query GC CPU time [v5]

Alan Bateman alanb at openjdk.org
Thu Oct 23 11:29:11 UTC 2025


On Wed, 22 Oct 2025 11:28:34 GMT, Jonas Norlinder <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.
>
> Jonas Norlinder has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Fix typo

src/java.management/share/classes/java/lang/management/MemoryMXBean.java line 271:

> 269: 
> 270:     /**
> 271:      * Returns the CPU time used by garbage collection.

The latest wording is much better. For the first sentence, you might to consider expanding is a bit to something like "Returns the approximate accumulated time, in nanoseconds, spent in garbage collection."

src/java.management/share/classes/java/lang/management/MemoryMXBean.java line 285:

> 283:      * workers, VM Operations and string deduplication (if enabled).
> 284:      * The return value can be -1 if called when measurement is
> 285:      * not possible, such as during shutdown.

For the implNote then I think you are looking to say something HotSpot VM specific. That is possible with something like:  The specifics on what constitutes the time spent in garbage collection is highly implementation dependent. In the HotSpot Virtual Machine implementation, ... 

(If you are using the term "driver threads" then you will probably need to say more on what it means).

It is still the intention to use this in conjunction with OperatingSystemMXBean:: getProcessCpuTime ? If so, this could also be included here, or in an API note.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/27537#discussion_r2454746445
PR Review Comment: https://git.openjdk.org/jdk/pull/27537#discussion_r2454797793


More information about the jmx-dev mailing list