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 package compiler.whitebox;
  24 
  25 import sun.hotspot.WhiteBox;
  26 import sun.hotspot.code.NMethod;
  27 import java.lang.reflect.Executable;
  28 import java.util.Objects;
  29 import java.util.concurrent.Callable;
  30 import java.util.function.Function;
  31 
  32 /**
  33  * Abstract class for WhiteBox testing of JIT.
  34  *
  35  * @author igor.ignatyev@oracle.com
  36  */
  37 public abstract class CompilerWhiteBoxTest {
  38     /** {@code CompLevel::CompLevel_none} -- Interpreter */
  39     public static final int COMP_LEVEL_NONE = 0;
  40     /** {@code CompLevel::CompLevel_any}, {@code CompLevel::CompLevel_all} */
  41     public static final int COMP_LEVEL_ANY = -1;
  42     /** {@code CompLevel::CompLevel_simple} -- C1 */
  43     public static final int COMP_LEVEL_SIMPLE = 1;
  44     /** {@code CompLevel::CompLevel_limited_profile} -- C1, invocation & backedge counters */
  45     public static final int COMP_LEVEL_LIMITED_PROFILE = 2;
  46     /** {@code CompLevel::CompLevel_full_profile} -- C1, invocation & backedge counters + mdo */
  47     public static final int COMP_LEVEL_FULL_PROFILE = 3;
  48     /** {@code CompLevel::CompLevel_full_optimization} -- C2 or Shark */
  49     public static final int COMP_LEVEL_FULL_OPTIMIZATION = 4;
  50     /** Maximal value for CompLevel */
  51     public static final int COMP_LEVEL_MAX = COMP_LEVEL_FULL_OPTIMIZATION;
  52 
  53     /** Instance of WhiteBox */
  54     protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  55     /** Value of {@code -XX:CompileThreshold} */
  56     protected static final int COMPILE_THRESHOLD
  57             = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
  58     /** Value of {@code -XX:BackgroundCompilation} */
  59     protected static final boolean BACKGROUND_COMPILATION
  60             = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
  61     /** Value of {@code -XX:TieredCompilation} */
  62     protected static final boolean TIERED_COMPILATION
  63             = Boolean.valueOf(getVMOption("TieredCompilation", "false"));
  64     /** Value of {@code -XX:TieredStopAtLevel} */
  65     protected static final int TIERED_STOP_AT_LEVEL
  66             = Integer.parseInt(getVMOption("TieredStopAtLevel", "0"));
  67     /** Flag for verbose output, true if {@code -Dverbose} specified */
  68     protected static final boolean IS_VERBOSE
  69             = System.getProperty("verbose") != null;
  70     /** invocation count to trigger compilation */
  71     public static final int THRESHOLD;
  72     /** invocation count to trigger OSR compilation */
  73     public static final long BACKEDGE_THRESHOLD;
  74     /** Value of {@code java.vm.info} (interpreted|mixed|comp mode) */
  75     protected static final String MODE = System.getProperty("java.vm.info");
  76 
  77     static {
  78         if (TIERED_COMPILATION) {
  79             BACKEDGE_THRESHOLD = THRESHOLD = 150000;
  80         } else {
  81             THRESHOLD = COMPILE_THRESHOLD;
  82             BACKEDGE_THRESHOLD = COMPILE_THRESHOLD * Long.parseLong(getVMOption(
  83                     "OnStackReplacePercentage"));
  84         }
  85     }
  86 
  87     /**
  88      * Returns value of VM option.
  89      *
  90      * @param name option's name
  91      * @return value of option or {@code null}, if option doesn't exist
  92      * @throws NullPointerException if name is null
  93      */
  94     protected static String getVMOption(String name) {
  95         Objects.requireNonNull(name);
  96         return Objects.toString(WHITE_BOX.getVMFlag(name), null);
  97     }
  98 
  99     /**
 100      * Returns value of VM option or default value.
 101      *
 102      * @param name         option's name
 103      * @param defaultValue default value
 104      * @return value of option or {@code defaultValue}, if option doesn't exist
 105      * @throws NullPointerException if name is null
 106      * @see #getVMOption(String)
 107      */
 108     protected static String getVMOption(String name, String defaultValue) {
 109         String result = getVMOption(name);
 110         return result == null ? defaultValue : result;
 111     }
 112 
 113     /** copy of is_c1_compile(int) from utilities/globalDefinitions.hpp */
 114     protected static boolean isC1Compile(int compLevel) {
 115         return (compLevel > COMP_LEVEL_NONE)
 116                 && (compLevel < COMP_LEVEL_FULL_OPTIMIZATION);
 117     }
 118 
 119     /** copy of is_c2_compile(int) from utilities/globalDefinitions.hpp */
 120     protected static boolean isC2Compile(int compLevel) {
 121         return compLevel == COMP_LEVEL_FULL_OPTIMIZATION;
 122     }
 123 
 124     protected static void main(
 125             Function<TestCase, CompilerWhiteBoxTest> constructor,
 126             String[] args) {
 127         if (args.length == 0) {
 128             for (TestCase test : SimpleTestCase.values()) {
 129                 constructor.apply(test).runTest();
 130             }
 131         } else {
 132             for (String name : args) {
 133                 constructor.apply(SimpleTestCase.valueOf(name)).runTest();
 134             }
 135         }
 136     }
 137 
 138     /** tested method */
 139     protected final Executable method;
 140     protected final TestCase testCase;
 141 
 142     /**
 143      * Constructor.
 144      *
 145      * @param testCase object, that contains tested method and way to invoke it.
 146      */
 147     protected CompilerWhiteBoxTest(TestCase testCase) {
 148         Objects.requireNonNull(testCase);
 149         System.out.println("TEST CASE:" + testCase.name());
 150         method = testCase.getExecutable();
 151         this.testCase = testCase;
 152     }
 153 
 154     /**
 155      * Template method for testing. Prints tested method's info before
 156      * {@linkplain #test()} and after {@linkplain #test()} or on thrown
 157      * exception.
 158      *
 159      * @throws RuntimeException if method {@linkplain #test()} throws any
 160      *                          exception
 161      * @see #test()
 162      */
 163     protected final void runTest() {
 164         if (CompilerWhiteBoxTest.MODE.startsWith("interpreted ")) {
 165             System.err.println(
 166                     "Warning: test is not applicable in interpreted mode");
 167             return;
 168         }
 169         System.out.println("at test's start:");
 170         printInfo();
 171         try {
 172             test();
 173         } catch (Exception e) {
 174             System.out.printf("on exception '%s':", e.getMessage());
 175             printInfo();
 176             e.printStackTrace();
 177             if (e instanceof RuntimeException) {
 178                 throw (RuntimeException) e;
 179             }
 180             throw new RuntimeException(e);
 181         }
 182         System.out.println("at test's end:");
 183         printInfo();
 184     }
 185 
 186     /**
 187      * Checks, that {@linkplain #method} is not compiled at the given compilation
 188      * level or above.
 189      *
 190      * @param compLevel
 191      *
 192      * @throws RuntimeException if {@linkplain #method} is in compiler queue or
 193      *                          is compiled, or if {@linkplain #method} has zero
 194      *                          compilation level.
 195      */
 196     protected final void checkNotCompiled(int compLevel) {
 197         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
 198             throw new RuntimeException(method + " must not be in queue");
 199         }
 200         if (WHITE_BOX.getMethodCompilationLevel(method, false) >= compLevel) {
 201             throw new RuntimeException(method + " comp_level must be >= maxCompLevel");
 202         }
 203         if (WHITE_BOX.getMethodCompilationLevel(method, true) >= compLevel) {
 204             throw new RuntimeException(method + " osr_comp_level must be >= maxCompLevel");
 205         }
 206     }
 207 
 208     /**
 209      * Checks, that {@linkplain #method} is not compiled.
 210      *
 211      * @throws RuntimeException if {@linkplain #method} is in compiler queue or
 212      *                          is compiled, or if {@linkplain #method} has zero
 213      *                          compilation level.
 214      */
 215     protected final void checkNotCompiled() {
 216         checkNotCompiled(true);
 217         checkNotCompiled(false);
 218     }
 219 
 220     /**
 221      * Checks, that {@linkplain #method} is not (OSR-)compiled.
 222      *
 223      * @param isOsr Check for OSR compilation if true
 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(boolean isOsr) {
 229         waitBackgroundCompilation();
 230         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
 231             throw new RuntimeException(method + " must not be in queue");
 232         }
 233         if (WHITE_BOX.isMethodCompiled(method, isOsr)) {
 234             throw new RuntimeException(method + " must not be " +
 235                                        (isOsr ? "osr_" : "") + "compiled");
 236         }
 237         if (WHITE_BOX.getMethodCompilationLevel(method, isOsr) != 0) {
 238             throw new RuntimeException(method + (isOsr ? " osr_" : " ") +
 239                                        "comp_level must be == 0");
 240         }
 241     }
 242 
 243     /**
 244      * Checks, that {@linkplain #method} is compiled.
 245      *
 246      * @throws RuntimeException if {@linkplain #method} isn't in compiler queue
 247      *                          and isn't compiled, or if {@linkplain #method}
 248      *                          has nonzero compilation level
 249      */
 250     protected final void checkCompiled() {
 251         final long start = System.currentTimeMillis();
 252         waitBackgroundCompilation();
 253         if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
 254             System.err.printf("Warning: %s is still in queue after %dms%n",
 255                     method, System.currentTimeMillis() - start);
 256             return;
 257         }
 258         if (!WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) {
 259             throw new RuntimeException(method + " must be "
 260                     + (testCase.isOsr() ? "osr_" : "") + "compiled");
 261         }
 262         if (WHITE_BOX.getMethodCompilationLevel(method, testCase.isOsr())
 263                 == 0) {
 264             throw new RuntimeException(method
 265                     + (testCase.isOsr() ? " osr_" : " ")
 266                     + "comp_level must be != 0");
 267         }
 268     }
 269 
 270     protected final void deoptimize() {
 271         WHITE_BOX.deoptimizeMethod(method, testCase.isOsr());
 272         if (testCase.isOsr()) {
 273             WHITE_BOX.deoptimizeMethod(method, false);
 274         }
 275     }
 276 
 277     protected final int getCompLevel() {
 278         NMethod nm = NMethod.get(method, testCase.isOsr());
 279         return nm == null ? COMP_LEVEL_NONE : nm.comp_level;
 280     }
 281 
 282     protected final boolean isCompilable() {
 283         return WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY,
 284                 testCase.isOsr());
 285     }
 286 
 287     protected final boolean isCompilable(int compLevel) {
 288         return WHITE_BOX
 289                 .isMethodCompilable(method, compLevel, testCase.isOsr());
 290     }
 291 
 292     protected final void makeNotCompilable() {
 293         WHITE_BOX.makeMethodNotCompilable(method, COMP_LEVEL_ANY,
 294                 testCase.isOsr());
 295     }
 296 
 297     protected final void makeNotCompilable(int compLevel) {
 298         WHITE_BOX.makeMethodNotCompilable(method, compLevel, testCase.isOsr());
 299     }
 300 
 301     /**
 302      * Waits for completion of background compilation of {@linkplain #method}.
 303      */
 304     protected final void waitBackgroundCompilation() {
 305         waitBackgroundCompilation(method);
 306     }
 307 
 308     /**
 309      * Waits for completion of background compilation of the given executable.
 310      *
 311      * @param executable Executable
 312      */
 313     public static final void waitBackgroundCompilation(Executable executable) {
 314         if (!BACKGROUND_COMPILATION) {
 315             return;
 316         }
 317         final Object obj = new Object();
 318         for (int i = 0; i < 10
 319                 && WHITE_BOX.isMethodQueuedForCompilation(executable); ++i) {
 320             synchronized (obj) {
 321                 try {
 322                     obj.wait(1000);
 323                 } catch (InterruptedException e) {
 324                     Thread.currentThread().interrupt();
 325                 }
 326             }
 327         }
 328     }
 329 
 330     /**
 331      * Prints information about {@linkplain #method}.
 332      */
 333     protected final void printInfo() {
 334         System.out.printf("%n%s:%n", method);
 335         System.out.printf("\tcompilable:\t%b%n",
 336                 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, false));
 337         boolean isCompiled = WHITE_BOX.isMethodCompiled(method, false);
 338         System.out.printf("\tcompiled:\t%b%n", isCompiled);
 339         if (isCompiled) {
 340             System.out.printf("\tcompile_id:\t%d%n",
 341                     NMethod.get(method, false).compile_id);
 342         }
 343         System.out.printf("\tcomp_level:\t%d%n",
 344                 WHITE_BOX.getMethodCompilationLevel(method, false));
 345         System.out.printf("\tosr_compilable:\t%b%n",
 346                 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, true));
 347         isCompiled = WHITE_BOX.isMethodCompiled(method, true);
 348         System.out.printf("\tosr_compiled:\t%b%n", isCompiled);
 349         if (isCompiled) {
 350             System.out.printf("\tosr_compile_id:\t%d%n",
 351                     NMethod.get(method, true).compile_id);
 352         }
 353         System.out.printf("\tosr_comp_level:\t%d%n",
 354                 WHITE_BOX.getMethodCompilationLevel(method, true));
 355         System.out.printf("\tin_queue:\t%b%n",
 356                 WHITE_BOX.isMethodQueuedForCompilation(method));
 357         System.out.printf("compile_queues_size:\t%d%n%n",
 358                 WHITE_BOX.getCompileQueuesSize());
 359     }
 360 
 361     /**
 362      * Executes testing.
 363      */
 364     protected abstract void test() throws Exception;
 365 
 366     /**
 367      * Tries to trigger compilation of {@linkplain #method} by call
 368      * {@linkplain TestCase#getCallable()} enough times.
 369      *
 370      * @return accumulated result
 371      * @see #compile(int)
 372      */
 373     protected final int compile() {
 374         if (testCase.isOsr()) {
 375             return compile(1);
 376         } else {
 377             return compile(THRESHOLD);
 378         }
 379     }
 380 
 381     /**
 382      * Tries to trigger compilation of {@linkplain #method} by call
 383      * {@linkplain TestCase#getCallable()} specified times.
 384      *
 385      * @param count invocation count
 386      * @return accumulated result
 387      */
 388     protected final int compile(int count) {
 389         int result = 0;
 390         Integer tmp;
 391         for (int i = 0; i < count; ++i) {
 392             try {
 393                 tmp = testCase.getCallable().call();
 394             } catch (Exception e) {
 395                 tmp = null;
 396             }
 397             result += tmp == null ? 0 : tmp;
 398         }
 399         if (IS_VERBOSE) {
 400             System.out.println("method was invoked " + count + " times");
 401         }
 402         return result;
 403     }
 404 
 405     /**
 406      * Utility interface provides tested method and object to invoke it.
 407      */
 408     public interface TestCase {
 409         /** the name of test case */
 410         String name();
 411 
 412         /** tested method */
 413         Executable getExecutable();
 414 
 415         /** object to invoke {@linkplain #getExecutable()} */
 416         Callable<Integer> getCallable();
 417 
 418         /** flag for OSR test case */
 419         boolean isOsr();
 420     }
 421 
 422     /**
 423      * @return {@code true} if the current test case is OSR and the mode is
 424      *          Xcomp, otherwise {@code false}
 425      */
 426     protected boolean skipXcompOSR() {
 427         boolean result =  testCase.isOsr()
 428                 && CompilerWhiteBoxTest.MODE.startsWith("compiled ");
 429         if (result && IS_VERBOSE) {
 430             System.err.printf("Warning: %s is not applicable in %s%n",
 431                     testCase.name(), CompilerWhiteBoxTest.MODE);
 432         }
 433         return result;
 434     }
 435 
 436     /**
 437      * Skip the test for the specified value of Tiered Compilation
 438      * @param value of TieredCompilation the test should not run with
 439      * @return {@code true} if the test should be skipped,
 440      *         {@code false} otherwise
 441      */
 442     public static boolean skipOnTieredCompilation(boolean value) {
 443         if (value == CompilerWhiteBoxTest.TIERED_COMPILATION) {
 444             System.err.println("Test isn't applicable w/ "
 445                     + (value ? "enabled" : "disabled")
 446                     + "TieredCompilation. Skip test.");
 447             return true;
 448         }
 449         return false;
 450     }
 451 }
 452