comparison test/compiler/whitebox/CompilerWhiteBoxTest.java @ 8051:12e01444ca2d

8006683: Add WhiteBox API to testing of compiler Reviewed-by: kvn, vlivanov
author iignatyev
date Wed, 13 Feb 2013 08:29:04 -0800
parents
children 4efac99a998b
comparison
equal deleted inserted replaced
8050:aaad39923cdb 8051:12e01444ca2d
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 */
23
24 import sun.hotspot.WhiteBox;
25 import sun.management.ManagementFactoryHelper;
26 import com.sun.management.HotSpotDiagnosticMXBean;
27
28 import java.lang.reflect.Method;
29
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
39 protected static Method getMethod(String name) {
40 try {
41 return CompilerWhiteBoxTest.class.getDeclaredMethod(name);
42 } catch (NoSuchMethodException | SecurityException e) {
43 throw new RuntimeException(
44 "exception on getting method " + name, e);
45 }
46 }
47
48 protected static String getVMOption(String name, String defaultValue) {
49 String result;
50 HotSpotDiagnosticMXBean diagnostic
51 = ManagementFactoryHelper.getDiagnosticMXBean();
52 result = diagnostic.getVMOption(name).getValue();
53 return result == null ? defaultValue : result;
54 }
55
56 protected final void runTest() throws RuntimeException {
57 if (ManagementFactoryHelper.getCompilationMXBean() == null) {
58 System.err.println(
59 "Warning: test is not applicable in interpreted mode");
60 return;
61 }
62 System.out.println("at test's start:");
63 printInfo(METHOD);
64 try {
65 test();
66 } catch (Exception e) {
67 System.out.printf("on exception '%s':", e.getMessage());
68 printInfo(METHOD);
69 throw new RuntimeException(e);
70 }
71 System.out.println("at test's end:");
72 printInfo(METHOD);
73 }
74
75 protected static void checkNotCompiled(Method method) {
76 if (WHITE_BOX.isMethodCompiled(method)) {
77 throw new RuntimeException(method + " must be not compiled");
78 }
79 if (WHITE_BOX.getMethodCompilationLevel(method) != 0) {
80 throw new RuntimeException(method + " comp_level must be == 0");
81 }
82 }
83
84 protected static void checkCompiled(Method method)
85 throws InterruptedException {
86 final long start = System.currentTimeMillis();
87 waitBackgroundCompilation(method);
88 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
89 System.err.printf("Warning: %s is still in queue after %dms%n",
90 method, System.currentTimeMillis() - start);
91 return;
92 }
93 if (!WHITE_BOX.isMethodCompiled(method)) {
94 throw new RuntimeException(method + " must be compiled");
95 }
96 if (WHITE_BOX.getMethodCompilationLevel(method) == 0) {
97 throw new RuntimeException(method + " comp_level must be != 0");
98 }
99 }
100
101 protected static void waitBackgroundCompilation(Method method)
102 throws InterruptedException {
103 final Object obj = new Object();
104 synchronized (obj) {
105 for (int i = 0; i < 10; ++i) {
106 if (!WHITE_BOX.isMethodQueuedForCompilation(method)) {
107 break;
108 }
109 obj.wait(1000);
110 }
111 }
112 }
113
114 protected static void printInfo(Method method) {
115 System.out.printf("%n%s:%n", method);
116 System.out.printf("\tcompilable:\t%b%n",
117 WHITE_BOX.isMethodCompilable(method));
118 System.out.printf("\tcompiled:\t%b%n",
119 WHITE_BOX.isMethodCompiled(method));
120 System.out.printf("\tcomp_level:\t%d%n",
121 WHITE_BOX.getMethodCompilationLevel(method));
122 System.out.printf("\tin_queue:\t%b%n",
123 WHITE_BOX.isMethodQueuedForCompilation(method));
124 System.out.printf("compile_queues_size:\t%d%n%n",
125 WHITE_BOX.getCompileQueuesSize());
126 }
127
128 protected abstract void test() throws Exception;
129
130 protected final int compile() {
131 int result = 0;
132 for (int i = 0; i < COMPILE_THRESHOLD; ++i) {
133 result += method();
134 }
135 return result;
136 }
137
138
139 protected int method() {
140 return 42;
141 }
142 }