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 protected static final boolean BACKGROUND_COMPILATION
39 = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
40
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 }
49
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 }
57
58 protected static String getVMOption(String name, String defaultValue) {
59 String result = getVMOption(name);
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 }
82
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 }
91
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 }
122 }
123 }
124
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 }
138
139 protected abstract void test() throws Exception;
140
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 }
150
151 protected int method() {
152 return 42;
153 }
154 }
|
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 protected static final boolean BACKGROUND_COMPILATION
39 = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
40 protected static final boolean TIERED_COMPILATION
41 = Boolean.valueOf(getVMOption("TieredCompilation", "false"));
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
52 protected static String getVMOption(String name) {
53 String result;
54 HotSpotDiagnosticMXBean diagnostic
55 = ManagementFactoryHelper.getDiagnosticMXBean();
56 result = diagnostic.getVMOption(name).getValue();
57 return result;
58 }
59
60 protected static String getVMOption(String name, String defaultValue) {
61 String result = getVMOption(name);
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);
78 e.printStackTrace();
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) {
86 if (WHITE_BOX.isMethodQueuedForCompilation(METHOD)) {
87 throw new RuntimeException(METHOD + " must not be in queue");
88 }
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 }
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() {
147 return compile(Math.max(COMPILE_THRESHOLD, 150000));
148 }
149
150 protected final int compile(int count) {
151 int result = 0;
152 for (int i = 0; i < count; ++i) {
153 result += method();
154 }
155 System.out.println("method was invoked " + count + " times");
156 return result;
157 }
158
159 protected int method() {
160 return 42;
161 }
162 }
|