Path: blob/master/test/hotspot/jtreg/compiler/rtm/locking/TestRTMAfterNonRTMDeopt.java
41155 views
/*1* Copyright (c) 2014, 2021, 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*/2223/**24* @test25* @bug 803132026* @summary Verify that if we use RTMDeopt, then deoptimization27* caused by reason other then rtm_state_change will reset28* method's RTM state. And if we don't use RTMDeopt, then29* RTM state remain the same after such deoptimization.30* @library /test/lib /31* @modules java.base/jdk.internal.misc32* java.management33* @requires vm.rtm.cpu & vm.rtm.compiler34* @build sun.hotspot.WhiteBox35* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox36* @run main/othervm/native -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions37* -XX:+WhiteBoxAPI38* compiler.rtm.locking.TestRTMAfterNonRTMDeopt39*/4041package compiler.rtm.locking;4243import compiler.testlibrary.rtm.AbortProvoker;44import compiler.testlibrary.rtm.XAbortProvoker;45import compiler.testlibrary.rtm.CompilableTest;46import compiler.testlibrary.rtm.RTMLockingStatistics;47import compiler.testlibrary.rtm.RTMTestBase;48import jdk.test.lib.Asserts;49import jdk.test.lib.process.OutputAnalyzer;50import jdk.test.lib.cli.CommandLineOptionTest;5152import java.util.List;5354/**55* To verify that with +UseRTMDeopt method's RTM state will be56* changed to ProfileRTM on deoptimization unrelated to57* rtm_state_change following sequence of events is used:58* <pre>59*60* rtm state ^61* |62* UseRTM | ******| ******63* | |64* ProfileRTM |******| |*****|65* | | | |66* 0-------|-----|-----|---------------------> time67* | | \ force abort68* | |69* | \ force deoptimization70* |71* \ force xabort72* </pre>73* When xabort is forced by native method call method should74* change it's state to UseRTM, because we use RTMAbortRatio=10075* and low RTMLockingThreshold, so at this point actual abort76* ratio will be below 100% and there should be enough lock77* attempts to recompile method without RTM profiling.78*/79public class TestRTMAfterNonRTMDeopt {80private static final int ABORT_THRESHOLD = 1000;81private static final String RANGE_CHECK = "range_check";8283protected void runTestCases() throws Throwable {84verifyRTMAfterDeopt(false, false);85verifyRTMAfterDeopt(true, false);8687verifyRTMAfterDeopt(false, true);88verifyRTMAfterDeopt(true, true);89}9091private void verifyRTMAfterDeopt(boolean useStackLock,92boolean useRTMDeopt) throws Throwable {93CompilableTest test = new Test();94String logFile = String.format("rtm_%s_stack_lock_%s_deopt.xml",95(useStackLock ? "use" : "no"), (useRTMDeopt ? "use" : "no"));9697OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(98logFile,99test,100"-XX:CompileThreshold=1",101CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",102useStackLock),103CommandLineOptionTest.prepareBooleanFlag("UseRTMDeopt",104useRTMDeopt),105"-XX:RTMAbortRatio=100",106CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold",107TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD),108CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",109TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD / 2L),110"-XX:RTMTotalCountIncrRate=1",111"-XX:+PrintPreciseRTMLockingStatistics",112Test.class.getName(),113Boolean.toString(!useStackLock)114);115116outputAnalyzer.shouldHaveExitValue(0);117118int traps = RTMTestBase.firedRTMStateChangeTraps(logFile);119120if (useRTMDeopt) {121Asserts.assertEQ(traps, 2, "Two uncommon traps with "122+ "reason rtm_state_change should be fired.");123} else {124Asserts.assertEQ(traps, 0, "No uncommon traps with "125+ "reason rtm_state_change should be fired.");126}127128int rangeCheckTraps = RTMTestBase.firedUncommonTraps(logFile,129TestRTMAfterNonRTMDeopt.RANGE_CHECK);130131Asserts.assertEQ(rangeCheckTraps, 1,132"One range_check uncommon trap should be fired.");133134List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(135test.getMethodWithLockName(), outputAnalyzer.getOutput());136137int expectedStatEntries = (useRTMDeopt ? 4 : 2);138139Asserts.assertEQ(statistics.size(), expectedStatEntries,140String.format("VM output should contain %d RTM locking "141+ "statistics entries.", expectedStatEntries));142}143144public static class Test implements CompilableTest {145// Following field have to be static in order to avoid escape analysis.146@SuppressWarnings("UnsuedDeclaration")147private static int field = 0;148private static final int ITERATIONS = 10000;149private static final int RANGE_CHECK_AT = ITERATIONS / 2;150private final XAbortProvoker xabort = new XAbortProvoker();151private final Object monitor = new Object();152153@Override154public String getMethodWithLockName() {155return this.getClass().getName() + "::forceAbort";156}157158@Override159public String[] getMethodsToCompileNames() {160return new String[] { getMethodWithLockName(),161XAbortProvoker.class.getName() + "::doAbort()" };162}163164public void forceAbort(int a[], boolean abort) {165try {166synchronized(monitor) {167a[0]++;168if (abort) {169Test.field = xabort.doAbort();170}171}172} catch (Throwable t) {173// suppress any throwables174}175}176177/**178* Usage:179* Test <inflate monitor>180*/181public static void main(String args[]) throws Throwable {182Test t = new Test();183184boolean shouldBeInflated = Boolean.valueOf(args[0]);185if (shouldBeInflated) {186AbortProvoker.inflateMonitor(t.monitor);187}188189int tmp[] = new int[1];190191for (int i = 0; i < Test.ITERATIONS; i++ ) {192AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated);193if (i == Test.RANGE_CHECK_AT) {194t.forceAbort(new int[0], false);195} else {196boolean isThreshold197= (i == TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD);198boolean isThresholdPlusRange199= (i == TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD200+ Test.RANGE_CHECK_AT);201t.forceAbort(tmp, isThreshold || isThresholdPlusRange);202}203}204}205}206207public static void main(String args[]) throws Throwable {208new TestRTMAfterNonRTMDeopt().runTestCases();209}210}211212213214