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