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         if (Platform.isPPC()) {
 67             SPIN_LOOP_COUNTS = new int[] { 0, 10, 100, 1_000, 10_000 };
 68         } else {
 69             SPIN_LOOP_COUNTS = new int[] { 0, 100, 1_000, 10_000, 100_000 };
 70         }
 71 
 72         long[] aborts = new long[TestRTMSpinLoopCount.SPIN_LOOP_COUNTS.length];
 73 
 74         for (int i = 0; i < TestRTMSpinLoopCount.SPIN_LOOP_COUNTS.length; i++) {
 75             aborts[i] = getAbortsCountOnLockBusy(
 76                     TestRTMSpinLoopCount.SPIN_LOOP_COUNTS[i]);
 77         }
 78 
 79         for (int i = 1; i < aborts.length; i++) {
 80             Asserts.assertLTE(aborts[i], aborts[i - 1], "Increased spin loop "
 81                     + "count should not increase retries count.");
 82         }
 83     }
 84 
 85     private long getAbortsCountOnLockBusy(int spinLoopCount) throws Throwable {
 86         CompilableTest test = new BusyLock();
 87 
 88         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
 89                 test,
 90                 CommandLineOptionTest.prepareNumericFlag("RTMRetryCount",
 91                         TestRTMSpinLoopCount.RTM_RETRY_COUNT),
 92                 CommandLineOptionTest.prepareNumericFlag("RTMSpinLoopCount",
 93                         spinLoopCount),
 94                 "-XX:-UseRTMXendForLockBusy",
 95                 "-XX:RTMTotalCountIncrRate=1",
 96                 "-XX:+PrintPreciseRTMLockingStatistics",
 97                 BusyLock.class.getName(),
 98                 Boolean.toString(TestRTMSpinLoopCount.INFLATE_MONITOR),
 99                 Integer.toString(TestRTMSpinLoopCount.LOCKING_TIME)
100         );
101 
102         outputAnalyzer.shouldHaveExitValue(0);
103 
104         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
105                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
106 
107         Asserts.assertEQ(statistics.size(), 1,
108                 "VM output should contain exactly one entry for method "
109                  + test.getMethodWithLockName());
110 
111         RTMLockingStatistics lock = statistics.get(0);
112 
113         Asserts.assertLTE(lock.getTotalAborts(),
114                 TestRTMSpinLoopCount.MAX_ABORTS, String.format("Total aborts "
115                         + "count (%d) should be less or equal to %d",
116                         lock.getTotalAborts(),
117                         TestRTMSpinLoopCount.MAX_ABORTS));
118 
119         return lock.getTotalAborts();
120     }
121 
122     public static void main(String args[]) throws Throwable {
123         new TestRTMSpinLoopCount().runTestCases();
124     }
125 }