tschatzl@5116: /* tschatzl@5116: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. tschatzl@5116: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tschatzl@5116: * tschatzl@5116: * This code is free software; you can redistribute it and/or modify it tschatzl@5116: * under the terms of the GNU General Public License version 2 only, as tschatzl@5116: * published by the Free Software Foundation. tschatzl@5116: * tschatzl@5116: * This code is distributed in the hope that it will be useful, but WITHOUT tschatzl@5116: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tschatzl@5116: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tschatzl@5116: * version 2 for more details (a copy is included in the LICENSE file that tschatzl@5116: * accompanied this code). tschatzl@5116: * tschatzl@5116: * You should have received a copy of the GNU General Public License version tschatzl@5116: * 2 along with this work; if not, write to the Free Software Foundation, tschatzl@5116: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tschatzl@5116: * tschatzl@5116: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tschatzl@5116: * or visit www.oracle.com if you need additional information or have any tschatzl@5116: * questions. tschatzl@5116: */ tschatzl@5116: tschatzl@5116: import java.util.regex.Matcher; tschatzl@5116: import java.util.regex.Pattern; tschatzl@5116: import java.util.ArrayList; tschatzl@5116: import java.util.Arrays; tschatzl@5116: tschatzl@5116: import com.oracle.java.testlibrary.*; tschatzl@5116: import sun.hotspot.WhiteBox; tschatzl@5116: tschatzl@5116: class ErgoArgsPrinter { tschatzl@5116: public static void main(String[] args) throws Exception { tschatzl@5116: WhiteBox wb = WhiteBox.getWhiteBox(); tschatzl@5116: wb.printHeapSizes(); tschatzl@5116: } tschatzl@5116: } tschatzl@5116: tschatzl@5116: final class MinInitialMaxValues { tschatzl@5116: public long minHeapSize; tschatzl@5116: public long initialHeapSize; tschatzl@5116: public long maxHeapSize; tschatzl@5116: ehelin@9622: public long spaceAlignment; ehelin@9622: public long heapAlignment; tschatzl@5116: } tschatzl@5116: tschatzl@5116: class TestMaxHeapSizeTools { tschatzl@5116: tschatzl@5116: public static void checkMinInitialMaxHeapFlags(String gcflag) throws Exception { tschatzl@5116: checkInvalidMinInitialHeapCombinations(gcflag); tschatzl@5116: checkValidMinInitialHeapCombinations(gcflag); tschatzl@5116: checkInvalidInitialMaxHeapCombinations(gcflag); tschatzl@5116: checkValidInitialMaxHeapCombinations(gcflag); tschatzl@5116: } tschatzl@5116: tschatzl@5116: public static void checkMinInitialErgonomics(String gcflag) throws Exception { tschatzl@5116: // heap sizing ergonomics use the value NewSize + OldSize as default values tschatzl@5116: // for ergonomics calculation. Retrieve these values. tschatzl@5116: long[] values = new long[2]; tschatzl@5116: getNewOldSize(gcflag, values); tschatzl@5116: tschatzl@5116: // we check cases with values smaller and larger than this default value. tschatzl@5116: long newPlusOldSize = values[0] + values[1]; tschatzl@5116: long smallValue = newPlusOldSize / 2; tschatzl@5116: long largeValue = newPlusOldSize * 2; jwilhelm@6085: long maxHeapSize = largeValue + (2 * 1024 * 1024); tschatzl@5116: tschatzl@5116: // -Xms is not set jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize }, values, -1, -1); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=0" }, values, -1, -1); tschatzl@5116: tschatzl@5116: // -Xms is set to zero jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0" }, values, -1, -1); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=0" }, values, -1, -1); tschatzl@5116: tschatzl@5116: // -Xms is set to small value jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue }, values, -1, -1); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=0" }, values, smallValue, -1); tschatzl@5116: tschatzl@5116: // -Xms is set to large value jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue }, values, largeValue, largeValue); jwilhelm@6085: checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue, "-XX:InitialHeapSize=0" }, values, largeValue, -1); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static long align_up(long value, long alignment) { tschatzl@5116: long alignmentMinusOne = alignment - 1; tschatzl@5116: return (value + alignmentMinusOne) & ~alignmentMinusOne; tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void getNewOldSize(String gcflag, long[] values) throws Exception { tschatzl@5116: ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag, tschatzl@5116: "-XX:+PrintFlagsFinal", "-version"); tschatzl@5116: OutputAnalyzer output = new OutputAnalyzer(pb.start()); tschatzl@5116: output.shouldHaveExitValue(0); tschatzl@5116: tschatzl@5116: String stdout = output.getStdout(); tschatzl@5116: values[0] = getFlagValue(" NewSize", stdout); tschatzl@5116: values[1] = getFlagValue(" OldSize", stdout); tschatzl@5116: } tschatzl@5116: tschatzl@5116: public static void checkGenMaxHeapErgo(String gcflag) throws Exception { tschatzl@5116: TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 3); tschatzl@5116: TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 4); tschatzl@5116: TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 5); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void checkInvalidMinInitialHeapCombinations(String gcflag) throws Exception { tschatzl@5116: expectError(new String[] { gcflag, "-Xms8M", "-XX:InitialHeapSize=4M", "-version" }); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void checkValidMinInitialHeapCombinations(String gcflag) throws Exception { tschatzl@5116: expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms4M", "-version" }); tschatzl@5116: expectValid(new String[] { gcflag, "-Xms4M", "-XX:InitialHeapSize=8M", "-version" }); tschatzl@5116: expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms8M", "-version" }); tschatzl@5116: // the following is not an error as -Xms sets both minimal and initial heap size tschatzl@5116: expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-Xms8M", "-version" }); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void checkInvalidInitialMaxHeapCombinations(String gcflag) throws Exception { tschatzl@5116: expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version" }); tschatzl@5116: expectError(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" }); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void checkValidInitialMaxHeapCombinations(String gcflag) throws Exception { tschatzl@5116: expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" }); tschatzl@5116: expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version" }); tschatzl@5116: expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version" }); tschatzl@5116: // a value of "0" for initial heap size means auto-detect tschatzl@5116: expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=0M", "-version" }); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static long valueAfter(String source, String match) { tschatzl@5116: int start = source.indexOf(match) + match.length(); tschatzl@5116: String tail = source.substring(start).split(" ")[0]; tschatzl@5116: return Long.parseLong(tail); tschatzl@5116: } tschatzl@5116: tschatzl@5116: /** tschatzl@5116: * Executes a new VM process with the given class and parameters. tschatzl@5116: * @param vmargs Arguments to the VM to run tschatzl@5116: * @param classname Name of the class to run tschatzl@5116: * @param arguments Arguments to the class tschatzl@5116: * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string tschatzl@5116: * @return The OutputAnalyzer with the results for the invocation. tschatzl@5116: */ tschatzl@5116: public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception { tschatzl@5116: ArrayList finalargs = new ArrayList(); tschatzl@5116: tschatzl@5116: String[] whiteboxOpts = new String[] { tschatzl@5116: "-Xbootclasspath/a:.", tschatzl@5116: "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", tschatzl@5116: "-cp", System.getProperty("java.class.path"), tschatzl@5116: }; tschatzl@5116: tschatzl@5116: if (useTestDotJavaDotOpts) { tschatzl@5116: // System.getProperty("test.java.opts") is '' if no options is set, tschatzl@5116: // we need to skip such a result tschatzl@5116: String[] externalVMOpts = new String[0]; tschatzl@5116: if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) { tschatzl@5116: externalVMOpts = System.getProperty("test.java.opts").split(" "); tschatzl@5116: } tschatzl@5116: finalargs.addAll(Arrays.asList(externalVMOpts)); tschatzl@5116: } tschatzl@5116: tschatzl@5116: finalargs.addAll(Arrays.asList(vmargs)); tschatzl@5116: finalargs.addAll(Arrays.asList(whiteboxOpts)); tschatzl@5116: finalargs.add(classname); tschatzl@5116: finalargs.addAll(Arrays.asList(arguments)); tschatzl@5116: tschatzl@5116: ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0])); tschatzl@5116: OutputAnalyzer output = new OutputAnalyzer(pb.start()); tschatzl@5116: output.shouldHaveExitValue(0); tschatzl@5116: tschatzl@5116: return output; tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception { tschatzl@5116: OutputAnalyzer output = runWhiteBoxTest(args, ErgoArgsPrinter.class.getName(), new String[] {}, false); tschatzl@5116: tschatzl@5116: // the output we watch for has the following format: tschatzl@5116: // tschatzl@5116: // "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B" tschatzl@5116: // tschatzl@5116: // where A, B, X, Y and Z are sizes in bytes. tschatzl@5116: // Unfortunately there is no other way to retrieve the minimum heap size and tschatzl@5116: // the alignments. tschatzl@5116: ehelin@9622: Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Space alignment \\d+ Heap alignment \\d+"). tschatzl@5116: matcher(output.getStdout()); tschatzl@5116: if (!m.find()) { tschatzl@5116: throw new RuntimeException("Could not find heap size string."); tschatzl@5116: } tschatzl@5116: tschatzl@5116: String match = m.group(); tschatzl@5116: tschatzl@5116: // actual values tschatzl@5116: val.minHeapSize = valueAfter(match, "Minimum heap "); tschatzl@5116: val.initialHeapSize = valueAfter(match, "Initial heap "); tschatzl@5116: val.maxHeapSize = valueAfter(match, "Maximum heap "); ehelin@9622: val.spaceAlignment = valueAfter(match, "Space alignment "); ehelin@9622: val.heapAlignment = valueAfter(match, "Heap alignment "); tschatzl@5116: } tschatzl@5116: tschatzl@5116: /** tschatzl@5116: * Verify whether the VM automatically synchronizes minimum and initial heap size if only tschatzl@5116: * one is given for the GC specified. tschatzl@5116: */ tschatzl@5116: public static void checkErgonomics(String[] args, long[] newoldsize, tschatzl@5116: long expectedMin, long expectedInitial) throws Exception { tschatzl@5116: tschatzl@5116: MinInitialMaxValues v = new MinInitialMaxValues(); tschatzl@5116: getMinInitialMaxHeap(args, v); tschatzl@5116: ehelin@9622: if ((expectedMin != -1) && (align_up(expectedMin, v.heapAlignment) != v.minHeapSize)) { tschatzl@5116: throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize + tschatzl@5116: " differs from expected minimum heap size of " + expectedMin); tschatzl@5116: } tschatzl@5116: ehelin@9622: if ((expectedInitial != -1) && (align_up(expectedInitial, v.heapAlignment) != v.initialHeapSize)) { tschatzl@5116: throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize + tschatzl@5116: " differs from expected initial heap size of " + expectedInitial); tschatzl@5116: } tschatzl@5116: tschatzl@5116: // always check the invariant min <= initial <= max heap size tschatzl@5116: if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) { tschatzl@5116: throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " + tschatzl@5116: v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize); tschatzl@5116: } tschatzl@5116: } tschatzl@5116: tschatzl@5116: /** tschatzl@5116: * Verify whether the VM respects the given maximum heap size in MB for the tschatzl@5116: * GC specified. tschatzl@5116: * @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC tschatzl@5116: * @param maxHeapSize the maximum heap size to verify, in MB. tschatzl@5116: */ tschatzl@5116: public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception { tschatzl@5116: final long K = 1024; tschatzl@5116: tschatzl@5116: MinInitialMaxValues v = new MinInitialMaxValues(); tschatzl@5116: getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v); tschatzl@5116: ehelin@9622: long expectedHeapSize = align_up(maxHeapsize * K * K, v.heapAlignment); tschatzl@5116: long actualHeapSize = v.maxHeapSize; tschatzl@5116: tschatzl@5116: if (actualHeapSize > expectedHeapSize) { tschatzl@5116: throw new RuntimeException("Heap has " + actualHeapSize + tschatzl@5116: " bytes, expected to be less than " + expectedHeapSize); tschatzl@5116: } tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static long getFlagValue(String flag, String where) { tschatzl@5116: Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where); tschatzl@5116: if (!m.find()) { tschatzl@5116: throw new RuntimeException("Could not find value for flag " + flag + " in output string"); tschatzl@5116: } tschatzl@5116: String match = m.group(); tschatzl@5116: return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length())); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception { tschatzl@5116: if (contains) { tschatzl@5116: output.shouldContain(message); tschatzl@5116: } else { tschatzl@5116: output.shouldNotContain(message); tschatzl@5116: } tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception { tschatzl@5116: ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags); tschatzl@5116: OutputAnalyzer output = new OutputAnalyzer(pb.start()); tschatzl@5116: shouldContainOrNot(output, hasWarning, "Warning"); tschatzl@5116: shouldContainOrNot(output, hasError, "Error"); tschatzl@5116: output.shouldHaveExitValue(errorcode); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void expectError(String[] flags) throws Exception { tschatzl@5116: expect(flags, false, true, 1); tschatzl@5116: } tschatzl@5116: tschatzl@5116: private static void expectValid(String[] flags) throws Exception { tschatzl@5116: expect(flags, false, false, 0); tschatzl@5116: } tschatzl@5116: } tschatzl@5116: