test/gc/arguments/TestMaxHeapSizeTools.java

Wed, 05 Feb 2014 10:09:54 +0100

author
ehelin
date
Wed, 05 Feb 2014 10:09:54 +0100
changeset 9622
1abddcd038df
parent 6085
8f07aa079343
child 9637
eef07cd490d4
permissions
-rw-r--r--

8028254: gc/arguments/TestMinInitialErgonomics.java failed with unexpected initial heap size
Reviewed-by: brutisso, tschatzl, sjohanss

tschatzl@5116 1 /*
tschatzl@5116 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
tschatzl@5116 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
tschatzl@5116 4 *
tschatzl@5116 5 * This code is free software; you can redistribute it and/or modify it
tschatzl@5116 6 * under the terms of the GNU General Public License version 2 only, as
tschatzl@5116 7 * published by the Free Software Foundation.
tschatzl@5116 8 *
tschatzl@5116 9 * This code is distributed in the hope that it will be useful, but WITHOUT
tschatzl@5116 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
tschatzl@5116 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
tschatzl@5116 12 * version 2 for more details (a copy is included in the LICENSE file that
tschatzl@5116 13 * accompanied this code).
tschatzl@5116 14 *
tschatzl@5116 15 * You should have received a copy of the GNU General Public License version
tschatzl@5116 16 * 2 along with this work; if not, write to the Free Software Foundation,
tschatzl@5116 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
tschatzl@5116 18 *
tschatzl@5116 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
tschatzl@5116 20 * or visit www.oracle.com if you need additional information or have any
tschatzl@5116 21 * questions.
tschatzl@5116 22 */
tschatzl@5116 23
tschatzl@5116 24 import java.util.regex.Matcher;
tschatzl@5116 25 import java.util.regex.Pattern;
tschatzl@5116 26 import java.util.ArrayList;
tschatzl@5116 27 import java.util.Arrays;
tschatzl@5116 28
tschatzl@5116 29 import com.oracle.java.testlibrary.*;
tschatzl@5116 30 import sun.hotspot.WhiteBox;
tschatzl@5116 31
tschatzl@5116 32 class ErgoArgsPrinter {
tschatzl@5116 33 public static void main(String[] args) throws Exception {
tschatzl@5116 34 WhiteBox wb = WhiteBox.getWhiteBox();
tschatzl@5116 35 wb.printHeapSizes();
tschatzl@5116 36 }
tschatzl@5116 37 }
tschatzl@5116 38
tschatzl@5116 39 final class MinInitialMaxValues {
tschatzl@5116 40 public long minHeapSize;
tschatzl@5116 41 public long initialHeapSize;
tschatzl@5116 42 public long maxHeapSize;
tschatzl@5116 43
ehelin@9622 44 public long spaceAlignment;
ehelin@9622 45 public long heapAlignment;
tschatzl@5116 46 }
tschatzl@5116 47
tschatzl@5116 48 class TestMaxHeapSizeTools {
tschatzl@5116 49
tschatzl@5116 50 public static void checkMinInitialMaxHeapFlags(String gcflag) throws Exception {
tschatzl@5116 51 checkInvalidMinInitialHeapCombinations(gcflag);
tschatzl@5116 52 checkValidMinInitialHeapCombinations(gcflag);
tschatzl@5116 53 checkInvalidInitialMaxHeapCombinations(gcflag);
tschatzl@5116 54 checkValidInitialMaxHeapCombinations(gcflag);
tschatzl@5116 55 }
tschatzl@5116 56
tschatzl@5116 57 public static void checkMinInitialErgonomics(String gcflag) throws Exception {
tschatzl@5116 58 // heap sizing ergonomics use the value NewSize + OldSize as default values
tschatzl@5116 59 // for ergonomics calculation. Retrieve these values.
tschatzl@5116 60 long[] values = new long[2];
tschatzl@5116 61 getNewOldSize(gcflag, values);
tschatzl@5116 62
tschatzl@5116 63 // we check cases with values smaller and larger than this default value.
tschatzl@5116 64 long newPlusOldSize = values[0] + values[1];
tschatzl@5116 65 long smallValue = newPlusOldSize / 2;
tschatzl@5116 66 long largeValue = newPlusOldSize * 2;
jwilhelm@6085 67 long maxHeapSize = largeValue + (2 * 1024 * 1024);
tschatzl@5116 68
tschatzl@5116 69 // -Xms is not set
jwilhelm@6085 70 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize }, values, -1, -1);
jwilhelm@6085 71 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue);
jwilhelm@6085 72 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);
jwilhelm@6085 73 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-XX:InitialHeapSize=0" }, values, -1, -1);
tschatzl@5116 74
tschatzl@5116 75 // -Xms is set to zero
jwilhelm@6085 76 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0" }, values, -1, -1);
jwilhelm@6085 77 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + smallValue }, values, -1, smallValue);
jwilhelm@6085 78 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);
jwilhelm@6085 79 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms0", "-XX:InitialHeapSize=0" }, values, -1, -1);
tschatzl@5116 80
tschatzl@5116 81 // -Xms is set to small value
jwilhelm@6085 82 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue }, values, -1, -1);
jwilhelm@6085 83 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);
jwilhelm@6085 84 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);
jwilhelm@6085 85 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + smallValue, "-XX:InitialHeapSize=0" }, values, smallValue, -1);
tschatzl@5116 86
tschatzl@5116 87 // -Xms is set to large value
jwilhelm@6085 88 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue }, values, largeValue, largeValue);
jwilhelm@6085 89 checkErgonomics(new String[] { gcflag, "-Xmx" + maxHeapSize, "-Xms" + largeValue, "-XX:InitialHeapSize=0" }, values, largeValue, -1);
tschatzl@5116 90 }
tschatzl@5116 91
tschatzl@5116 92 private static long align_up(long value, long alignment) {
tschatzl@5116 93 long alignmentMinusOne = alignment - 1;
tschatzl@5116 94 return (value + alignmentMinusOne) & ~alignmentMinusOne;
tschatzl@5116 95 }
tschatzl@5116 96
tschatzl@5116 97 private static void getNewOldSize(String gcflag, long[] values) throws Exception {
tschatzl@5116 98 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag,
tschatzl@5116 99 "-XX:+PrintFlagsFinal", "-version");
tschatzl@5116 100 OutputAnalyzer output = new OutputAnalyzer(pb.start());
tschatzl@5116 101 output.shouldHaveExitValue(0);
tschatzl@5116 102
tschatzl@5116 103 String stdout = output.getStdout();
tschatzl@5116 104 values[0] = getFlagValue(" NewSize", stdout);
tschatzl@5116 105 values[1] = getFlagValue(" OldSize", stdout);
tschatzl@5116 106 }
tschatzl@5116 107
tschatzl@5116 108 public static void checkGenMaxHeapErgo(String gcflag) throws Exception {
tschatzl@5116 109 TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 3);
tschatzl@5116 110 TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 4);
tschatzl@5116 111 TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 5);
tschatzl@5116 112 }
tschatzl@5116 113
tschatzl@5116 114 private static void checkInvalidMinInitialHeapCombinations(String gcflag) throws Exception {
tschatzl@5116 115 expectError(new String[] { gcflag, "-Xms8M", "-XX:InitialHeapSize=4M", "-version" });
tschatzl@5116 116 }
tschatzl@5116 117
tschatzl@5116 118 private static void checkValidMinInitialHeapCombinations(String gcflag) throws Exception {
tschatzl@5116 119 expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms4M", "-version" });
tschatzl@5116 120 expectValid(new String[] { gcflag, "-Xms4M", "-XX:InitialHeapSize=8M", "-version" });
tschatzl@5116 121 expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms8M", "-version" });
tschatzl@5116 122 // the following is not an error as -Xms sets both minimal and initial heap size
tschatzl@5116 123 expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-Xms8M", "-version" });
tschatzl@5116 124 }
tschatzl@5116 125
tschatzl@5116 126 private static void checkInvalidInitialMaxHeapCombinations(String gcflag) throws Exception {
tschatzl@5116 127 expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version" });
tschatzl@5116 128 expectError(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" });
tschatzl@5116 129 }
tschatzl@5116 130
tschatzl@5116 131 private static void checkValidInitialMaxHeapCombinations(String gcflag) throws Exception {
tschatzl@5116 132 expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" });
tschatzl@5116 133 expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version" });
tschatzl@5116 134 expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version" });
tschatzl@5116 135 // a value of "0" for initial heap size means auto-detect
tschatzl@5116 136 expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=0M", "-version" });
tschatzl@5116 137 }
tschatzl@5116 138
tschatzl@5116 139 private static long valueAfter(String source, String match) {
tschatzl@5116 140 int start = source.indexOf(match) + match.length();
tschatzl@5116 141 String tail = source.substring(start).split(" ")[0];
tschatzl@5116 142 return Long.parseLong(tail);
tschatzl@5116 143 }
tschatzl@5116 144
tschatzl@5116 145 /**
tschatzl@5116 146 * Executes a new VM process with the given class and parameters.
tschatzl@5116 147 * @param vmargs Arguments to the VM to run
tschatzl@5116 148 * @param classname Name of the class to run
tschatzl@5116 149 * @param arguments Arguments to the class
tschatzl@5116 150 * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string
tschatzl@5116 151 * @return The OutputAnalyzer with the results for the invocation.
tschatzl@5116 152 */
tschatzl@5116 153 public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception {
tschatzl@5116 154 ArrayList<String> finalargs = new ArrayList<String>();
tschatzl@5116 155
tschatzl@5116 156 String[] whiteboxOpts = new String[] {
tschatzl@5116 157 "-Xbootclasspath/a:.",
tschatzl@5116 158 "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
tschatzl@5116 159 "-cp", System.getProperty("java.class.path"),
tschatzl@5116 160 };
tschatzl@5116 161
tschatzl@5116 162 if (useTestDotJavaDotOpts) {
tschatzl@5116 163 // System.getProperty("test.java.opts") is '' if no options is set,
tschatzl@5116 164 // we need to skip such a result
tschatzl@5116 165 String[] externalVMOpts = new String[0];
tschatzl@5116 166 if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) {
tschatzl@5116 167 externalVMOpts = System.getProperty("test.java.opts").split(" ");
tschatzl@5116 168 }
tschatzl@5116 169 finalargs.addAll(Arrays.asList(externalVMOpts));
tschatzl@5116 170 }
tschatzl@5116 171
tschatzl@5116 172 finalargs.addAll(Arrays.asList(vmargs));
tschatzl@5116 173 finalargs.addAll(Arrays.asList(whiteboxOpts));
tschatzl@5116 174 finalargs.add(classname);
tschatzl@5116 175 finalargs.addAll(Arrays.asList(arguments));
tschatzl@5116 176
tschatzl@5116 177 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
tschatzl@5116 178 OutputAnalyzer output = new OutputAnalyzer(pb.start());
tschatzl@5116 179 output.shouldHaveExitValue(0);
tschatzl@5116 180
tschatzl@5116 181 return output;
tschatzl@5116 182 }
tschatzl@5116 183
tschatzl@5116 184 private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception {
tschatzl@5116 185 OutputAnalyzer output = runWhiteBoxTest(args, ErgoArgsPrinter.class.getName(), new String[] {}, false);
tschatzl@5116 186
tschatzl@5116 187 // the output we watch for has the following format:
tschatzl@5116 188 //
tschatzl@5116 189 // "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B"
tschatzl@5116 190 //
tschatzl@5116 191 // where A, B, X, Y and Z are sizes in bytes.
tschatzl@5116 192 // Unfortunately there is no other way to retrieve the minimum heap size and
tschatzl@5116 193 // the alignments.
tschatzl@5116 194
ehelin@9622 195 Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Space alignment \\d+ Heap alignment \\d+").
tschatzl@5116 196 matcher(output.getStdout());
tschatzl@5116 197 if (!m.find()) {
tschatzl@5116 198 throw new RuntimeException("Could not find heap size string.");
tschatzl@5116 199 }
tschatzl@5116 200
tschatzl@5116 201 String match = m.group();
tschatzl@5116 202
tschatzl@5116 203 // actual values
tschatzl@5116 204 val.minHeapSize = valueAfter(match, "Minimum heap ");
tschatzl@5116 205 val.initialHeapSize = valueAfter(match, "Initial heap ");
tschatzl@5116 206 val.maxHeapSize = valueAfter(match, "Maximum heap ");
ehelin@9622 207 val.spaceAlignment = valueAfter(match, "Space alignment ");
ehelin@9622 208 val.heapAlignment = valueAfter(match, "Heap alignment ");
tschatzl@5116 209 }
tschatzl@5116 210
tschatzl@5116 211 /**
tschatzl@5116 212 * Verify whether the VM automatically synchronizes minimum and initial heap size if only
tschatzl@5116 213 * one is given for the GC specified.
tschatzl@5116 214 */
tschatzl@5116 215 public static void checkErgonomics(String[] args, long[] newoldsize,
tschatzl@5116 216 long expectedMin, long expectedInitial) throws Exception {
tschatzl@5116 217
tschatzl@5116 218 MinInitialMaxValues v = new MinInitialMaxValues();
tschatzl@5116 219 getMinInitialMaxHeap(args, v);
tschatzl@5116 220
ehelin@9622 221 if ((expectedMin != -1) && (align_up(expectedMin, v.heapAlignment) != v.minHeapSize)) {
tschatzl@5116 222 throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize +
tschatzl@5116 223 " differs from expected minimum heap size of " + expectedMin);
tschatzl@5116 224 }
tschatzl@5116 225
ehelin@9622 226 if ((expectedInitial != -1) && (align_up(expectedInitial, v.heapAlignment) != v.initialHeapSize)) {
tschatzl@5116 227 throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize +
tschatzl@5116 228 " differs from expected initial heap size of " + expectedInitial);
tschatzl@5116 229 }
tschatzl@5116 230
tschatzl@5116 231 // always check the invariant min <= initial <= max heap size
tschatzl@5116 232 if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) {
tschatzl@5116 233 throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " +
tschatzl@5116 234 v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize);
tschatzl@5116 235 }
tschatzl@5116 236 }
tschatzl@5116 237
tschatzl@5116 238 /**
tschatzl@5116 239 * Verify whether the VM respects the given maximum heap size in MB for the
tschatzl@5116 240 * GC specified.
tschatzl@5116 241 * @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC
tschatzl@5116 242 * @param maxHeapSize the maximum heap size to verify, in MB.
tschatzl@5116 243 */
tschatzl@5116 244 public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception {
tschatzl@5116 245 final long K = 1024;
tschatzl@5116 246
tschatzl@5116 247 MinInitialMaxValues v = new MinInitialMaxValues();
tschatzl@5116 248 getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v);
tschatzl@5116 249
ehelin@9622 250 long expectedHeapSize = align_up(maxHeapsize * K * K, v.heapAlignment);
tschatzl@5116 251 long actualHeapSize = v.maxHeapSize;
tschatzl@5116 252
tschatzl@5116 253 if (actualHeapSize > expectedHeapSize) {
tschatzl@5116 254 throw new RuntimeException("Heap has " + actualHeapSize +
tschatzl@5116 255 " bytes, expected to be less than " + expectedHeapSize);
tschatzl@5116 256 }
tschatzl@5116 257 }
tschatzl@5116 258
tschatzl@5116 259 private static long getFlagValue(String flag, String where) {
tschatzl@5116 260 Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
tschatzl@5116 261 if (!m.find()) {
tschatzl@5116 262 throw new RuntimeException("Could not find value for flag " + flag + " in output string");
tschatzl@5116 263 }
tschatzl@5116 264 String match = m.group();
tschatzl@5116 265 return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length()));
tschatzl@5116 266 }
tschatzl@5116 267
tschatzl@5116 268 private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception {
tschatzl@5116 269 if (contains) {
tschatzl@5116 270 output.shouldContain(message);
tschatzl@5116 271 } else {
tschatzl@5116 272 output.shouldNotContain(message);
tschatzl@5116 273 }
tschatzl@5116 274 }
tschatzl@5116 275
tschatzl@5116 276 private static void expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {
tschatzl@5116 277 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);
tschatzl@5116 278 OutputAnalyzer output = new OutputAnalyzer(pb.start());
tschatzl@5116 279 shouldContainOrNot(output, hasWarning, "Warning");
tschatzl@5116 280 shouldContainOrNot(output, hasError, "Error");
tschatzl@5116 281 output.shouldHaveExitValue(errorcode);
tschatzl@5116 282 }
tschatzl@5116 283
tschatzl@5116 284 private static void expectError(String[] flags) throws Exception {
tschatzl@5116 285 expect(flags, false, true, 1);
tschatzl@5116 286 }
tschatzl@5116 287
tschatzl@5116 288 private static void expectValid(String[] flags) throws Exception {
tschatzl@5116 289 expect(flags, false, false, 0);
tschatzl@5116 290 }
tschatzl@5116 291 }
tschatzl@5116 292

mercurial