Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/arguments/TestMaxHeapSizeTools.java
32285 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.regex.Matcher;24import java.util.regex.Pattern;25import java.util.ArrayList;26import java.util.Arrays;2728import com.oracle.java.testlibrary.*;29import sun.hotspot.WhiteBox;3031class ErgoArgsPrinter {32public static void main(String[] args) throws Exception {33WhiteBox wb = WhiteBox.getWhiteBox();34wb.printHeapSizes();35}36}3738final class MinInitialMaxValues {39public long minHeapSize;40public long initialHeapSize;41public long maxHeapSize;4243public long spaceAlignment;44public long heapAlignment;45}4647class TestMaxHeapSizeTools {4849public static void checkMinInitialMaxHeapFlags(String gcflag) throws Exception {50checkInvalidMinInitialHeapCombinations(gcflag);51checkValidMinInitialHeapCombinations(gcflag);52checkInvalidInitialMaxHeapCombinations(gcflag);53checkValidInitialMaxHeapCombinations(gcflag);54}5556public static void checkMinInitialErgonomics(String gcflag) throws Exception {57// heap sizing ergonomics use the value NewSize + OldSize as default values58// for ergonomics calculation. Retrieve these values.59long[] values = new long[2];60getNewOldSize(gcflag, values);6162// we check cases with values smaller and larger than this default value.63long newPlusOldSize = values[0] + values[1];64long smallValue = newPlusOldSize / 2;65long largeValue = newPlusOldSize * 2;66long maxHeapSize = largeValue + (2 * 1024 * 1024);6768// -Xms is not set69checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize }, values, -1, -1);70checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue);71checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);72checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=0" }, values, -1, -1);7374// -Xms is set to zero75checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0" }, values, -1, -1);76checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue);77checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);78checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=0" }, values, -1, -1);7980// -Xms is set to small value81checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue }, values, -1, -1);82checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);83checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);84checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=0" }, values, smallValue, -1);8586// -Xms is set to large value87checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue }, values, largeValue, largeValue);88checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue, "-XX:InitialHeapSize=0" }, values, largeValue, -1);89}9091private static long align_up(long value, long alignment) {92long alignmentMinusOne = alignment - 1;93return (value + alignmentMinusOne) & ~alignmentMinusOne;94}9596private static void getNewOldSize(String gcflag, long[] values) throws Exception {97ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag,98"-XX:+PrintFlagsFinal", "-version");99OutputAnalyzer output = new OutputAnalyzer(pb.start());100output.shouldHaveExitValue(0);101102String stdout = output.getStdout();103values[0] = getFlagValue(" NewSize", stdout);104values[1] = getFlagValue(" OldSize", stdout);105}106107public static void checkGenMaxHeapErgo(String gcflag) throws Exception {108TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 3);109TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 4);110TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 5);111}112113private static void checkInvalidMinInitialHeapCombinations(String gcflag) throws Exception {114expectError(new String[] { gcflag, "-Xms8M", "-XX:InitialHeapSize=4M", "-version" });115}116117private static void checkValidMinInitialHeapCombinations(String gcflag) throws Exception {118expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms4M", "-version" });119expectValid(new String[] { gcflag, "-Xms4M", "-XX:InitialHeapSize=8M", "-version" });120expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms8M", "-version" });121// the following is not an error as -Xms sets both minimal and initial heap size122expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-Xms8M", "-version" });123}124125private static void checkInvalidInitialMaxHeapCombinations(String gcflag) throws Exception {126expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version" });127expectError(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" });128}129130private static void checkValidInitialMaxHeapCombinations(String gcflag) throws Exception {131expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" });132expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version" });133expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version" });134// a value of "0" for initial heap size means auto-detect135expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=0M", "-version" });136}137138private static long valueAfter(String source, String match) {139int start = source.indexOf(match) + match.length();140String tail = source.substring(start).split(" ")[0];141return Long.parseLong(tail);142}143144/**145* Executes a new VM process with the given class and parameters.146* @param vmargs Arguments to the VM to run147* @param classname Name of the class to run148* @param arguments Arguments to the class149* @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string150* @return The OutputAnalyzer with the results for the invocation.151*/152public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception {153ArrayList<String> finalargs = new ArrayList<String>();154155String[] whiteboxOpts = new String[] {156"-Xbootclasspath/a:.",157"-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",158"-cp", System.getProperty("java.class.path"),159};160161if (useTestDotJavaDotOpts) {162// System.getProperty("test.java.opts") is '' if no options is set,163// we need to skip such a result164String[] externalVMOpts = new String[0];165if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) {166externalVMOpts = System.getProperty("test.java.opts").split(" ");167}168finalargs.addAll(Arrays.asList(externalVMOpts));169}170171finalargs.addAll(Arrays.asList(vmargs));172finalargs.addAll(Arrays.asList(whiteboxOpts));173finalargs.add(classname);174finalargs.addAll(Arrays.asList(arguments));175176ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));177OutputAnalyzer output = new OutputAnalyzer(pb.start());178output.shouldHaveExitValue(0);179180return output;181}182183private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception {184OutputAnalyzer output = runWhiteBoxTest(args, ErgoArgsPrinter.class.getName(), new String[] {}, false);185186// the output we watch for has the following format:187//188// "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B"189//190// where A, B, X, Y and Z are sizes in bytes.191// Unfortunately there is no other way to retrieve the minimum heap size and192// the alignments.193194Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Space alignment \\d+ Heap alignment \\d+").195matcher(output.getStdout());196if (!m.find()) {197throw new RuntimeException("Could not find heap size string.");198}199200String match = m.group();201202// actual values203val.minHeapSize = valueAfter(match, "Minimum heap ");204val.initialHeapSize = valueAfter(match, "Initial heap ");205val.maxHeapSize = valueAfter(match, "Maximum heap ");206val.spaceAlignment = valueAfter(match, "Space alignment ");207val.heapAlignment = valueAfter(match, "Heap alignment ");208}209210/**211* Verify whether the VM automatically synchronizes minimum and initial heap size if only212* one is given for the GC specified.213*/214public static void checkErgonomics(String[] args, long[] newoldsize,215long expectedMin, long expectedInitial) throws Exception {216217MinInitialMaxValues v = new MinInitialMaxValues();218getMinInitialMaxHeap(args, v);219220if ((expectedMin != -1) && (align_up(expectedMin, v.heapAlignment) != v.minHeapSize)) {221throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize +222" differs from expected minimum heap size of " + expectedMin);223}224225if ((expectedInitial != -1) && (align_up(expectedInitial, v.heapAlignment) != v.initialHeapSize)) {226throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize +227" differs from expected initial heap size of " + expectedInitial);228}229230// always check the invariant min <= initial <= max heap size231if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) {232throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " +233v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize);234}235}236237/**238* Verify whether the VM respects the given maximum heap size in MB for the239* GC specified.240* @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC241* @param maxHeapSize the maximum heap size to verify, in MB.242*/243public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception {244final long K = 1024;245246MinInitialMaxValues v = new MinInitialMaxValues();247getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v);248249long expectedHeapSize = align_up(maxHeapsize * K * K, v.heapAlignment);250long actualHeapSize = v.maxHeapSize;251252if (actualHeapSize > expectedHeapSize) {253throw new RuntimeException("Heap has " + actualHeapSize +254" bytes, expected to be less than " + expectedHeapSize);255}256}257258private static long getFlagValue(String flag, String where) {259Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);260if (!m.find()) {261throw new RuntimeException("Could not find value for flag " + flag + " in output string");262}263String match = m.group();264return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length()));265}266267private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception {268if (contains) {269output.shouldContain(message);270} else {271output.shouldNotContain(message);272}273}274275private static void expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {276ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);277OutputAnalyzer output = new OutputAnalyzer(pb.start());278shouldContainOrNot(output, hasWarning, "Warning");279shouldContainOrNot(output, hasError, "Error");280output.shouldHaveExitValue(errorcode);281}282283private static void expectError(String[] flags) throws Exception {284expect(flags, false, true, 1);285}286287private static void expectValid(String[] flags) throws Exception {288expect(flags, false, false, 0);289}290}291292293294