CompilerWhiteBoxTest.java 25.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

24 25
import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.VMOption;
26
import sun.hotspot.WhiteBox;
I
iignatyev 已提交
27
import sun.hotspot.code.NMethod;
28 29
import sun.management.ManagementFactoryHelper;

30 31
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
32
import java.lang.reflect.Method;
33 34
import java.util.Objects;
import java.util.concurrent.Callable;
35
import java.util.function.Function;
36

37 38 39
/**
 * Abstract class for WhiteBox testing of JIT.
 *
40 41 42
 * @author igor.ignatyev@oracle.com
 */
public abstract class CompilerWhiteBoxTest {
43
    /** {@code CompLevel::CompLevel_none} -- Interpreter */
44
    protected static final int COMP_LEVEL_NONE = 0;
45
    /** {@code CompLevel::CompLevel_any}, {@code CompLevel::CompLevel_all} */
46
    protected static final int COMP_LEVEL_ANY = -1;
47
    /** {@code CompLevel::CompLevel_simple} -- C1 */
48
    protected static final int COMP_LEVEL_SIMPLE = 1;
49
    /** {@code CompLevel::CompLevel_limited_profile} -- C1, invocation & backedge counters */
50
    protected static final int COMP_LEVEL_LIMITED_PROFILE = 2;
51
    /** {@code CompLevel::CompLevel_full_profile} -- C1, invocation & backedge counters + mdo */
52
    protected static final int COMP_LEVEL_FULL_PROFILE = 3;
53
    /** {@code CompLevel::CompLevel_full_optimization} -- C2 or Shark */
54
    protected static final int COMP_LEVEL_FULL_OPTIMIZATION = 4;
55
    /** Maximal value for CompLevel */
56
    protected static final int COMP_LEVEL_MAX = COMP_LEVEL_FULL_OPTIMIZATION;
57

58
    /** Instance of WhiteBox */
59
    protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
60
    /** Value of {@code -XX:CompileThreshold} */
61 62
    protected static final int COMPILE_THRESHOLD
            = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
63
    /** Value of {@code -XX:BackgroundCompilation} */
64 65
    protected static final boolean BACKGROUND_COMPILATION
            = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
66
    /** Value of {@code -XX:TieredCompilation} */
67 68
    protected static final boolean TIERED_COMPILATION
            = Boolean.valueOf(getVMOption("TieredCompilation", "false"));
69 70 71
    /** Value of {@code -XX:TieredStopAtLevel} */
    protected static final int TIERED_STOP_AT_LEVEL
            = Integer.parseInt(getVMOption("TieredStopAtLevel", "0"));
72 73 74
    /** Flag for verbose output, true if {@code -Dverbose} specified */
    protected static final boolean IS_VERBOSE
            = System.getProperty("verbose") != null;
75
    /** invocation count to trigger compilation */
76
    protected static final int THRESHOLD;
77
    /** invocation count to trigger OSR compilation */
78
    protected static final long BACKEDGE_THRESHOLD;
79
    /** Value of {@code java.vm.info} (interpreted|mixed|comp mode) */
80
    protected static final String MODE = System.getProperty("java.vm.info");
81 82 83

    static {
        if (TIERED_COMPILATION) {
84
            BACKEDGE_THRESHOLD = THRESHOLD = 150000;
85 86
        } else {
            THRESHOLD = COMPILE_THRESHOLD;
87 88
            BACKEDGE_THRESHOLD = Math.max(10000, COMPILE_THRESHOLD *
                    Long.parseLong(getVMOption("OnStackReplacePercentage")));
89 90
        }
    }
91

92 93 94 95 96 97 98
    /**
     * Returns value of VM option.
     *
     * @param name option's name
     * @return value of option or {@code null}, if option doesn't exist
     * @throws NullPointerException if name is null
     */
99
    protected static String getVMOption(String name) {
100
        Objects.requireNonNull(name);
101 102
        HotSpotDiagnosticMXBean diagnostic
                = ManagementFactoryHelper.getDiagnosticMXBean();
103 104 105 106 107 108 109
        VMOption tmp;
        try {
            tmp = diagnostic.getVMOption(name);
        } catch (IllegalArgumentException e) {
            tmp = null;
        }
        return (tmp == null ? null : tmp.getValue());
110 111
    }

112 113 114 115 116 117 118 119 120
    /**
     * Returns value of VM option or default value.
     *
     * @param name         option's name
     * @param defaultValue default value
     * @return value of option or {@code defaultValue}, if option doesn't exist
     * @throws NullPointerException if name is null
     * @see #getVMOption(String)
     */
121 122
    protected static String getVMOption(String name, String defaultValue) {
        String result = getVMOption(name);
123 124 125
        return result == null ? defaultValue : result;
    }

126 127 128 129 130 131 132 133 134 135 136
    /** copy of is_c1_compile(int) from utilities/globalDefinitions.hpp */
    protected static boolean isC1Compile(int compLevel) {
        return (compLevel > COMP_LEVEL_NONE)
                && (compLevel < COMP_LEVEL_FULL_OPTIMIZATION);
    }

    /** copy of is_c2_compile(int) from utilities/globalDefinitions.hpp */
    protected static boolean isC2Compile(int compLevel) {
        return compLevel == COMP_LEVEL_FULL_OPTIMIZATION;
    }

137 138 139 140 141 142 143 144 145 146 147 148 149 150
    protected static void main(
            Function<TestCase, CompilerWhiteBoxTest> constructor,
            String[] args) {
        if (args.length == 0) {
            for (TestCase test : SimpleTestCase.values()) {
                constructor.apply(test).runTest();
            }
        } else {
            for (String name : args) {
                constructor.apply(SimpleTestCase.valueOf(name)).runTest();
            }
        }
    }

151 152
    /** tested method */
    protected final Executable method;
153
    protected final TestCase testCase;
154 155 156 157 158 159 160 161 162

    /**
     * Constructor.
     *
     * @param testCase object, that contains tested method and way to invoke it.
     */
    protected CompilerWhiteBoxTest(TestCase testCase) {
        Objects.requireNonNull(testCase);
        System.out.println("TEST CASE:" + testCase.name());
163
        method = testCase.getExecutable();
164
        this.testCase = testCase;
165 166 167 168 169 170 171 172 173 174 175 176
    }

    /**
     * Template method for testing. Prints tested method's info before
     * {@linkplain #test()} and after {@linkplain #test()} or on thrown
     * exception.
     *
     * @throws RuntimeException if method {@linkplain #test()} throws any
     *                          exception
     * @see #test()
     */
    protected final void runTest() {
177 178 179 180 181 182
        if (ManagementFactoryHelper.getCompilationMXBean() == null) {
            System.err.println(
                    "Warning: test is not applicable in interpreted mode");
            return;
        }
        System.out.println("at test's start:");
183
        printInfo();
184 185 186 187
        try {
            test();
        } catch (Exception e) {
            System.out.printf("on exception '%s':", e.getMessage());
188
            printInfo();
189
            e.printStackTrace();
190 191 192
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            }
193 194 195
            throw new RuntimeException(e);
        }
        System.out.println("at test's end:");
196
        printInfo();
197 198
    }

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    /**
     * Checks, that {@linkplain #method} is not compiled at the given compilation
     * level or above.
     *
     * @param compLevel
     *
     * @throws RuntimeException if {@linkplain #method} is in compiler queue or
     *                          is compiled, or if {@linkplain #method} has zero
     *                          compilation level.
     */
    protected final void checkNotCompiled(int compLevel) {
        if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
            throw new RuntimeException(method + " must not be in queue");
        }
        if (WHITE_BOX.getMethodCompilationLevel(method, false) >= compLevel) {
            throw new RuntimeException(method + " comp_level must be >= maxCompLevel");
        }
        if (WHITE_BOX.getMethodCompilationLevel(method, true) >= compLevel) {
            throw new RuntimeException(method + " osr_comp_level must be >= maxCompLevel");
        }
    }

221 222 223 224 225 226 227 228
    /**
     * Checks, that {@linkplain #method} is not compiled.
     *
     * @throws RuntimeException if {@linkplain #method} is in compiler queue or
     *                          is compiled, or if {@linkplain #method} has zero
     *                          compilation level.
     */
    protected final void checkNotCompiled() {
229 230
        checkNotCompiled(true);
        checkNotCompiled(false);
231 232
    }

233 234 235 236 237 238 239 240 241 242
    /**
     * Checks, that {@linkplain #method} is not (OSR-)compiled.
     *
     * @param isOsr Check for OSR compilation if true
     * @throws RuntimeException if {@linkplain #method} is in compiler queue or
     *                          is compiled, or if {@linkplain #method} has zero
     *                          compilation level.
     */
    protected final void checkNotCompiled(boolean isOsr) {
        waitBackgroundCompilation();
243 244 245
        if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
            throw new RuntimeException(method + " must not be in queue");
        }
246 247 248
        if (WHITE_BOX.isMethodCompiled(method, isOsr)) {
            throw new RuntimeException(method + " must not be " +
                                       (isOsr ? "osr_" : "") + "compiled");
249
        }
250 251 252
        if (WHITE_BOX.getMethodCompilationLevel(method, isOsr) != 0) {
            throw new RuntimeException(method + (isOsr ? " osr_" : " ") +
                                       "comp_level must be == 0");
253
        }
254
    }
255

256 257 258 259 260 261 262 263
    /**
     * Checks, that {@linkplain #method} is compiled.
     *
     * @throws RuntimeException if {@linkplain #method} isn't in compiler queue
     *                          and isn't compiled, or if {@linkplain #method}
     *                          has nonzero compilation level
     */
    protected final void checkCompiled() {
264
        final long start = System.currentTimeMillis();
265
        waitBackgroundCompilation();
266 267 268 269 270
        if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
            System.err.printf("Warning: %s is still in queue after %dms%n",
                    method, System.currentTimeMillis() - start);
            return;
        }
271
        if (!WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) {
272
            throw new RuntimeException(method + " must be "
273
                    + (testCase.isOsr() ? "osr_" : "") + "compiled");
274
        }
275 276
        if (WHITE_BOX.getMethodCompilationLevel(method, testCase.isOsr())
                == 0) {
277
            throw new RuntimeException(method
278
                    + (testCase.isOsr() ? " osr_" : " ")
279 280 281 282 283
                    + "comp_level must be != 0");
        }
    }

    protected final void deoptimize() {
284 285
        WHITE_BOX.deoptimizeMethod(method, testCase.isOsr());
        if (testCase.isOsr()) {
286
            WHITE_BOX.deoptimizeMethod(method, false);
287 288 289
        }
    }

290
    protected final int getCompLevel() {
I
iignatyev 已提交
291 292
        NMethod nm = NMethod.get(method, testCase.isOsr());
        return nm == null ? COMP_LEVEL_NONE : nm.comp_level;
293 294 295 296
    }

    protected final boolean isCompilable() {
        return WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY,
297
                testCase.isOsr());
298 299 300
    }

    protected final boolean isCompilable(int compLevel) {
301 302
        return WHITE_BOX
                .isMethodCompilable(method, compLevel, testCase.isOsr());
303 304 305 306
    }

    protected final void makeNotCompilable() {
        WHITE_BOX.makeMethodNotCompilable(method, COMP_LEVEL_ANY,
307
                testCase.isOsr());
308 309 310
    }

    protected final void makeNotCompilable(int compLevel) {
311
        WHITE_BOX.makeMethodNotCompilable(method, compLevel, testCase.isOsr());
312 313
    }

314 315 316 317
    /**
     * Waits for completion of background compilation of {@linkplain #method}.
     */
    protected final void waitBackgroundCompilation() {
318 319 320 321 322 323 324 325 326
        waitBackgroundCompilation(method);
    }

    /**
     * Waits for completion of background compilation of the given executable.
     *
     * @param executable Executable
     */
    protected static final void waitBackgroundCompilation(Executable executable) {
327 328 329
        if (!BACKGROUND_COMPILATION) {
            return;
        }
330
        final Object obj = new Object();
331
        for (int i = 0; i < 10
332
                && WHITE_BOX.isMethodQueuedForCompilation(executable); ++i) {
333 334 335 336 337
            synchronized (obj) {
                try {
                    obj.wait(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
338 339 340 341 342
                }
            }
        }
    }

343 344 345 346
    /**
     * Prints information about {@linkplain #method}.
     */
    protected final void printInfo() {
347 348
        System.out.printf("%n%s:%n", method);
        System.out.printf("\tcompilable:\t%b%n",
349
                WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, false));
350 351 352 353 354 355
        boolean isCompiled = WHITE_BOX.isMethodCompiled(method, false);
        System.out.printf("\tcompiled:\t%b%n", isCompiled);
        if (isCompiled) {
            System.out.printf("\tcompile_id:\t%d%n",
                    NMethod.get(method, false).compile_id);
        }
356
        System.out.printf("\tcomp_level:\t%d%n",
357 358 359
                WHITE_BOX.getMethodCompilationLevel(method, false));
        System.out.printf("\tosr_compilable:\t%b%n",
                WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, true));
360 361 362 363 364 365
        isCompiled = WHITE_BOX.isMethodCompiled(method, true);
        System.out.printf("\tosr_compiled:\t%b%n", isCompiled);
        if (isCompiled) {
            System.out.printf("\tosr_compile_id:\t%d%n",
                    NMethod.get(method, true).compile_id);
        }
366 367
        System.out.printf("\tosr_comp_level:\t%d%n",
                WHITE_BOX.getMethodCompilationLevel(method, true));
368
        System.out.printf("\tin_queue:\t%b%n",
369 370 371 372 373
                WHITE_BOX.isMethodQueuedForCompilation(method));
        System.out.printf("compile_queues_size:\t%d%n%n",
                WHITE_BOX.getCompileQueuesSize());
    }

374 375 376
    /**
     * Executes testing.
     */
377 378
    protected abstract void test() throws Exception;

379 380
    /**
     * Tries to trigger compilation of {@linkplain #method} by call
381
     * {@linkplain TestCase#getCallable()} enough times.
382 383 384 385
     *
     * @return accumulated result
     * @see #compile(int)
     */
386
    protected final int compile() {
387
        if (testCase.isOsr()) {
388 389 390 391
            return compile(1);
        } else {
            return compile(THRESHOLD);
        }
392 393
    }

394 395
    /**
     * Tries to trigger compilation of {@linkplain #method} by call
396
     * {@linkplain TestCase#getCallable()} specified times.
397 398 399 400
     *
     * @param count invocation count
     * @return accumulated result
     */
401
    protected final int compile(int count) {
402
        int result = 0;
403
        Integer tmp;
404
        for (int i = 0; i < count; ++i) {
405
            try {
406
                tmp = testCase.getCallable().call();
407 408 409 410
            } catch (Exception e) {
                tmp = null;
            }
            result += tmp == null ? 0 : tmp;
411
        }
412 413 414
        if (IS_VERBOSE) {
            System.out.println("method was invoked " + count + " times");
        }
415 416
        return result;
    }
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

    /**
     * Utility interface provides tested method and object to invoke it.
     */
    public interface TestCase {
        /** the name of test case */
        String name();

        /** tested method */
        Executable getExecutable();

        /** object to invoke {@linkplain #getExecutable()} */
        Callable<Integer> getCallable();

        /** flag for OSR test case */
        boolean isOsr();
    }
434 435 436 437 438 439 440 441 442 443 444 445 446 447

    /**
     * @return {@code true} if the current test case is OSR and the mode is
     *          Xcomp, otherwise {@code false}
     */
    protected boolean skipXcompOSR() {
        boolean result =  testCase.isOsr()
                && CompilerWhiteBoxTest.MODE.startsWith("compiled ");
        if (result && IS_VERBOSE) {
            System.err.printf("Warning: %s is not applicable in %s%n",
                    testCase.name(), CompilerWhiteBoxTest.MODE);
        }
        return result;
    }
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

    /**
     * Skip the test for the specified value of Tiered Compilation
     * @param value of TieredCompilation the test should not run with
     * @return {@code true} if the test should be skipped,
     *         {@code false} otherwise
     */
    protected static boolean skipOnTieredCompilation(boolean value) {
        if (value == CompilerWhiteBoxTest.TIERED_COMPILATION) {
            System.err.println("Test isn't applicable w/ "
                    + (value ? "enabled" : "disabled")
                    + "TieredCompilation. Skip test.");
            return true;
        }
        return false;
    }
464
}
465

466
enum SimpleTestCase implements CompilerWhiteBoxTest.TestCase {
467
    /** constructor test case */
468
    CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE, false),
469
    /** method test case */
470
    METHOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE, false),
471
    /** static method test case */
472 473 474 475
    STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE, false),
    /** OSR constructor test case */
    OSR_CONSTRUCTOR_TEST(Helper.OSR_CONSTRUCTOR,
            Helper.OSR_CONSTRUCTOR_CALLABLE, true),
476
    /** OSR method test case */
477
    OSR_METHOD_TEST(Helper.OSR_METHOD, Helper.OSR_METHOD_CALLABLE, true),
478 479
    /** OSR static method test case */
    OSR_STATIC_TEST(Helper.OSR_STATIC, Helper.OSR_STATIC_CALLABLE, true);
480

481 482 483
    private final Executable executable;
    private final Callable<Integer> callable;
    private final boolean isOsr;
484

485
    private SimpleTestCase(Executable executable, Callable<Integer> callable,
486
            boolean isOsr) {
487 488
        this.executable = executable;
        this.callable = callable;
489
        this.isOsr = isOsr;
490 491
    }

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    @Override
    public Executable getExecutable() {
        return executable;
    }

    @Override
    public Callable<Integer> getCallable() {
        return callable;
    }

    @Override
    public boolean isOsr() {
        return isOsr;
    }

507
    private static class Helper {
508

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        private static final Callable<Integer> CONSTRUCTOR_CALLABLE
                = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return new Helper(1337).hashCode();
            }
        };

        private static final Callable<Integer> METHOD_CALLABLE
                = new Callable<Integer>() {
            private final Helper helper = new Helper();

            @Override
            public Integer call() throws Exception {
                return helper.method();
            }
        };

        private static final Callable<Integer> STATIC_CALLABLE
                = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return staticMethod();
            }
        };

535 536 537 538
        private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE
                = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
539
                return new Helper(null, CompilerWhiteBoxTest.BACKEDGE_THRESHOLD).hashCode();
540 541 542 543 544 545 546 547 548
            }
        };

        private static final Callable<Integer> OSR_METHOD_CALLABLE
                = new Callable<Integer>() {
            private final Helper helper = new Helper();

            @Override
            public Integer call() throws Exception {
549
                return helper.osrMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
550 551 552 553 554 555 556
            }
        };

        private static final Callable<Integer> OSR_STATIC_CALLABLE
                = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
557
                return osrStaticMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
558 559 560
            }
        };

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
        private static final Constructor CONSTRUCTOR;
        private static final Constructor OSR_CONSTRUCTOR;
        private static final Method METHOD;
        private static final Method STATIC;
        private static final Method OSR_METHOD;
        private static final Method OSR_STATIC;

        static {
            try {
                CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
            } catch (NoSuchMethodException | SecurityException e) {
                throw new RuntimeException(
                        "exception on getting method Helper.<init>(int)", e);
            }
            try {
                OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor(
                        Object.class, long.class);
            } catch (NoSuchMethodException | SecurityException e) {
                throw new RuntimeException(
                        "exception on getting method Helper.<init>(Object, long)", e);
            }
            METHOD = getMethod("method");
            STATIC = getMethod("staticMethod");
            OSR_METHOD = getMethod("osrMethod", long.class);
            OSR_STATIC = getMethod("osrStaticMethod", long.class);
        }

        private static Method getMethod(String name, Class<?>... parameterTypes) {
            try {
                return Helper.class.getDeclaredMethod(name, parameterTypes);
            } catch (NoSuchMethodException | SecurityException e) {
                throw new RuntimeException(
                        "exception on getting method Helper." + name, e);
            }
        }

        private static int staticMethod() {
            return 1138;
        }

        private int method() {
            return 42;
        }

605 606 607 608 609 610 611
        /**
         * Deoptimizes all non-osr versions of the given executable after
         * compilation finished.
         *
         * @param e Executable
         * @throws Exception
         */
612
        private static void waitAndDeoptimize(Executable e) {
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
            CompilerWhiteBoxTest.waitBackgroundCompilation(e);
            if (WhiteBox.getWhiteBox().isMethodQueuedForCompilation(e)) {
                throw new RuntimeException(e + " must not be in queue");
            }
            // Deoptimize non-osr versions of executable
            WhiteBox.getWhiteBox().deoptimizeMethod(e, false);
        }

        /**
         * Executes the method multiple times to make sure we have
         * enough profiling information before triggering an OSR
         * compilation. Otherwise the C2 compiler may add uncommon traps.
         *
         * @param m Method to be executed
         * @return Number of times the method was executed
         * @throws Exception
         */
        private static int warmup(Method m) throws Exception {
631
            waitAndDeoptimize(m);
632 633
            Helper helper = new Helper();
            int result = 0;
634
            for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) {
635 636
                result += (int)m.invoke(helper, 1);
            }
637 638 639
            // Wait to make sure OSR compilation is not blocked by
            // non-OSR compilation in the compile queue
            CompilerWhiteBoxTest.waitBackgroundCompilation(m);
640 641 642 643 644 645 646 647 648 649 650 651 652
            return result;
        }

        /**
         * Executes the constructor multiple times to make sure we
         * have enough profiling information before triggering an OSR
         * compilation. Otherwise the C2 compiler may add uncommon traps.
         *
         * @param c Constructor to be executed
         * @return Number of times the constructor was executed
         * @throws Exception
         */
        private static int warmup(Constructor c) throws Exception {
653
            waitAndDeoptimize(c);
654
            int result = 0;
655
            for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) {
656 657
                result += c.newInstance(null, 1).hashCode();
            }
658 659 660
            // Wait to make sure OSR compilation is not blocked by
            // non-OSR compilation in the compile queue
            CompilerWhiteBoxTest.waitBackgroundCompilation(c);
661 662 663
            return result;
        }

664 665
        private static int osrStaticMethod(long limit) throws Exception {
            int result = 0;
666
            if (limit != 1) {
667
                result = warmup(OSR_STATIC);
668 669
            }
            // Trigger osr compilation
670
            for (long i = 0; i < limit; ++i) {
671 672 673 674 675
                result += staticMethod();
            }
            return result;
        }

676 677
        private int osrMethod(long limit) throws Exception {
            int result = 0;
678
            if (limit != 1) {
679
                result = warmup(OSR_METHOD);
680 681
            }
            // Trigger osr compilation
682
            for (long i = 0; i < limit; ++i) {
683 684 685 686 687
                result += method();
            }
            return result;
        }

688 689
        private final int x;

690
        // for method and OSR method test case
691 692 693 694
        public Helper() {
            x = 0;
        }

695
        // for OSR constructor test case
696 697
        private Helper(Object o, long limit) throws Exception {
            int result = 0;
698
            if (limit != 1) {
699
                result = warmup(OSR_CONSTRUCTOR);
700 701
            }
            // Trigger osr compilation
702
            for (long i = 0; i < limit; ++i) {
703 704 705 706 707 708
                result += method();
            }
            x = result;
        }

        // for constructor test case
709 710 711 712 713 714 715 716
        private Helper(int x) {
            this.x = x;
        }

        @Override
        public int hashCode() {
            return x;
        }
717 718
    }
}