AllocateCompileIdTest.java revision 12651:6ef01bd40ce2
1162922Sariff/* 2162922Sariff * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3162922Sariff * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4162922Sariff * 5162922Sariff * This code is free software; you can redistribute it and/or modify it 6162922Sariff * under the terms of the GNU General Public License version 2 only, as 7162922Sariff * published by the Free Software Foundation. 8162922Sariff * 9162922Sariff * This code is distributed in the hope that it will be useful, but WITHOUT 10162922Sariff * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11162922Sariff * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12162922Sariff * version 2 for more details (a copy is included in the LICENSE file that 13162922Sariff * accompanied this code). 14162922Sariff * 15162922Sariff * You should have received a copy of the GNU General Public License version 16162922Sariff * 2 along with this work; if not, write to the Free Software Foundation, 17162922Sariff * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18162922Sariff * 19162922Sariff * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20162922Sariff * or visit www.oracle.com if you need additional information or have any 21162922Sariff * questions. 22162922Sariff */ 23162922Sariff 24162922Sariff/** 25162922Sariff * @test 26162922Sariff * @bug 8136421 27162922Sariff * @requires vm.jvmci 28162922Sariff * @library /test/lib / 29162922Sariff * @library ../common/patches 30162922Sariff * @modules java.base/jdk.internal.misc 31162922Sariff * @modules java.base/jdk.internal.org.objectweb.asm 32162922Sariff * java.base/jdk.internal.org.objectweb.asm.tree 33162922Sariff * jdk.internal.vm.ci/jdk.vm.ci.hotspot 34162922Sariff * jdk.internal.vm.ci/jdk.vm.ci.code 35162922Sariff * 36162922Sariff * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox 37162922Sariff * @run driver ClassFileInstaller sun.hotspot.WhiteBox 38162922Sariff * sun.hotspot.WhiteBox$WhiteBoxPermission 39162922Sariff * @run main/othervm -Xbootclasspath/a:. 40162922Sariff * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 41162922Sariff * -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 42162922Sariff * -XX:-BackgroundCompilation 43162922Sariff * compiler.jvmci.compilerToVM.AllocateCompileIdTest 44162922Sariff */ 45162922Sariff 46162922Sariffpackage compiler.jvmci.compilerToVM; 47164614Sariff 48162922Sariffimport compiler.jvmci.common.CTVMUtilities; 49162922Sariffimport jdk.test.lib.Asserts; 50162922Sariffimport jdk.test.lib.util.Pair; 51162922Sariffimport jdk.test.lib.Utils; 52162922Sariffimport jdk.vm.ci.hotspot.CompilerToVMHelper; 53162922Sariffimport jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 54162922Sariffimport sun.hotspot.code.NMethod; 55162922Sariff 56162922Sariffimport java.lang.reflect.Executable; 57162922Sariffimport java.lang.reflect.Method; 58162922Sariffimport java.util.ArrayList; 59162922Sariffimport java.util.HashSet; 60162922Sariffimport java.util.List; 61162922Sariffimport java.util.stream.Collectors; 62162922Sariffimport java.util.stream.Stream; 63162922Sariff 64162922Sariffpublic class AllocateCompileIdTest { 65162922Sariff 66162922Sariff private static final int SOME_REPEAT_VALUE = 5; 67162922Sariff private final HashSet<Integer> ids = new HashSet<>(); 68162922Sariff 69162922Sariff public static void main(String[] args) { 70163057Sariff AllocateCompileIdTest test = new AllocateCompileIdTest(); 71163057Sariff createTestCasesCorrectBci().forEach(test::runSanityCorrectTest); 72162922Sariff createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest); 73162922Sariff } 74162922Sariff 75162922Sariff private static List<CompileCodeTestCase> createTestCasesCorrectBci() { 76162922Sariff List<CompileCodeTestCase> result = new ArrayList<>(); 77162922Sariff try { 78162922Sariff Class<?> aClass = DummyClass.class; 79162922Sariff Method method = aClass.getMethod("withLoop"); 80162922Sariff Object receiver = new DummyClass(); 81162922Sariff result.add(new CompileCodeTestCase(receiver, method, 17)); 82162922Sariff result.add(new CompileCodeTestCase(receiver, method, -1)); 83165069Sariff } catch (NoSuchMethodException e) { 84162922Sariff throw new Error("TEST BUG : " + e, e); 85162922Sariff } 86162922Sariff return result; 87162922Sariff } 88162922Sariff 89162922Sariff private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> 90162922Sariff createTestCasesIncorrectBci() { 91162922Sariff List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result 92163057Sariff = new ArrayList<>(); 93163057Sariff try { 94162922Sariff Class<?> aClass = DummyClass.class; 95162922Sariff Object receiver = new DummyClass(); 96163057Sariff Method method = aClass.getMethod("dummyInstanceFunction"); 97162922Sariff // greater than bytecode.length 98162922Sariff byte[] bytecode = CompilerToVMHelper.getBytecode(CTVMUtilities 99163057Sariff .getResolvedMethod(method)); 100162922Sariff Stream.of( 101162922Sariff // greater than bytecode.length 102162922Sariff bytecode.length + 4, 103162922Sariff bytecode.length + 50, 104162922Sariff bytecode.length + 200, 105162965Sariff // negative cases 106162922Sariff -4, -50, -200) 107162922Sariff .map(bci -> new Pair<CompileCodeTestCase, 108162922Sariff Class<? extends Throwable>>( 109162922Sariff new CompileCodeTestCase(receiver, method, bci), 110162965Sariff IllegalArgumentException.class)) 111162965Sariff .collect(Collectors.toList()); 112163057Sariff } catch (NoSuchMethodException e) { 113163057Sariff throw new Error("TEST BUG : " + e.getMessage(), e); 114162922Sariff } 115162965Sariff return result; 116163257Sariff } 117163257Sariff 118163257Sariff private void runSanityCorrectTest(CompileCodeTestCase testCase) { 119163257Sariff System.out.println(testCase); 120163257Sariff Executable aMethod = testCase.executable; 121163257Sariff // to generate ciTypeFlow 122162965Sariff testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes())); 123162965Sariff int bci = testCase.bci; 124162965Sariff HotSpotResolvedJavaMethod method = CTVMUtilities 125162922Sariff .getResolvedMethod(aMethod); 126162922Sariff for (int i = 0; i < SOME_REPEAT_VALUE; ++i) { 127162922Sariff int wbCompileID = getWBCompileID(testCase); 128162922Sariff int id = CompilerToVMHelper.allocateCompileId(method, bci); 129162922Sariff Asserts.assertNE(id, 0, testCase + " : zero compile id"); 130162922Sariff Asserts.assertGT(id, wbCompileID, testCase 131162922Sariff + " : allocated 'compile id' not greater than existed"); 132162922Sariff Asserts.assertTrue(ids.add(wbCompileID), testCase 133162922Sariff + " : vm compilation allocated existing id " + id); 134163136Sariff Asserts.assertTrue(ids.add(id), testCase 135163136Sariff + " : allocateCompileId returned existing id " + id); 136162922Sariff } 137162922Sariff } 138162922Sariff 139162922Sariff private void runSanityIncorrectTest( 140162922Sariff Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) { 141162922Sariff System.out.println(testCase); 142163136Sariff Class<? extends Throwable> exception = testCase.second; 143163136Sariff Executable aMethod = testCase.first.executable; 144163136Sariff int bci = testCase.first.bci; 145163136Sariff HotSpotResolvedJavaMethod method = CTVMUtilities 146162922Sariff .getResolvedMethod(aMethod); 147162922Sariff Utils.runAndCheckException( 148162922Sariff () -> CompilerToVMHelper.allocateCompileId(method, bci), 149162922Sariff exception); 150162922Sariff } 151163136Sariff 152162922Sariff private int getWBCompileID(CompileCodeTestCase testCase) { 153162922Sariff NMethod nm = testCase.deoptimizeAndCompile(); 154163136Sariff if (nm == null || nm.compile_id <= 0) { 155163136Sariff throw new Error("TEST BUG : cannot compile method " + testCase); 156163136Sariff } 157163136Sariff return nm.compile_id; 158163136Sariff } 159163136Sariff} 160163136Sariff