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