test/compiler/whitebox/CompilerWhiteBoxTest.java

Thu, 11 Apr 2013 21:45:21 -0700

author
amurillo
date
Thu, 11 Apr 2013 21:45:21 -0700
changeset 4915
5201379fe487
parent 4908
b84fd7d73702
child 4951
4b2eebe03f93
permissions
-rw-r--r--

Added tag hs25-b28 for changeset 6d88a566d369

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

mercurial