1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package compiler.calls.common;
import compiler.testlibrary.CompilerUtils;
import jdk.test.lib.Asserts;
import sun.hotspot.WhiteBox;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* A common class for Invoke* classes
*/
public abstract class CallsBase {
public static final String CALL_ERR_MSG = "Call insuccessfull";
protected final Method calleeMethod;
protected final Method callerMethod;
protected final WhiteBox wb = WhiteBox.getWhiteBox();
protected int compileCallee = -1;
protected int compileCaller = -1;
protected boolean nativeCallee = false;
protected boolean nativeCaller = false;
protected boolean calleeVisited = false;
protected boolean checkCallerCompilationLevel;
protected boolean checkCalleeCompilationLevel;
protected int expectedCallerCompilationLevel;
protected int expectedCalleeCompilationLevel;
protected CallsBase() {
try {
callerMethod = getClass().getDeclaredMethod("caller");
calleeMethod = getClass().getDeclaredMethod("callee",
getCalleeParametersTypes());
wb.testSetDontInlineMethod(callerMethod, /* dontinline= */ true);
wb.testSetDontInlineMethod(calleeMethod, /* dontinline= */ true);
} catch (NoSuchMethodException e) {
throw new Error("TEST BUG: can't find test method", e);
}
}
/**
* Provides callee parameters types to search method
* @return array of types
*/
protected Class[] getCalleeParametersTypes() {
return new Class[] {int.class, long.class, float.class,
double.class, String.class};
}
/**
* Loads native library(libCallsNative.so)
*/
protected static void loadNativeLibrary() {
System.loadLibrary("CallsNative");
}
/**
* Checks if requested compilation levels are inside of current vm capabilities
* @return true if vm is capable of requested compilation levels
*/
protected final boolean compilationLevelsSupported() {
int[] compLevels = CompilerUtils.getAvailableCompilationLevels();
boolean callerCompLevelSupported = compileCaller <= 0 || (compileCaller > 0
&& Arrays.stream(compLevels)
.filter(elem -> elem == compileCaller)
.findAny()
.isPresent());
boolean calleeCompLevelSupported = compileCallee <= 0 || (compileCallee > 0
&& Arrays.stream(compLevels)
.filter(elem -> elem == compileCallee)
.findAny()
.isPresent());
return callerCompLevelSupported && calleeCompLevelSupported;
}
/**
* Parse test arguments
* @param args test arguments
*/
protected final void parseArgs(String args[]) {
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-nativeCallee":
nativeCallee = true;
break;
case "-nativeCaller":
nativeCaller = true;
break;
case "-compileCallee":
compileCallee = Integer.parseInt(args[++i]);
break;
case "-compileCaller":
compileCaller = Integer.parseInt(args[++i]);
break;
case "-checkCallerCompileLevel":
checkCallerCompilationLevel = true;
expectedCallerCompilationLevel = Integer.parseInt(args[++i]);
break;
case "-checkCalleeCompileLevel":
checkCalleeCompilationLevel = true;
expectedCalleeCompilationLevel = Integer.parseInt(args[++i]);
break;
default:
throw new Error("Can't parse test parameter:" + args[i]);
}
}
}
/**
* Run basic logic of a test by doing compile
* action(if needed). An arguments can be -compileCallee
* $calleeCompilationLevel and/or -compileCaller $callerCompilationLevel
* and/or -nativeCaller and/or -nativeCallee to indicate that native methods
* for caller/callee should be used
* @param args test args
*/
protected final void runTest(String args[]) {
parseArgs(args);
if (compilationLevelsSupported()) {
if (nativeCaller || nativeCallee) {
CallsBase.loadNativeLibrary();
}
Object lock = getLockObject();
Asserts.assertNotNull(lock, "Lock object is null");
/* a following lock is needed in case several instances of this
test are launched in same vm */
synchronized (lock) {
if (compileCaller > 0 || compileCallee > 0) {
caller(); // call once to have everything loaded
calleeVisited = false; // reset state
}
// compile with requested level if needed
if (compileCallee > 0 && !compileMethod(calleeMethod, compileCallee)) {
System.out.println("WARNING: Blocking compilation failed for calleeMethod (timeout?). Skipping.");
return;
}
if (checkCalleeCompilationLevel) {
Asserts.assertEQ(expectedCalleeCompilationLevel,
wb.getMethodCompilationLevel(calleeMethod),
"Unexpected callee compilation level");
}
if (compileCaller > 0 && !compileMethod(callerMethod, compileCaller)) {
System.out.println("WARNING: Blocking compilation failed for callerMethod (timeout?). Skipping.");
return;
}
if (checkCallerCompilationLevel) {
Asserts.assertEQ(expectedCallerCompilationLevel,
wb.getMethodCompilationLevel(callerMethod),
"Unexpected caller compilation level");
}
// do calling work
if (nativeCaller) {
callerNative();
} else {
caller();
}
}
} else {
System.out.println("WARNING: Requested compilation levels are "
+ "out of current vm capabilities. Skipping.");
}
}
/**
* A method to compile another method, searching it by name in current class
* @param method a method to compile
* @param compLevel a compilation level
* @return true if method was enqueued for compilation
*/
protected final boolean compileMethod(Method method, int compLevel) {
wb.deoptimizeMethod(method);
Asserts.assertTrue(wb.isMethodCompilable(method, compLevel));
return wb.enqueueMethodForCompilation(method, compLevel);
}
/*
* @return Object to lock on during execution
*/
protected abstract Object getLockObject();
protected abstract void caller();
protected abstract void callerNative();
/**
* A method checking values. Should be used to verify if all parameters are
* passed as expected. Parameter N should have a value indicating number "N"
* in respective type representation.
*/
public static void checkValues(int param1, long param2, float param3,
double param4, String param5) {
Asserts.assertEQ(param1, 1);
Asserts.assertEQ(param2, 2L);
Asserts.assertEQ(param3, 3.0f);
Asserts.assertEQ(param4, 4.0d);
Asserts.assertEQ(param5, "5");
}
}
|