1 /* 2 * Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 8031320 27 * @summary Verify that RTMSpinLoopCount affects time spent 28 * between locking attempts. 29 * @library /test/lib / 30 * @modules java.base/jdk.internal.misc 31 * java.management 32 * @requires vm.rtm.cpu & vm.rtm.compiler 33 * @build jdk.test.whitebox.WhiteBox 34 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox 35 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 36 * -XX:+WhiteBoxAPI 37 * compiler.rtm.locking.TestRTMSpinLoopCount 38 */ 39 40 package compiler.rtm.locking; 41 42 import compiler.testlibrary.rtm.BusyLock; 43 import compiler.testlibrary.rtm.CompilableTest; 44 import compiler.testlibrary.rtm.RTMLockingStatistics; 45 import compiler.testlibrary.rtm.RTMTestBase; 46 import jdk.test.lib.Asserts; 47 import jdk.test.lib.process.OutputAnalyzer; 48 import jdk.test.lib.cli.CommandLineOptionTest; 49 import jdk.test.lib.Platform; 50 51 import java.util.List; 52 53 /** 54 * Test verifies that RTMSpinLoopCount increase time spent between retries 55 * by comparing amount of retries done with different RTMSpinLoopCount's values. 56 */ 57 public class TestRTMSpinLoopCount { 58 private static final int LOCKING_TIME = 1000; 59 private static final int RTM_RETRY_COUNT = 1000; 60 private static final boolean INFLATE_MONITOR = true; 61 private static final long MAX_ABORTS = RTM_RETRY_COUNT + 1L; 62 private static int[] SPIN_LOOP_COUNTS; 63 64 protected void runTestCases() throws Throwable { 65 66 SPIN_LOOP_COUNTS = new int[] { 0, 100, 1_000, 10_000, 100_000 }; 67 68 long[] aborts = new long[TestRTMSpinLoopCount.SPIN_LOOP_COUNTS.length]; 69 70 for (int i = 0; i < TestRTMSpinLoopCount.SPIN_LOOP_COUNTS.length; i++) { 71 aborts[i] = getAbortsCountOnLockBusy( 72 TestRTMSpinLoopCount.SPIN_LOOP_COUNTS[i]); 73 } 74 75 for (int i = 1; i < aborts.length; i++) { 76 Asserts.assertLTE(aborts[i], aborts[i - 1], "Increased spin loop " 77 + "count should not increase retries count."); 78 } 79 } 80 81 private long getAbortsCountOnLockBusy(int spinLoopCount) throws Throwable { 82 CompilableTest test = new BusyLock(); 83 84 OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 85 test, 86 CommandLineOptionTest.prepareNumericFlag("RTMRetryCount", 87 TestRTMSpinLoopCount.RTM_RETRY_COUNT), 88 CommandLineOptionTest.prepareNumericFlag("RTMSpinLoopCount", 89 spinLoopCount), 90 "-XX:-UseRTMXendForLockBusy", 91 "-XX:RTMTotalCountIncrRate=1", 92 "-XX:+PrintPreciseRTMLockingStatistics", 93 BusyLock.class.getName(), 94 Boolean.toString(TestRTMSpinLoopCount.INFLATE_MONITOR), 95 Integer.toString(TestRTMSpinLoopCount.LOCKING_TIME) 96 ); 97 98 outputAnalyzer.shouldHaveExitValue(0); 99 100 List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 101 test.getMethodWithLockName(), outputAnalyzer.getOutput()); 102 103 Asserts.assertEQ(statistics.size(), 1, 104 "VM output should contain exactly one entry for method " 105 + test.getMethodWithLockName()); 106 107 RTMLockingStatistics lock = statistics.get(0); 108 109 Asserts.assertLTE(lock.getTotalAborts(), 110 TestRTMSpinLoopCount.MAX_ABORTS, String.format("Total aborts " 111 + "count (%d) should be less or equal to %d", 112 lock.getTotalAborts(), 113 TestRTMSpinLoopCount.MAX_ABORTS)); 114 115 return lock.getTotalAborts(); 116 } 117 118 public static void main(String args[]) throws Throwable { 119 new TestRTMSpinLoopCount().runTestCases(); 120 } 121 }