--- /dev/null 2019-10-08 16:01:21.069130487 +0800 +++ new/test/hotspot/jtreg/gc/g1/TestSoftMaxHeapSize.java 2020-01-03 17:56:51.551856615 +0800 @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2012, 2019, Oracle and/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. + */ + +package gc.g1; + +/* + * @test TestSoftMaxHeapSize + * @bug 8236073 + * @summary G1: Use SoftMaxHeapSize to guide GC heuristics + * @requires vm.gc.G1 + * @library /test/lib + * @modules java.base/jdk.internal.misc + * java.management + * @build sun.hotspot.WhiteBox + * @run driver ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. gc.g1.TestSoftMaxHeapSize + */ + +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; +import jdk.test.lib.JDKToolLauncher; +import sun.hotspot.WhiteBox; + +public class TestSoftMaxHeapSize { + + private static final int heapSize = 128; // MB + private static final int heapRegionSize = 1; // MB + + public static final WhiteBox WB = WhiteBox.getWhiteBox(); + + public static void main(String[] args) throws Exception { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( + "-XX:+UnlockDiagnosticVMOptions", + "-XX:+WhiteBoxAPI", + "-Xbootclasspath/a:.", + "-XX:+UseG1GC", + "-Xms" + heapSize + "m", + "-Xmx" + heapSize + "m", + "-XX:G1HeapRegionSize=" + heapRegionSize + "m", + "-Xlog:gc*=debug", + "-Dtest.jdk=" + System.getProperty("test.jdk"), + SoftMaxHeapSizeShrinkInYoungGC.class.getName()); + + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + System.out.println(output.getOutput()); + output.shouldContain("Pause Young"); + output.shouldContain("Shrink the heap in concurrent. shrinking amount: 100663296B"); + output.shouldMatch("Pause Young.*32M"); + // Should have explicit full GC within SoftMaxHeapSize + output.shouldMatch("Pause Full.*32M"); + output.shouldHaveExitValue(0); + + pb = ProcessTools.createJavaProcessBuilder( + "-XX:+UnlockDiagnosticVMOptions", + "-XX:+WhiteBoxAPI", + "-Xbootclasspath/a:.", + "-XX:+UseG1GC", + "-Xms" + heapSize + "m", + "-Xmx" + heapSize + "m", + "-XX:G1HeapRegionSize=" + heapRegionSize + "m", + "-Xlog:gc*", + "-Dtest.jdk=" + System.getProperty("test.jdk"), + SoftMaxHeapSizeShrinkAfterMixedGC.class.getName()); + + output = new OutputAnalyzer(pb.start()); + System.out.println(output.getOutput()); + output.shouldContain("Pause Full"); + output.shouldContain("Pause Young (Mixed)"); + output.shouldMatch("Pause Young.*32M"); + output.shouldHaveExitValue(0); + + pb = ProcessTools.createJavaProcessBuilder( + "-XX:+UnlockDiagnosticVMOptions", + "-XX:+WhiteBoxAPI", + "-Xbootclasspath/a:.", + "-XX:+UseG1GC", + "-Xms" + heapSize + "m", + "-Xmx" + heapSize + "m", + "-XX:G1HeapRegionSize=" + heapRegionSize + "m", + "-Xlog:gc*", + "-Dtest.jdk=" + System.getProperty("test.jdk"), + SoftMaxHeapSizeExpansion.class.getName()); + + output = new OutputAnalyzer(pb.start()); + System.out.println(output.getOutput()); + output.shouldMatch("Pause Young.*32M"); + // Should expand to Xmx in Full GC + output.shouldMatch("Pause Full.*128M"); + output.shouldContain("java.lang.OutOfMemoryError: Java heap space"); + output.shouldHaveExitValue(1); + } + + public static OutputAnalyzer jinfo(String... toolArgs) throws Exception { + JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jinfo"); + if (toolArgs != null) { + for (String toolArg : toolArgs) { + launcher.addToolArg(toolArg); + } + } + + String pid = Long.toString(ProcessTools.getProcessId()); + launcher.addToolArg(pid); + + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(launcher.getCommand()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); + return output; + } + + static class SoftMaxHeapSizeShrinkInYoungGC { + + public static void main(String [] args) throws Exception { + Object[] array = new Object[1024]; + for (int i = 0; i < 1024; i++) { + array[i] = new int[1024]; + } + WB.youngGC(); + OutputAnalyzer output = TestSoftMaxHeapSize.jinfo("-flag", + "SoftMaxHeapSize=" + + 32 * 1024 * 1024); + output.shouldHaveExitValue(0); + WB.youngGC(); + WB.fullGC(); + } + } + + static class SoftMaxHeapSizeShrinkAfterMixedGC { + + public static void main(String [] args) throws Exception { + // Create 20MB+ objects + Object[] array = new Object[1024*1024]; + for (int i = 0; i < 1024*1024; i++) { + array[i] = new byte[10]; + } + WB.youngGC(); + WB.fullGC(); + OutputAnalyzer output = TestSoftMaxHeapSize.jinfo("-flag", + "SoftMaxHeapSize=" + + 32 * 1024 * 1024); + output.shouldHaveExitValue(0); + for (int i = 0; i < 1024*1024; i++) { + array[i] = null; + } + WB.youngGC(); // Cannot trigger shrink + // Make sure the mixed GC and shrink happen + WB.youngGC(); + Thread.sleep(1000); + WB.youngGC(); + Thread.sleep(1000); + WB.youngGC(); + Thread.sleep(1000); + WB.youngGC(); + Thread.sleep(1000); + WB.youngGC(); + } + } + + static class SoftMaxHeapSizeExpansion { + + public static void main(String [] args) throws Exception { + // Create 20MB+ objects + Object[] array = new Object[1024*1024]; + for (int i = 0; i < 1024; i++) { + array[i] = new byte[10]; + } + OutputAnalyzer output = TestSoftMaxHeapSize.jinfo("-flag", + "SoftMaxHeapSize=" + + 32 * 1024 * 1024); + WB.youngGC(); + output.shouldHaveExitValue(0); + // Allocate to OOM + for (int i = 0; i < 1024*1024; i++) { + array[i] = new byte[120]; + } + } + } + +}