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