32 * sun.hotspot.WhiteBox$WhiteBoxPermission |
32 * sun.hotspot.WhiteBox$WhiteBoxPermission |
33 * jdk.vm.ci.hotspot.CompilerToVMHelper |
33 * jdk.vm.ci.hotspot.CompilerToVMHelper |
34 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI |
34 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI |
35 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. |
35 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. |
36 * -XX:-BackgroundCompilation |
36 * -XX:-BackgroundCompilation |
37 -XX:+LogCompilation |
|
38 * compiler.jvmci.compilerToVM.AllocateCompileIdTest |
37 * compiler.jvmci.compilerToVM.AllocateCompileIdTest |
39 */ |
38 */ |
40 |
39 |
41 package compiler.jvmci.compilerToVM; |
40 package compiler.jvmci.compilerToVM; |
42 |
41 |
43 import compiler.jvmci.common.CTVMUtilities; |
42 import compiler.jvmci.common.CTVMUtilities; |
44 |
43 |
45 import java.lang.reflect.Executable; |
44 import java.lang.reflect.Executable; |
46 import java.lang.reflect.Method; |
45 import java.lang.reflect.Method; |
47 import java.util.ArrayList; |
46 import java.util.ArrayList; |
48 import java.util.HashMap; |
|
49 import java.util.List; |
47 import java.util.List; |
50 import java.util.Map; |
|
51 import java.util.HashSet; |
48 import java.util.HashSet; |
|
49 import java.util.stream.Collectors; |
|
50 import java.util.stream.Stream; |
52 |
51 |
53 import compiler.jvmci.common.testcases.TestCase; |
|
54 import jdk.vm.ci.hotspot.CompilerToVMHelper; |
52 import jdk.vm.ci.hotspot.CompilerToVMHelper; |
55 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; |
53 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; |
56 import jdk.test.lib.Asserts; |
54 import jdk.test.lib.Asserts; |
57 import jdk.test.lib.Pair; |
55 import jdk.test.lib.Pair; |
58 import jdk.test.lib.Utils; |
56 import jdk.test.lib.Utils; |
59 import sun.hotspot.WhiteBox; |
|
60 import sun.hotspot.code.NMethod; |
57 import sun.hotspot.code.NMethod; |
61 |
58 |
62 public class AllocateCompileIdTest { |
59 public class AllocateCompileIdTest { |
63 |
60 |
|
61 private static final int SOME_REPEAT_VALUE = 5; |
64 private final HashSet<Integer> ids = new HashSet<>(); |
62 private final HashSet<Integer> ids = new HashSet<>(); |
65 |
63 |
66 public static void main(String[] args) { |
64 public static void main(String[] args) { |
67 AllocateCompileIdTest test = new AllocateCompileIdTest(); |
65 AllocateCompileIdTest test = new AllocateCompileIdTest(); |
68 createTestCasesCorrectBci().forEach(test::runSanityCorrectTest); |
66 createTestCasesCorrectBci().forEach(test::runSanityCorrectTest); |
69 createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest); |
67 createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest); |
70 } |
68 } |
71 |
|
72 |
69 |
73 private static List<CompileCodeTestCase> createTestCasesCorrectBci() { |
70 private static List<CompileCodeTestCase> createTestCasesCorrectBci() { |
74 List<CompileCodeTestCase> result = new ArrayList<>(); |
71 List<CompileCodeTestCase> result = new ArrayList<>(); |
75 try { |
72 try { |
76 Class<?> aClass = DummyClass.class; |
73 Class<?> aClass = DummyClass.class; |
82 throw new Error("TEST BUG : " + e, e); |
79 throw new Error("TEST BUG : " + e, e); |
83 } |
80 } |
84 return result; |
81 return result; |
85 } |
82 } |
86 |
83 |
87 |
|
88 private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> |
84 private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> |
89 createTestCasesIncorrectBci() { |
85 createTestCasesIncorrectBci() { |
90 List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result |
86 List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result |
91 = new ArrayList<>(); |
87 = new ArrayList<>(); |
92 |
|
93 try { |
88 try { |
94 Class<?> aClass = DummyClass.class; |
89 Class<?> aClass = DummyClass.class; |
95 Object receiver = new DummyClass(); |
90 Object receiver = new DummyClass(); |
96 Method method = aClass.getMethod("dummyInstanceFunction"); |
91 Method method = aClass.getMethod("dummyInstanceFunction"); |
97 // greater than bytecode.length |
92 // greater than bytecode.length |
98 int[] bcis = new int[] {30, 50, 200}; |
93 byte[] bytecode = CompilerToVMHelper.getBytecode(CTVMUtilities |
99 for (int bci : bcis) { |
94 .getResolvedMethod(method)); |
100 result.add(new Pair<>( |
95 Stream.of( |
101 new CompileCodeTestCase(receiver, method, bci), |
96 // greater than bytecode.length |
102 IllegalArgumentException.class)); |
97 bytecode.length + 4, |
103 } |
98 bytecode.length + 50, |
104 bcis = new int[] {-4, -50, -200}; |
99 bytecode.length + 200, |
105 for (int bci : bcis) { |
100 // negative cases |
106 result.add(new Pair<>( |
101 -4, -50, -200) |
107 new CompileCodeTestCase(receiver, method, bci), |
102 .map(bci -> new Pair<CompileCodeTestCase, |
108 IllegalArgumentException.class)); |
103 Class<? extends Throwable>>( |
109 } |
104 new CompileCodeTestCase(receiver, method, bci), |
|
105 IllegalArgumentException.class)) |
|
106 .collect(Collectors.toList()); |
110 } catch (NoSuchMethodException e) { |
107 } catch (NoSuchMethodException e) { |
111 throw new Error("TEST BUG : " + e.getMessage(), e); |
108 throw new Error("TEST BUG : " + e.getMessage(), e); |
112 } |
109 } |
113 return result; |
110 return result; |
114 } |
111 } |
115 |
112 |
116 private void runSanityCorrectTest(CompileCodeTestCase testCase) { |
113 private void runSanityCorrectTest(CompileCodeTestCase testCase) { |
117 System.out.println(testCase); |
114 System.out.println(testCase); |
118 Executable aMethod = testCase.executable; |
115 Executable aMethod = testCase.executable; |
119 // to generate ciTypeFlow |
116 // to generate ciTypeFlow |
120 System.out.println(testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes()))); |
117 testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes())); |
121 int bci = testCase.bci; |
118 int bci = testCase.bci; |
122 HotSpotResolvedJavaMethod method = CTVMUtilities |
119 HotSpotResolvedJavaMethod method = CTVMUtilities |
123 .getResolvedMethod(aMethod); |
120 .getResolvedMethod(aMethod); |
124 int wbCompileID = getWBCompileID(testCase); |
121 for (int i = 0; i < SOME_REPEAT_VALUE; ++i) { |
125 int id = CompilerToVMHelper.allocateCompileId(method, bci); |
122 int wbCompileID = getWBCompileID(testCase); |
126 Asserts.assertNE(id, 0, testCase + " : zero compile id"); |
123 int id = CompilerToVMHelper.allocateCompileId(method, bci); |
127 |
124 Asserts.assertNE(id, 0, testCase + " : zero compile id"); |
128 if (wbCompileID > 0) { |
|
129 Asserts.assertGT(id, wbCompileID, testCase |
125 Asserts.assertGT(id, wbCompileID, testCase |
130 + " : allocated 'compile id' not greater than existed"); |
126 + " : allocated 'compile id' not greater than existed"); |
131 if (!ids.add(wbCompileID)) { |
127 Asserts.assertTrue(ids.add(wbCompileID), testCase |
132 throw new AssertionError(String.format( |
128 + " : vm compilation allocated existing id " + id); |
133 "%s : vm compilation allocated existed id -- %d", |
129 Asserts.assertTrue(ids.add(id), testCase |
134 testCase, id)); |
130 + " : allocateCompileId returned existing id " + id); |
135 } |
|
136 } |
|
137 if (!ids.add(id)) { |
|
138 throw new AssertionError(String.format( |
|
139 "%s : allocateCompileId returned existed id %d", |
|
140 testCase, id)); |
|
141 } |
131 } |
142 } |
132 } |
143 |
133 |
144 private void runSanityIncorrectTest( |
134 private void runSanityIncorrectTest( |
145 Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) { |
135 Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) { |