HasCompiledCodeForOSRTest.java revision 9287:40bd4478a362
1/*
2 * Copyright (c) 2015, 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
25/**
26 * @test
27 * @bug 8136421
28 * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
29 * @library /testlibrary /../../test/lib /
30 * @compile ../common/CompilerToVMHelper.java
31 * @build sun.hotspot.WhiteBox
32 * @run main ClassFileInstaller sun.hotspot.WhiteBox
33 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
34 *                              jdk.vm.ci.hotspot.CompilerToVMHelper
35 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
36 *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
37 *      -XX:-BackgroundCompilation
38 *      compiler.jvmci.compilerToVM.HasCompiledCodeForOSRTest
39 */
40
41package compiler.jvmci.compilerToVM;
42
43import compiler.jvmci.common.CTVMUtilities;
44
45import java.lang.reflect.Executable;
46import java.util.ArrayList;
47import java.util.List;
48
49import compiler.testlibrary.CompilerUtils;
50import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
51import jdk.vm.ci.hotspot.CompilerToVMHelper;
52import jdk.test.lib.Asserts;
53import jdk.test.lib.Utils;
54import sun.hotspot.code.NMethod;
55
56public class HasCompiledCodeForOSRTest {
57    public static void main(String[] args) {
58        List<CompileCodeTestCase> testCases = createTestCases();
59        testCases.forEach(HasCompiledCodeForOSRTest::runSanityTest);
60    }
61
62    public static List<CompileCodeTestCase> createTestCases() {
63        List<CompileCodeTestCase> testCases = new ArrayList<>();
64
65        try {
66            Class<?> aClass = DummyClass.class;
67            Object receiver = new DummyClass();
68            testCases.add(new CompileCodeTestCase(receiver,
69                    aClass.getMethod("withLoop"), 17));
70        } catch (NoSuchMethodException e) {
71            throw new Error("TEST BUG : " + e.getMessage(), e);
72        }
73        return testCases;
74    }
75
76    private static void runSanityTest(CompileCodeTestCase testCase) {
77        System.out.println(testCase);
78        Executable aMethod = testCase.executable;
79        HotSpotResolvedJavaMethod method = CTVMUtilities
80                .getResolvedMethod(aMethod);
81        testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes()));
82        testCase.deoptimize();
83        int[] levels = CompilerUtils.getAvailableCompilationLevels();
84        // not compiled
85        for (int level : levels) {
86            boolean isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
87                    method, testCase.bci, level);
88            Asserts.assertFalse(isCompiled, String.format(
89                    "%s : unexpected return value for non-compiled method at "
90                            + "level %d", testCase, level));
91        }
92        NMethod nm = testCase.compile();
93        if (nm == null) {
94            throw new Error(String.format(
95                    "TEST BUG : %s : cannot compile method", testCase));
96        }
97
98        boolean isCompiled;
99        int level = nm.comp_level;
100        int[] someLevels = new int[] {-4, 0, 1, 2, 3, 4, 5, 45};
101        // check levels
102        for (int i : someLevels) {
103            isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
104                    method, testCase.bci, i);
105            Asserts.assertEQ(isCompiled, level == i, String.format(
106                    "%s : unexpected return value for compiled method at "
107                            + "level %d", testCase, i));
108        }
109
110        // check bci
111        byte[] bytecode = CompilerToVMHelper.getBytecode(CTVMUtilities
112                .getResolvedMethod(testCase.executable));
113        int[] incorrectBci = new int[] {
114                testCase.bci + 1,
115                testCase.bci - 1,
116                -200,
117                -10,
118                bytecode.length,
119                bytecode.length + 1,
120                bytecode.length + 4,
121                bytecode.length + 200
122        };
123        for (int bci : incorrectBci) {
124            isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(
125                    method, bci, level);
126            Asserts.assertFalse(isCompiled, String.format(
127                    "%s : unexpected return value for compiled method at "
128                            + "level %d with bci = %d ",
129                    testCase, level, bci));
130        }
131    }
132}
133