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