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