On Sun, 5 Oct 2025 13:00:44 GMT, Jonas Norlinder <[email protected]> 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:
> 
>   Move from j.l.management to com.sun.management etc.

I introduced a JDK-specific MemoryMXBean in `com.sun.management` and added 
`getTotalGcCpuTime` there. I believe the Java idiomatic way to retrieve GC CPU 
time and process CPU time would be something like:


import java.lang.management.ManagementFactory;
import com.sun.management.MemoryMXBean;
import com.sun.management.OperatingSystemMXBean;

public class Main2 {
    public static void main(String[] args) {
        final MemoryMXBean mxMemoryBean = 
ManagementFactory.getPlatformMXBean(MemoryMXBean.class);
        final OperatingSystemMXBean mxOSBean = 
ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);

        System.out.println("getTotalGcCpuTime: " + 
mxMemoryBean.getTotalGcCpuTime());
        System.out.println("getProcessCpuTime: " + 
mxOSBean.getProcessCpuTime());
    }
}


Given that `getTotalGcCpuTime` is most useful when you also sample process CPU 
time I like this symmetry, thanks for the suggestion.

I updated the language in the documentation:


    /**
     * Returns the CPU time used by garbage collection.
     *
     * <p> CPU time used by all garbage collection. In
     * general this includes time for all driver threads,
     * workers, VM operations on the VM thread and the string
     * deduplication thread (if enabled). May be non-zero even if no
     * GC cycle occurred. This method returns {@code -1} if the
     * platform does not support this operation or if called during
     * shutdown.
     *
     * @return the total accumulated CPU time for garbage collection
     * in nanoseconds.
     *
     * @since 26
     */


Since we include time om VM thread which strictly is not a garbage collection 
thread I think it is better to constrain it to "garbage collection" in general. 
Let me know what you think.

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

PR Comment: https://git.openjdk.org/jdk/pull/27537#issuecomment-3369043846

Reply via email to