-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8368527: JMX: Add an MXBeans method to query GC CPU time #27537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
649e1ac
ae29e03
6431310
d046f4a
b03db63
8d83dd9
a188df0
f8a9f7a
08af7b5
5d360f4
5960a1d
57577ae
f395e8d
44f5d86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
|
|
@@ -267,6 +267,24 @@ public interface MemoryMXBean extends PlatformManagedObject { | |
| */ | ||
| public MemoryUsage getNonHeapMemoryUsage(); | ||
|
|
||
| /** | ||
| * Returns the CPU time used by all garbage collection threads. | ||
| * | ||
| * <p> This include time since genesis, so the value can be | ||
|
||
| * non-zero even if no garbage collection cycles occured. In | ||
| * general this includes time for all driver threads, | ||
| * workers, VM operations on the VM thread and the string | ||
| * deduplication thread (if enabled). This method returns | ||
| * {@code -1} if the platform does not support this operation | ||
| * or if called during shutdown. | ||
|
||
| * | ||
| * @return the total CPU time for all garbage collection | ||
| * threads in nanoseconds. | ||
|
||
| * | ||
| * @since 26 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The latest API docs is good, thanks for accepting all the comments/suggestions to get this right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should say thank you all for the attention to detail :-) The API docs is indeed much better than the original suggestion. |
||
| */ | ||
| public long getGcCpuTime(); | ||
|
|
||
| /** | ||
| * Tests if verbose output for the memory system is enabled. | ||
| * | ||
|
|
@@ -302,5 +320,4 @@ public interface MemoryMXBean extends PlatformManagedObject { | |
| * @see java.lang.System#gc() | ||
| */ | ||
| public void gc(); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| /* | ||
| * @test | ||
| * @bug 8368527 | ||
| * @library /test/lib | ||
| * @summary Stress MemoryMXBean.getGcCpuTime during shutdown | ||
| * | ||
| * @run main/othervm -XX:+UseSerialGC GetGcCpuTime _ | ||
| * @run main/othervm -XX:+UseParallelGC GetGcCpuTime _ | ||
| * @run main/othervm -XX:+UseG1GC GetGcCpuTime _ | ||
| * @run main/othervm -XX:+UseZGC GetGcCpuTime _ | ||
| */ | ||
|
|
||
| import jdk.test.lib.process.OutputAnalyzer; | ||
| import static jdk.test.lib.process.ProcessTools.createTestJavaProcessBuilder; | ||
| import static jdk.test.lib.process.ProcessTools.executeProcess; | ||
|
|
||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.management.MemoryMXBean; | ||
| import java.lang.management.ThreadMXBean; | ||
|
|
||
| public class GetGcCpuTime { | ||
| static final ThreadMXBean mxThreadBean = ManagementFactory.getThreadMXBean(); | ||
| static final MemoryMXBean mxMemoryBean = ManagementFactory.getMemoryMXBean(); | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| if (args.length > 0) { | ||
| ProcessBuilder pb = createTestJavaProcessBuilder("GetGcCpuTime"); | ||
| OutputAnalyzer output = executeProcess(pb); | ||
| output.shouldNotContain("GC CPU time should"); | ||
| output.shouldHaveExitValue(0); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| if (!mxThreadBean.isThreadCpuTimeEnabled()) { | ||
| return; | ||
| } | ||
| } catch (UnsupportedOperationException e) { | ||
| if (mxMemoryBean.getGcCpuTime() != -1) { | ||
| throw new Error("GC CPU time should be -1"); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| final int numberOfThreads = Runtime.getRuntime().availableProcessors() * 8; | ||
| for (int i = 0; i < numberOfThreads; i++) { | ||
| Thread t = new Thread(() -> { | ||
| while (true) { | ||
| long gcCpuTimeFromThread = mxMemoryBean.getGcCpuTime(); | ||
| if (gcCpuTimeFromThread < -1) { | ||
| throw new Error("GC CPU time should never be less than -1 but was " + gcCpuTimeFromThread); | ||
| } | ||
| } | ||
| }); | ||
| t.start(); | ||
| } | ||
|
|
||
| System.exit(0); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks a bit odd to me to change the existing define of UPTIME here.
OK it is not a public interface used between different versions, and people should not be mixing up jmm.h and management implementations... But usually we would just add the new definition? (items below are not strictly grouped by name) Maybe this is just a nit, and only makes cross-version comparisons easier.
Looks good overall.