|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 TestMixedGCLiveThreshold |
| 28 | + * @summary Test G1MixedGCLiveThresholdPercent. Fill up a region to at least 1/3 region-size, |
| 29 | + * the region should not be selected for mixed GC cycle if liveness is above threshold. |
| 30 | + * @requires vm.gc.G1 |
| 31 | + * @library /test/lib |
| 32 | + * @build sun.hotspot.WhiteBox |
| 33 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox |
| 34 | + * @run driver gc.g1.TestMixedGCLiveThreshold |
| 35 | + */ |
| 36 | + |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.regex.Pattern; |
| 40 | +import java.util.regex.Matcher; |
| 41 | + |
| 42 | +import jdk.test.lib.process.OutputAnalyzer; |
| 43 | +import jdk.test.lib.process.ProcessTools; |
| 44 | +import jdk.test.lib.Asserts; |
| 45 | +import sun.hotspot.WhiteBox; |
| 46 | + |
| 47 | +public class TestMixedGCLiveThreshold { |
| 48 | + private static final String pattern = "Remembered Set Tracking update regions total ([0-9]+), selected ([0-9]+)$"; |
| 49 | + |
| 50 | + public static void main(String[] args) throws Exception { |
| 51 | + // -XX:G1MixedGCLiveThresholdPercent=0 |
| 52 | + testMixedGCLiveThresholdPercent(0, false); |
| 53 | + |
| 54 | + // -XX:G1MixedGCLiveThresholdPercent=25 |
| 55 | + testMixedGCLiveThresholdPercent(25, false); |
| 56 | + |
| 57 | + // -XX:G1MixedGCLiveThresholdPercent=100 |
| 58 | + testMixedGCLiveThresholdPercent(100, true); |
| 59 | + } |
| 60 | + |
| 61 | + private static void testMixedGCLiveThresholdPercent(int liveThresholdPercent, boolean expectedRebuild) throws Exception { |
| 62 | + OutputAnalyzer output = testWithMixedGCLiveThresholdPercent(liveThresholdPercent); |
| 63 | + |
| 64 | + boolean regionsSelected = regionsSelectedForRebuild(output.getStdout()); |
| 65 | + |
| 66 | + Asserts.assertEquals(regionsSelected, expectedRebuild, |
| 67 | + (expectedRebuild ? |
| 68 | + "No Regions selected for rebuild. G1MixedGCLiveThresholdPercent=" + liveThresholdPercent + |
| 69 | + " at least one region should be selected" : |
| 70 | + "Regions selected for rebuild. G1MixedGCLiveThresholdPercent=" + liveThresholdPercent + |
| 71 | + " no regions should be selected") |
| 72 | + ); |
| 73 | + output.shouldHaveExitValue(0); |
| 74 | + } |
| 75 | + |
| 76 | + private static OutputAnalyzer testWithMixedGCLiveThresholdPercent(int percent) throws Exception { |
| 77 | + ArrayList<String> basicOpts = new ArrayList<>(); |
| 78 | + Collections.addAll(basicOpts, new String[] { |
| 79 | + "-Xbootclasspath/a:.", |
| 80 | + "-XX:+UseG1GC", |
| 81 | + "-XX:+UnlockDiagnosticVMOptions", |
| 82 | + "-XX:+UnlockExperimentalVMOptions", |
| 83 | + "-XX:+WhiteBoxAPI", |
| 84 | + "-Xlog:gc+remset+tracking=trace", |
| 85 | + "-Xms10M", |
| 86 | + "-Xmx10M"}); |
| 87 | + |
| 88 | + basicOpts.add("-XX:G1MixedGCLiveThresholdPercent=" + percent); |
| 89 | + |
| 90 | + basicOpts.add(GCTest.class.getName()); |
| 91 | + |
| 92 | + ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(basicOpts); |
| 93 | + OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start()); |
| 94 | + return analyzer; |
| 95 | + } |
| 96 | + |
| 97 | + private static boolean regionsSelectedForRebuild(String output) throws Exception { |
| 98 | + Matcher m = Pattern.compile(pattern, Pattern.MULTILINE).matcher(output); |
| 99 | + |
| 100 | + if (!m.find()) { |
| 101 | + throw new Exception("Could not find correct output for Remembered Set Tracking in stdout," + |
| 102 | + " should match the pattern \"" + pattern + "\", but stdout is \n" + output); |
| 103 | + } |
| 104 | + return Integer.parseInt(m.group(2)) > 0; |
| 105 | + } |
| 106 | + |
| 107 | + public static class GCTest { |
| 108 | + public static void main(String args[]) throws Exception { |
| 109 | + WhiteBox wb = WhiteBox.getWhiteBox(); |
| 110 | + // Allocate some memory less than region size. |
| 111 | + Object used = allocate(); |
| 112 | + |
| 113 | + // Trigger the full GC using the WhiteBox API. |
| 114 | + wb.fullGC(); // full |
| 115 | + |
| 116 | + // Memory objects have been promoted to old by full GC. |
| 117 | + // Concurrent cycle may select regions for rebuilding |
| 118 | + wb.g1StartConcMarkCycle(); // concurrent-start, remark and cleanup |
| 119 | + |
| 120 | + // Sleep to make sure concurrent cycle is done |
| 121 | + while (wb.g1InConcurrentMark()) { |
| 122 | + Thread.sleep(1000); |
| 123 | + } |
| 124 | + System.out.println(used); |
| 125 | + } |
| 126 | + |
| 127 | + private static Object allocate() { |
| 128 | + final int objectSize = WhiteBox.getWhiteBox().g1RegionSize() / 3; |
| 129 | + Object ret = new byte[objectSize]; |
| 130 | + return ret; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments