test/gc/arguments/TestMaxHeapSizeTools.java

Thu, 19 Sep 2013 09:36:51 -0700

author
cl
date
Thu, 19 Sep 2013 09:36:51 -0700
changeset 5664
34aa07e92d22
parent 5116
7a95933197d0
child 6085
8f07aa079343
permissions
-rw-r--r--

Added tag jdk8-b108 for changeset 85072013aad4

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
tschatzl@5116 44 public long minAlignment;
tschatzl@5116 45 public long maxAlignment;
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;
tschatzl@5116 67
tschatzl@5116 68 // -Xms is not set
tschatzl@5116 69 checkErgonomics(new String[] { gcflag, "-Xmx16M" }, values, -1, -1);
tschatzl@5116 70 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);
tschatzl@5116 71 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);
tschatzl@5116 72 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-XX:InitialHeapSize=0" }, values, -1, -1);
tschatzl@5116 73
tschatzl@5116 74 // -Xms is set to zero
tschatzl@5116 75 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms0" }, values, -1, -1);
tschatzl@5116 76 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms0", "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);
tschatzl@5116 77 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms0", "-XX:InitialHeapSize=" + largeValue }, values, -1, largeValue);
tschatzl@5116 78 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms0", "-XX:InitialHeapSize=0" }, values, -1, -1);
tschatzl@5116 79
tschatzl@5116 80 // -Xms is set to small value
tschatzl@5116 81 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + smallValue }, values, -1, -1);
tschatzl@5116 82 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + smallValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);
tschatzl@5116 83 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + smallValue, "-XX:InitialHeapSize=" + largeValue }, values, smallValue, largeValue);
tschatzl@5116 84 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + smallValue, "-XX:InitialHeapSize=0" }, values, smallValue, -1);
tschatzl@5116 85
tschatzl@5116 86 // -Xms is set to large value
tschatzl@5116 87 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + largeValue }, values, largeValue, largeValue);
tschatzl@5116 88 // the next case has already been checked elsewhere and gives an error
tschatzl@5116 89 // checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + largeValue, "-XX:InitialHeapSize=" + smallValue }, values, smallValue, smallValue);
tschatzl@5116 90 // the next case has already been checked elsewhere too
tschatzl@5116 91 // checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + largeValue, "-XX:InitialHeapSize=" + largeValue }, values, values[0], largeValue);
tschatzl@5116 92 checkErgonomics(new String[] { gcflag, "-Xmx16M", "-Xms" + largeValue, "-XX:InitialHeapSize=0" }, values, largeValue, -1);
tschatzl@5116 93 }
tschatzl@5116 94
tschatzl@5116 95 private static long align_up(long value, long alignment) {
tschatzl@5116 96 long alignmentMinusOne = alignment - 1;
tschatzl@5116 97 return (value + alignmentMinusOne) & ~alignmentMinusOne;
tschatzl@5116 98 }
tschatzl@5116 99
tschatzl@5116 100 private static void getNewOldSize(String gcflag, long[] values) throws Exception {
tschatzl@5116 101 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(gcflag,
tschatzl@5116 102 "-XX:+PrintFlagsFinal", "-version");
tschatzl@5116 103 OutputAnalyzer output = new OutputAnalyzer(pb.start());
tschatzl@5116 104 output.shouldHaveExitValue(0);
tschatzl@5116 105
tschatzl@5116 106 String stdout = output.getStdout();
tschatzl@5116 107 values[0] = getFlagValue(" NewSize", stdout);
tschatzl@5116 108 values[1] = getFlagValue(" OldSize", stdout);
tschatzl@5116 109 }
tschatzl@5116 110
tschatzl@5116 111 public static void checkGenMaxHeapErgo(String gcflag) throws Exception {
tschatzl@5116 112 TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 3);
tschatzl@5116 113 TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 4);
tschatzl@5116 114 TestMaxHeapSizeTools.checkGenMaxHeapSize(gcflag, 5);
tschatzl@5116 115 }
tschatzl@5116 116
tschatzl@5116 117 private static void checkInvalidMinInitialHeapCombinations(String gcflag) throws Exception {
tschatzl@5116 118 expectError(new String[] { gcflag, "-Xms8M", "-XX:InitialHeapSize=4M", "-version" });
tschatzl@5116 119 }
tschatzl@5116 120
tschatzl@5116 121 private static void checkValidMinInitialHeapCombinations(String gcflag) throws Exception {
tschatzl@5116 122 expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms4M", "-version" });
tschatzl@5116 123 expectValid(new String[] { gcflag, "-Xms4M", "-XX:InitialHeapSize=8M", "-version" });
tschatzl@5116 124 expectValid(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-Xms8M", "-version" });
tschatzl@5116 125 // the following is not an error as -Xms sets both minimal and initial heap size
tschatzl@5116 126 expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-Xms8M", "-version" });
tschatzl@5116 127 }
tschatzl@5116 128
tschatzl@5116 129 private static void checkInvalidInitialMaxHeapCombinations(String gcflag) throws Exception {
tschatzl@5116 130 expectError(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=8M", "-version" });
tschatzl@5116 131 expectError(new String[] { gcflag, "-XX:InitialHeapSize=8M", "-XX:MaxHeapSize=4M", "-version" });
tschatzl@5116 132 }
tschatzl@5116 133
tschatzl@5116 134 private static void checkValidInitialMaxHeapCombinations(String gcflag) throws Exception {
tschatzl@5116 135 expectValid(new String[] { gcflag, "-XX:InitialHeapSize=4M", "-XX:MaxHeapSize=8M", "-version" });
tschatzl@5116 136 expectValid(new String[] { gcflag, "-XX:MaxHeapSize=8M", "-XX:InitialHeapSize=4M", "-version" });
tschatzl@5116 137 expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=4M", "-version" });
tschatzl@5116 138 // a value of "0" for initial heap size means auto-detect
tschatzl@5116 139 expectValid(new String[] { gcflag, "-XX:MaxHeapSize=4M", "-XX:InitialHeapSize=0M", "-version" });
tschatzl@5116 140 }
tschatzl@5116 141
tschatzl@5116 142 private static long valueAfter(String source, String match) {
tschatzl@5116 143 int start = source.indexOf(match) + match.length();
tschatzl@5116 144 String tail = source.substring(start).split(" ")[0];
tschatzl@5116 145 return Long.parseLong(tail);
tschatzl@5116 146 }
tschatzl@5116 147
tschatzl@5116 148 /**
tschatzl@5116 149 * Executes a new VM process with the given class and parameters.
tschatzl@5116 150 * @param vmargs Arguments to the VM to run
tschatzl@5116 151 * @param classname Name of the class to run
tschatzl@5116 152 * @param arguments Arguments to the class
tschatzl@5116 153 * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string
tschatzl@5116 154 * @return The OutputAnalyzer with the results for the invocation.
tschatzl@5116 155 */
tschatzl@5116 156 public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception {
tschatzl@5116 157 ArrayList<String> finalargs = new ArrayList<String>();
tschatzl@5116 158
tschatzl@5116 159 String[] whiteboxOpts = new String[] {
tschatzl@5116 160 "-Xbootclasspath/a:.",
tschatzl@5116 161 "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
tschatzl@5116 162 "-cp", System.getProperty("java.class.path"),
tschatzl@5116 163 };
tschatzl@5116 164
tschatzl@5116 165 if (useTestDotJavaDotOpts) {
tschatzl@5116 166 // System.getProperty("test.java.opts") is '' if no options is set,
tschatzl@5116 167 // we need to skip such a result
tschatzl@5116 168 String[] externalVMOpts = new String[0];
tschatzl@5116 169 if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) {
tschatzl@5116 170 externalVMOpts = System.getProperty("test.java.opts").split(" ");
tschatzl@5116 171 }
tschatzl@5116 172 finalargs.addAll(Arrays.asList(externalVMOpts));
tschatzl@5116 173 }
tschatzl@5116 174
tschatzl@5116 175 finalargs.addAll(Arrays.asList(vmargs));
tschatzl@5116 176 finalargs.addAll(Arrays.asList(whiteboxOpts));
tschatzl@5116 177 finalargs.add(classname);
tschatzl@5116 178 finalargs.addAll(Arrays.asList(arguments));
tschatzl@5116 179
tschatzl@5116 180 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
tschatzl@5116 181 OutputAnalyzer output = new OutputAnalyzer(pb.start());
tschatzl@5116 182 output.shouldHaveExitValue(0);
tschatzl@5116 183
tschatzl@5116 184 return output;
tschatzl@5116 185 }
tschatzl@5116 186
tschatzl@5116 187 private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception {
tschatzl@5116 188 OutputAnalyzer output = runWhiteBoxTest(args, ErgoArgsPrinter.class.getName(), new String[] {}, false);
tschatzl@5116 189
tschatzl@5116 190 // the output we watch for has the following format:
tschatzl@5116 191 //
tschatzl@5116 192 // "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B"
tschatzl@5116 193 //
tschatzl@5116 194 // where A, B, X, Y and Z are sizes in bytes.
tschatzl@5116 195 // Unfortunately there is no other way to retrieve the minimum heap size and
tschatzl@5116 196 // the alignments.
tschatzl@5116 197
tschatzl@5116 198 Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Min alignment \\d+ Max alignment \\d+").
tschatzl@5116 199 matcher(output.getStdout());
tschatzl@5116 200 if (!m.find()) {
tschatzl@5116 201 throw new RuntimeException("Could not find heap size string.");
tschatzl@5116 202 }
tschatzl@5116 203
tschatzl@5116 204 String match = m.group();
tschatzl@5116 205
tschatzl@5116 206 // actual values
tschatzl@5116 207 val.minHeapSize = valueAfter(match, "Minimum heap ");
tschatzl@5116 208 val.initialHeapSize = valueAfter(match, "Initial heap ");
tschatzl@5116 209 val.maxHeapSize = valueAfter(match, "Maximum heap ");
tschatzl@5116 210 val.minAlignment = valueAfter(match, "Min alignment ");
tschatzl@5116 211 val.maxAlignment = valueAfter(match, "Max alignment ");
tschatzl@5116 212 }
tschatzl@5116 213
tschatzl@5116 214 /**
tschatzl@5116 215 * Verify whether the VM automatically synchronizes minimum and initial heap size if only
tschatzl@5116 216 * one is given for the GC specified.
tschatzl@5116 217 */
tschatzl@5116 218 public static void checkErgonomics(String[] args, long[] newoldsize,
tschatzl@5116 219 long expectedMin, long expectedInitial) throws Exception {
tschatzl@5116 220
tschatzl@5116 221 MinInitialMaxValues v = new MinInitialMaxValues();
tschatzl@5116 222 getMinInitialMaxHeap(args, v);
tschatzl@5116 223
tschatzl@5116 224 if ((expectedMin != -1) && (align_up(expectedMin, v.minAlignment) != v.minHeapSize)) {
tschatzl@5116 225 throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize +
tschatzl@5116 226 " differs from expected minimum heap size of " + expectedMin);
tschatzl@5116 227 }
tschatzl@5116 228
tschatzl@5116 229 if ((expectedInitial != -1) && (align_up(expectedInitial, v.minAlignment) != v.initialHeapSize)) {
tschatzl@5116 230 throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize +
tschatzl@5116 231 " differs from expected initial heap size of " + expectedInitial);
tschatzl@5116 232 }
tschatzl@5116 233
tschatzl@5116 234 // always check the invariant min <= initial <= max heap size
tschatzl@5116 235 if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) {
tschatzl@5116 236 throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " +
tschatzl@5116 237 v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize);
tschatzl@5116 238 }
tschatzl@5116 239 }
tschatzl@5116 240
tschatzl@5116 241 /**
tschatzl@5116 242 * Verify whether the VM respects the given maximum heap size in MB for the
tschatzl@5116 243 * GC specified.
tschatzl@5116 244 * @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC
tschatzl@5116 245 * @param maxHeapSize the maximum heap size to verify, in MB.
tschatzl@5116 246 */
tschatzl@5116 247 public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception {
tschatzl@5116 248 final long K = 1024;
tschatzl@5116 249
tschatzl@5116 250 MinInitialMaxValues v = new MinInitialMaxValues();
tschatzl@5116 251 getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v);
tschatzl@5116 252
tschatzl@5116 253 long expectedHeapSize = align_up(maxHeapsize * K * K, v.maxAlignment);
tschatzl@5116 254 long actualHeapSize = v.maxHeapSize;
tschatzl@5116 255
tschatzl@5116 256 if (actualHeapSize > expectedHeapSize) {
tschatzl@5116 257 throw new RuntimeException("Heap has " + actualHeapSize +
tschatzl@5116 258 " bytes, expected to be less than " + expectedHeapSize);
tschatzl@5116 259 }
tschatzl@5116 260 }
tschatzl@5116 261
tschatzl@5116 262 private static long getFlagValue(String flag, String where) {
tschatzl@5116 263 Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
tschatzl@5116 264 if (!m.find()) {
tschatzl@5116 265 throw new RuntimeException("Could not find value for flag " + flag + " in output string");
tschatzl@5116 266 }
tschatzl@5116 267 String match = m.group();
tschatzl@5116 268 return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length()));
tschatzl@5116 269 }
tschatzl@5116 270
tschatzl@5116 271 private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception {
tschatzl@5116 272 if (contains) {
tschatzl@5116 273 output.shouldContain(message);
tschatzl@5116 274 } else {
tschatzl@5116 275 output.shouldNotContain(message);
tschatzl@5116 276 }
tschatzl@5116 277 }
tschatzl@5116 278
tschatzl@5116 279 private static void expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {
tschatzl@5116 280 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);
tschatzl@5116 281 OutputAnalyzer output = new OutputAnalyzer(pb.start());
tschatzl@5116 282 shouldContainOrNot(output, hasWarning, "Warning");
tschatzl@5116 283 shouldContainOrNot(output, hasError, "Error");
tschatzl@5116 284 output.shouldHaveExitValue(errorcode);
tschatzl@5116 285 }
tschatzl@5116 286
tschatzl@5116 287 private static void expectError(String[] flags) throws Exception {
tschatzl@5116 288 expect(flags, false, true, 1);
tschatzl@5116 289 }
tschatzl@5116 290
tschatzl@5116 291 private static void expectValid(String[] flags) throws Exception {
tschatzl@5116 292 expect(flags, false, false, 0);
tschatzl@5116 293 }
tschatzl@5116 294 }
tschatzl@5116 295

mercurial