Path: blob/master/test/hotspot/jtreg/compiler/rtm/locking/TestRTMAbortRatio.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 RTMAbortRatio affects amount of aborts before27* deoptimization.28* @library /test/lib /29* @modules java.base/jdk.internal.misc30* java.management31* @requires vm.rtm.cpu & vm.rtm.compiler32* @build sun.hotspot.WhiteBox33* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox34* @run main/othervm/native -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions35* -XX:+WhiteBoxAPI36* compiler.rtm.locking.TestRTMAbortRatio37*/3839package compiler.rtm.locking;4041import compiler.testlibrary.rtm.AbortProvoker;42import compiler.testlibrary.rtm.XAbortProvoker;43import compiler.testlibrary.rtm.CompilableTest;44import compiler.testlibrary.rtm.RTMLockingStatistics;45import compiler.testlibrary.rtm.RTMTestBase;46import jdk.test.lib.Asserts;47import jdk.test.lib.process.OutputAnalyzer;48import jdk.test.lib.cli.CommandLineOptionTest;4950import java.util.List;5152/**53* Test verifies that method will be deoptimized on high abort ratio54* as soon as abort ratio reaches RTMAbortRatio's value.55*/56public class TestRTMAbortRatio {5758protected void runTestCases() throws Throwable {59verifyAbortRatio(0, false);60verifyAbortRatio(10, false);61verifyAbortRatio(50, false);62verifyAbortRatio(100, false);6364verifyAbortRatio(0, true);65verifyAbortRatio(10, true);66verifyAbortRatio(50, true);67verifyAbortRatio(100, true);68}6970private void verifyAbortRatio(int abortRatio, boolean useStackLock)71throws Throwable {72CompilableTest test = new Test();7374OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(75test,76CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",77useStackLock),78"-XX:+UseRTMDeopt",79"-XX:RTMTotalCountIncrRate=1",80"-XX:RTMAbortThreshold=0",81CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",8210 * Test.TOTAL_ITERATIONS),83CommandLineOptionTest.prepareNumericFlag("RTMAbortRatio",84abortRatio),85"-XX:+PrintPreciseRTMLockingStatistics",86test.getClass().getName(),87Boolean.toString(!useStackLock));8889outputAnalyzer.shouldHaveExitValue(0);9091List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(92test.getMethodWithLockName(), outputAnalyzer.getOutput());9394Asserts.assertEQ(statistics.size(), 1, "VM output should contain "95+ "exactly one RTM locking statistics entry.");9697RTMLockingStatistics lock = statistics.get(0);98int actualRatio;99100if (lock.getTotalAborts() == 1L) {101actualRatio = 0;102} else {103actualRatio = (int) (lock.getTotalLocks()104/ (lock.getTotalAborts() - 1L));105}106107Asserts.assertLTE(actualRatio, abortRatio, String.format(108"Actual abort ratio (%d) should lower or equal to "109+ "specified (%d).", actualRatio, abortRatio));110}111112/**113* Force abort after {@code Test.WARMUP_ITERATIONS} is done.114*/115public static class Test implements CompilableTest {116private static final int TOTAL_ITERATIONS = 10000;117private static final int WARMUP_ITERATIONS = 1000;118private final XAbortProvoker xabort = new XAbortProvoker();119private final Object monitor = new Object();120// Following field have to be static in order to avoid escape analysis.121@SuppressWarnings("UnsuedDeclaration")122private static int field = 0;123124@Override125public String getMethodWithLockName() {126return this.getClass().getName() + "::lock";127}128129@Override130public String[] getMethodsToCompileNames() {131return new String[] { getMethodWithLockName(), "*.doAbort" };132}133134public void lock(boolean abort) {135synchronized(monitor) {136if (abort) {137xabort.doAbort();138}139}140}141142/**143* Usage:144* Test <inflate monitor>145*/146public static void main(String args[]) throws Throwable {147Asserts.assertGTE(args.length, 1, "One argument required.");148Test t = new Test();149boolean shouldBeInflated = Boolean.valueOf(args[0]);150if (shouldBeInflated) {151AbortProvoker.inflateMonitor(t.monitor);152}153for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) {154AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated);155t.lock(i >= Test.WARMUP_ITERATIONS);156}157}158}159160public static void main(String args[]) throws Throwable {161new TestRTMAbortRatio().runTestCases();162}163}164165166167