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 jdk.testlib.WhiteBox; 25 import jdk.testlib.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 protected final void checkNotCompiled(int compLevel) { 199 if (WHITE_BOX.isMethodQueuedForCompilation(method)) { 200 throw new RuntimeException(method + " must not be in queue"); 201 } 202 if (WHITE_BOX.getMethodCompilationLevel(method, false) >= compLevel) { 203 throw new RuntimeException(method + " comp_level must be >= maxCompLevel"); 204 } 205 if (WHITE_BOX.getMethodCompilationLevel(method, true) >= compLevel) { 206 throw new RuntimeException(method + " osr_comp_level must be >= maxCompLevel"); 207 } 208 } 209 210 /** 211 * Checks, that {@linkplain #method} is not compiled. 212 * 213 * @throws RuntimeException if {@linkplain #method} is in compiler queue or 214 * is compiled, or if {@linkplain #method} has zero 215 * compilation level. 216 */ 217 protected final void checkNotCompiled() { 218 checkNotCompiled(true); 219 checkNotCompiled(false); 220 } 221 222 /** 223 * Checks, that {@linkplain #method} is not (OSR-)compiled. 224 * 225 * @param isOsr Check for OSR compilation if true 226 * @throws RuntimeException if {@linkplain #method} is in compiler queue or 227 * is compiled, or if {@linkplain #method} has zero 228 * compilation level. 229 */ 230 protected final void checkNotCompiled(boolean isOsr) { 231 waitBackgroundCompilation(); 232 if (WHITE_BOX.isMethodQueuedForCompilation(method)) { 233 throw new RuntimeException(method + " must not be in queue"); 234 } 235 if (WHITE_BOX.isMethodCompiled(method, isOsr)) { 236 throw new RuntimeException(method + " must not be " + 237 (isOsr ? "osr_" : "") + "compiled"); 238 } 239 if (WHITE_BOX.getMethodCompilationLevel(method, isOsr) != 0) { 240 throw new RuntimeException(method + (isOsr ? " osr_" : " ") + 241 "comp_level must be == 0"); 242 } 243 } 244 245 /** 246 * Checks, that {@linkplain #method} is compiled. 247 * 248 * @throws RuntimeException if {@linkplain #method} isn't in compiler queue 249 * and isn't compiled, or if {@linkplain #method} 250 * has nonzero compilation level 251 */ 252 protected final void checkCompiled() { 253 final long start = System.currentTimeMillis(); 254 waitBackgroundCompilation(); 255 if (WHITE_BOX.isMethodQueuedForCompilation(method)) { 256 System.err.printf("Warning: %s is still in queue after %dms%n", 257 method, System.currentTimeMillis() - start); 258 return; 259 } 260 if (!WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) { 261 throw new RuntimeException(method + " must be " 262 + (testCase.isOsr() ? "osr_" : "") + "compiled"); 263 } 264 if (WHITE_BOX.getMethodCompilationLevel(method, testCase.isOsr()) 265 == 0) { 266 throw new RuntimeException(method 267 + (testCase.isOsr() ? " osr_" : " ") 268 + "comp_level must be != 0"); 269 } 270 } 271 272 protected final void deoptimize() { 273 WHITE_BOX.deoptimizeMethod(method, testCase.isOsr()); 274 if (testCase.isOsr()) { 275 WHITE_BOX.deoptimizeMethod(method, false); 276 } 277 } 278 279 protected final int getCompLevel() { 280 NMethod nm = NMethod.get(method, testCase.isOsr()); 281 return nm == null ? COMP_LEVEL_NONE : nm.comp_level; 282 } 283 284 protected final boolean isCompilable() { 285 return WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, 286 testCase.isOsr()); 287 } 288 289 protected final boolean isCompilable(int compLevel) { 290 return WHITE_BOX 291 .isMethodCompilable(method, compLevel, testCase.isOsr()); 292 } 293 294 protected final void makeNotCompilable() { 295 WHITE_BOX.makeMethodNotCompilable(method, COMP_LEVEL_ANY, 296 testCase.isOsr()); 297 } 298 299 protected final void makeNotCompilable(int compLevel) { 300 WHITE_BOX.makeMethodNotCompilable(method, compLevel, testCase.isOsr()); 301 } 302 303 /** 304 * Waits for completion of background compilation of {@linkplain #method}. 305 */ 306 protected final void waitBackgroundCompilation() { 307 waitBackgroundCompilation(method); 308 } 309 310 /** 311 * Waits for completion of background compilation of the given executable. 312 * 313 * @param executable Executable 314 */ 315 protected static final void waitBackgroundCompilation(Executable executable) { 316 if (!BACKGROUND_COMPILATION) { 317 return; 318 } 319 final Object obj = new Object(); 320 for (int i = 0; i < 10 321 && WHITE_BOX.isMethodQueuedForCompilation(executable); ++i) { 322 synchronized (obj) { 323 try { 324 obj.wait(1000); 325 } catch (InterruptedException e) { 326 Thread.currentThread().interrupt(); 327 } 328 } 329 } 330 } 331 332 /** 333 * Prints information about {@linkplain #method}. 334 */ 335 protected final void printInfo() { 336 System.out.printf("%n%s:%n", method); 337 System.out.printf("\tcompilable:\t%b%n", 338 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, false)); 339 System.out.printf("\tcompiled:\t%b%n", 340 WHITE_BOX.isMethodCompiled(method, false)); 341 System.out.printf("\tcomp_level:\t%d%n", 342 WHITE_BOX.getMethodCompilationLevel(method, false)); 343 System.out.printf("\tosr_compilable:\t%b%n", 344 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, true)); 345 System.out.printf("\tosr_compiled:\t%b%n", 346 WHITE_BOX.isMethodCompiled(method, true)); 347 System.out.printf("\tosr_comp_level:\t%d%n", 348 WHITE_BOX.getMethodCompilationLevel(method, true)); 349 System.out.printf("\tin_queue:\t%b%n", 350 WHITE_BOX.isMethodQueuedForCompilation(method)); 351 System.out.printf("compile_queues_size:\t%d%n%n", 352 WHITE_BOX.getCompileQueuesSize()); 353 } 354 355 /** 356 * Executes testing. 357 */ 358 protected abstract void test() throws Exception; 359 360 /** 361 * Tries to trigger compilation of {@linkplain #method} by call 362 * {@linkplain TestCase#getCallable()} enough times. 363 * 364 * @return accumulated result 365 * @see #compile(int) 366 */ 367 protected final int compile() { 368 if (testCase.isOsr()) { 369 return compile(1); 370 } else { 371 return compile(THRESHOLD); 372 } 373 } 374 375 /** 376 * Tries to trigger compilation of {@linkplain #method} by call 377 * {@linkplain TestCase#getCallable()} specified times. 378 * 379 * @param count invocation count 380 * @return accumulated result 381 */ 382 protected final int compile(int count) { 383 int result = 0; 384 Integer tmp; 385 for (int i = 0; i < count; ++i) { 386 try { 387 tmp = testCase.getCallable().call(); 388 } catch (Exception e) { 389 tmp = null; 390 } 391 result += tmp == null ? 0 : tmp; 392 } 393 if (IS_VERBOSE) { 394 System.out.println("method was invoked " + count + " times"); 395 } 396 return result; 397 } 398 399 /** 400 * Utility interface provides tested method and object to invoke it. 401 */ 402 public interface TestCase { 403 /** the name of test case */ 404 String name(); 405 406 /** tested method */ 407 Executable getExecutable(); 408 409 /** object to invoke {@linkplain #getExecutable()} */ 410 Callable<Integer> getCallable(); 411 412 /** flag for OSR test case */ 413 boolean isOsr(); 414 } 415 416 /** 417 * @return {@code true} if the current test case is OSR and the mode is 418 * Xcomp, otherwise {@code false} 419 */ 420 protected boolean skipXcompOSR() { 421 boolean result = testCase.isOsr() 422 && CompilerWhiteBoxTest.MODE.startsWith("compiled "); 423 if (result && IS_VERBOSE) { 424 System.err.printf("Warning: %s is not applicable in %s%n", 425 testCase.name(), CompilerWhiteBoxTest.MODE); 426 } 427 return result; 428 } 429 } 430 431 enum SimpleTestCase implements CompilerWhiteBoxTest.TestCase { 432 /** constructor test case */ 433 CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE, false), 434 /** method test case */ 435 METHOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE, false), 436 /** static method test case */ 437 STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE, false), 438 /** OSR constructor test case */ 439 OSR_CONSTRUCTOR_TEST(Helper.OSR_CONSTRUCTOR, 440 Helper.OSR_CONSTRUCTOR_CALLABLE, true), 441 /** OSR method test case */ 442 OSR_METHOD_TEST(Helper.OSR_METHOD, Helper.OSR_METHOD_CALLABLE, true), 443 /** OSR static method test case */ 444 OSR_STATIC_TEST(Helper.OSR_STATIC, Helper.OSR_STATIC_CALLABLE, true); 445 446 private final Executable executable; 447 private final Callable<Integer> callable; 448 private final boolean isOsr; 449 450 private SimpleTestCase(Executable executable, Callable<Integer> callable, 451 boolean isOsr) { 452 this.executable = executable; 453 this.callable = callable; 454 this.isOsr = isOsr; 455 } 456 457 @Override 458 public Executable getExecutable() { 459 return executable; 460 } 461 462 @Override 463 public Callable<Integer> getCallable() { 464 return callable; 465 } 466 467 @Override 468 public boolean isOsr() { 469 return isOsr; 470 } 471 472 private static class Helper { 473 474 private static final Callable<Integer> CONSTRUCTOR_CALLABLE 475 = new Callable<Integer>() { 476 @Override 477 public Integer call() throws Exception { 478 return new Helper(1337).hashCode(); 479 } 480 }; 481 482 private static final Callable<Integer> METHOD_CALLABLE 483 = new Callable<Integer>() { 484 private final Helper helper = new Helper(); 485 486 @Override 487 public Integer call() throws Exception { 488 return helper.method(); 489 } 490 }; 491 492 private static final Callable<Integer> STATIC_CALLABLE 493 = new Callable<Integer>() { 494 @Override 495 public Integer call() throws Exception { 496 return staticMethod(); 497 } 498 }; 499 500 private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE 501 = new Callable<Integer>() { 502 @Override 503 public Integer call() throws Exception { 504 return new Helper(null, CompilerWhiteBoxTest.BACKEDGE_THRESHOLD).hashCode(); 505 } 506 }; 507 508 private static final Callable<Integer> OSR_METHOD_CALLABLE 509 = new Callable<Integer>() { 510 private final Helper helper = new Helper(); 511 512 @Override 513 public Integer call() throws Exception { 514 return helper.osrMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD); 515 } 516 }; 517 518 private static final Callable<Integer> OSR_STATIC_CALLABLE 519 = new Callable<Integer>() { 520 @Override 521 public Integer call() throws Exception { 522 return osrStaticMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD); 523 } 524 }; 525 526 private static final Constructor CONSTRUCTOR; 527 private static final Constructor OSR_CONSTRUCTOR; 528 private static final Method METHOD; 529 private static final Method STATIC; 530 private static final Method OSR_METHOD; 531 private static final Method OSR_STATIC; 532 533 static { 534 try { 535 CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class); 536 } catch (NoSuchMethodException | SecurityException e) { 537 throw new RuntimeException( 538 "exception on getting method Helper.<init>(int)", e); 539 } 540 try { 541 OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor( 542 Object.class, long.class); 543 } catch (NoSuchMethodException | SecurityException e) { 544 throw new RuntimeException( 545 "exception on getting method Helper.<init>(Object, long)", e); 546 } 547 METHOD = getMethod("method"); 548 STATIC = getMethod("staticMethod"); 549 OSR_METHOD = getMethod("osrMethod", long.class); 550 OSR_STATIC = getMethod("osrStaticMethod", long.class); 551 } 552 553 private static Method getMethod(String name, Class<?>... parameterTypes) { 554 try { 555 return Helper.class.getDeclaredMethod(name, parameterTypes); 556 } catch (NoSuchMethodException | SecurityException e) { 557 throw new RuntimeException( 558 "exception on getting method Helper." + name, e); 559 } 560 } 561 562 private static int staticMethod() { 563 return 1138; 564 } 565 566 private int method() { 567 return 42; 568 } 569 570 /** 571 * Deoptimizes all non-osr versions of the given executable after 572 * compilation finished. 573 * 574 * @param e Executable 575 * @throws Exception 576 */ 577 private static void waitAndDeoptimize(Executable e) { 578 CompilerWhiteBoxTest.waitBackgroundCompilation(e); 579 if (WhiteBox.getWhiteBox().isMethodQueuedForCompilation(e)) { 580 throw new RuntimeException(e + " must not be in queue"); 581 } 582 // Deoptimize non-osr versions of executable 583 WhiteBox.getWhiteBox().deoptimizeMethod(e, false); 584 } 585 586 /** 587 * Executes the method multiple times to make sure we have 588 * enough profiling information before triggering an OSR 589 * compilation. Otherwise the C2 compiler may add uncommon traps. 590 * 591 * @param m Method to be executed 592 * @return Number of times the method was executed 593 * @throws Exception 594 */ 595 private static int warmup(Method m) throws Exception { 596 waitAndDeoptimize(m); 597 Helper helper = new Helper(); 598 int result = 0; 599 for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) { 600 result += (int)m.invoke(helper, 1); 601 } 602 // Wait to make sure OSR compilation is not blocked by 603 // non-OSR compilation in the compile queue 604 CompilerWhiteBoxTest.waitBackgroundCompilation(m); 605 return result; 606 } 607 608 /** 609 * Executes the constructor multiple times to make sure we 610 * have enough profiling information before triggering an OSR 611 * compilation. Otherwise the C2 compiler may add uncommon traps. 612 * 613 * @param c Constructor to be executed 614 * @return Number of times the constructor was executed 615 * @throws Exception 616 */ 617 private static int warmup(Constructor c) throws Exception { 618 waitAndDeoptimize(c); 619 int result = 0; 620 for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) { 621 result += c.newInstance(null, 1).hashCode(); 622 } 623 // Wait to make sure OSR compilation is not blocked by 624 // non-OSR compilation in the compile queue 625 CompilerWhiteBoxTest.waitBackgroundCompilation(c); 626 return result; 627 } 628 629 private static int osrStaticMethod(long limit) throws Exception { 630 int result = 0; 631 if (limit != 1) { 632 result = warmup(OSR_STATIC); 633 } 634 // Trigger osr compilation 635 for (long i = 0; i < limit; ++i) { 636 result += staticMethod(); 637 } 638 return result; 639 } 640 641 private int osrMethod(long limit) throws Exception { 642 int result = 0; 643 if (limit != 1) { 644 result = warmup(OSR_METHOD); 645 } 646 // Trigger osr compilation 647 for (long i = 0; i < limit; ++i) { 648 result += method(); 649 } 650 return result; 651 } 652 653 private final int x; 654 655 // for method and OSR method test case 656 public Helper() { 657 x = 0; 658 } 659 660 // for OSR constructor test case 661 private Helper(Object o, long limit) throws Exception { 662 int result = 0; 663 if (limit != 1) { 664 result = warmup(OSR_CONSTRUCTOR); 665 } 666 // Trigger osr compilation 667 for (long i = 0; i < limit; ++i) { 668 result += method(); 669 } 670 x = result; 671 } 672 673 // for constructor test case 674 private Helper(int x) { 675 this.x = x; 676 } 677 678 @Override 679 public int hashCode() { 680 return x; 681 } 682 } 683 }