1 /* 2 * Copyright (c) 2014, 2019, 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 RTMLockingThreshold affects rtm state transition 28 * ProfileRTM => UseRTM. 29 * @library /test/lib / 30 * @modules java.base/jdk.internal.misc 31 * java.management 32 * @requires vm.rtm.cpu & vm.rtm.compiler 33 * @build sun.hotspot.WhiteBox 34 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 35 * sun.hotspot.WhiteBox$WhiteBoxPermission 36 * @run main/othervm/native -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 37 * -XX:+WhiteBoxAPI 38 * compiler.rtm.locking.TestRTMLockingThreshold 39 */ 40 41 package compiler.rtm.locking; 42 43 import compiler.testlibrary.rtm.AbortProvoker; 44 import compiler.testlibrary.rtm.XAbortProvoker; 45 import compiler.testlibrary.rtm.CompilableTest; 46 import compiler.testlibrary.rtm.RTMLockingStatistics; 47 import compiler.testlibrary.rtm.RTMTestBase; 48 import jdk.test.lib.Asserts; 49 import jdk.test.lib.process.OutputAnalyzer; 50 import jdk.test.lib.cli.CommandLineOptionTest; 51 52 import java.util.List; 53 54 /** 55 * Test verifies that RTMLockingThreshold option actually affects how soon 56 * method will be deoptimized on low abort ratio. 57 */ 58 public class TestRTMLockingThreshold { 59 60 /** 61 * We use non-zero abort threshold to avoid abort related to 62 * interrupts, VMM calls, etc. during first lock attempt. 63 * 64 */ 65 private static final int MIN_ABORT_THRESHOLD = 10; 66 67 protected void runTestCases() throws Throwable { 68 verifyLockingThreshold(0, false); 69 verifyLockingThreshold(100, false); 70 verifyLockingThreshold(1000, false); 71 72 verifyLockingThreshold(0, true); 73 verifyLockingThreshold(100, true); 74 verifyLockingThreshold(1000, true); 75 } 76 77 private void verifyLockingThreshold(int lockingThreshold, 78 boolean useStackLock) throws Throwable { 79 CompilableTest test = new Test(); 80 81 int abortThreshold = Math.max(lockingThreshold / 2, 82 TestRTMLockingThreshold.MIN_ABORT_THRESHOLD); 83 84 OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 85 test, 86 "-XX:CompileThreshold=1", 87 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks", 88 useStackLock), 89 "-XX:+UseRTMDeopt", 90 "-XX:RTMTotalCountIncrRate=1", 91 "-XX:RTMRetryCount=0", 92 CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold", 93 abortThreshold), 94 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold", 95 lockingThreshold), 96 "-XX:RTMAbortRatio=100", 97 "-XX:+PrintPreciseRTMLockingStatistics", 98 Test.class.getName(), 99 Boolean.toString(!useStackLock), 100 Integer.toString(lockingThreshold) 101 ); 102 103 outputAnalyzer.shouldHaveExitValue(0); 104 105 List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 106 test.getMethodWithLockName(), outputAnalyzer.getOutput()); 107 108 Asserts.assertEQ(statistics.size(), 2, "VM output should contain two " 109 + "RTM locking statistics entries."); 110 111 /** 112 * If RTMLockingThreshold==0, then we have to make at least 1 call. 113 */ 114 long expectedValue = lockingThreshold; 115 if (expectedValue == 0) { 116 expectedValue++; 117 } 118 119 RTMLockingStatistics statBeforeDeopt = null; 120 for (RTMLockingStatistics s : statistics) { 121 if (s.getTotalLocks() == expectedValue) { 122 Asserts.assertNull(statBeforeDeopt, 123 "Only one statistics entry should contain aborts"); 124 statBeforeDeopt = s; 125 } 126 } 127 128 Asserts.assertNotNull(statBeforeDeopt, "There should be exactly one " 129 + "statistics entry corresponding to ProfileRTM state."); 130 } 131 132 public static class Test implements CompilableTest { 133 // Following field have to be static in order to avoid escape analysis. 134 @SuppressWarnings("UnsuedDeclaration") 135 private static int field = 0; 136 private static final int TOTAL_ITERATIONS = 10000; 137 private final XAbortProvoker xabort = new XAbortProvoker(); 138 private final Object monitor = new Object(); 139 140 @Override 141 public String getMethodWithLockName() { 142 return this.getClass().getName() + "::lock"; 143 } 144 145 @Override 146 public String[] getMethodsToCompileNames() { 147 return new String[] { getMethodWithLockName(), 148 XAbortProvoker.class.getName() + "::doAbort" }; 149 } 150 151 public void lock(boolean abort) { 152 synchronized(monitor) { 153 if (abort) { 154 Test.field += xabort.doAbort(); 155 } 156 } 157 } 158 159 /** 160 * Usage: 161 * Test <inflate monitor> 162 */ 163 public static void main(String args[]) throws Throwable { 164 Asserts.assertGTE(args.length, 2, "Two arguments required."); 165 Test t = new Test(); 166 boolean shouldBeInflated = Boolean.valueOf(args[0]); 167 int lockingThreshold = Integer.valueOf(args[1]); 168 if (shouldBeInflated) { 169 AbortProvoker.inflateMonitor(t.monitor); 170 } 171 for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) { 172 AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated); 173 t.lock(i >= lockingThreshold / 2); 174 } 175 } 176 } 177 178 public static void main(String args[]) throws Throwable { 179 new TestRTMLockingThreshold().runTestCases(); 180 } 181 }