Path: blob/master/test/hotspot/jtreg/compiler/calls/common/CallsBase.java
41153 views
/*1* Copyright (c) 2015, 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*/2223package compiler.calls.common;2425import compiler.testlibrary.CompilerUtils;26import jdk.test.lib.Asserts;27import sun.hotspot.WhiteBox;2829import java.lang.reflect.Method;30import java.util.Arrays;3132/**33* A common class for Invoke* classes34*/35public abstract class CallsBase {36public static final String CALL_ERR_MSG = "Call insuccessfull";37protected final Method calleeMethod;38protected final Method callerMethod;39protected final WhiteBox wb = WhiteBox.getWhiteBox();40protected int compileCallee = -1;41protected int compileCaller = -1;42protected boolean nativeCallee = false;43protected boolean nativeCaller = false;44protected boolean calleeVisited = false;45protected boolean checkCallerCompilationLevel;46protected boolean checkCalleeCompilationLevel;47protected int expectedCallerCompilationLevel;48protected int expectedCalleeCompilationLevel;4950protected CallsBase() {51try {52callerMethod = getClass().getDeclaredMethod("caller");53calleeMethod = getClass().getDeclaredMethod("callee",54getCalleeParametersTypes());55wb.testSetDontInlineMethod(callerMethod, /* dontinline= */ true);56wb.testSetDontInlineMethod(calleeMethod, /* dontinline= */ true);57} catch (NoSuchMethodException e) {58throw new Error("TEST BUG: can't find test method", e);59}60}6162/**63* Provides callee parameters types to search method64* @return array of types65*/66protected Class[] getCalleeParametersTypes() {67return new Class[] {int.class, long.class, float.class,68double.class, String.class};69}7071/**72* Loads native library(libCallsNative.so)73*/74protected static void loadNativeLibrary() {75System.loadLibrary("CallsNative");76}7778/**79* Checks if requested compilation levels are inside of current vm capabilities80* @return true if vm is capable of requested compilation levels81*/82protected final boolean compilationLevelsSupported() {83int[] compLevels = CompilerUtils.getAvailableCompilationLevels();84boolean callerCompLevelSupported = compileCaller <= 0 || (compileCaller > 085&& Arrays.stream(compLevels)86.filter(elem -> elem == compileCaller)87.findAny()88.isPresent());89boolean calleeCompLevelSupported = compileCallee <= 0 || (compileCallee > 090&& Arrays.stream(compLevels)91.filter(elem -> elem == compileCallee)92.findAny()93.isPresent());94return callerCompLevelSupported && calleeCompLevelSupported;95}9697/**98* Parse test arguments99* @param args test arguments100*/101protected final void parseArgs(String args[]) {102for (int i = 0; i < args.length; i++) {103switch (args[i]) {104case "-nativeCallee":105nativeCallee = true;106break;107case "-nativeCaller":108nativeCaller = true;109break;110case "-compileCallee":111compileCallee = Integer.parseInt(args[++i]);112break;113case "-compileCaller":114compileCaller = Integer.parseInt(args[++i]);115break;116case "-checkCallerCompileLevel":117checkCallerCompilationLevel = true;118expectedCallerCompilationLevel = Integer.parseInt(args[++i]);119break;120case "-checkCalleeCompileLevel":121checkCalleeCompilationLevel = true;122expectedCalleeCompilationLevel = Integer.parseInt(args[++i]);123break;124default:125throw new Error("Can't parse test parameter:" + args[i]);126}127}128}129130/**131* Run basic logic of a test by doing compile132* action(if needed). An arguments can be -compileCallee133* $calleeCompilationLevel and/or -compileCaller $callerCompilationLevel134* and/or -nativeCaller and/or -nativeCallee to indicate that native methods135* for caller/callee should be used136* @param args test args137*/138protected final void runTest(String args[]) {139parseArgs(args);140if (compilationLevelsSupported()) {141if (nativeCaller || nativeCallee) {142CallsBase.loadNativeLibrary();143}144Object lock = getLockObject();145Asserts.assertNotNull(lock, "Lock object is null");146/* a following lock is needed in case several instances of this147test are launched in same vm */148synchronized (lock) {149if (compileCaller > 0 || compileCallee > 0) {150caller(); // call once to have everything loaded151calleeVisited = false; // reset state152}153// compile with requested level if needed154if (compileCallee > 0 && !compileMethod(calleeMethod, compileCallee)) {155System.out.println("WARNING: Blocking compilation failed for calleeMethod (timeout?). Skipping.");156return;157}158if (checkCalleeCompilationLevel) {159Asserts.assertEQ(expectedCalleeCompilationLevel,160wb.getMethodCompilationLevel(calleeMethod),161"Unexpected callee compilation level");162}163if (compileCaller > 0 && !compileMethod(callerMethod, compileCaller)) {164System.out.println("WARNING: Blocking compilation failed for callerMethod (timeout?). Skipping.");165return;166}167if (checkCallerCompilationLevel) {168Asserts.assertEQ(expectedCallerCompilationLevel,169wb.getMethodCompilationLevel(callerMethod),170"Unexpected caller compilation level");171}172// do calling work173if (nativeCaller) {174callerNative();175} else {176caller();177}178}179} else {180System.out.println("WARNING: Requested compilation levels are "181+ "out of current vm capabilities. Skipping.");182}183}184185/**186* A method to compile another method, searching it by name in current class187* @param method a method to compile188* @param compLevel a compilation level189* @return true if method was enqueued for compilation190*/191protected final boolean compileMethod(Method method, int compLevel) {192wb.deoptimizeMethod(method);193Asserts.assertTrue(wb.isMethodCompilable(method, compLevel));194return wb.enqueueMethodForCompilation(method, compLevel);195}196197/*198* @return Object to lock on during execution199*/200201protected abstract Object getLockObject();202203protected abstract void caller();204205protected abstract void callerNative();206207/**208* A method checking values. Should be used to verify if all parameters are209* passed as expected. Parameter N should have a value indicating number "N"210* in respective type representation.211*/212public static void checkValues(int param1, long param2, float param3,213double param4, String param5) {214Asserts.assertEQ(param1, 1);215Asserts.assertEQ(param2, 2L);216Asserts.assertEQ(param3, 3.0f);217Asserts.assertEQ(param4, 4.0d);218Asserts.assertEQ(param5, "5");219}220}221222223