Path: blob/master/test/hotspot/jtreg/compiler/jvmci/compilerToVM/AllocateCompileIdTest.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.CompilerToVMHelper39* sun.hotspot.WhiteBox sun.hotspot.parser.DiagnosticCommand40* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox41* sun.hotspot.parser.DiagnosticCommand42* @run main/othervm -Xbootclasspath/a:.43* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI44* -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI45* -XX:-BackgroundCompilation46* compiler.jvmci.compilerToVM.AllocateCompileIdTest47*/4849package compiler.jvmci.compilerToVM;5051import compiler.jvmci.common.CTVMUtilities;52import jdk.test.lib.Asserts;53import jdk.test.lib.util.Pair;54import jdk.test.lib.Utils;55import jdk.vm.ci.hotspot.CompilerToVMHelper;56import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;57import sun.hotspot.code.NMethod;5859import java.lang.reflect.Executable;60import java.lang.reflect.Method;61import java.util.ArrayList;62import java.util.HashSet;63import java.util.List;64import java.util.stream.Collectors;65import java.util.stream.Stream;6667public class AllocateCompileIdTest {6869private static final int SOME_REPEAT_VALUE = 5;70private final HashSet<Integer> ids = new HashSet<>();7172public static void main(String[] args) {73AllocateCompileIdTest test = new AllocateCompileIdTest();74createTestCasesCorrectBci().forEach(test::runSanityCorrectTest);75createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest);76}7778private static List<CompileCodeTestCase> createTestCasesCorrectBci() {79List<CompileCodeTestCase> result = new ArrayList<>();80try {81Class<?> aClass = DummyClass.class;82Method method = aClass.getMethod("withLoop");83Object receiver = new DummyClass();84result.add(new CompileCodeTestCase(receiver, method, 17));85result.add(new CompileCodeTestCase(receiver, method, -1));86} catch (NoSuchMethodException e) {87throw new Error("TEST BUG : " + e, e);88}89return result;90}9192private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>>93createTestCasesIncorrectBci() {94List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result95= new ArrayList<>();96try {97Class<?> aClass = DummyClass.class;98Object receiver = new DummyClass();99Method method = aClass.getMethod("dummyInstanceFunction");100// greater than bytecode.length101byte[] bytecode = CompilerToVMHelper.getBytecode(CTVMUtilities102.getResolvedMethod(method));103Stream.of(104// greater than bytecode.length105bytecode.length + 4,106bytecode.length + 50,107bytecode.length + 200,108// negative cases109-4, -50, -200)110.map(bci -> new Pair<CompileCodeTestCase,111Class<? extends Throwable>>(112new CompileCodeTestCase(receiver, method, bci),113IllegalArgumentException.class))114.collect(Collectors.toList());115} catch (NoSuchMethodException e) {116throw new Error("TEST BUG : " + e.getMessage(), e);117}118return result;119}120121private void runSanityCorrectTest(CompileCodeTestCase testCase) {122System.out.println(testCase);123Executable aMethod = testCase.executable;124// to generate ciTypeFlow125testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes()));126int bci = testCase.bci;127HotSpotResolvedJavaMethod method = CTVMUtilities128.getResolvedMethod(aMethod);129for (int i = 0; i < SOME_REPEAT_VALUE; ++i) {130int wbCompileID = getWBCompileID(testCase);131int id = CompilerToVMHelper.allocateCompileId(method, bci);132Asserts.assertNE(id, 0, testCase + " : zero compile id");133Asserts.assertGT(id, wbCompileID, testCase134+ " : allocated 'compile id' not greater than existed");135Asserts.assertTrue(ids.add(wbCompileID), testCase136+ " : vm compilation allocated existing id " + id);137Asserts.assertTrue(ids.add(id), testCase138+ " : allocateCompileId returned existing id " + id);139}140}141142private void runSanityIncorrectTest(143Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) {144System.out.println(testCase);145Class<? extends Throwable> exception = testCase.second;146Executable aMethod = testCase.first.executable;147int bci = testCase.first.bci;148HotSpotResolvedJavaMethod method = CTVMUtilities149.getResolvedMethod(aMethod);150Utils.runAndCheckException(151() -> CompilerToVMHelper.allocateCompileId(method, bci),152exception);153}154155private int getWBCompileID(CompileCodeTestCase testCase) {156NMethod nm = testCase.deoptimizeAndCompile();157if (nm == null || nm.compile_id <= 0) {158throw new Error("TEST BUG : cannot compile method " + testCase);159}160return nm.compile_id;161}162}163164165