Path: blob/master/test/hotspot/jtreg/compiler/rtm/locking/TestRTMLockingThreshold.java
41154 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 RTMLockingThreshold affects rtm state transition27* ProfileRTM => UseRTM.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.TestRTMLockingThreshold37*/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 RTMLockingThreshold option actually affects how soon54* method will be deoptimized on low abort ratio.55*/56public class TestRTMLockingThreshold {5758/**59* We use non-zero abort threshold to avoid abort related to60* interrupts, VMM calls, etc. during first lock attempt.61*62*/63private static final int MIN_ABORT_THRESHOLD = 10;6465protected void runTestCases() throws Throwable {66verifyLockingThreshold(0, false);67verifyLockingThreshold(100, false);68verifyLockingThreshold(1000, false);6970verifyLockingThreshold(0, true);71verifyLockingThreshold(100, true);72verifyLockingThreshold(1000, true);73}7475private void verifyLockingThreshold(int lockingThreshold,76boolean useStackLock) throws Throwable {77CompilableTest test = new Test();7879int abortThreshold = Math.max(lockingThreshold / 2,80TestRTMLockingThreshold.MIN_ABORT_THRESHOLD);8182OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(83test,84"-XX:CompileThreshold=1",85CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",86useStackLock),87"-XX:+UseRTMDeopt",88"-XX:RTMTotalCountIncrRate=1",89"-XX:RTMRetryCount=0",90CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold",91abortThreshold),92CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",93lockingThreshold),94"-XX:RTMAbortRatio=100",95"-XX:+PrintPreciseRTMLockingStatistics",96Test.class.getName(),97Boolean.toString(!useStackLock),98Integer.toString(lockingThreshold)99);100101outputAnalyzer.shouldHaveExitValue(0);102103List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(104test.getMethodWithLockName(), outputAnalyzer.getOutput());105106Asserts.assertEQ(statistics.size(), 2, "VM output should contain two "107+ "RTM locking statistics entries.");108109/**110* If RTMLockingThreshold==0, then we have to make at least 1 call.111*/112long expectedValue = lockingThreshold;113if (expectedValue == 0) {114expectedValue++;115}116117RTMLockingStatistics statBeforeDeopt = null;118for (RTMLockingStatistics s : statistics) {119if (s.getTotalLocks() == expectedValue) {120Asserts.assertNull(statBeforeDeopt,121"Only one statistics entry should contain aborts");122statBeforeDeopt = s;123}124}125126Asserts.assertNotNull(statBeforeDeopt, "There should be exactly one "127+ "statistics entry corresponding to ProfileRTM state.");128}129130public static class Test implements CompilableTest {131// Following field have to be static in order to avoid escape analysis.132@SuppressWarnings("UnsuedDeclaration")133private static int field = 0;134private static final int TOTAL_ITERATIONS = 10000;135private final XAbortProvoker xabort = new XAbortProvoker();136private final Object monitor = new Object();137138@Override139public String getMethodWithLockName() {140return this.getClass().getName() + "::lock";141}142143@Override144public String[] getMethodsToCompileNames() {145return new String[] { getMethodWithLockName(),146XAbortProvoker.class.getName() + "::doAbort" };147}148149public void lock(boolean abort) {150synchronized(monitor) {151if (abort) {152Test.field += xabort.doAbort();153}154}155}156157/**158* Usage:159* Test <inflate monitor>160*/161public static void main(String args[]) throws Throwable {162Asserts.assertGTE(args.length, 2, "Two arguments required.");163Test t = new Test();164boolean shouldBeInflated = Boolean.valueOf(args[0]);165int lockingThreshold = Integer.valueOf(args[1]);166if (shouldBeInflated) {167AbortProvoker.inflateMonitor(t.monitor);168}169for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) {170AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated);171t.lock(i >= lockingThreshold / 2);172}173}174}175176public static void main(String args[]) throws Throwable {177new TestRTMLockingThreshold().runTestCases();178}179}180181182