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