|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +package gc.g1; |
| 25 | + |
| 26 | +/* |
| 27 | + * @test TestRemarkCleanupMXBeanCollectionUsage |
| 28 | + * @bug 8386332 |
| 29 | + * @summary Test that Remark and Cleanup correctly update old pool's getCollectionUsage() bean. |
| 30 | + * @requires vm.gc.G1 |
| 31 | + * @library /test/lib / |
| 32 | + * @build jdk.test.whitebox.WhiteBox |
| 33 | + * @modules java.base/jdk.internal.misc |
| 34 | + * java.management |
| 35 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 36 | + * @run main/othervm -XX:+UseG1GC -Xlog:gc -XX:G1HeapRegionSize=1m -Xms128m -Xmx128m |
| 37 | + * -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
| 38 | + * gc.g1.TestRemarkCleanupMXBeanCollectionUsage |
| 39 | + */ |
| 40 | + |
| 41 | +import java.lang.management.ManagementFactory; |
| 42 | +import java.lang.management.MemoryPoolMXBean; |
| 43 | +import java.lang.management.MemoryUsage; |
| 44 | +import java.lang.ref.Reference; |
| 45 | + |
| 46 | +import jdk.test.lib.Asserts; |
| 47 | +import jdk.test.whitebox.WhiteBox; |
| 48 | + |
| 49 | +public class TestRemarkCleanupMXBeanCollectionUsage { |
| 50 | + private static WhiteBox wb = WhiteBox.getWhiteBox(); |
| 51 | + private static final int M = 1024 * 1024; |
| 52 | + |
| 53 | + private static MemoryPoolMXBean findPoolMXBean(String name) throws Exception { |
| 54 | + for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { |
| 55 | + if (pool.getName().equals(name)) { |
| 56 | + return pool; |
| 57 | + } |
| 58 | + } |
| 59 | + throw new RuntimeException("Pool " + name + " not found."); |
| 60 | + } |
| 61 | + |
| 62 | + private static long getCollectionUsageUsedAndPrint(MemoryPoolMXBean pool, String message) { |
| 63 | + long result = pool.getCollectionUsage().getUsed(); |
| 64 | + System.out.println(message + ": " + result); |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + public static void main(String[] args) throws Exception { |
| 69 | + Object throwaway = new Object(); |
| 70 | + try { |
| 71 | + MemoryPoolMXBean oldPool = findPoolMXBean("G1 Old Gen"); |
| 72 | + |
| 73 | + wb.concurrentGCAcquireControl(); |
| 74 | + wb.fullGC(); |
| 75 | + long initialUsage = getCollectionUsageUsedAndPrint(oldPool, "Initial Usage"); |
| 76 | + |
| 77 | + // Allocate something in old gen. CollectionUsage should be updated. |
| 78 | + throwaway = new byte[M]; // Humongous allocation. |
| 79 | + wb.fullGC(); |
| 80 | + long afterFirstUsage = getCollectionUsageUsedAndPrint(oldPool, "After first alloc usage"); |
| 81 | + Asserts.assertTrue(afterFirstUsage >= initialUsage + M, |
| 82 | + "Full GC should updated collectionUsage. Before " + afterFirstUsage + " after " + initialUsage); |
| 83 | + |
| 84 | + // Remark pause should update collectionUsage, i.e. the following release of the memory be noticed. |
| 85 | + throwaway = null; |
| 86 | + wb.concurrentGCRunTo(wb.G1_AFTER_REBUILD_STARTED); |
| 87 | + long afterRemarkUsage = getCollectionUsageUsedAndPrint(oldPool, "After Remark usage"); |
| 88 | + Asserts.assertTrue(afterRemarkUsage < afterFirstUsage - M, |
| 89 | + "Remark pause should have updated getCollectionUsage(). Before " + afterFirstUsage + " after " + afterRemarkUsage); |
| 90 | + |
| 91 | + // Cleanup pause should not update collectionUsage, i.e. the following allocation go unnoticed. |
| 92 | + throwaway = new byte[M]; |
| 93 | + wb.concurrentGCRunTo(wb.G1_AFTER_CLEANUP_STARTED); |
| 94 | + long afterCleanupUsage = getCollectionUsageUsedAndPrint(oldPool, "After Cleanup usage"); |
| 95 | + Asserts.assertTrue(afterCleanupUsage == afterRemarkUsage, |
| 96 | + "Cleanup pause should not update getCollectionUsage(). Before " + afterRemarkUsage + " after " + afterCleanupUsage); |
| 97 | + } finally { |
| 98 | + wb.concurrentGCReleaseControl(); |
| 99 | + Reference.reachabilityFence(throwaway); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments