author | iignatyev |
Tue, 09 Apr 2013 09:54:17 -0700 | |
changeset 16689 | efce070b8d42 |
parent 16367 | 9cd60b8380a1 |
child 17015 | 92390f57e8b1 |
permissions | -rw-r--r-- |
15621 | 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")); |
|
16367 | 38 |
protected static final boolean BACKGROUND_COMPILATION |
39 |
= Boolean.valueOf(getVMOption("BackgroundCompilation", "true")); |
|
16689
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
40 |
protected static final boolean TIERED_COMPILATION |
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
41 |
= Boolean.valueOf(getVMOption("TieredCompilation", "false")); |
15621 | 42 |
|
43 |
protected static Method getMethod(String name) { |
|
44 |
try { |
|
45 |
return CompilerWhiteBoxTest.class.getDeclaredMethod(name); |
|
46 |
} catch (NoSuchMethodException | SecurityException e) { |
|
47 |
throw new RuntimeException( |
|
48 |
"exception on getting method " + name, e); |
|
49 |
} |
|
50 |
} |
|
51 |
||
16367 | 52 |
protected static String getVMOption(String name) { |
15621 | 53 |
String result; |
54 |
HotSpotDiagnosticMXBean diagnostic |
|
55 |
= ManagementFactoryHelper.getDiagnosticMXBean(); |
|
56 |
result = diagnostic.getVMOption(name).getValue(); |
|
16367 | 57 |
return result; |
58 |
} |
|
59 |
||
60 |
protected static String getVMOption(String name, String defaultValue) { |
|
61 |
String result = getVMOption(name); |
|
15621 | 62 |
return result == null ? defaultValue : result; |
63 |
} |
|
64 |
||
65 |
protected final void runTest() throws RuntimeException { |
|
66 |
if (ManagementFactoryHelper.getCompilationMXBean() == null) { |
|
67 |
System.err.println( |
|
68 |
"Warning: test is not applicable in interpreted mode"); |
|
69 |
return; |
|
70 |
} |
|
71 |
System.out.println("at test's start:"); |
|
72 |
printInfo(METHOD); |
|
73 |
try { |
|
74 |
test(); |
|
75 |
} catch (Exception e) { |
|
76 |
System.out.printf("on exception '%s':", e.getMessage()); |
|
77 |
printInfo(METHOD); |
|
16367 | 78 |
e.printStackTrace(); |
15621 | 79 |
throw new RuntimeException(e); |
80 |
} |
|
81 |
System.out.println("at test's end:"); |
|
82 |
printInfo(METHOD); |
|
83 |
} |
|
84 |
||
85 |
protected static void checkNotCompiled(Method method) { |
|
16689
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
86 |
if (WHITE_BOX.isMethodQueuedForCompilation(method)) { |
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
87 |
throw new RuntimeException(method + " must not be in queue"); |
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
88 |
} |
15621 | 89 |
if (WHITE_BOX.isMethodCompiled(method)) { |
90 |
throw new RuntimeException(method + " must be not compiled"); |
|
91 |
} |
|
92 |
if (WHITE_BOX.getMethodCompilationLevel(method) != 0) { |
|
93 |
throw new RuntimeException(method + " comp_level must be == 0"); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
protected static void checkCompiled(Method method) |
|
98 |
throws InterruptedException { |
|
99 |
final long start = System.currentTimeMillis(); |
|
100 |
waitBackgroundCompilation(method); |
|
101 |
if (WHITE_BOX.isMethodQueuedForCompilation(method)) { |
|
102 |
System.err.printf("Warning: %s is still in queue after %dms%n", |
|
103 |
method, System.currentTimeMillis() - start); |
|
104 |
return; |
|
105 |
} |
|
106 |
if (!WHITE_BOX.isMethodCompiled(method)) { |
|
107 |
throw new RuntimeException(method + " must be compiled"); |
|
108 |
} |
|
109 |
if (WHITE_BOX.getMethodCompilationLevel(method) == 0) { |
|
110 |
throw new RuntimeException(method + " comp_level must be != 0"); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
protected static void waitBackgroundCompilation(Method method) |
|
115 |
throws InterruptedException { |
|
16367 | 116 |
if (!BACKGROUND_COMPILATION) { |
117 |
return; |
|
118 |
} |
|
15621 | 119 |
final Object obj = new Object(); |
120 |
synchronized (obj) { |
|
121 |
for (int i = 0; i < 10; ++i) { |
|
122 |
if (!WHITE_BOX.isMethodQueuedForCompilation(method)) { |
|
123 |
break; |
|
124 |
} |
|
125 |
obj.wait(1000); |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
protected static void printInfo(Method method) { |
|
131 |
System.out.printf("%n%s:%n", method); |
|
132 |
System.out.printf("\tcompilable:\t%b%n", |
|
133 |
WHITE_BOX.isMethodCompilable(method)); |
|
134 |
System.out.printf("\tcompiled:\t%b%n", |
|
135 |
WHITE_BOX.isMethodCompiled(method)); |
|
136 |
System.out.printf("\tcomp_level:\t%d%n", |
|
137 |
WHITE_BOX.getMethodCompilationLevel(method)); |
|
138 |
System.out.printf("\tin_queue:\t%b%n", |
|
139 |
WHITE_BOX.isMethodQueuedForCompilation(method)); |
|
140 |
System.out.printf("compile_queues_size:\t%d%n%n", |
|
141 |
WHITE_BOX.getCompileQueuesSize()); |
|
142 |
} |
|
143 |
||
144 |
protected abstract void test() throws Exception; |
|
145 |
||
146 |
protected final int compile() { |
|
16689
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
147 |
return compile(Math.max(COMPILE_THRESHOLD, 150000)); |
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
148 |
} |
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
149 |
|
efce070b8d42
8007288: Additional WB API for compiler's testing
iignatyev
parents:
16367
diff
changeset
|
150 |
protected final int compile(int count) { |
15621 | 151 |
int result = 0; |
16367 | 152 |
for (int i = 0; i < count; ++i) { |
15621 | 153 |
result += method(); |
154 |
} |
|
16367 | 155 |
System.out.println("method was invoked " + count + " times"); |
15621 | 156 |
return result; |
157 |
} |
|
158 |
||
159 |
protected int method() { |
|
160 |
return 42; |
|
161 |
} |
|
162 |
} |