Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/HasCompiledCodeForOSRTest.java
41153 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key randomness26* @bug 813642127* @requires vm.jvmci28* @library /test/lib /29* @library ../common/patches30* @modules java.base/jdk.internal.misc31* @modules java.base/jdk.internal.org.objectweb.asm32* java.base/jdk.internal.org.objectweb.asm.tree33* jdk.internal.vm.ci/jdk.vm.ci.hotspot34* jdk.internal.vm.ci/jdk.vm.ci.code35* jdk.internal.vm.ci/jdk.vm.ci.meta36* jdk.internal.vm.ci/jdk.vm.ci.runtime37*38* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox39* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox40* @run main/othervm -Xbootclasspath/a:.41* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI42* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI43* -XX:-BackgroundCompilation44* compiler.jvmci.compilerToVM.HasCompiledCodeForOSRTest45*/4647package compiler.jvmci.compilerToVM;4849import compiler.jvmci.common.CTVMUtilities;50import compiler.testlibrary.CompilerUtils;51import jdk.test.lib.Asserts;52import jdk.test.lib.Utils;53import jdk.vm.ci.hotspot.CompilerToVMHelper;54import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;55import sun.hotspot.code.NMethod;5657import java.lang.reflect.Executable;58import java.util.ArrayList;59import java.util.List;6061public class HasCompiledCodeForOSRTest {62public static void main(String[] args) {63List<CompileCodeTestCase> testCases = createTestCases();64testCases.forEach(HasCompiledCodeForOSRTest::runSanityTest);65}6667public static List<CompileCodeTestCase> createTestCases() {68List<CompileCodeTestCase> testCases = new ArrayList<>();6970try {71Class<?> aClass = DummyClass.class;72Object receiver = new DummyClass();73testCases.add(new CompileCodeTestCase(receiver,74aClass.getMethod("withLoop"), 17));75} catch (NoSuchMethodException e) {76throw new Error("TEST BUG : " + e.getMessage(), e);77}78return testCases;79}8081private static void runSanityTest(CompileCodeTestCase testCase) {82System.out.println(testCase);83Executable aMethod = testCase.executable;84HotSpotResolvedJavaMethod method = CTVMUtilities85.getResolvedMethod(aMethod);86testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes()));87testCase.deoptimize();88int[] levels = CompilerUtils.getAvailableCompilationLevels();89// not compiled90for (int level : levels) {91boolean isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(92method, testCase.bci, level);93Asserts.assertFalse(isCompiled, String.format(94"%s : unexpected return value for non-compiled method at "95+ "level %d", testCase, level));96}97NMethod nm = testCase.compile();98if (nm == null) {99throw new Error(String.format(100"TEST BUG : %s : cannot compile method", testCase));101}102103boolean isCompiled;104int level = nm.comp_level;105int[] someLevels = new int[] {-4, 0, 1, 2, 3, 4, 5, 45};106// check levels107for (int i : someLevels) {108isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(109method, testCase.bci, i);110Asserts.assertEQ(isCompiled, level == i, String.format(111"%s : unexpected return value for compiled method at "112+ "level %d", testCase, i));113}114115// check bci116byte[] bytecode = CompilerToVMHelper.getBytecode(CTVMUtilities117.getResolvedMethod(testCase.executable));118int[] incorrectBci = new int[] {119testCase.bci + 1,120testCase.bci - 1,121-200,122-10,123bytecode.length,124bytecode.length + 1,125bytecode.length + 4,126bytecode.length + 200127};128for (int bci : incorrectBci) {129isCompiled = CompilerToVMHelper.hasCompiledCodeForOSR(130method, bci, level);131Asserts.assertFalse(isCompiled, String.format(132"%s : unexpected return value for compiled method at "133+ "level %d with bci = %d ",134testCase, level, bci));135}136}137}138139140