8039489: Refactor test framework for dynamic VM options
Reviewed-by: jmasa, ehelin, jwilhelm
Contributed-by: dmitry.fazunenko@oracle.com
--- a/hotspot/test/gc/arguments/TestDynMaxHeapFreeRatio.java Tue May 20 10:04:03 2014 -0700
+++ b/hotspot/test/gc/arguments/TestDynMaxHeapFreeRatio.java Tue May 20 18:25:14 2014 +0400
@@ -21,6 +21,11 @@
* questions.
*/
+import static com.oracle.java.testlibrary.Asserts.assertEQ;
+import static com.oracle.java.testlibrary.Asserts.assertFalse;
+import static com.oracle.java.testlibrary.Asserts.assertTrue;
+import com.oracle.java.testlibrary.DynamicVMOption;
+
/**
* @test TestDynMaxHeapFreeRatio
* @bug 8028391
@@ -33,32 +38,45 @@
* @run main/othervm -XX:MinHeapFreeRatio=51 -XX:MaxHeapFreeRatio=52 TestDynMaxHeapFreeRatio
* @run main/othervm -XX:MinHeapFreeRatio=75 -XX:MaxHeapFreeRatio=100 TestDynMaxHeapFreeRatio
*/
-import com.oracle.java.testlibrary.TestDynamicVMOption;
-import com.oracle.java.testlibrary.DynamicVMOptionChecker;
-
-public class TestDynMaxHeapFreeRatio extends TestDynamicVMOption {
-
- public static final String MinFreeRatioFlagName = "MinHeapFreeRatio";
- public static final String MaxFreeRatioFlagName = "MaxHeapFreeRatio";
-
- public TestDynMaxHeapFreeRatio() {
- super(MaxFreeRatioFlagName);
- }
-
- public void test() {
-
- int minHeapFreeValue = DynamicVMOptionChecker.getIntValue(MinFreeRatioFlagName);
- System.out.println(MinFreeRatioFlagName + " = " + minHeapFreeValue);
-
- testPercentageValues();
-
- checkInvalidValue(Integer.toString(minHeapFreeValue - 1));
- checkValidValue(Integer.toString(minHeapFreeValue));
- checkValidValue("100");
- }
+public class TestDynMaxHeapFreeRatio {
public static void main(String args[]) throws Exception {
- new TestDynMaxHeapFreeRatio().test();
+
+ // low boundary value
+ int minValue = DynamicVMOption.getInt("MinHeapFreeRatio");
+ System.out.println("MinHeapFreeRatio= " + minValue);
+
+ String badValues[] = {
+ null,
+ "",
+ "not a number",
+ "8.5", "-0.01",
+ Integer.toString(Integer.MIN_VALUE),
+ Integer.toString(Integer.MAX_VALUE),
+ Integer.toString(minValue - 1),
+ "-1024", "-1", "101", "1997"
+ };
+
+ String goodValues[] = {
+ Integer.toString(minValue),
+ Integer.toString(minValue + 1),
+ Integer.toString((minValue + 100) / 2),
+ "99", "100"
+ };
+
+ DynamicVMOption option = new DynamicVMOption("MaxHeapFreeRatio");
+
+ assertTrue(option.isWriteable(), "Option " + option.name
+ + " is expected to be writable");
+
+ for (String v : badValues) {
+ assertFalse(option.isValidValue(v),
+ "'" + v + "' is expected to be illegal for flag " + option.name);
+ }
+ for (String v : goodValues) {
+ option.setValue(v);
+ String newValue = option.getValue();
+ assertEQ(v, newValue);
+ }
}
-
}
--- a/hotspot/test/gc/arguments/TestDynMinHeapFreeRatio.java Tue May 20 10:04:03 2014 -0700
+++ b/hotspot/test/gc/arguments/TestDynMinHeapFreeRatio.java Tue May 20 18:25:14 2014 +0400
@@ -33,30 +33,52 @@
* @run main/othervm -XX:MinHeapFreeRatio=51 -XX:MaxHeapFreeRatio=52 TestDynMinHeapFreeRatio
* @run main/othervm -XX:MinHeapFreeRatio=75 -XX:MaxHeapFreeRatio=100 TestDynMinHeapFreeRatio
*/
-import com.oracle.java.testlibrary.TestDynamicVMOption;
-import com.oracle.java.testlibrary.DynamicVMOptionChecker;
-
-public class TestDynMinHeapFreeRatio extends TestDynamicVMOption {
-
- public static final String MinFreeRatioFlagName = "MinHeapFreeRatio";
- public static final String MaxFreeRatioFlagName = "MaxHeapFreeRatio";
+import static com.oracle.java.testlibrary.Asserts.assertEQ;
+import static com.oracle.java.testlibrary.Asserts.assertFalse;
+import static com.oracle.java.testlibrary.Asserts.assertTrue;
+import com.oracle.java.testlibrary.DynamicVMOption;
- public TestDynMinHeapFreeRatio() {
- super(MinFreeRatioFlagName);
- }
-
- public void test() {
- int maxHeapFreeValue = DynamicVMOptionChecker.getIntValue(MaxFreeRatioFlagName);
- System.out.println(MaxFreeRatioFlagName + " = " + maxHeapFreeValue);
-
- testPercentageValues();
-
- checkInvalidValue(Integer.toString(maxHeapFreeValue + 1));
- checkValidValue(Integer.toString(maxHeapFreeValue));
- checkValidValue("0");
- }
+public class TestDynMinHeapFreeRatio {
public static void main(String args[]) throws Exception {
- new TestDynMinHeapFreeRatio().test();
+
+ // high boundary value
+ int maxValue = DynamicVMOption.getInt("MaxHeapFreeRatio");
+ System.out.println("MaxHeapFreeRatio= " + maxValue);
+
+ String badValues[] = {
+ null,
+ "",
+ "not a number",
+ "8.5", "-0.01",
+ Integer.toString(Integer.MIN_VALUE),
+ Integer.toString(Integer.MAX_VALUE),
+ Integer.toString(maxValue + 1),
+ "-1024", "-1", "101", "1997"
+ };
+
+ String goodValues[] = {
+ Integer.toString(maxValue),
+ Integer.toString(maxValue - 1),
+ Integer.toString(maxValue / 2),
+ "0", "1"
+ };
+
+ // option under test
+ DynamicVMOption option = new DynamicVMOption("MinHeapFreeRatio");
+
+ assertTrue(option.isWriteable(), "Option " + option.name
+ + " is expected to be writable");
+
+ for (String v : badValues) {
+ assertFalse(option.isValidValue(v),
+ "'" + v + "' is expected to be illegal for flag " + option.name);
+ }
+
+ for (String v : goodValues) {
+ option.setValue(v);
+ String newValue = option.getValue();
+ assertEQ(v, newValue);
+ }
}
}
--- a/hotspot/test/gc/parallelScavenge/TestDynShrinkHeap.java Tue May 20 10:04:03 2014 -0700
+++ b/hotspot/test/gc/parallelScavenge/TestDynShrinkHeap.java Tue May 20 18:25:14 2014 +0400
@@ -28,8 +28,7 @@
* @library /testlibrary
* @run main/othervm -XX:+UseAdaptiveSizePolicyWithSystemGC -XX:+UseParallelGC -XX:MinHeapFreeRatio=0 -XX:MaxHeapFreeRatio=100 -verbose:gc TestDynShrinkHeap
*/
-
-import com.oracle.java.testlibrary.TestDynamicVMOption;
+import com.oracle.java.testlibrary.DynamicVMOption;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
@@ -44,12 +43,7 @@
private static ArrayList<byte[]> list = new ArrayList<>(0);
private static final int M = 1024 * 1024; // to make heap more manageable by test code
- private final TestDynamicVMOption maxRatioOption;
- private final TestDynamicVMOption minRatioOption;
-
public TestDynShrinkHeap() {
- minRatioOption = new TestDynamicVMOption(MIN_FREE_RATIO_FLAG_NAME);
- maxRatioOption = new TestDynamicVMOption(MAX_FREE_RATIO_FLAG_NAME);
}
private final void test() {
@@ -86,7 +80,8 @@
}
private void free() {
- maxRatioOption.setIntValue(minRatioOption.getIntValue() + 1);
+ int min = DynamicVMOption.getInt(MIN_FREE_RATIO_FLAG_NAME);
+ DynamicVMOption.setInt(MAX_FREE_RATIO_FLAG_NAME, min);
System.gc();
MemoryUsagePrinter.printMemoryUsage("under pressure");
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/testlibrary/com/oracle/java/testlibrary/DynamicVMOption.java Tue May 20 18:25:14 2014 +0400
@@ -0,0 +1,165 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.java.testlibrary;
+
+import com.sun.management.HotSpotDiagnosticMXBean;
+import java.lang.management.ManagementFactory;
+
+/**
+ * A utility class to work with VM options which could be altered during
+ * execution.
+ *
+ * This class is a wrapper around {@code com.sun.management.VMOption}.
+ * It provides more convenient interface to read/write the values.
+ *
+ */
+public class DynamicVMOption {
+
+ private final HotSpotDiagnosticMXBean mxBean;
+
+ /**
+ * VM option name, like "MinHeapFreeRatio".
+ */
+ public final String name;
+
+ /**
+ * Creates an instance of DynamicVMOption.
+ *
+ * @param name the VM option name
+ */
+ public DynamicVMOption(String name) {
+ this.name = name;
+ mxBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
+ }
+
+ /**
+ * Sets a new value for the option.
+ * Trying to set not applicable value will cause IllegalArgumentException.
+ * Behavior with null is undefined, most likely NPE will be thrown.
+ *
+ * @param newValue the value to be set
+ * @see #getValue()
+ * @throws IllegalArgumentException if newValue is not applicable to the option
+ */
+ public final void setValue(String newValue) {
+ mxBean.setVMOption(name, newValue);
+ }
+
+ /**
+ * Returns the value of option.
+ *
+ * @return the current option value
+ * @see #setValue(java.lang.String)
+ */
+ public final String getValue() {
+ return mxBean.getVMOption(name).getValue();
+ }
+
+ /**
+ * Returns true, if option is writable, false otherwise.
+ *
+ * @return true, if option is writable, false otherwise
+ */
+ public final boolean isWriteable() {
+ return mxBean.getVMOption(name).isWriteable();
+ }
+
+ /**
+ * Checks if the given value is applicable for the option.
+ *
+ * This method tries to set the option to the new value. If no exception
+ * has been thrown the value is treated as valid.
+ *
+ * Calling this method will not change the option value. After an attempt
+ * to set a new value, the option will be restored to its previous value.
+ *
+ * @param value the value to verify
+ * @return true if option could be set to the given value
+ */
+ public boolean isValidValue(String value) {
+ boolean isValid = true;
+ String oldValue = getValue();
+ try {
+ setValue(value);
+ } catch (NullPointerException e) {
+ if (value == null) {
+ isValid = false;
+ }
+ } catch (IllegalArgumentException e) {
+ isValid = false;
+ } finally {
+ setValue(oldValue);
+ }
+ return isValid;
+ }
+
+ /**
+ * Returns the value of the given VM option as String.
+ *
+ * This is a simple shortcut for {@code new DynamicVMOption(name).getValue()}
+ *
+ * @param name the name of VM option
+ * @return value as a string
+ * @see #getValue()
+ */
+ public static String getString(String name) {
+ return new DynamicVMOption(name).getValue();
+ }
+
+ /**
+ * Returns the value of the given option as int.
+ *
+ * @param name the name of VM option
+ * @return value parsed as integer
+ * @see #getString(java.lang.String)
+ *
+ */
+ public static int getInt(String name) {
+ return Integer.parseInt(getString(name));
+ }
+
+ /**
+ * Sets the VM option to a new value.
+ *
+ * This is a simple shortcut for {@code new DynamicVMOption(name).setValue(value)}
+ *
+ * @param name the name of VM option
+ * @param value the value to be set
+ * @see #setValue(java.lang.String)
+ */
+ public static void setString(String name, String value) {
+ new DynamicVMOption(name).setValue(value);
+ }
+
+ /**
+ * Sets the VM option value to a new integer value.
+ *
+ * @param name the name of VM option
+ * @param value the integer value to be set
+ * @see #setString(java.lang.String, java.lang.String)
+ */
+ public static void setInt(String name, int value) {
+ new DynamicVMOption(name).setValue(Integer.toString(value));
+ }
+
+}
--- a/hotspot/test/testlibrary/com/oracle/java/testlibrary/DynamicVMOptionChecker.java Tue May 20 10:04:03 2014 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,121 +0,0 @@
-/*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.java.testlibrary;
-
-import com.sun.management.HotSpotDiagnosticMXBean;
-import com.sun.management.VMOption;
-import java.lang.management.ManagementFactory;
-
-/**
- * Simple class to check writeability, invalid and valid values for VMOption
- */
-public class DynamicVMOptionChecker {
-
- /**
- * Reads VM option from PlatformMXBean and parse it to integer value
- *
- * @param name of option
- * @return parsed value
- */
- public static int getIntValue(String name) {
-
- VMOption option = ManagementFactory.
- getPlatformMXBean(HotSpotDiagnosticMXBean.class).
- getVMOption(name);
-
- return Integer.parseInt(option.getValue());
- }
-
- /**
- * Sets VM option value
- *
- * @param name of option
- * @param value to set
- */
- public static void setIntValue(String name, int value) {
- ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class).setVMOption(name, Integer.toString(value));
- }
-
- /**
- * Checks that VM option is dynamically writable
- *
- * @param name
- * @throws RuntimeException if option if not writable
- * @return always true
- */
- public static boolean checkIsWritable(String name) {
- VMOption option = ManagementFactory.
- getPlatformMXBean(HotSpotDiagnosticMXBean.class).
- getVMOption(name);
-
- if (!option.isWriteable()) {
- throw new RuntimeException(name + " is not writable");
- }
-
- return true;
- }
-
- /**
- * Checks that value cannot be set
- *
- * @param name of flag
- * @param value string representation of value to set
- * @throws RuntimeException on error - when expected exception hasn't been thrown
- */
- public static void checkInvalidValue(String name, String value) {
- // should throw
- try {
- ManagementFactory.
- getPlatformMXBean(HotSpotDiagnosticMXBean.class).
- setVMOption(name, value);
-
- } catch (IllegalArgumentException e) {
- return;
- }
-
- throw new RuntimeException("Expected IllegalArgumentException was not thrown, " + name + "= " + value);
- }
-
- /**
- * Checks that value can be set
- *
- * @param name of flag to set
- * @param value string representation of value to set
- * @throws RuntimeException on error - when value in VM is not equal to origin
- */
- public static void checkValidValue(String name, String value) {
- ManagementFactory.
- getPlatformMXBean(HotSpotDiagnosticMXBean.class).
- setVMOption(name, value);
-
- VMOption option = ManagementFactory.
- getPlatformMXBean(HotSpotDiagnosticMXBean.class).
- getVMOption(name);
-
- if (!option.getValue().equals(value)) {
- throw new RuntimeException("Actual value of " + name + " \"" + option.getValue()
- + "\" not equal origin \"" + value + "\"");
- }
- }
-
-}
--- a/hotspot/test/testlibrary/com/oracle/java/testlibrary/TestDynamicVMOption.java Tue May 20 10:04:03 2014 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.java.testlibrary;
-
-/**
- * Simple class to check writeability, invalid and valid values for concrete VMOption
- */
-public class TestDynamicVMOption {
-
- private final String name;
- private final int value;
-
- /**
- * Constructor
- *
- * @param name of VM option to test
- */
- public TestDynamicVMOption(String name) {
- this.name = name;
- this.value = DynamicVMOptionChecker.getIntValue(name);
- System.out.println(this.name + " = " + this.value);
- }
-
- /**
- * Checks that this value can accept valid percentage values and cannot accept invalid percentage values
- *
- * @throws RuntimeException
- */
- public void testPercentageValues() {
- checkInvalidValue(Integer.toString(Integer.MIN_VALUE));
- checkInvalidValue(Integer.toString(Integer.MAX_VALUE));
- checkInvalidValue("-10");
- checkInvalidValue("190");
- }
-
- /**
- * Reads VM option from PlatformMXBean and parse it to integer value
- *
- * @return value
- */
- public int getIntValue() {
- return DynamicVMOptionChecker.getIntValue(this.name);
- }
-
- /**
- * Sets VM option value
- *
- * @param value to set
- */
- public void setIntValue(int value) {
- DynamicVMOptionChecker.setIntValue(this.name, value);
- }
-
- /**
- * Checks that this VM option is dynamically writable
- *
- * @throws RuntimeException if option if not writable
- * @return true
- */
- public boolean checkIsWritable() throws RuntimeException {
- return DynamicVMOptionChecker.checkIsWritable(this.name);
- }
-
- /**
- * Checks that value for this VM option cannot be set
- *
- * @param value to check
- * @throws RuntimeException on error - when expected exception hasn't been thrown
- */
- public void checkInvalidValue(String value) {
- DynamicVMOptionChecker.checkInvalidValue(this.name, value);
- }
-
- /**
- * Checks that value for this VM option can be set
- *
- * @param value to check
- * @throws RuntimeException on error - when value in VM is not equal to origin
- */
- public void checkValidValue(String value) {
- DynamicVMOptionChecker.checkValidValue(this.name, value);
- }
-
-}