Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/share/actions/CompileAction.java
41161 views
/*1* Copyright (c) 2015, 2020, 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.compilercontrol.share.actions;2425import compiler.compilercontrol.share.pool.PoolHelper;26import compiler.compilercontrol.share.scenario.State;27import compiler.testlibrary.CompilerUtils;28import jdk.test.lib.Asserts;29import jdk.test.lib.util.Pair;30import jdk.test.lib.Utils;31import sun.hotspot.WhiteBox;3233import java.lang.reflect.Executable;34import java.util.List;35import java.util.concurrent.Callable;3637public class CompileAction {38private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();39private static final int[] COMP_LEVELS;40private static final List<Pair<Executable, Callable<?>>> METHODS41= new PoolHelper().getAllMethods();42private static final int EXEC_AMOUNT = 100;4344static {45COMP_LEVELS = CompilerUtils.getAvailableCompilationLevels();46if (COMP_LEVELS.length == 0) {47throw new Error("TESTBUG: test requires JIT " +48"compiler to be available");49}50}5152/**53* Checks executable if it could be compiled54*55* @param executable given executable to check56* @param state method compilation state57*/58public static void checkCompiled(Executable executable,59State state) {60{ // Dumping the state being checked61System.out.println("Checking expected compilation state: {");62System.out.println(" method: " + executable);63state.toString().lines()64.map(line -> " " + line).forEach(System.out::println);65System.out.println("}");66}67int first = COMP_LEVELS[0];68if (first < 4) {69checkCompilation(executable, first, state.isC1Compilable());70}71int last = COMP_LEVELS[COMP_LEVELS.length - 1];72if (last == 4) {73checkCompilation(executable, last, state.isC2Compilable());74}75}7677private static void checkCompilation(Executable executable,78int level,79boolean expectedCompiled) {80execute(executable);81WHITE_BOX.enqueueMethodForCompilation(executable, level);82Utils.waitForCondition(83() -> {84execute(executable);85return !WHITE_BOX.isMethodQueuedForCompilation(executable);86}, 100L);87execute(executable);88boolean isCompilable = WHITE_BOX.isMethodCompilable(executable, level);89Asserts.assertEQ(isCompilable, expectedCompiled,90String.format("FAILED: method %s compilable: %b, but should: %b"91+ " on required level: %d", executable, isCompilable,92expectedCompiled, level));93}9495private static void execute(Executable executable) {96Callable<?> callable = getCallableFor(executable);97try {98for (int i = 0; i < EXEC_AMOUNT; i++) {99callable.call();100}101} catch (Exception e) {102throw new Error("Got exception during execution", e);103}104}105106private static Callable<?> getCallableFor(Executable executable) {107for (Pair<Executable, Callable<?>> pair : METHODS) {108if (pair.first == executable) {109return pair.second;110}111}112throw new Error("TESTBUG: wrong executable: " + executable);113}114}115116117