hotspot/test/gc/arguments/TestG1HeapRegionSize.java
changeset 32613 73bec9f941d7
parent 32601 c5df671276bd
child 33144 c56850e328fc
--- a/hotspot/test/gc/arguments/TestG1HeapRegionSize.java	Wed Sep 02 11:41:42 2015 -0400
+++ b/hotspot/test/gc/arguments/TestG1HeapRegionSize.java	Wed Sep 02 17:49:46 2015 -0700
@@ -25,60 +25,42 @@
  * @test TestG1HeapRegionSize
  * @key gc
  * @bug 8021879
- * @requires vm.gc=="null" | vm.gc=="G1"
  * @summary Verify that the flag G1HeapRegionSize is updated properly
  * @modules java.management/sun.management
- * @library /testlibrary
- * @run main TestG1HeapRegionSize
+ * @run main/othervm -Xmx64m TestG1HeapRegionSize 1048576
+ * @run main/othervm -XX:G1HeapRegionSize=2m -Xmx64m TestG1HeapRegionSize 2097152
+ * @run main/othervm -XX:G1HeapRegionSize=3m -Xmx64m TestG1HeapRegionSize 2097152
+ * @run main/othervm -XX:G1HeapRegionSize=64m -Xmx256m TestG1HeapRegionSize 33554432
  */
 
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-
-import jdk.test.lib.*;
+import com.sun.management.HotSpotDiagnosticMXBean;
+import com.sun.management.VMOption;
+import java.lang.management.ManagementFactory;
 
 public class TestG1HeapRegionSize {
 
-  private static void checkG1HeapRegionSize(String[] flags, int expectedValue, int exitValue) throws Exception {
-    ArrayList<String> flagList = new ArrayList<String>();
-    flagList.addAll(Arrays.asList(flags));
-    flagList.add("-XX:+UseG1GC");
-    flagList.add("-XX:+PrintFlagsFinal");
-    flagList.add("-version");
+  public static void main(String[] args) {
+    HotSpotDiagnosticMXBean diagnostic =
+        ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
 
-    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flagList.toArray(new String[0]));
-    OutputAnalyzer output = new OutputAnalyzer(pb.start());
-    output.shouldHaveExitValue(exitValue);
+    String expectedValue = getExpectedValue(args);
+    VMOption option = diagnostic.getVMOption("UseG1GC");
+    if (option.getValue().equals("false")) {
+      System.out.println("Skipping this test. It is only a G1 test.");
+      return;
+    }
 
-    if (exitValue == 0) {
-      String stdout = output.getStdout();
-      //System.out.println(stdout);
-      int flagValue = getFlagValue("G1HeapRegionSize", stdout);
-      if (flagValue != expectedValue) {
-        throw new RuntimeException("Wrong value for G1HeapRegionSize. Expected " + expectedValue + " but got " + flagValue);
-      }
+    option = diagnostic.getVMOption("G1HeapRegionSize");
+    if (!expectedValue.equals(option.getValue())) {
+      throw new RuntimeException("Wrong value for G1HeapRegionSize. Expected " + expectedValue + " but got " + option.getValue());
     }
   }
 
-  private static int getFlagValue(String flag, String where) {
-    Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
-    if (!m.find()) {
-      throw new RuntimeException("Could not find value for flag " + flag + " in output string");
+  private static String getExpectedValue(String[] args) {
+    if (args.length != 1) {
+      throw new RuntimeException("Wrong number of arguments. Expected 1 but got " + args.length);
     }
-    String match = m.group();
-    return Integer.parseInt(match.substring(match.lastIndexOf(" ") + 1, match.length()));
+    return args[0];
   }
 
-  public static void main(String args[]) throws Exception {
-    final int M = 1024 * 1024;
-
-    checkG1HeapRegionSize(new String[] { "-Xmx64m"   /* default is 1m */        },  1*M, 0);
-    checkG1HeapRegionSize(new String[] { "-Xmx64m",  "-XX:G1HeapRegionSize=2m"  },  2*M, 0);
-    checkG1HeapRegionSize(new String[] { "-Xmx64m",  "-XX:G1HeapRegionSize=3m"  },  2*M, 0);
-    checkG1HeapRegionSize(new String[] { "-Xmx256m", "-XX:G1HeapRegionSize=32m" }, 32*M, 0);
-    checkG1HeapRegionSize(new String[] { "-Xmx256m", "-XX:G1HeapRegionSize=64m" }, 32*M, 1);
-  }
 }