1 /* 2 * Copyright (c) 2014, 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 /** 26 * @test 27 * @bug 8031320 28 * @summary Verify that RTMSpinLoopCount affects time spent 29 * between locking attempts. 30 * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary 31 * @build TestRTMSpinLoopCount 32 * @run main/othervm/bootclasspath -XX:+UnlockDiagnosticVMOptions 33 * -XX:+WhiteBoxAPI TestRTMSpinLoopCount 34 */ 35 36 import java.util.List; 37 import com.oracle.java.testlibrary.*; 38 import com.oracle.java.testlibrary.cli.CommandLineOptionTest; 39 import com.oracle.java.testlibrary.cli.predicate.AndPredicate; 40 import rtm.*; 41 import rtm.predicate.SupportedCPU; 42 import rtm.predicate.SupportedVM; 43 44 /** 45 * Test verifies that RTMSpinLoopCount increase time spent between retries 46 * by comparing amount of retries done with different RTMSpinLoopCount's values. 47 */ 48 public class TestRTMSpinLoopCount extends CommandLineOptionTest { 49 private static final int LOCKING_TIME = 1000; 50 private static final int RTM_RETRY_COUNT = 1000; 51 private static final boolean INFLATE_MONITOR = true; 52 private static final long MAX_ABORTS = RTM_RETRY_COUNT + 1L; 53 private static final int[] SPIN_LOOP_COUNTS 54 = new int[] { 0, 100, 1_000, 1_000_000, 10_000_000 }; 55 56 private TestRTMSpinLoopCount() { 57 super(new AndPredicate(new SupportedVM(), new SupportedCPU())); 58 } 59 60 @Override 61 protected void runTestCases() throws Throwable { 62 long[] aborts = new long[TestRTMSpinLoopCount.SPIN_LOOP_COUNTS.length]; 63 for (int i = 0; i < TestRTMSpinLoopCount.SPIN_LOOP_COUNTS.length; i++) { 64 aborts[i] = getAbortsCountOnLockBusy( 65 TestRTMSpinLoopCount.SPIN_LOOP_COUNTS[i]); 66 } 67 68 for (int i = 1; i < aborts.length; i++) { 69 Asserts.assertLTE(aborts[i], aborts[i - 1], "Increased spin loop " 70 + "count should not increase retries count."); 71 } 72 } 73 74 private long getAbortsCountOnLockBusy(int spinLoopCount) throws Throwable { 75 CompilableTest test = new BusyLock(); 76 77 OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 78 test, 79 CommandLineOptionTest.prepareNumericFlag("RTMRetryCount", 80 TestRTMSpinLoopCount.RTM_RETRY_COUNT), 81 CommandLineOptionTest.prepareNumericFlag("RTMSpinLoopCount", 82 spinLoopCount), 83 "-XX:-UseRTMXendForLockBusy", 84 "-XX:RTMTotalCountIncrRate=1", 85 "-XX:+PrintPreciseRTMLockingStatistics", 86 BusyLock.class.getName(), 87 Boolean.toString(TestRTMSpinLoopCount.INFLATE_MONITOR), 88 Integer.toString(TestRTMSpinLoopCount.LOCKING_TIME) 89 ); 90 91 outputAnalyzer.shouldHaveExitValue(0); 92 93 List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 94 test.getMethodWithLockName(), outputAnalyzer.getOutput()); 95 96 Asserts.assertEQ(statistics.size(), 1, 97 "VM output should contain exactly one entry for method " 98 + test.getMethodWithLockName()); 99 100 RTMLockingStatistics lock = statistics.get(0); 101 102 Asserts.assertLTE(lock.getTotalAborts(), 103 TestRTMSpinLoopCount.MAX_ABORTS, String.format("Total aborts " 104 + "count (%d) should be less or equal to %d", 105 lock.getTotalAborts(), 106 TestRTMSpinLoopCount.MAX_ABORTS)); 107 108 return lock.getTotalAborts(); 109 } 110 111 public static void main(String args[]) throws Throwable { 112 new TestRTMSpinLoopCount().test(); 113 } 114 }