|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +/* @test |
| 25 | + * @bug 8351921 8352508 |
| 26 | + * @summary Test that pinned regions with no Java references into them |
| 27 | + * do not make the garbage collector reclaim that region. |
| 28 | + * This test simulates this behavior using Whitebox methods |
| 29 | + * to pin a Java object in a region with no other pinnable objects and |
| 30 | +* lose the reference to it before the garbage collection. |
| 31 | + * @requires vm.gc.G1 |
| 32 | + * @requires vm.debug |
| 33 | + * @library /test/lib / |
| 34 | + * @modules java.management |
| 35 | + * @build jdk.test.whitebox.WhiteBox |
| 36 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 37 | + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseG1GC |
| 38 | + * -Xbootclasspath/a:. -Xlog:gc=debug,gc+ergo+cset=trace,gc+phases=debug -XX:G1HeapRegionSize=1m -Xms30m gc.g1.pinnedobjs.TestPinnedEvacEmpty regular |
| 39 | + * |
| 40 | + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseG1GC |
| 41 | + * -Xbootclasspath/a:. -Xlog:gc=debug,gc+ergo+cset=trace,gc+phases=debug -XX:G1HeapRegionSize=1m -Xms30m gc.g1.pinnedobjs.TestPinnedEvacEmpty humongous |
| 42 | + */ |
| 43 | + |
| 44 | +package gc.g1.pinnedobjs; |
| 45 | + |
| 46 | +import gc.testlibrary.Helpers; |
| 47 | + |
| 48 | +import jdk.test.lib.Asserts; |
| 49 | +import jdk.test.lib.process.OutputAnalyzer; |
| 50 | +import jdk.test.lib.process.ProcessTools; |
| 51 | +import jdk.test.whitebox.WhiteBox; |
| 52 | + |
| 53 | +public class TestPinnedEvacEmpty { |
| 54 | + |
| 55 | + private static final WhiteBox wb = WhiteBox.getWhiteBox(); |
| 56 | + |
| 57 | + private static final int objSize = (int)wb.getObjectSize(new Object()); |
| 58 | + |
| 59 | + private static Object allocHumongous() { |
| 60 | + final int M = 1024 * 1024; |
| 61 | + // The target object to pin. Since it is humongous, it will always be in its |
| 62 | + // own regions. |
| 63 | + return new int[M]; |
| 64 | + } |
| 65 | + |
| 66 | + private static Object allocRegular() { |
| 67 | + // How many j.l.Object should we allocate when creating garbage. |
| 68 | + final int numAllocations = 1024 * 1024 * 3 / objSize; |
| 69 | + |
| 70 | + // Allocate garbage so that the target object will be in a new region. |
| 71 | + for (int i = 0; i < numAllocations; i++) { |
| 72 | + Object z = new Object(); |
| 73 | + } |
| 74 | + int[] o = new int[100]; // The target object to pin. |
| 75 | + // Further allocate garbage so that any additional allocations of potentially |
| 76 | + // pinned objects can not be allocated in the same region as the target object. |
| 77 | + for (int i = 0; i < numAllocations; i++) { |
| 78 | + Object z = new Object(); |
| 79 | + } |
| 80 | + |
| 81 | + Asserts.assertTrue(!wb.isObjectInOldGen(o), "should not be in old gen already"); |
| 82 | + return o; |
| 83 | + } |
| 84 | + |
| 85 | + public static void main(String[] args) throws Exception { |
| 86 | + System.out.println("Testing " + args[0] + " objects..."); |
| 87 | + |
| 88 | + // Remove garbage from VM initialization. |
| 89 | + wb.fullGC(); |
| 90 | + |
| 91 | + // Allocate target object according to arguments. |
| 92 | + Object o = args[0].equals("regular") ? allocRegular() : allocHumongous(); |
| 93 | + |
| 94 | + // Pin the object. |
| 95 | + wb.pinObject(o); |
| 96 | + |
| 97 | + // And forget the (Java) reference to the int array. After this, the garbage |
| 98 | + // collection should find a completely empty pinned region. The collector |
| 99 | + // must not collect/free it. |
| 100 | + o = null; |
| 101 | + |
| 102 | + // Full collection should not crash the VM in case of "empty" pinned regions. |
| 103 | + wb.fullGC(); |
| 104 | + |
| 105 | + // Do a young garbage collection to zap the data in the pinned region. This test |
| 106 | + // achieves that by executing a concurrent cycle that both performs both a young |
| 107 | + // garbage collection as well as checks that errorneous reclamation does not occur |
| 108 | + // in the Remark pause. |
| 109 | + wb.g1RunConcurrentGC(); |
| 110 | + System.out.println("Done"); |
| 111 | + } |
| 112 | +} |
0 commit comments