From b32e9b0f9be7349f797ced4a260e1e10418af3bb Mon Sep 17 00:00:00 2001 From: William Kemper Date: Fri, 20 Mar 2026 16:20:40 +0000 Subject: [PATCH] 8378806: Genshen: Add scope of collection to end of cycle message in JMX notification Reviewed-by: kdnilsen, xpeng --- .../share/gc/shenandoah/shenandoahUtils.cpp | 18 +++- .../share/gc/shenandoah/shenandoahUtils.hpp | 2 + .../mxbeans/TestCycleEndMessage.java | 83 +++++++++++++++++++ .../TestStringDeduplicationTools.java | 3 +- 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 test/hotspot/jtreg/gc/shenandoah/mxbeans/TestCycleEndMessage.java diff --git a/src/hotspot/share/gc/shenandoah/shenandoahUtils.cpp b/src/hotspot/share/gc/shenandoah/shenandoahUtils.cpp index 176baa133c8..dea47fcbf4f 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahUtils.cpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahUtils.cpp @@ -40,6 +40,22 @@ ShenandoahPhaseTimings::Phase ShenandoahTimingsTracker::_current_phase = ShenandoahPhaseTimings::_invalid_phase; +const char* ShenandoahGCSession::cycle_end_message(ShenandoahGenerationType type) { + switch (type) { + case NON_GEN: + return "end of GC cycle"; + case GLOBAL: + return "end of Global GC cycle"; + case YOUNG: + return "end of Young GC cycle"; + case OLD: + return "end of Old GC cycle"; + default: + ShouldNotReachHere(); + return "end of GC cycle"; + } +} + ShenandoahGCSession::ShenandoahGCSession(GCCause::Cause cause, ShenandoahGeneration* generation) : _heap(ShenandoahHeap::heap()), _generation(generation), @@ -54,7 +70,7 @@ ShenandoahGCSession::ShenandoahGCSession(GCCause::Cause cause, ShenandoahGenerat _heap->trace_heap_before_gc(_tracer); _trace_cycle.initialize(_heap->cycle_memory_manager(), cause, - "end of GC cycle", + cycle_end_message(_generation->type()), /* allMemoryPoolsAffected */ true, /* recordGCBeginTime = */ true, /* recordPreGCUsage = */ true, diff --git a/src/hotspot/share/gc/shenandoah/shenandoahUtils.hpp b/src/hotspot/share/gc/shenandoah/shenandoahUtils.hpp index 8a508c4afd8..6ef4cd7c702 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahUtils.hpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahUtils.hpp @@ -67,6 +67,8 @@ private: GCTracer* const _tracer; TraceMemoryManagerStats _trace_cycle; + + static const char* cycle_end_message(ShenandoahGenerationType type); public: ShenandoahGCSession(GCCause::Cause cause, ShenandoahGeneration* generation); ~ShenandoahGCSession(); diff --git a/test/hotspot/jtreg/gc/shenandoah/mxbeans/TestCycleEndMessage.java b/test/hotspot/jtreg/gc/shenandoah/mxbeans/TestCycleEndMessage.java new file mode 100644 index 00000000000..e7a4cfc9605 --- /dev/null +++ b/test/hotspot/jtreg/gc/shenandoah/mxbeans/TestCycleEndMessage.java @@ -0,0 +1,83 @@ +/* + * Copyright Amazon.com Inc. 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 + * @summary Check that GC cycle end message contains generation name + * @library /test/lib / + * @requires vm.gc.Shenandoah + * + * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions + * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational + * TestCycleEndMessage + */ + +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.management.Notification; +import javax.management.NotificationEmitter; +import javax.management.NotificationListener; +import javax.management.openmbean.CompositeData; + +import com.sun.management.GarbageCollectionNotificationInfo; + +public class TestCycleEndMessage { + + public static void main(String[] args) throws Exception { + final AtomicBoolean foundGenerationInCycle = new AtomicBoolean(false); + + NotificationListener listener = new NotificationListener() { + @Override + public void handleNotification(Notification n, Object o) { + if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { + GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData()); + + String name = info.getGcName(); + String action = info.getGcAction(); + + System.out.println("Received: " + name + " / " + action); + + if (name.equals("Shenandoah Cycles") && + (action.contains("Global") || action.contains("Young") || action.contains("Old"))) { + foundGenerationInCycle.set(true); + } + } + } + }; + + for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) { + ((NotificationEmitter) bean).addNotificationListener(listener, null, null); + } + + System.gc(); + Thread.sleep(2000); + + if (!foundGenerationInCycle.get()) { + throw new IllegalStateException("Expected to find generation name (Global/Young/Old) in Shenandoah Cycles action message"); + } + + System.out.println("Test passed: Found generation name in cycle end message"); + } +} diff --git a/test/hotspot/jtreg/gc/stringdedup/TestStringDeduplicationTools.java b/test/hotspot/jtreg/gc/stringdedup/TestStringDeduplicationTools.java index 3dbedd61d12..e4185dd0f12 100644 --- a/test/hotspot/jtreg/gc/stringdedup/TestStringDeduplicationTools.java +++ b/test/hotspot/jtreg/gc/stringdedup/TestStringDeduplicationTools.java @@ -129,7 +129,8 @@ class TestStringDeduplicationTools { GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData()); // Shenandoah and Z GC also report GC pauses, skip them if (info.getGcName().startsWith("Shenandoah")) { - if ("end of GC cycle".equals(info.getGcAction())) { + String action = info.getGcAction(); + if (action != null && action.contains("GC cycle")) { gcCount++; } } else if (info.getGcName().startsWith("ZGC")) {