|
| 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 8352969 |
| 26 | + * @summary Tests optional evacuation. |
| 27 | + * @requires vm.gc.G1 |
| 28 | + * @requires vm.debug |
| 29 | + * @library /test/lib |
| 30 | + * @build jdk.test.whitebox.WhiteBox |
| 31 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 32 | + * @run driver gc.g1.TestOptionalRegionGC |
| 33 | + */ |
| 34 | + |
| 35 | +package gc.g1; |
| 36 | + |
| 37 | +import jdk.test.lib.Asserts; |
| 38 | +import jdk.test.lib.process.OutputAnalyzer; |
| 39 | +import jdk.test.lib.process.ProcessTools; |
| 40 | +import jdk.test.whitebox.WhiteBox; |
| 41 | + |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
| 44 | +import java.util.Objects; |
| 45 | +import java.util.Random; |
| 46 | +import java.util.regex.Matcher; |
| 47 | +import java.util.regex.Pattern; |
| 48 | + |
| 49 | +public class TestOptionalRegionGC { |
| 50 | + |
| 51 | + private static OutputAnalyzer run() throws Exception { |
| 52 | + return ProcessTools.executeLimitedTestJava( |
| 53 | + "-XX:+WhiteBoxAPI", |
| 54 | + "-Xbootclasspath/a:.", |
| 55 | + "-Xmx300M", |
| 56 | + "-Xms300M", |
| 57 | + "-XX:G1HeapRegionSize=1M", |
| 58 | + "-XX:+UseG1GC", |
| 59 | + "-XX:MaxTenuringThreshold=1", |
| 60 | + "-Xlog:gc+ergo+cset=trace", |
| 61 | + "-XX:+G1ForceOptionalEvacuation", |
| 62 | + "-XX:+VerifyAfterGC", |
| 63 | + TestOptionalRegionGC.Action.class.getName()); |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String args[]) throws Exception { |
| 67 | + OutputAnalyzer out = run(); |
| 68 | + out.shouldHaveExitValue(0); |
| 69 | + Pattern pattern = Pattern.compile("Prepared (\\d+) regions out of (\\d+) for optional evacuation"); |
| 70 | + Matcher matcher = pattern.matcher(out.getOutput()); |
| 71 | + Asserts.assertTrue(matcher.find()); |
| 72 | + String selectedNum = matcher.group(1); |
| 73 | + String totalNum = matcher.group(2); |
| 74 | + Asserts.assertTrue(Objects.equals(selectedNum, totalNum), "Error info: " + selectedNum + ", " + totalNum); |
| 75 | + } |
| 76 | + |
| 77 | + public static class Action { |
| 78 | + private static final WhiteBox wb = WhiteBox.getWhiteBox(); |
| 79 | + private static final int MIN_OBJECT_SIZE = 64 * 1024; |
| 80 | + private static final int MAX_OBJECT_SIZE = 120 * 1024; |
| 81 | + private static final int NUM_OBJECTS = 1200; |
| 82 | + |
| 83 | + public static void main(String [] args) throws Exception { |
| 84 | + // Remove garbage from VM initialization. |
| 85 | + wb.fullGC(); |
| 86 | + Random rand = new Random(42); |
| 87 | + List<byte[]> objectList = new ArrayList<>(); |
| 88 | + for (int i = 0; i < NUM_OBJECTS; i++) { |
| 89 | + int objSize = MIN_OBJECT_SIZE + rand.nextInt(MAX_OBJECT_SIZE - MIN_OBJECT_SIZE); |
| 90 | + byte[] obj = new byte[objSize]; |
| 91 | + objectList.add(obj); |
| 92 | + } |
| 93 | + // Young GC promotes some objects to the old generation. |
| 94 | + wb.youngGC(); |
| 95 | + // Clear certain references for mixed GC. |
| 96 | + for (int i = 0; i < NUM_OBJECTS; i+=2) { |
| 97 | + objectList.set(i, null); |
| 98 | + } |
| 99 | + wb.g1RunConcurrentGC(); |
| 100 | + // Perform the "Prepare Mixed" GC. |
| 101 | + wb.youngGC(); |
| 102 | + // Perform the "Mixed" GC. |
| 103 | + wb.youngGC(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments