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;
175 finalargs.addAll(Arrays.asList(arguments));
176
177 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
178 OutputAnalyzer output = new OutputAnalyzer(pb.start());
179 output.shouldHaveExitValue(0);
180
181 return output;
182 }
183
184 private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception {
185 OutputAnalyzer output = runWhiteBoxTest(args, ErgoArgsPrinter.class.getName(), new String[] {}, false);
186
187 // the output we watch for has the following format:
188 //
189 // "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B"
190 //
191 // where A, B, X, Y and Z are sizes in bytes.
192 // Unfortunately there is no other way to retrieve the minimum heap size and
193 // the alignments.
194
195 Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Min alignment \\d+ Max alignment \\d+").
196 matcher(output.getStdout());
197 if (!m.find()) {
198 throw new RuntimeException("Could not find heap size string.");
199 }
200
201 String match = m.group();
202
203 // actual values
204 val.minHeapSize = valueAfter(match, "Minimum heap ");
205 val.initialHeapSize = valueAfter(match, "Initial heap ");
206 val.maxHeapSize = valueAfter(match, "Maximum heap ");
207 val.minAlignment = valueAfter(match, "Min alignment ");
208 val.maxAlignment = valueAfter(match, "Max alignment ");
209 }
210
211 /**
212 * Verify whether the VM automatically synchronizes minimum and initial heap size if only
213 * one is given for the GC specified.
214 */
215 public static void checkErgonomics(String[] args, long[] newoldsize,
216 long expectedMin, long expectedInitial) throws Exception {
217
218 MinInitialMaxValues v = new MinInitialMaxValues();
219 getMinInitialMaxHeap(args, v);
220
221 if ((expectedMin != -1) && (align_up(expectedMin, v.minAlignment) != v.minHeapSize)) {
222 throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize +
223 " differs from expected minimum heap size of " + expectedMin);
224 }
225
226 if ((expectedInitial != -1) && (align_up(expectedInitial, v.minAlignment) != v.initialHeapSize)) {
227 throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize +
228 " differs from expected initial heap size of " + expectedInitial);
229 }
230
231 // always check the invariant min <= initial <= max heap size
232 if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) {
233 throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " +
234 v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize);
235 }
236 }
237
238 /**
239 * Verify whether the VM respects the given maximum heap size in MB for the
240 * GC specified.
241 * @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC
242 * @param maxHeapSize the maximum heap size to verify, in MB.
243 */
244 public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception {
245 final long K = 1024;
246
247 MinInitialMaxValues v = new MinInitialMaxValues();
248 getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v);
249
250 long expectedHeapSize = align_up(maxHeapsize * K * K, v.maxAlignment);
251 long actualHeapSize = v.maxHeapSize;
252
253 if (actualHeapSize > expectedHeapSize) {
254 throw new RuntimeException("Heap has " + actualHeapSize +
255 " bytes, expected to be less than " + expectedHeapSize);
256 }
257 }
258
259 private static long getFlagValue(String flag, String where) {
260 Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
261 if (!m.find()) {
262 throw new RuntimeException("Could not find value for flag " + flag + " in output string");
263 }
264 String match = m.group();
265 return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length()));
266 }
267
268 private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception {
269 if (contains) {
270 output.shouldContain(message);
|
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 spaceAlignment;
45 public long heapAlignment;
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;
175 finalargs.addAll(Arrays.asList(arguments));
176
177 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
178 OutputAnalyzer output = new OutputAnalyzer(pb.start());
179 output.shouldHaveExitValue(0);
180
181 return output;
182 }
183
184 private static void getMinInitialMaxHeap(String[] args, MinInitialMaxValues val) throws Exception {
185 OutputAnalyzer output = runWhiteBoxTest(args, ErgoArgsPrinter.class.getName(), new String[] {}, false);
186
187 // the output we watch for has the following format:
188 //
189 // "Minimum heap X Initial heap Y Maximum heap Z Min alignment A Max Alignment B"
190 //
191 // where A, B, X, Y and Z are sizes in bytes.
192 // Unfortunately there is no other way to retrieve the minimum heap size and
193 // the alignments.
194
195 Matcher m = Pattern.compile("Minimum heap \\d+ Initial heap \\d+ Maximum heap \\d+ Space alignment \\d+ Heap alignment \\d+").
196 matcher(output.getStdout());
197 if (!m.find()) {
198 throw new RuntimeException("Could not find heap size string.");
199 }
200
201 String match = m.group();
202
203 // actual values
204 val.minHeapSize = valueAfter(match, "Minimum heap ");
205 val.initialHeapSize = valueAfter(match, "Initial heap ");
206 val.maxHeapSize = valueAfter(match, "Maximum heap ");
207 val.spaceAlignment = valueAfter(match, "Space alignment ");
208 val.heapAlignment = valueAfter(match, "Heap alignment ");
209 }
210
211 /**
212 * Verify whether the VM automatically synchronizes minimum and initial heap size if only
213 * one is given for the GC specified.
214 */
215 public static void checkErgonomics(String[] args, long[] newoldsize,
216 long expectedMin, long expectedInitial) throws Exception {
217
218 MinInitialMaxValues v = new MinInitialMaxValues();
219 getMinInitialMaxHeap(args, v);
220
221 if ((expectedMin != -1) && (align_up(expectedMin, v.heapAlignment) != v.minHeapSize)) {
222 throw new RuntimeException("Actual minimum heap size of " + v.minHeapSize +
223 " differs from expected minimum heap size of " + expectedMin);
224 }
225
226 if ((expectedInitial != -1) && (align_up(expectedInitial, v.heapAlignment) != v.initialHeapSize)) {
227 throw new RuntimeException("Actual initial heap size of " + v.initialHeapSize +
228 " differs from expected initial heap size of " + expectedInitial);
229 }
230
231 // always check the invariant min <= initial <= max heap size
232 if (!(v.minHeapSize <= v.initialHeapSize && v.initialHeapSize <= v.maxHeapSize)) {
233 throw new RuntimeException("Inconsistent min/initial/max heap sizes, they are " +
234 v.minHeapSize + "/" + v.initialHeapSize + "/" + v.maxHeapSize);
235 }
236 }
237
238 /**
239 * Verify whether the VM respects the given maximum heap size in MB for the
240 * GC specified.
241 * @param gcflag The garbage collector to test as command line flag. E.g. -XX:+UseG1GC
242 * @param maxHeapSize the maximum heap size to verify, in MB.
243 */
244 public static void checkGenMaxHeapSize(String gcflag, long maxHeapsize) throws Exception {
245 final long K = 1024;
246
247 MinInitialMaxValues v = new MinInitialMaxValues();
248 getMinInitialMaxHeap(new String[] { gcflag, "-XX:MaxHeapSize=" + maxHeapsize + "M" }, v);
249
250 long expectedHeapSize = align_up(maxHeapsize * K * K, v.heapAlignment);
251 long actualHeapSize = v.maxHeapSize;
252
253 if (actualHeapSize > expectedHeapSize) {
254 throw new RuntimeException("Heap has " + actualHeapSize +
255 " bytes, expected to be less than " + expectedHeapSize);
256 }
257 }
258
259 private static long getFlagValue(String flag, String where) {
260 Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
261 if (!m.find()) {
262 throw new RuntimeException("Could not find value for flag " + flag + " in output string");
263 }
264 String match = m.group();
265 return Long.parseLong(match.substring(match.lastIndexOf(" ") + 1, match.length()));
266 }
267
268 private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception {
269 if (contains) {
270 output.shouldContain(message);
|