Print this page
rev 6860 : 8046268: compiler/whitebox/ tests fail : must be osr_compiled
Summary: Added code to 'warm up' the methods before triggering OSR compilation by executing them a limited number of times. Like this, the profile information marks the loop exit as taken and we don't add an uncommon trap.
Reviewed-by: kvn, dlong, iignatyev
rev 6861 : 8060454: [TESTBUG] Whitebox tests fail with -XX:CompileThreshold=100
Summary: Move the call to 'waitAndDeoptimize' from the warmup methods to the osr triggering methods to make sure that no non-osr compilation is in the queue after warmup.
Reviewed-by: kvn
rev 6862 : 8061486: [TESTBUG] compiler/whitebox/ tests fail : must be osr_compiled (reappeared in nightlies)
Summary: Call warmup code from OSR triggering method to make sure no non-OSR compilation is triggered in the loop.
Reviewed-by: kvn
rev 6863 : 8061983: [TESTBUG] compiler/whitebox/MakeMethodNotCompilableTest.java fails with "must not be in queue"
Summary: Added a method checkNotCompiled(boolean isOsr) to either check if the method is OSR compiled or to check if it is non-OSR compiled.
Reviewed-by: kvn
Split |
Split |
Close |
Expand all |
Collapse all |
--- old/test/compiler/whitebox/CompilerWhiteBoxTest.java
+++ new/test/compiler/whitebox/CompilerWhiteBoxTest.java
1 1 /*
2 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 import com.sun.management.HotSpotDiagnosticMXBean;
25 25 import com.sun.management.VMOption;
26 26 import sun.hotspot.WhiteBox;
27 27 import sun.hotspot.code.NMethod;
28 28 import sun.management.ManagementFactoryHelper;
29 29
30 30 import java.lang.reflect.Constructor;
31 31 import java.lang.reflect.Executable;
32 32 import java.lang.reflect.Method;
33 33 import java.util.Objects;
34 34 import java.util.concurrent.Callable;
35 35 import java.util.function.Function;
36 36
37 37 /**
38 38 * Abstract class for WhiteBox testing of JIT.
39 39 *
40 40 * @author igor.ignatyev@oracle.com
41 41 */
42 42 public abstract class CompilerWhiteBoxTest {
43 43 /** {@code CompLevel::CompLevel_none} -- Interpreter */
44 44 protected static int COMP_LEVEL_NONE = 0;
45 45 /** {@code CompLevel::CompLevel_any}, {@code CompLevel::CompLevel_all} */
46 46 protected static int COMP_LEVEL_ANY = -1;
47 47 /** {@code CompLevel::CompLevel_simple} -- C1 */
48 48 protected static int COMP_LEVEL_SIMPLE = 1;
49 49 /** {@code CompLevel::CompLevel_limited_profile} -- C1, invocation & backedge counters */
50 50 protected static int COMP_LEVEL_LIMITED_PROFILE = 2;
51 51 /** {@code CompLevel::CompLevel_full_profile} -- C1, invocation & backedge counters + mdo */
52 52 protected static int COMP_LEVEL_FULL_PROFILE = 3;
53 53 /** {@code CompLevel::CompLevel_full_optimization} -- C2 or Shark */
54 54 protected static int COMP_LEVEL_FULL_OPTIMIZATION = 4;
55 55 /** Maximal value for CompLevel */
56 56 protected static int COMP_LEVEL_MAX = COMP_LEVEL_FULL_OPTIMIZATION;
57 57
58 58 /** Instance of WhiteBox */
59 59 protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
60 60 /** Value of {@code -XX:CompileThreshold} */
61 61 protected static final int COMPILE_THRESHOLD
62 62 = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
63 63 /** Value of {@code -XX:BackgroundCompilation} */
64 64 protected static final boolean BACKGROUND_COMPILATION
↓ open down ↓ |
64 lines elided |
↑ open up ↑ |
65 65 = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
66 66 /** Value of {@code -XX:TieredCompilation} */
67 67 protected static final boolean TIERED_COMPILATION
68 68 = Boolean.valueOf(getVMOption("TieredCompilation", "false"));
69 69 /** Value of {@code -XX:TieredStopAtLevel} */
70 70 protected static final int TIERED_STOP_AT_LEVEL
71 71 = Integer.parseInt(getVMOption("TieredStopAtLevel", "0"));
72 72 /** Flag for verbose output, true if {@code -Dverbose} specified */
73 73 protected static final boolean IS_VERBOSE
74 74 = System.getProperty("verbose") != null;
75 - /** count of invocation to triger compilation */
75 + /** invocation count to trigger compilation */
76 76 protected static final int THRESHOLD;
77 - /** count of invocation to triger OSR compilation */
77 + /** invocation count to trigger OSR compilation */
78 78 protected static final long BACKEDGE_THRESHOLD;
79 79 /** Value of {@code java.vm.info} (interpreted|mixed|comp mode) */
80 80 protected static final String MODE = System.getProperty("java.vm.info");
81 81
82 82 static {
83 83 if (TIERED_COMPILATION) {
84 84 BACKEDGE_THRESHOLD = THRESHOLD = 150000;
85 85 } else {
86 86 THRESHOLD = COMPILE_THRESHOLD;
87 87 BACKEDGE_THRESHOLD = COMPILE_THRESHOLD * Long.parseLong(getVMOption(
88 88 "OnStackReplacePercentage"));
89 89 }
90 90 }
91 91
92 92 /**
93 93 * Returns value of VM option.
94 94 *
95 95 * @param name option's name
96 96 * @return value of option or {@code null}, if option doesn't exist
97 97 * @throws NullPointerException if name is null
98 98 */
99 99 protected static String getVMOption(String name) {
100 100 Objects.requireNonNull(name);
101 101 HotSpotDiagnosticMXBean diagnostic
102 102 = ManagementFactoryHelper.getDiagnosticMXBean();
103 103 VMOption tmp;
104 104 try {
105 105 tmp = diagnostic.getVMOption(name);
106 106 } catch (IllegalArgumentException e) {
107 107 tmp = null;
108 108 }
109 109 return (tmp == null ? null : tmp.getValue());
110 110 }
111 111
112 112 /**
113 113 * Returns value of VM option or default value.
114 114 *
115 115 * @param name option's name
116 116 * @param defaultValue default value
117 117 * @return value of option or {@code defaultValue}, if option doesn't exist
118 118 * @throws NullPointerException if name is null
119 119 * @see #getVMOption(String)
120 120 */
121 121 protected static String getVMOption(String name, String defaultValue) {
122 122 String result = getVMOption(name);
123 123 return result == null ? defaultValue : result;
124 124 }
125 125
126 126 /** copy of is_c1_compile(int) from utilities/globalDefinitions.hpp */
127 127 protected static boolean isC1Compile(int compLevel) {
128 128 return (compLevel > COMP_LEVEL_NONE)
129 129 && (compLevel < COMP_LEVEL_FULL_OPTIMIZATION);
130 130 }
131 131
132 132 /** copy of is_c2_compile(int) from utilities/globalDefinitions.hpp */
133 133 protected static boolean isC2Compile(int compLevel) {
134 134 return compLevel == COMP_LEVEL_FULL_OPTIMIZATION;
135 135 }
136 136
137 137 protected static void main(
138 138 Function<TestCase, CompilerWhiteBoxTest> constructor,
139 139 String[] args) {
140 140 if (args.length == 0) {
141 141 for (TestCase test : SimpleTestCase.values()) {
142 142 constructor.apply(test).runTest();
143 143 }
144 144 } else {
145 145 for (String name : args) {
146 146 constructor.apply(SimpleTestCase.valueOf(name)).runTest();
147 147 }
148 148 }
149 149 }
150 150
151 151 /** tested method */
152 152 protected final Executable method;
153 153 protected final TestCase testCase;
154 154
155 155 /**
156 156 * Constructor.
157 157 *
158 158 * @param testCase object, that contains tested method and way to invoke it.
159 159 */
160 160 protected CompilerWhiteBoxTest(TestCase testCase) {
161 161 Objects.requireNonNull(testCase);
162 162 System.out.println("TEST CASE:" + testCase.name());
163 163 method = testCase.getExecutable();
164 164 this.testCase = testCase;
165 165 }
166 166
167 167 /**
168 168 * Template method for testing. Prints tested method's info before
169 169 * {@linkplain #test()} and after {@linkplain #test()} or on thrown
170 170 * exception.
171 171 *
172 172 * @throws RuntimeException if method {@linkplain #test()} throws any
173 173 * exception
174 174 * @see #test()
175 175 */
176 176 protected final void runTest() {
177 177 if (ManagementFactoryHelper.getCompilationMXBean() == null) {
178 178 System.err.println(
179 179 "Warning: test is not applicable in interpreted mode");
180 180 return;
181 181 }
182 182 System.out.println("at test's start:");
183 183 printInfo();
184 184 try {
185 185 test();
186 186 } catch (Exception e) {
187 187 System.out.printf("on exception '%s':", e.getMessage());
188 188 printInfo();
189 189 e.printStackTrace();
190 190 if (e instanceof RuntimeException) {
191 191 throw (RuntimeException) e;
192 192 }
193 193 throw new RuntimeException(e);
194 194 }
195 195 System.out.println("at test's end:");
196 196 printInfo();
197 197 }
198 198
↓ open down ↓ |
111 lines elided |
↑ open up ↑ |
199 199 /**
200 200 * Checks, that {@linkplain #method} is not compiled at the given compilation
201 201 * level or above.
202 202 *
203 203 * @param compLevel
204 204 *
205 205 * @throws RuntimeException if {@linkplain #method} is in compiler queue or
206 206 * is compiled, or if {@linkplain #method} has zero
207 207 * compilation level.
208 208 */
209 -
210 209 protected final void checkNotCompiled(int compLevel) {
211 210 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
212 211 throw new RuntimeException(method + " must not be in queue");
213 212 }
214 213 if (WHITE_BOX.getMethodCompilationLevel(method, false) >= compLevel) {
215 214 throw new RuntimeException(method + " comp_level must be >= maxCompLevel");
216 215 }
217 216 if (WHITE_BOX.getMethodCompilationLevel(method, true) >= compLevel) {
218 217 throw new RuntimeException(method + " osr_comp_level must be >= maxCompLevel");
219 218 }
220 219 }
221 220
222 221 /**
223 222 * Checks, that {@linkplain #method} is not compiled.
224 223 *
225 224 * @throws RuntimeException if {@linkplain #method} is in compiler queue or
226 225 * is compiled, or if {@linkplain #method} has zero
227 226 * compilation level.
228 227 */
229 228 protected final void checkNotCompiled() {
229 + checkNotCompiled(true);
230 + checkNotCompiled(false);
231 + }
232 +
233 + /**
234 + * Checks, that {@linkplain #method} is not (OSR-)compiled.
235 + *
236 + * @param isOsr Check for OSR compilation if true
237 + * @throws RuntimeException if {@linkplain #method} is in compiler queue or
238 + * is compiled, or if {@linkplain #method} has zero
239 + * compilation level.
240 + */
241 + protected final void checkNotCompiled(boolean isOsr) {
242 + waitBackgroundCompilation();
230 243 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
231 244 throw new RuntimeException(method + " must not be in queue");
232 245 }
233 - if (WHITE_BOX.isMethodCompiled(method, false)) {
234 - throw new RuntimeException(method + " must be not compiled");
235 - }
236 - if (WHITE_BOX.getMethodCompilationLevel(method, false) != 0) {
237 - throw new RuntimeException(method + " comp_level must be == 0");
238 - }
239 - if (WHITE_BOX.isMethodCompiled(method, true)) {
240 - throw new RuntimeException(method + " must be not osr_compiled");
241 - }
242 - if (WHITE_BOX.getMethodCompilationLevel(method, true) != 0) {
243 - throw new RuntimeException(method + " osr_comp_level must be == 0");
246 + if (WHITE_BOX.isMethodCompiled(method, isOsr)) {
247 + throw new RuntimeException(method + " must not be " +
248 + (isOsr ? "osr_" : "") + "compiled");
249 + }
250 + if (WHITE_BOX.getMethodCompilationLevel(method, isOsr) != 0) {
251 + throw new RuntimeException(method + (isOsr ? " osr_" : " ") +
252 + "comp_level must be == 0");
244 253 }
245 254 }
246 255
247 256 /**
248 257 * Checks, that {@linkplain #method} is compiled.
249 258 *
250 259 * @throws RuntimeException if {@linkplain #method} isn't in compiler queue
251 260 * and isn't compiled, or if {@linkplain #method}
252 261 * has nonzero compilation level
253 262 */
254 263 protected final void checkCompiled() {
255 264 final long start = System.currentTimeMillis();
256 265 waitBackgroundCompilation();
257 266 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
258 267 System.err.printf("Warning: %s is still in queue after %dms%n",
259 268 method, System.currentTimeMillis() - start);
260 269 return;
261 270 }
262 271 if (!WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) {
263 272 throw new RuntimeException(method + " must be "
264 273 + (testCase.isOsr() ? "osr_" : "") + "compiled");
265 274 }
266 275 if (WHITE_BOX.getMethodCompilationLevel(method, testCase.isOsr())
267 276 == 0) {
268 277 throw new RuntimeException(method
269 278 + (testCase.isOsr() ? " osr_" : " ")
270 279 + "comp_level must be != 0");
271 280 }
272 281 }
273 282
274 283 protected final void deoptimize() {
275 284 WHITE_BOX.deoptimizeMethod(method, testCase.isOsr());
276 285 if (testCase.isOsr()) {
277 286 WHITE_BOX.deoptimizeMethod(method, false);
278 287 }
279 288 }
280 289
281 290 protected final int getCompLevel() {
282 291 NMethod nm = NMethod.get(method, testCase.isOsr());
283 292 return nm == null ? COMP_LEVEL_NONE : nm.comp_level;
284 293 }
285 294
286 295 protected final boolean isCompilable() {
287 296 return WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY,
288 297 testCase.isOsr());
289 298 }
290 299
291 300 protected final boolean isCompilable(int compLevel) {
292 301 return WHITE_BOX
293 302 .isMethodCompilable(method, compLevel, testCase.isOsr());
294 303 }
295 304
296 305 protected final void makeNotCompilable() {
297 306 WHITE_BOX.makeMethodNotCompilable(method, COMP_LEVEL_ANY,
298 307 testCase.isOsr());
↓ open down ↓ |
45 lines elided |
↑ open up ↑ |
299 308 }
300 309
301 310 protected final void makeNotCompilable(int compLevel) {
302 311 WHITE_BOX.makeMethodNotCompilable(method, compLevel, testCase.isOsr());
303 312 }
304 313
305 314 /**
306 315 * Waits for completion of background compilation of {@linkplain #method}.
307 316 */
308 317 protected final void waitBackgroundCompilation() {
318 + waitBackgroundCompilation(method);
319 + }
320 +
321 + /**
322 + * Waits for completion of background compilation of the given executable.
323 + *
324 + * @param executable Executable
325 + */
326 + protected static final void waitBackgroundCompilation(Executable executable) {
309 327 if (!BACKGROUND_COMPILATION) {
310 328 return;
311 329 }
312 330 final Object obj = new Object();
313 331 for (int i = 0; i < 10
314 - && WHITE_BOX.isMethodQueuedForCompilation(method); ++i) {
332 + && WHITE_BOX.isMethodQueuedForCompilation(executable); ++i) {
315 333 synchronized (obj) {
316 334 try {
317 335 obj.wait(1000);
318 336 } catch (InterruptedException e) {
319 337 Thread.currentThread().interrupt();
320 338 }
321 339 }
322 340 }
323 341 }
324 342
325 343 /**
326 344 * Prints information about {@linkplain #method}.
327 345 */
328 346 protected final void printInfo() {
329 347 System.out.printf("%n%s:%n", method);
330 348 System.out.printf("\tcompilable:\t%b%n",
331 349 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, false));
332 350 System.out.printf("\tcompiled:\t%b%n",
333 351 WHITE_BOX.isMethodCompiled(method, false));
334 352 System.out.printf("\tcomp_level:\t%d%n",
335 353 WHITE_BOX.getMethodCompilationLevel(method, false));
336 354 System.out.printf("\tosr_compilable:\t%b%n",
337 355 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, true));
338 356 System.out.printf("\tosr_compiled:\t%b%n",
339 357 WHITE_BOX.isMethodCompiled(method, true));
340 358 System.out.printf("\tosr_comp_level:\t%d%n",
341 359 WHITE_BOX.getMethodCompilationLevel(method, true));
342 360 System.out.printf("\tin_queue:\t%b%n",
343 361 WHITE_BOX.isMethodQueuedForCompilation(method));
344 362 System.out.printf("compile_queues_size:\t%d%n%n",
345 363 WHITE_BOX.getCompileQueuesSize());
346 364 }
347 365
348 366 /**
349 367 * Executes testing.
350 368 */
351 369 protected abstract void test() throws Exception;
352 370
353 371 /**
354 372 * Tries to trigger compilation of {@linkplain #method} by call
355 373 * {@linkplain TestCase#getCallable()} enough times.
356 374 *
357 375 * @return accumulated result
358 376 * @see #compile(int)
359 377 */
360 378 protected final int compile() {
361 379 if (testCase.isOsr()) {
362 380 return compile(1);
363 381 } else {
364 382 return compile(THRESHOLD);
365 383 }
366 384 }
367 385
368 386 /**
369 387 * Tries to trigger compilation of {@linkplain #method} by call
370 388 * {@linkplain TestCase#getCallable()} specified times.
371 389 *
372 390 * @param count invocation count
373 391 * @return accumulated result
374 392 */
375 393 protected final int compile(int count) {
376 394 int result = 0;
377 395 Integer tmp;
378 396 for (int i = 0; i < count; ++i) {
379 397 try {
380 398 tmp = testCase.getCallable().call();
381 399 } catch (Exception e) {
382 400 tmp = null;
383 401 }
384 402 result += tmp == null ? 0 : tmp;
385 403 }
386 404 if (IS_VERBOSE) {
387 405 System.out.println("method was invoked " + count + " times");
388 406 }
389 407 return result;
390 408 }
391 409
392 410 /**
393 411 * Utility interface provides tested method and object to invoke it.
394 412 */
395 413 public interface TestCase {
396 414 /** the name of test case */
397 415 String name();
398 416
399 417 /** tested method */
400 418 Executable getExecutable();
401 419
402 420 /** object to invoke {@linkplain #getExecutable()} */
403 421 Callable<Integer> getCallable();
404 422
405 423 /** flag for OSR test case */
406 424 boolean isOsr();
407 425 }
408 426
409 427 /**
410 428 * @return {@code true} if the current test case is OSR and the mode is
411 429 * Xcomp, otherwise {@code false}
412 430 */
413 431 protected boolean skipXcompOSR() {
414 432 boolean result = testCase.isOsr()
415 433 && CompilerWhiteBoxTest.MODE.startsWith("compiled ");
416 434 if (result && IS_VERBOSE) {
417 435 System.err.printf("Warning: %s is not applicable in %s%n",
↓ open down ↓ |
93 lines elided |
↑ open up ↑ |
418 436 testCase.name(), CompilerWhiteBoxTest.MODE);
419 437 }
420 438 return result;
421 439 }
422 440 }
423 441
424 442 enum SimpleTestCase implements CompilerWhiteBoxTest.TestCase {
425 443 /** constructor test case */
426 444 CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE, false),
427 445 /** method test case */
428 - METOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE, false),
446 + METHOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE, false),
429 447 /** static method test case */
430 448 STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE, false),
431 449 /** OSR constructor test case */
432 450 OSR_CONSTRUCTOR_TEST(Helper.OSR_CONSTRUCTOR,
433 451 Helper.OSR_CONSTRUCTOR_CALLABLE, true),
434 452 /** OSR method test case */
435 - OSR_METOD_TEST(Helper.OSR_METHOD, Helper.OSR_METHOD_CALLABLE, true),
453 + OSR_METHOD_TEST(Helper.OSR_METHOD, Helper.OSR_METHOD_CALLABLE, true),
436 454 /** OSR static method test case */
437 455 OSR_STATIC_TEST(Helper.OSR_STATIC, Helper.OSR_STATIC_CALLABLE, true);
438 456
439 457 private final Executable executable;
440 458 private final Callable<Integer> callable;
441 459 private final boolean isOsr;
442 460
443 461 private SimpleTestCase(Executable executable, Callable<Integer> callable,
444 462 boolean isOsr) {
445 463 this.executable = executable;
446 464 this.callable = callable;
447 465 this.isOsr = isOsr;
448 466 }
449 467
450 468 @Override
451 469 public Executable getExecutable() {
452 470 return executable;
453 471 }
454 472
455 473 @Override
456 474 public Callable<Integer> getCallable() {
457 475 return callable;
458 476 }
459 477
460 478 @Override
461 479 public boolean isOsr() {
462 480 return isOsr;
463 481 }
464 482
465 483 private static class Helper {
466 484
467 485 private static final Callable<Integer> CONSTRUCTOR_CALLABLE
468 486 = new Callable<Integer>() {
469 487 @Override
470 488 public Integer call() throws Exception {
471 489 return new Helper(1337).hashCode();
472 490 }
473 491 };
474 492
475 493 private static final Callable<Integer> METHOD_CALLABLE
476 494 = new Callable<Integer>() {
477 495 private final Helper helper = new Helper();
478 496
479 497 @Override
480 498 public Integer call() throws Exception {
481 499 return helper.method();
482 500 }
483 501 };
484 502
485 503 private static final Callable<Integer> STATIC_CALLABLE
486 504 = new Callable<Integer>() {
↓ open down ↓ |
41 lines elided |
↑ open up ↑ |
487 505 @Override
488 506 public Integer call() throws Exception {
489 507 return staticMethod();
490 508 }
491 509 };
492 510
493 511 private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE
494 512 = new Callable<Integer>() {
495 513 @Override
496 514 public Integer call() throws Exception {
497 - return new Helper(null).hashCode();
515 + return new Helper(null, CompilerWhiteBoxTest.BACKEDGE_THRESHOLD).hashCode();
498 516 }
499 517 };
500 518
501 519 private static final Callable<Integer> OSR_METHOD_CALLABLE
502 520 = new Callable<Integer>() {
503 521 private final Helper helper = new Helper();
504 522
505 523 @Override
506 524 public Integer call() throws Exception {
507 - return helper.osrMethod();
525 + return helper.osrMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
508 526 }
509 527 };
510 528
511 529 private static final Callable<Integer> OSR_STATIC_CALLABLE
512 530 = new Callable<Integer>() {
513 531 @Override
514 532 public Integer call() throws Exception {
515 - return osrStaticMethod();
533 + return osrStaticMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
516 534 }
517 535 };
518 536
519 537 private static final Constructor CONSTRUCTOR;
520 538 private static final Constructor OSR_CONSTRUCTOR;
521 539 private static final Method METHOD;
522 540 private static final Method STATIC;
523 541 private static final Method OSR_METHOD;
524 542 private static final Method OSR_STATIC;
525 543
526 544 static {
527 545 try {
528 546 CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
529 547 } catch (NoSuchMethodException | SecurityException e) {
530 548 throw new RuntimeException(
531 549 "exception on getting method Helper.<init>(int)", e);
532 550 }
533 551 try {
534 552 OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor(
535 - Object.class);
553 + Object.class, long.class);
536 554 } catch (NoSuchMethodException | SecurityException e) {
537 555 throw new RuntimeException(
538 - "exception on getting method Helper.<init>(Object)", e);
556 + "exception on getting method Helper.<init>(Object, long)", e);
539 557 }
540 558 METHOD = getMethod("method");
541 559 STATIC = getMethod("staticMethod");
542 - OSR_METHOD = getMethod("osrMethod");
543 - OSR_STATIC = getMethod("osrStaticMethod");
560 + OSR_METHOD = getMethod("osrMethod", long.class);
561 + OSR_STATIC = getMethod("osrStaticMethod", long.class);
544 562 }
545 563
546 - private static Method getMethod(String name) {
564 + private static Method getMethod(String name, Class<?>... parameterTypes) {
547 565 try {
548 - return Helper.class.getDeclaredMethod(name);
566 + return Helper.class.getDeclaredMethod(name, parameterTypes);
549 567 } catch (NoSuchMethodException | SecurityException e) {
550 568 throw new RuntimeException(
551 569 "exception on getting method Helper." + name, e);
552 570 }
553 -
554 571 }
555 572
556 573 private static int staticMethod() {
557 574 return 1138;
558 575 }
559 576
560 577 private int method() {
561 578 return 42;
562 579 }
563 580
564 - private static int osrStaticMethod() {
581 + /**
582 + * Deoptimizes all non-osr versions of the given executable after
583 + * compilation finished.
584 + *
585 + * @param e Executable
586 + * @throws Exception
587 + */
588 + private static void waitAndDeoptimize(Executable e) {
589 + CompilerWhiteBoxTest.waitBackgroundCompilation(e);
590 + if (WhiteBox.getWhiteBox().isMethodQueuedForCompilation(e)) {
591 + throw new RuntimeException(e + " must not be in queue");
592 + }
593 + // Deoptimize non-osr versions of executable
594 + WhiteBox.getWhiteBox().deoptimizeMethod(e, false);
595 + }
596 +
597 + /**
598 + * Executes the method multiple times to make sure we have
599 + * enough profiling information before triggering an OSR
600 + * compilation. Otherwise the C2 compiler may add uncommon traps.
601 + *
602 + * @param m Method to be executed
603 + * @return Number of times the method was executed
604 + * @throws Exception
605 + */
606 + private static int warmup(Method m) throws Exception {
607 + waitAndDeoptimize(m);
608 + Helper helper = new Helper();
609 + int result = 0;
610 + for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) {
611 + result += (int)m.invoke(helper, 1);
612 + }
613 + // Wait to make sure OSR compilation is not blocked by
614 + // non-OSR compilation in the compile queue
615 + CompilerWhiteBoxTest.waitBackgroundCompilation(m);
616 + return result;
617 + }
618 +
619 + /**
620 + * Executes the constructor multiple times to make sure we
621 + * have enough profiling information before triggering an OSR
622 + * compilation. Otherwise the C2 compiler may add uncommon traps.
623 + *
624 + * @param c Constructor to be executed
625 + * @return Number of times the constructor was executed
626 + * @throws Exception
627 + */
628 + private static int warmup(Constructor c) throws Exception {
629 + waitAndDeoptimize(c);
565 630 int result = 0;
566 - for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
631 + for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) {
632 + result += c.newInstance(null, 1).hashCode();
633 + }
634 + // Wait to make sure OSR compilation is not blocked by
635 + // non-OSR compilation in the compile queue
636 + CompilerWhiteBoxTest.waitBackgroundCompilation(c);
637 + return result;
638 + }
639 +
640 + private static int osrStaticMethod(long limit) throws Exception {
641 + int result = 0;
642 + if (limit != 1) {
643 + result = warmup(OSR_STATIC);
644 + }
645 + // Trigger osr compilation
646 + for (long i = 0; i < limit; ++i) {
567 647 result += staticMethod();
568 648 }
569 649 return result;
570 650 }
571 651
572 - private int osrMethod() {
652 + private int osrMethod(long limit) throws Exception {
573 653 int result = 0;
574 - for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
654 + if (limit != 1) {
655 + result = warmup(OSR_METHOD);
656 + }
657 + // Trigger osr compilation
658 + for (long i = 0; i < limit; ++i) {
575 659 result += method();
576 660 }
577 661 return result;
578 662 }
579 663
580 664 private final int x;
581 665
582 666 // for method and OSR method test case
583 667 public Helper() {
584 668 x = 0;
585 669 }
586 670
587 671 // for OSR constructor test case
588 - private Helper(Object o) {
672 + private Helper(Object o, long limit) throws Exception {
589 673 int result = 0;
590 - for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
674 + if (limit != 1) {
675 + result = warmup(OSR_CONSTRUCTOR);
676 + }
677 + // Trigger osr compilation
678 + for (long i = 0; i < limit; ++i) {
591 679 result += method();
592 680 }
593 681 x = result;
594 682 }
595 683
596 684 // for constructor test case
597 685 private Helper(int x) {
598 686 this.x = x;
599 687 }
600 688
601 689 @Override
602 690 public int hashCode() {
603 691 return x;
604 692 }
605 693 }
606 694 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX