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 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 at the given compilation
 200      * level or above.
 201      *
 202      * @param compLevel
 203      *
 204      * @throws RuntimeException if {@linkplain #method} is in compiler queue or
 205      *                          is compiled, or if {@linkplain #method} has zero
 206      *                          compilation level.
 207      */
 208 
 209     protected final void checkNotCompiled(int compLevel) {
 210         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
 211             throw new RuntimeException(method + " must not be in queue");
 212         }
 213         if (WHITE_BOX.getMethodCompilationLevel(method, false) >= compLevel) {
 214             throw new RuntimeException(method + " comp_level must be >= maxCompLevel");
 215         }
 216         if (WHITE_BOX.getMethodCompilationLevel(method, true) >= compLevel) {
 217             throw new RuntimeException(method + " osr_comp_level must be >= maxCompLevel");
 218         }
 219     }
 220 
 221     /**
 222      * Checks, that {@linkplain #method} is not compiled.
 223      *
 224      * @throws RuntimeException if {@linkplain #method} is in compiler queue or
 225      *                          is compiled, or if {@linkplain #method} has zero
 226      *                          compilation level.
 227      */
 228     protected final void checkNotCompiled() {
 229         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
 230             throw new RuntimeException(method + " must not be in queue");
 231         }
 232         if (WHITE_BOX.isMethodCompiled(method, false)) {
 233             throw new RuntimeException(method + " must be not compiled");
 234         }
 235         if (WHITE_BOX.getMethodCompilationLevel(method, false) != 0) {
 236             throw new RuntimeException(method + " comp_level must be == 0");
 237         }
 238         if (WHITE_BOX.isMethodCompiled(method, true)) {
 239             throw new RuntimeException(method + " must be not osr_compiled");
 240         }
 241         if (WHITE_BOX.getMethodCompilationLevel(method, true) != 0) {
 242             throw new RuntimeException(method + " osr_comp_level must be == 0");
 243         }
 244     }
 245 
 246     /**
 247      * Checks, that {@linkplain #method} is compiled.
 248      *
 249      * @throws RuntimeException if {@linkplain #method} isn't in compiler queue
 250      *                          and isn't compiled, or if {@linkplain #method}
 251      *                          has nonzero compilation level
 252      */
 253     protected final void checkCompiled() {
 254         final long start = System.currentTimeMillis();
 255         waitBackgroundCompilation();
 256         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
 257             System.err.printf("Warning: %s is still in queue after %dms%n",
 258                     method, System.currentTimeMillis() - start);
 259             return;
 260         }
 261         if (!WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) {
 262             throw new RuntimeException(method + " must be "
 263                     + (testCase.isOsr() ? "osr_" : "") + "compiled");
 264         }
 265         if (WHITE_BOX.getMethodCompilationLevel(method, testCase.isOsr())
 266                 == 0) {
 267             throw new RuntimeException(method
 268                     + (testCase.isOsr() ? " osr_" : " ")
 269                     + "comp_level must be != 0");
 270         }
 271     }
 272 
 273     protected final void deoptimize() {
 274         WHITE_BOX.deoptimizeMethod(method, testCase.isOsr());
 275         if (testCase.isOsr()) {
 276             WHITE_BOX.deoptimizeMethod(method, false);
 277         }
 278     }
 279 
 280     protected final int getCompLevel() {
 281         return WHITE_BOX.getMethodCompilationLevel(method, testCase.isOsr());
 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         if (!BACKGROUND_COMPILATION) {
 308             return;
 309         }
 310         final Object obj = new Object();
 311         for (int i = 0; i < 10
 312                 && WHITE_BOX.isMethodQueuedForCompilation(method); ++i) {
 313             synchronized (obj) {
 314                 try {
 315                     obj.wait(1000);
 316                 } catch (InterruptedException e) {
 317                     Thread.currentThread().interrupt();
 318                 }
 319             }
 320         }
 321     }
 322 
 323     /**
 324      * Prints information about {@linkplain #method}.
 325      */
 326     protected final void printInfo() {
 327         System.out.printf("%n%s:%n", method);
 328         System.out.printf("\tcompilable:\t%b%n",
 329                 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, false));
 330         System.out.printf("\tcompiled:\t%b%n",
 331                 WHITE_BOX.isMethodCompiled(method, false));
 332         System.out.printf("\tcomp_level:\t%d%n",
 333                 WHITE_BOX.getMethodCompilationLevel(method, false));
 334         System.out.printf("\tosr_compilable:\t%b%n",
 335                 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, true));
 336         System.out.printf("\tosr_compiled:\t%b%n",
 337                 WHITE_BOX.isMethodCompiled(method, true));
 338         System.out.printf("\tosr_comp_level:\t%d%n",
 339                 WHITE_BOX.getMethodCompilationLevel(method, true));
 340         System.out.printf("\tin_queue:\t%b%n",
 341                 WHITE_BOX.isMethodQueuedForCompilation(method));
 342         System.out.printf("compile_queues_size:\t%d%n%n",
 343                 WHITE_BOX.getCompileQueuesSize());
 344     }
 345 
 346     /**
 347      * Executes testing.
 348      */
 349     protected abstract void test() throws Exception;
 350 
 351     /**
 352      * Tries to trigger compilation of {@linkplain #method} by call
 353      * {@linkplain TestCase#getCallable()} enough times.
 354      *
 355      * @return accumulated result
 356      * @see #compile(int)
 357      */
 358     protected final int compile() {
 359         if (testCase.isOsr()) {
 360             return compile(1);
 361         } else {
 362             return compile(THRESHOLD);
 363         }
 364     }
 365 
 366     /**
 367      * Tries to trigger compilation of {@linkplain #method} by call
 368      * {@linkplain TestCase#getCallable()} specified times.
 369      *
 370      * @param count invocation count
 371      * @return accumulated result
 372      */
 373     protected final int compile(int count) {
 374         int result = 0;
 375         Integer tmp;
 376         for (int i = 0; i < count; ++i) {
 377             try {
 378                 tmp = testCase.getCallable().call();
 379             } catch (Exception e) {
 380                 tmp = null;
 381             }
 382             result += tmp == null ? 0 : tmp;
 383         }
 384         if (IS_VERBOSE) {
 385             System.out.println("method was invoked " + count + " times");
 386         }
 387         return result;
 388     }
 389 
 390     /**
 391      * Utility interface provides tested method and object to invoke it.
 392      */
 393     public interface TestCase {
 394         /** the name of test case */
 395         String name();
 396 
 397         /** tested method */
 398         Executable getExecutable();
 399 
 400         /** object to invoke {@linkplain #getExecutable()} */
 401         Callable<Integer> getCallable();
 402 
 403         /** flag for OSR test case */
 404         boolean isOsr();
 405     }
 406 
 407     /**
 408      * @return {@code true} if the current test case is OSR and the mode is
 409      *          Xcomp, otherwise {@code false}
 410      */
 411     protected boolean skipXcompOSR() {
 412         boolean result =  testCase.isOsr()
 413                 && CompilerWhiteBoxTest.MODE.startsWith("compiled ");
 414         if (result && IS_VERBOSE) {
 415             System.err.printf("Warning: %s is not applicable in %s%n",
 416                     testCase.name(), CompilerWhiteBoxTest.MODE);
 417         }
 418         return result;
 419     }
 420 }
 421 
 422 enum SimpleTestCase implements CompilerWhiteBoxTest.TestCase {
 423     /** constructor test case */
 424     CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE, false),
 425     /** method test case */
 426     METOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE, false),
 427     /** static method test case */
 428     STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE, false),
 429     /** OSR constructor test case */
 430     OSR_CONSTRUCTOR_TEST(Helper.OSR_CONSTRUCTOR,
 431             Helper.OSR_CONSTRUCTOR_CALLABLE, true),
 432     /** OSR method test case */
 433     OSR_METOD_TEST(Helper.OSR_METHOD, Helper.OSR_METHOD_CALLABLE, true),
 434     /** OSR static method test case */
 435     OSR_STATIC_TEST(Helper.OSR_STATIC, Helper.OSR_STATIC_CALLABLE, true);
 436 
 437     private final Executable executable;
 438     private final Callable<Integer> callable;
 439     private final boolean isOsr;
 440 
 441     private SimpleTestCase(Executable executable, Callable<Integer> callable,
 442             boolean isOsr) {
 443         this.executable = executable;
 444         this.callable = callable;
 445         this.isOsr = isOsr;
 446     }
 447 
 448     @Override
 449     public Executable getExecutable() {
 450         return executable;
 451     }
 452 
 453     @Override
 454     public Callable<Integer> getCallable() {
 455         return callable;
 456     }
 457 
 458     @Override
 459     public boolean isOsr() {
 460         return isOsr;
 461     }
 462 
 463     private static class Helper {
 464 
 465         private static final Callable<Integer> CONSTRUCTOR_CALLABLE
 466                 = new Callable<Integer>() {
 467             @Override
 468             public Integer call() throws Exception {
 469                 return new Helper(1337).hashCode();
 470             }
 471         };
 472 
 473         private static final Callable<Integer> METHOD_CALLABLE
 474                 = new Callable<Integer>() {
 475             private final Helper helper = new Helper();
 476 
 477             @Override
 478             public Integer call() throws Exception {
 479                 return helper.method();
 480             }
 481         };
 482 
 483         private static final Callable<Integer> STATIC_CALLABLE
 484                 = new Callable<Integer>() {
 485             @Override
 486             public Integer call() throws Exception {
 487                 return staticMethod();
 488             }
 489         };
 490 
 491         private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE
 492                 = new Callable<Integer>() {
 493             @Override
 494             public Integer call() throws Exception {
 495                 return new Helper(null).hashCode();
 496             }
 497         };
 498 
 499         private static final Callable<Integer> OSR_METHOD_CALLABLE
 500                 = new Callable<Integer>() {
 501             private final Helper helper = new Helper();
 502 
 503             @Override
 504             public Integer call() throws Exception {
 505                 return helper.osrMethod();
 506             }
 507         };
 508 
 509         private static final Callable<Integer> OSR_STATIC_CALLABLE
 510                 = new Callable<Integer>() {
 511             @Override
 512             public Integer call() throws Exception {
 513                 return osrStaticMethod();
 514             }
 515         };
 516 
 517         private static final Constructor CONSTRUCTOR;
 518         private static final Constructor OSR_CONSTRUCTOR;
 519         private static final Method METHOD;
 520         private static final Method STATIC;
 521         private static final Method OSR_METHOD;
 522         private static final Method OSR_STATIC;
 523 
 524         static {
 525             try {
 526                 CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
 527             } catch (NoSuchMethodException | SecurityException e) {
 528                 throw new RuntimeException(
 529                         "exception on getting method Helper.<init>(int)", e);
 530             }
 531             try {
 532                 OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor(
 533                         Object.class);
 534             } catch (NoSuchMethodException | SecurityException e) {
 535                 throw new RuntimeException(
 536                         "exception on getting method Helper.<init>(Object)", e);
 537             }
 538             METHOD = getMethod("method");
 539             STATIC = getMethod("staticMethod");
 540             OSR_METHOD = getMethod("osrMethod");
 541             OSR_STATIC = getMethod("osrStaticMethod");
 542         }
 543 
 544         private static Method getMethod(String name) {
 545             try {
 546                 return Helper.class.getDeclaredMethod(name);
 547             } catch (NoSuchMethodException | SecurityException e) {
 548                 throw new RuntimeException(
 549                         "exception on getting method Helper." + name, e);
 550             }
 551 
 552         }
 553 
 554         private static int staticMethod() {
 555             return 1138;
 556         }
 557 
 558         private int method() {
 559             return 42;
 560         }
 561 
 562         private static int osrStaticMethod() {
 563             int result = 0;
 564             for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
 565                 result += staticMethod();
 566             }
 567             return result;
 568         }
 569 
 570         private int osrMethod() {
 571             int result = 0;
 572             for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
 573                 result += method();
 574             }
 575             return result;
 576         }
 577 
 578         private final int x;
 579 
 580         // for method and OSR method test case
 581         public Helper() {
 582             x = 0;
 583         }
 584 
 585         // for OSR constructor test case
 586         private Helper(Object o) {
 587             int result = 0;
 588             for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) {
 589                 result += method();
 590             }
 591             x = result;
 592         }
 593 
 594         // for constructor test case
 595         private Helper(int x) {
 596             this.x = x;
 597         }
 598 
 599         @Override
 600         public int hashCode() {
 601             return x;
 602         }
 603     }
 604 }