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