diff test/compiler/whitebox/CompilerWhiteBoxTest.java @ 10113:4b2eebe03f93

8011971: WB API doesn't accept j.l.reflect.Constructor Reviewed-by: kvn, vlivanov
author iignatyev
date Tue, 16 Apr 2013 10:04:01 -0700
parents b84fd7d73702
children d1c9384eecb4
line wrap: on
line diff
--- a/test/compiler/whitebox/CompilerWhiteBoxTest.java	Tue Apr 16 10:37:16 2013 -0400
+++ b/test/compiler/whitebox/CompilerWhiteBoxTest.java	Tue Apr 16 10:04:01 2013 -0700
@@ -21,68 +21,132 @@
  * questions.
  */
 
+import com.sun.management.HotSpotDiagnosticMXBean;
+import com.sun.management.VMOption;
 import sun.hotspot.WhiteBox;
 import sun.management.ManagementFactoryHelper;
-import com.sun.management.HotSpotDiagnosticMXBean;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
 import java.lang.reflect.Method;
+import java.util.Objects;
+import java.util.concurrent.Callable;
 
-/*
+/**
+ * Abstract class for WhiteBox testing of JIT.
+ *
  * @author igor.ignatyev@oracle.com
  */
 public abstract class CompilerWhiteBoxTest {
+    /** {@code CompLevel::CompLevel_none} -- Interpreter */
+    protected static int COMP_LEVEL_NONE = 0;
+    /** {@code CompLevel::CompLevel_any}, {@code CompLevel::CompLevel_all} */
+    protected static int COMP_LEVEL_ANY = -1;
+    /** Instance of WhiteBox */
     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
-    protected static final Method METHOD = getMethod("method");
+    /** Value of {@code -XX:CompileThreshold} */
     protected static final int COMPILE_THRESHOLD
             = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
+    /** Value of {@code -XX:BackgroundCompilation} */
     protected static final boolean BACKGROUND_COMPILATION
             = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
+    /** Value of {@code -XX:TieredCompilation} */
     protected static final boolean TIERED_COMPILATION
             = Boolean.valueOf(getVMOption("TieredCompilation", "false"));
+    /** Value of {@code -XX:TieredStopAtLevel} */
+    protected static final int TIERED_STOP_AT_LEVEL
+            = Integer.parseInt(getVMOption("TieredStopAtLevel", "0"));
 
-    protected static Method getMethod(String name) {
+    /**
+     * Returns value of VM option.
+     *
+     * @param name option's name
+     * @return value of option or {@code null}, if option doesn't exist
+     * @throws NullPointerException if name is null
+     */
+    protected static String getVMOption(String name) {
+        Objects.requireNonNull(name);
+        HotSpotDiagnosticMXBean diagnostic
+                = ManagementFactoryHelper.getDiagnosticMXBean();
+        VMOption tmp;
         try {
-            return CompilerWhiteBoxTest.class.getDeclaredMethod(name);
-        } catch (NoSuchMethodException | SecurityException e) {
-            throw new RuntimeException(
-                    "exception on getting method " + name, e);
+            tmp = diagnostic.getVMOption(name);
+        } catch (IllegalArgumentException e) {
+            tmp = null;
         }
+        return (tmp == null ? null : tmp.getValue());
     }
 
-    protected static String getVMOption(String name) {
-        String result;
-        HotSpotDiagnosticMXBean diagnostic
-                = ManagementFactoryHelper.getDiagnosticMXBean();
-        result = diagnostic.getVMOption(name).getValue();
-        return result;
-    }
-
+    /**
+     * Returns value of VM option or default value.
+     *
+     * @param name         option's name
+     * @param defaultValue default value
+     * @return value of option or {@code defaultValue}, if option doesn't exist
+     * @throws NullPointerException if name is null
+     * @see #getVMOption(String)
+     */
     protected static String getVMOption(String name, String defaultValue) {
         String result = getVMOption(name);
         return result == null ? defaultValue : result;
     }
 
-    protected final void runTest() throws RuntimeException {
+    /** tested method */
+    protected final Executable method;
+    private final Callable<Integer> callable;
+
+    /**
+     * Constructor.
+     *
+     * @param testCase object, that contains tested method and way to invoke it.
+     */
+    protected CompilerWhiteBoxTest(TestCase testCase) {
+        Objects.requireNonNull(testCase);
+        System.out.println("TEST CASE:" + testCase.name());
+        method = testCase.executable;
+        callable = testCase.callable;
+    }
+
+    /**
+     * Template method for testing. Prints tested method's info before
+     * {@linkplain #test()} and after {@linkplain #test()} or on thrown
+     * exception.
+     *
+     * @throws RuntimeException if method {@linkplain #test()} throws any
+     *                          exception
+     * @see #test()
+     */
+    protected final void runTest() {
         if (ManagementFactoryHelper.getCompilationMXBean() == null) {
             System.err.println(
                     "Warning: test is not applicable in interpreted mode");
             return;
         }
         System.out.println("at test's start:");
-        printInfo(METHOD);
+        printInfo();
         try {
             test();
         } catch (Exception e) {
             System.out.printf("on exception '%s':", e.getMessage());
-            printInfo(METHOD);
+            printInfo();
             e.printStackTrace();
+            if (e instanceof RuntimeException) {
+                throw (RuntimeException) e;
+            }
             throw new RuntimeException(e);
         }
         System.out.println("at test's end:");
-        printInfo(METHOD);
+        printInfo();
     }
 
-    protected static void checkNotCompiled(Method method) {
+    /**
+     * Checks, that {@linkplain #method} is not compiled.
+     *
+     * @throws RuntimeException if {@linkplain #method} is in compiler queue or
+     *                          is compiled, or if {@linkplain #method} has zero
+     *                          compilation level.
+     */
+    protected final void checkNotCompiled() {
         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
             throw new RuntimeException(method + " must not be in queue");
         }
@@ -94,10 +158,16 @@
         }
     }
 
-    protected static void checkCompiled(Method method)
-            throws InterruptedException {
+    /**
+     * Checks, that {@linkplain #method} is compiled.
+     *
+     * @throws RuntimeException if {@linkplain #method} isn't in compiler queue
+     *                          and isn't compiled, or if {@linkplain #method}
+     *                          has nonzero compilation level
+     */
+    protected final void checkCompiled() {
         final long start = System.currentTimeMillis();
-        waitBackgroundCompilation(method);
+        waitBackgroundCompilation();
         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
             System.err.printf("Warning: %s is still in queue after %dms%n",
                     method, System.currentTimeMillis() - start);
@@ -111,23 +181,30 @@
         }
     }
 
-    protected static void waitBackgroundCompilation(Method method)
-            throws InterruptedException {
+    /**
+     * Waits for completion of background compilation of {@linkplain #method}.
+     */
+    protected final void waitBackgroundCompilation() {
         if (!BACKGROUND_COMPILATION) {
             return;
         }
         final Object obj = new Object();
-        synchronized (obj) {
-            for (int i = 0; i < 10; ++i) {
-                if (!WHITE_BOX.isMethodQueuedForCompilation(method)) {
-                    break;
+        for (int i = 0; i < 10
+                && WHITE_BOX.isMethodQueuedForCompilation(method); ++i) {
+            synchronized (obj) {
+                try {
+                    obj.wait(1000);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
                 }
-                obj.wait(1000);
             }
         }
     }
 
-    protected static void printInfo(Method method) {
+    /**
+     * Prints information about {@linkplain #method}.
+     */
+    protected final void printInfo() {
         System.out.printf("%n%s:%n", method);
         System.out.printf("\tcompilable:\t%b%n",
                 WHITE_BOX.isMethodCompilable(method));
@@ -141,22 +218,139 @@
                 WHITE_BOX.getCompileQueuesSize());
     }
 
+    /**
+     * Executes testing.
+     */
     protected abstract void test() throws Exception;
 
+    /**
+     * Tries to trigger compilation of {@linkplain #method} by call
+     * {@linkplain #callable} enough times.
+     *
+     * @return accumulated result
+     * @see #compile(int)
+     */
     protected final int compile() {
         return compile(Math.max(COMPILE_THRESHOLD, 150000));
     }
 
+    /**
+     * Tries to trigger compilation of {@linkplain #method} by call
+     * {@linkplain #callable} specified times.
+     *
+     * @param count invocation count
+     * @return accumulated result
+     */
     protected final int compile(int count) {
         int result = 0;
+        Integer tmp;
         for (int i = 0; i < count; ++i) {
-            result += method();
+            try {
+                tmp = callable.call();
+            } catch (Exception e) {
+                tmp = null;
+            }
+            result += tmp == null ? 0 : tmp;
         }
         System.out.println("method was invoked " + count + " times");
         return result;
     }
+}
 
-    protected int method() {
-        return 42;
+/**
+ * Utility structure containing tested method and object to invoke it.
+ */
+enum TestCase {
+    /** constructor test case */
+    CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE),
+    /** method test case */
+    METOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE),
+    /** static method test case */
+    STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE);
+
+    /** tested method */
+    final Executable executable;
+    /** object to invoke {@linkplain #executable} */
+    final Callable<Integer> callable;
+
+    private TestCase(Executable executable, Callable<Integer> callable) {
+        this.executable = executable;
+        this.callable = callable;
+    }
+
+    private static class Helper {
+        private static final Callable<Integer> CONSTRUCTOR_CALLABLE
+                = new Callable<Integer>() {
+            @Override
+            public Integer call() throws Exception {
+                return new Helper(1337).hashCode();
+            }
+        };
+
+        private static final Callable<Integer> METHOD_CALLABLE
+                = new Callable<Integer>() {
+            private final Helper helper = new Helper();
+
+            @Override
+            public Integer call() throws Exception {
+                return helper.method();
+            }
+        };
+
+        private static final Callable<Integer> STATIC_CALLABLE
+                = new Callable<Integer>() {
+            @Override
+            public Integer call() throws Exception {
+                return staticMethod();
+            }
+        };
+
+        private static final Constructor CONSTRUCTOR;
+        private static final Method METHOD;
+        private static final Method STATIC;
+
+        static {
+            try {
+                CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
+            } catch (NoSuchMethodException | SecurityException e) {
+                throw new RuntimeException(
+                        "exception on getting method Helper.<init>(int)", e);
+            }
+            try {
+                METHOD = Helper.class.getDeclaredMethod("method");
+            } catch (NoSuchMethodException | SecurityException e) {
+                throw new RuntimeException(
+                        "exception on getting method Helper.method()", e);
+            }
+            try {
+                STATIC = Helper.class.getDeclaredMethod("staticMethod");
+            } catch (NoSuchMethodException | SecurityException e) {
+                throw new RuntimeException(
+                        "exception on getting method Helper.staticMethod()", e);
+            }
+        }
+
+        private static int staticMethod() {
+            return 1138;
+        }
+
+        private int method() {
+            return 42;
+        }
+
+        private final int x;
+
+        public Helper() {
+            x = 0;
+        }
+
+        private Helper(int x) {
+            this.x = x;
+        }
+
+        @Override
+        public int hashCode() {
+            return x;
+        }
     }
 }