Path: blob/master/test/hotspot/jtreg/gc/arguments/TestMaxHeapSizeTools.java
40943 views
/*1* Copyright (c) 2013, 2020, 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*/2223package gc.arguments;2425import java.util.regex.Matcher;26import java.util.regex.Pattern;27import java.util.ArrayList;28import java.util.Arrays;2930import jdk.test.lib.process.ProcessTools;31import jdk.test.lib.process.OutputAnalyzer;32import sun.hotspot.WhiteBox;3334class ErgoArgsPrinter {35public static void main(String[] args) throws Exception {36WhiteBox wb = WhiteBox.getWhiteBox();37wb.printHeapSizes();38}39}4041final class MinInitialMaxValues {42public long minHeapSize;43public long initialHeapSize;44public long maxHeapSize;4546public long spaceAlignment;47public long heapAlignment;48}4950class TestMaxHeapSizeTools {5152public static void checkMinInitialMaxHeapFlags(String gcflag) throws Exception {53checkInvalidMinInitialHeapCombinations(gcflag);54checkValidMinInitialHeapCombinations(gcflag);55checkInvalidInitialMaxHeapCombinations(gcflag);56checkValidInitialMaxHeapCombinations(gcflag);57checkInvalidMinMaxHeapCombinations(gcflag);58checkValidMinMaxHeapCombinations(gcflag);59}6061public static void checkMinInitialErgonomics(String gcflag) throws Exception {62// heap sizing ergonomics use the value NewSize + OldSize as default values63// for ergonomics calculation. Retrieve these values.64long[] values = new long[2];65getNewOldSize(gcflag, values);6667// we check cases with values smaller and larger than this default value.68long newPlusOldSize = values[0] + values[1];69long smallValue = newPlusOldSize / 2;70long largeValue = newPlusOldSize * 2;71long maxHeapSize = largeValue + (2 * 1024 * 1024);7273// -Xms is not set74checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize }, values, -1, -1);75checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + smallValue }, values, smallValue, -1);76checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + largeValue }, values, largeValue, -1);77checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=0" }, values, -1, -1);78checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue);79checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);80checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=0" }, values, -1, -1);81// Some extra checks when both are set.82checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);83checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);84checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:MinHeapSize=" + largeValue, "-XX:InitialHeapSize=" + largeValue }, values, largeValue, largeValue);8586// -Xms is set to zero87checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0" }, values, -1, -1);88checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + smallValue }, values, smallValue, -1);89checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + largeValue }, values, largeValue, -1);90checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=0" }, values, -1, -1);91checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue);92checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);93checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=0" }, values, -1, -1);94checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);95checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);96checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:MinHeapSize=" + largeValue, "-XX:InitialHeapSize=" + largeValue }, values, largeValue, largeValue);9798// -Xms is set to small value99checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue }, values, -1, -1);100checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:MinHeapSize=" + smallValue }, values, smallValue, smallValue);101checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:MinHeapSize=0" }, values, -1, smallValue);102checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);103checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);104checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=0" }, values, smallValue, -1);105106// -Xms is set to large value107checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue }, values, largeValue, largeValue);108checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue, "-XX:InitialHeapSize=0" }, values, largeValue, -1);109checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue, "-XX:MinHeapSize=0" }, values, -1, largeValue);110}111112private static long align_up(long value, long alignment) {113long alignmentMinusOne = alignment - 1;114return (value + alignmentMinusOne) & ~alignmentMinusOne;115}116117private static void getNewOldSize(String gcflag, long[] values) throws Exception {118ProcessBuilder pb = GCArguments.createJavaProcessBuilder(gcflag,119"-XX:+PrintFlagsFinal", "-version");120OutputAnalyzer output = new OutputAnalyzer(pb.start());121output.shouldHaveExitValue(0);122123String stdout = output.getStdout();124values[0] = getFlagValue(" NewSize", stdout);125values[1] = getFlagValue(" OldSize", stdout);126}127128public static void checkGenMaxHeapErgo(String gcflag) throws Exception {129TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 4);130TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 5);131}132133private static void checkInvalidMinInitialHeapCombinations(String gcflag) throws Exception {134expectError(new String[] { gcflag, "-XX:InitialHeapSize=1023K", "-version" });135expectError(new String[] { gcflag, "-Xms64M", "-XX:InitialHeapSize=32M", "-version" });136expectError(new String[] { gcflag, "-XX:MinHeapSize=1023K", "-version" });137// Note: MinHeapSize values get aligned up by HeapAlignment which is 32M with 64k pages.138expectError(new String[] { gcflag, "-Xms4M", "-XX:MinHeapSize=64M", "-version" });139expectError(new String[] { gcflag, "-XX:MinHeapSize=8M -XX:InitialHeapSize=4m" });140}141142private static void checkValidMinInitialHeapCombinations(String gcflag) throws Exception {143expectValid(new String[] { gcflag, "-XX:InitialHeapSize=1024K", "-version" });144expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms4M", "-version" });145expectValid(new String[] { gcflag, "-Xms4M", "-XX:InitialHeapSize=8M", "-version" });146expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms8M", "-version" });147expectValid(new String[] { gcflag, "-XX:MinHeapSize=1024K", "-version" });148expectValid(new String[] { gcflag, "-XX:MinHeapSize=8M", "-Xms4M", "-version" });149expectValid(new String[] { gcflag, "-XX:MinHeapSize=8M", "-Xms8M", "-version" });150expectValid(new String[] { gcflag, "-Xms8M", "-XX:MinHeapSize=4M", "-version" });151// the following is not an error as -Xms sets both minimal and initial heap size152expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-Xms8M", "-version" });153expectValid(new String[] { gcflag, "-XX:MinHeapSize=4M", "-Xms8M", "-version" });154}155156private static void checkInvalidInitialMaxHeapCombinations(String gcflag) throws Exception {157expectError(new String[] { gcflag, "-XX:MaxHeapSize=2047K", "-version" });158expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version" });159expectError(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" });160}161162private static void checkInvalidMinMaxHeapCombinations(String gcflag) throws Exception {163expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:MinHeapSize=8M", "-version" });164expectError(new String[] { gcflag, "-XX:MinHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" });165}166167168private static void checkValidInitialMaxHeapCombinations(String gcflag) throws Exception {169expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" });170expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version" });171expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version" });172// a value of "0" for initial heap size means auto-detect173expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=0M", "-version" });174}175176private static void checkValidMinMaxHeapCombinations(String gcflag) throws Exception {177expectValid(new String[] { gcflag, "-XX:MinHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" });178expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:MinHeapSize=4M", "-version" });179expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:MinHeapSize=4M", "-version" });180// a value of "0" for min heap size means auto-detect181expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:MinHeapSize=0M", "-version" });182}183184private static long valueAfter(String source, String match) {185int start = source.indexOf(match) + match.length();186String tail = source.substring(start).split(" ")[0];187return Long.parseLong(tail);188}189190/**191* Executes a new VM process with the given class and parameters.192* @param vmargs Arguments to the VM to run193* @param classname Name of the class to run194* @param arguments Arguments to the class195* @return The OutputAnalyzer with the results for the invocation.196*/197public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments) throws Exception {198ArrayList<String> finalargs = new ArrayList<String>();199200String[] whiteboxOpts = new String[] {201"-Xbootclasspath/a:.",202"-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",203"-cp", System.getProperty("java.class.path"),204};205206finalargs.addAll(Arrays.asList(vmargs));207finalargs.addAll(Arrays.asList(whiteboxOpts));208finalargs.add(classname);209finalargs.addAll(Arrays.asList(arguments));210211ProcessBuilder pb = GCArguments.createJavaProcessBuilder(finalargs.toArray(String[]::new));212OutputAnalyzer output = new OutputAnalyzer(pb.start());213output.shouldHaveExitValue(0);214215return output;216}217218private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception {219OutputAnalyzer output = runWhiteBoxTest(args, ErgoArgsPrinter.class.getName(), new String[] {});220221// the output we watch for has the following format:222//223// "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B"224//225// where A, B, X, Y and Z are sizes in bytes.226// Unfortunately there is no other way to retrieve the minimum heap size and227// the alignments.228229Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Space alignment \\d+ Heap alignment \\d+").230matcher(output.getStdout());231if (!m.find()) {232throw new RuntimeException("Could not find heap size string.");233}234235String match = m.group();236237// actual values238val.minHeapSize = valueAfter(match, "Minimum heap ");239val.initialHeapSize = valueAfter(match, "Initial heap ");240val.maxHeapSize = valueAfter(match, "Maximum heap ");241val.spaceAlignment = valueAfter(match, "Space alignment ");242val.heapAlignment = valueAfter(match, "Heap alignment ");243}244245/**246* Verify whether the VM automatically synchronizes minimum and initial heap size if only247* one is given for the GC specified.248*/249public static void checkErgonomics(String[] args, long[] newoldsize,250long expectedMin, long expectedInitial) throws Exception {251252MinInitialMaxValues v = new MinInitialMaxValues();253getMinInitialMaxHeap(args, v);254255if ((expectedMin != -1) && (align_up(expectedMin, v.heapAlignment) != v.minHeapSize)) {256throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize +257" differs from expected minimum heap size of " + expectedMin);258}259260if ((expectedInitial != -1) && (align_up(expectedInitial, v.heapAlignment) != v.initialHeapSize)) {261throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize +262" differs from expected initial heap size of " + expectedInitial);263}264265// always check the invariant min <= initial <= max heap size266if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) {267throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " +268v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize);269}270}271272/**273* Verify whether the VM respects the given maximum heap size in MB for the274* GC specified.275* @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC276* @param maxHeapSize the maximum heap size to verify, in MB.277*/278public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception {279final long K = 1024;280281MinInitialMaxValues v = new MinInitialMaxValues();282getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v);283284long expectedHeapSize = align_up(maxHeapsize * K * K, v.heapAlignment);285long actualHeapSize = v.maxHeapSize;286287if (actualHeapSize > expectedHeapSize) {288throw new RuntimeException("Heap has " + actualHeapSize +289" bytes, expected to be less than " + expectedHeapSize);290}291}292293private static long getFlagValue(String flag, String where) {294Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);295if (!m.find()) {296throw new RuntimeException("Could not find value for flag " + flag + " in output string");297}298String match = m.group();299return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length()));300}301302private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception {303if (contains) {304output.shouldContain(message);305} else {306output.shouldNotContain(message);307}308}309310private static void expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {311ProcessBuilder pb = GCArguments.createJavaProcessBuilder(flags);312OutputAnalyzer output = new OutputAnalyzer(pb.start());313shouldContainOrNot(output, hasWarning, "Warning");314shouldContainOrNot(output, hasError, "Error");315output.shouldHaveExitValue(errorcode);316}317318private static void expectError(String[] flags) throws Exception {319expect(flags, false, true, 1);320}321322private static void expectValid(String[] flags) throws Exception {323expect(flags, false, false, 0);324}325}326327328