|
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 package compiler.calls.common; |
|
25 |
|
26 import compiler.testlibrary.CompilerUtils; |
|
27 import java.lang.reflect.Method; |
|
28 import java.util.Arrays; |
|
29 import jdk.test.lib.Asserts; |
|
30 import sun.hotspot.WhiteBox; |
|
31 |
|
32 /** |
|
33 * A common class for Invoke* classes |
|
34 */ |
|
35 public abstract class CallsBase { |
|
36 public static final String CALL_ERR_MSG = "Call insuccessfull"; |
|
37 protected final Method calleeMethod; |
|
38 protected final Method callerMethod; |
|
39 protected final WhiteBox wb = WhiteBox.getWhiteBox(); |
|
40 protected int compileCallee = -1; |
|
41 protected int compileCaller = -1; |
|
42 protected boolean nativeCallee = false; |
|
43 protected boolean nativeCaller = false; |
|
44 protected boolean calleeVisited = false; |
|
45 protected boolean checkCallerCompilationLevel; |
|
46 protected boolean checkCalleeCompilationLevel; |
|
47 protected int expectedCallerCompilationLevel; |
|
48 protected int expectedCalleeCompilationLevel; |
|
49 |
|
50 protected CallsBase() { |
|
51 try { |
|
52 callerMethod = getClass().getDeclaredMethod("caller"); |
|
53 calleeMethod = getClass().getDeclaredMethod("callee", |
|
54 getCalleeParametersTypes()); |
|
55 wb.testSetDontInlineMethod(callerMethod, /* dontinline= */ true); |
|
56 wb.testSetDontInlineMethod(calleeMethod, /* dontinline= */ true); |
|
57 } catch (NoSuchMethodException e) { |
|
58 throw new Error("TEST BUG: can't find test method", e); |
|
59 } |
|
60 } |
|
61 |
|
62 /** |
|
63 * Provides callee parameters types to search method |
|
64 * @return array of types |
|
65 */ |
|
66 protected Class[] getCalleeParametersTypes() { |
|
67 return new Class[] {int.class, long.class, float.class, |
|
68 double.class, String.class}; |
|
69 } |
|
70 |
|
71 /** |
|
72 * Loads native library(libCallsNative.so) |
|
73 */ |
|
74 protected static void loadNativeLibrary() { |
|
75 System.loadLibrary("CallsNative"); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Checks if requested compilation levels are inside of current vm capabilities |
|
80 * @return true if vm is capable of requested compilation levels |
|
81 */ |
|
82 protected final boolean compilationLevelsSupported() { |
|
83 int[] compLevels = CompilerUtils.getAvailableCompilationLevels(); |
|
84 boolean callerCompLevelSupported = compileCaller > 0 |
|
85 && Arrays.stream(compLevels) |
|
86 .filter(elem -> elem == compileCaller) |
|
87 .findAny() |
|
88 .isPresent(); |
|
89 boolean calleeCompLevelSupported = compileCallee > 0 |
|
90 && Arrays.stream(compLevels) |
|
91 .filter(elem -> elem == compileCallee) |
|
92 .findAny() |
|
93 .isPresent(); |
|
94 return callerCompLevelSupported && calleeCompLevelSupported; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Parse test arguments |
|
99 * @param args test arguments |
|
100 */ |
|
101 protected final void parseArgs(String args[]) { |
|
102 for (int i = 0; i < args.length; i++) { |
|
103 switch (args[i]) { |
|
104 case "-nativeCallee": |
|
105 nativeCallee = true; |
|
106 break; |
|
107 case "-nativeCaller": |
|
108 nativeCaller = true; |
|
109 break; |
|
110 case "-compileCallee": |
|
111 compileCallee = Integer.parseInt(args[++i]); |
|
112 break; |
|
113 case "-compileCaller": |
|
114 compileCaller = Integer.parseInt(args[++i]); |
|
115 break; |
|
116 case "-checkCallerCompileLevel": |
|
117 checkCallerCompilationLevel = true; |
|
118 expectedCallerCompilationLevel = Integer.parseInt(args[++i]); |
|
119 break; |
|
120 case "-checkCalleeCompileLevel": |
|
121 checkCalleeCompilationLevel = true; |
|
122 expectedCalleeCompilationLevel = Integer.parseInt(args[++i]); |
|
123 break; |
|
124 default: |
|
125 throw new Error("Can't parse test parameter:" + args[i]); |
|
126 } |
|
127 } |
|
128 } |
|
129 |
|
130 /** |
|
131 * Run basic logic of a test by doing compile |
|
132 * action(if needed). An arguments can be -compileCallee |
|
133 * $calleeCompilationLevel and/or -compileCaller $callerCompilationLevel |
|
134 * and/or -nativeCaller and/or -nativeCallee to indicate that native methods |
|
135 * for caller/callee should be used |
|
136 * @param args test args |
|
137 */ |
|
138 protected final void runTest(String args[]) { |
|
139 parseArgs(args); |
|
140 if (compilationLevelsSupported()) { |
|
141 if (nativeCaller || nativeCallee) { |
|
142 CallsBase.loadNativeLibrary(); |
|
143 } |
|
144 Object lock = getLockObject(); |
|
145 Asserts.assertNotNull(lock, "Lock object is null"); |
|
146 /* a following lock is needed in case several instances of this |
|
147 test are launched in same vm */ |
|
148 synchronized (lock) { |
|
149 if (compileCaller > 0 || compileCallee > 0) { |
|
150 caller(); // call once to have everything loaded |
|
151 calleeVisited = false; // reset state |
|
152 } |
|
153 // compile with requested level if needed |
|
154 if (compileCallee > 0) { |
|
155 compileMethod(calleeMethod, compileCallee); |
|
156 } |
|
157 if (checkCalleeCompilationLevel) { |
|
158 Asserts.assertEQ(expectedCalleeCompilationLevel, |
|
159 wb.getMethodCompilationLevel(calleeMethod), |
|
160 "Unexpected callee compilation level"); |
|
161 } |
|
162 if (compileCaller > 0) { |
|
163 compileMethod(callerMethod, compileCaller); |
|
164 } |
|
165 if (checkCallerCompilationLevel) { |
|
166 Asserts.assertEQ(expectedCallerCompilationLevel, |
|
167 wb.getMethodCompilationLevel(callerMethod), |
|
168 "Unexpected caller compilation level"); |
|
169 } |
|
170 // do calling work |
|
171 if (nativeCaller) { |
|
172 callerNative(); |
|
173 } else { |
|
174 caller(); |
|
175 } |
|
176 } |
|
177 } else { |
|
178 System.out.println("WARNING: Requested compilation levels are " |
|
179 + "out of current vm capabilities. Skipping."); |
|
180 } |
|
181 } |
|
182 |
|
183 /** |
|
184 * A method to compile another method, searching it by name in current class |
|
185 * @param method a method to compile |
|
186 * @param compLevel a compilation level |
|
187 */ |
|
188 protected final void compileMethod(Method method, int compLevel) { |
|
189 wb.deoptimizeMethod(method); |
|
190 Asserts.assertTrue(wb.isMethodCompilable(method, compLevel)); |
|
191 wb.enqueueMethodForCompilation(method, compLevel); |
|
192 } |
|
193 |
|
194 /* |
|
195 * @return Object to lock on during execution |
|
196 */ |
|
197 |
|
198 protected abstract Object getLockObject(); |
|
199 |
|
200 protected abstract void caller(); |
|
201 |
|
202 protected abstract void callerNative(); |
|
203 |
|
204 /** |
|
205 * A method checking values. Should be used to verify if all parameters are |
|
206 * passed as expected. Parameter N should have a value indicating number "N" |
|
207 * in respective type representation. |
|
208 */ |
|
209 public static void checkValues(int param1, long param2, float param3, |
|
210 double param4, String param5) { |
|
211 Asserts.assertEQ(param1, 1); |
|
212 Asserts.assertEQ(param2, 2L); |
|
213 Asserts.assertEQ(param3, 3.0f); |
|
214 Asserts.assertEQ(param4, 4.0d); |
|
215 Asserts.assertEQ(param5, "5"); |
|
216 } |
|
217 } |