test/compiler/whitebox/CompilerWhiteBoxTest.java

Thu, 11 Apr 2013 09:39:57 -0700

author
katleman
date
Thu, 11 Apr 2013 09:39:57 -0700
changeset 4883
5dcfeb396fed
parent 4766
4efac99a998b
child 4908
b84fd7d73702
permissions
-rw-r--r--

Added tag jdk8-b85 for changeset 42fe530cd478

     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 import sun.hotspot.WhiteBox;
    25 import sun.management.ManagementFactoryHelper;
    26 import com.sun.management.HotSpotDiagnosticMXBean;
    28 import java.lang.reflect.Method;
    30 /*
    31  * @author igor.ignatyev@oracle.com
    32  */
    33 public abstract class CompilerWhiteBoxTest {
    34     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
    35     protected static final Method METHOD = getMethod("method");
    36     protected static final int COMPILE_THRESHOLD
    37             = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
    38     protected static final boolean BACKGROUND_COMPILATION
    39             = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
    41     protected static Method getMethod(String name) {
    42         try {
    43             return CompilerWhiteBoxTest.class.getDeclaredMethod(name);
    44         } catch (NoSuchMethodException | SecurityException e) {
    45             throw new RuntimeException(
    46                     "exception on getting method " + name, e);
    47         }
    48     }
    50     protected static String getVMOption(String name) {
    51         String result;
    52         HotSpotDiagnosticMXBean diagnostic
    53                 = ManagementFactoryHelper.getDiagnosticMXBean();
    54         result = diagnostic.getVMOption(name).getValue();
    55         return result;
    56     }
    58     protected static String getVMOption(String name, String defaultValue) {
    59         String result = getVMOption(name);
    60         return result == null ? defaultValue : result;
    61     }
    63     protected final void runTest() throws RuntimeException {
    64         if (ManagementFactoryHelper.getCompilationMXBean() == null) {
    65             System.err.println(
    66                     "Warning: test is not applicable in interpreted mode");
    67             return;
    68         }
    69         System.out.println("at test's start:");
    70         printInfo(METHOD);
    71         try {
    72             test();
    73         } catch (Exception e) {
    74             System.out.printf("on exception '%s':", e.getMessage());
    75             printInfo(METHOD);
    76             e.printStackTrace();
    77             throw new RuntimeException(e);
    78         }
    79         System.out.println("at test's end:");
    80         printInfo(METHOD);
    81     }
    83     protected static void checkNotCompiled(Method method) {
    84         if (WHITE_BOX.isMethodCompiled(method)) {
    85             throw new RuntimeException(method + " must be not compiled");
    86         }
    87         if (WHITE_BOX.getMethodCompilationLevel(method) != 0) {
    88             throw new RuntimeException(method + " comp_level must be == 0");
    89         }
    90     }
    92     protected static void checkCompiled(Method method)
    93             throws InterruptedException {
    94         final long start = System.currentTimeMillis();
    95         waitBackgroundCompilation(method);
    96         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
    97             System.err.printf("Warning: %s is still in queue after %dms%n",
    98                     method, System.currentTimeMillis() - start);
    99             return;
   100         }
   101         if (!WHITE_BOX.isMethodCompiled(method)) {
   102             throw new RuntimeException(method + " must be compiled");
   103         }
   104         if (WHITE_BOX.getMethodCompilationLevel(method) == 0) {
   105             throw new RuntimeException(method + " comp_level must be != 0");
   106         }
   107     }
   109     protected static void waitBackgroundCompilation(Method method)
   110             throws InterruptedException {
   111         if (!BACKGROUND_COMPILATION) {
   112             return;
   113         }
   114         final Object obj = new Object();
   115         synchronized (obj) {
   116             for (int i = 0; i < 10; ++i) {
   117                 if (!WHITE_BOX.isMethodQueuedForCompilation(method)) {
   118                     break;
   119                 }
   120                 obj.wait(1000);
   121             }
   122         }
   123     }
   125     protected static void printInfo(Method method) {
   126         System.out.printf("%n%s:%n", method);
   127         System.out.printf("\tcompilable:\t%b%n",
   128                 WHITE_BOX.isMethodCompilable(method));
   129         System.out.printf("\tcompiled:\t%b%n",
   130                 WHITE_BOX.isMethodCompiled(method));
   131         System.out.printf("\tcomp_level:\t%d%n",
   132                 WHITE_BOX.getMethodCompilationLevel(method));
   133         System.out.printf("\tin_queue:\t%b%n",
   134                 WHITE_BOX.isMethodQueuedForCompilation(method));
   135         System.out.printf("compile_queues_size:\t%d%n%n",
   136                 WHITE_BOX.getCompileQueuesSize());
   137     }
   139     protected abstract void test() throws Exception;
   141     protected final int compile() {
   142         int result = 0;
   143         int count = Math.max(COMPILE_THRESHOLD, 150000);
   144         for (int i = 0; i < count; ++i) {
   145             result += method();
   146         }
   147         System.out.println("method was invoked " + count + " times");
   148         return result;
   149     }
   151     protected int method() {
   152         return 42;
   153     }
   154 }

mercurial