29 import java.io.Reader; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.List; 33 import java.util.LinkedHashMap; 34 import java.util.Map; 35 import java.util.StringTokenizer; 36 import java.util.function.Predicate; 37 import jdk.test.lib.OutputAnalyzer; 38 import jdk.test.lib.Platform; 39 import jdk.test.lib.ProcessTools; 40 41 public class JVMOptionsUtils { 42 43 /* Java option which print options with ranges */ 44 private static final String PRINT_FLAGS_RANGES = "-XX:+PrintFlagsRanges"; 45 46 /* StringBuilder to accumulate failed message */ 47 private static final StringBuilder finalFailedMessage = new StringBuilder(); 48 49 /** 50 * Add dependency for option depending on it's name. E.g. enable G1 GC for 51 * G1 options or add prepend options to not hit constraints. 52 * 53 * @param option option 54 */ 55 private static void addNameDependency(JVMOption option) { 56 String name = option.getName(); 57 58 if (name.startsWith("G1")) { 59 option.addPrepend("-XX:+UseG1GC"); 60 } 61 62 if (name.startsWith("CMS")) { 63 option.addPrepend("-XX:+UseConcMarkSweepGC"); 64 } 65 66 switch (name) { 67 case "MinHeapFreeRatio": 68 option.addPrepend("-XX:MaxHeapFreeRatio=100"); 314 * @param withRanges true if needed options with defined ranges inside JVM 315 * @param acceptOrigin predicate for option origins. Origins can be 316 * "product", "diagnostic" etc. Accept option only if acceptOrigin evaluates 317 * to true. 318 * @param additionalArgs additional arguments to the Java process which ran 319 * with "-XX:+PrintFlagsRanges" 320 * @return map from option name to the JVMOption object 321 * @throws Exception if a new process can not be created or an error 322 * occurred while reading the data 323 */ 324 public static Map<String, JVMOption> getOptionsAsMap(boolean withRanges, Predicate<String> acceptOrigin, 325 String... additionalArgs) throws Exception { 326 Map<String, JVMOption> result; 327 Process p; 328 List<String> runJava = new ArrayList<>(); 329 330 if (additionalArgs.length > 0) { 331 runJava.addAll(Arrays.asList(additionalArgs)); 332 } 333 334 if (Platform.isClient()) { 335 runJava.add("-client"); 336 } 337 338 runJava.add(PRINT_FLAGS_RANGES); 339 runJava.add("-version"); 340 341 p = ProcessTools.createJavaProcessBuilder(runJava.toArray(new String[0])).start(); 342 343 result = getJVMOptions(new InputStreamReader(p.getInputStream()), withRanges, acceptOrigin); 344 345 p.waitFor(); 346 347 return result; 348 } 349 350 /** 351 * Get JVM options as list. Can return options with defined ranges or 352 * options without range depending on "withRanges" argument. "acceptOrigin" 353 * predicate can be used to filter option origin. 354 * 355 * @param withRanges true if needed options with defined ranges inside JVM 356 * @param acceptOrigin predicate for option origins. Origins can be 357 * "product", "diagnostic" etc. Accept option only if acceptOrigin evaluates | 29 import java.io.Reader; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.List; 33 import java.util.LinkedHashMap; 34 import java.util.Map; 35 import java.util.StringTokenizer; 36 import java.util.function.Predicate; 37 import jdk.test.lib.OutputAnalyzer; 38 import jdk.test.lib.Platform; 39 import jdk.test.lib.ProcessTools; 40 41 public class JVMOptionsUtils { 42 43 /* Java option which print options with ranges */ 44 private static final String PRINT_FLAGS_RANGES = "-XX:+PrintFlagsRanges"; 45 46 /* StringBuilder to accumulate failed message */ 47 private static final StringBuilder finalFailedMessage = new StringBuilder(); 48 49 /* Used to start the JVM with the same type as current */ 50 static String VMType; 51 52 static { 53 if (Platform.isServer()) { 54 VMType = "-server"; 55 } else if (Platform.isClient()) { 56 VMType = "-client"; 57 } else if (Platform.isMinimal()) { 58 VMType = "-minimal"; 59 } else if (Platform.isGraal()) { 60 VMType = "-graal"; 61 } else { 62 VMType = null; 63 } 64 } 65 66 /** 67 * Add dependency for option depending on it's name. E.g. enable G1 GC for 68 * G1 options or add prepend options to not hit constraints. 69 * 70 * @param option option 71 */ 72 private static void addNameDependency(JVMOption option) { 73 String name = option.getName(); 74 75 if (name.startsWith("G1")) { 76 option.addPrepend("-XX:+UseG1GC"); 77 } 78 79 if (name.startsWith("CMS")) { 80 option.addPrepend("-XX:+UseConcMarkSweepGC"); 81 } 82 83 switch (name) { 84 case "MinHeapFreeRatio": 85 option.addPrepend("-XX:MaxHeapFreeRatio=100"); 331 * @param withRanges true if needed options with defined ranges inside JVM 332 * @param acceptOrigin predicate for option origins. Origins can be 333 * "product", "diagnostic" etc. Accept option only if acceptOrigin evaluates 334 * to true. 335 * @param additionalArgs additional arguments to the Java process which ran 336 * with "-XX:+PrintFlagsRanges" 337 * @return map from option name to the JVMOption object 338 * @throws Exception if a new process can not be created or an error 339 * occurred while reading the data 340 */ 341 public static Map<String, JVMOption> getOptionsAsMap(boolean withRanges, Predicate<String> acceptOrigin, 342 String... additionalArgs) throws Exception { 343 Map<String, JVMOption> result; 344 Process p; 345 List<String> runJava = new ArrayList<>(); 346 347 if (additionalArgs.length > 0) { 348 runJava.addAll(Arrays.asList(additionalArgs)); 349 } 350 351 if (VMType != null) { 352 runJava.add(VMType); 353 } 354 runJava.add(PRINT_FLAGS_RANGES); 355 runJava.add("-version"); 356 357 p = ProcessTools.createJavaProcessBuilder(runJava.toArray(new String[0])).start(); 358 359 result = getJVMOptions(new InputStreamReader(p.getInputStream()), withRanges, acceptOrigin); 360 361 p.waitFor(); 362 363 return result; 364 } 365 366 /** 367 * Get JVM options as list. Can return options with defined ranges or 368 * options without range depending on "withRanges" argument. "acceptOrigin" 369 * predicate can be used to filter option origin. 370 * 371 * @param withRanges true if needed options with defined ranges inside JVM 372 * @param acceptOrigin predicate for option origins. Origins can be 373 * "product", "diagnostic" etc. Accept option only if acceptOrigin evaluates |