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