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 if we use RTMDeopt, then deoptimization 29 * caused by reason other then rtm_state_change will reset 30 * method's RTM state. And if we don't use RTMDeopt, then 31 * RTM state remain the same after such deoptimization. 32 * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary 33 * @build TestRTMAfterNonRTMDeopt 34 * @run main ClassFileInstaller sun.hotspot.WhiteBox 35 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 36 * -XX:+WhiteBoxAPI TestRTMAfterNonRTMDeopt 37 */ 38 39 import java.util.List; 40 import com.oracle.java.testlibrary.*; 41 import com.oracle.java.testlibrary.cli.CommandLineOptionTest; 42 import com.oracle.java.testlibrary.cli.predicate.AndPredicate; 43 import rtm.*; 44 import rtm.predicate.SupportedCPU; 45 import rtm.predicate.SupportedVM; 46 import sun.misc.Unsafe; 47 48 /** 49 * To verify that with +UseRTMDeopt method's RTM state will be 50 * changed to ProfileRTM on deoptimization unrelated to 51 * rtm_state_change following sequence of events is used: 52 * <pre> 53 * 54 * rtm state ^ 55 * | 56 * UseRTM | ******| ****** 57 * | | 58 * ProfileRTM |******| |*****| 59 * | | | | 60 * 0-------|-----|-----|---------------------> time 61 * | | \ force abort 62 * | | 63 * | \ force deoptimization 64 * | 65 * \ force xabort 66 * </pre> 67 * When xabort is forced by native method call method should 68 * change it's state to UseRTM, because we use RTMAbortRatio=100 69 * and low RTMLockingThreshold, so at this point actual abort 70 * ratio will be below 100% and there should be enough lock 71 * attempts to recompile method without RTM profiling. 72 */ 73 public class TestRTMAfterNonRTMDeopt extends CommandLineOptionTest { 74 private static final int ABORT_THRESHOLD = 1000; 75 private static final String RANGE_CHECK = "range_check"; 76 77 private TestRTMAfterNonRTMDeopt() { 78 super(new AndPredicate(new SupportedCPU(), new SupportedVM())); 79 } 80 81 @Override 82 protected void runTestCases() throws Throwable { 83 verifyRTMAfterDeopt(false, false); 84 verifyRTMAfterDeopt(true, false); 85 86 verifyRTMAfterDeopt(false, true); 87 verifyRTMAfterDeopt(true, true); 88 } 89 90 private void verifyRTMAfterDeopt(boolean useStackLock, 91 boolean useRTMDeopt) throws Throwable { 92 CompilableTest test = new Test(); 93 String logFile = String.format("rtm_%s_stack_lock_%s_deopt.xml", 94 (useStackLock ? "use" : "no"), (useRTMDeopt ? "use" : "no")); 95 96 OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 97 logFile, 98 test, 99 "-XX:CompileThreshold=1", 100 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks", 101 useStackLock), 102 CommandLineOptionTest.prepareBooleanFlag("UseRTMDeopt", 103 useRTMDeopt), 104 "-XX:RTMAbortRatio=100", 105 CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold", 106 TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD), 107 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold", 108 TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD / 2L), 109 "-XX:RTMTotalCountIncrRate=1", 110 "-XX:+PrintPreciseRTMLockingStatistics", 111 Test.class.getName(), 112 Boolean.toString(!useStackLock) 113 ); 114 115 outputAnalyzer.shouldHaveExitValue(0); 116 117 int traps = RTMTestBase.firedRTMStateChangeTraps(logFile); 118 119 if (useRTMDeopt) { 120 Asserts.assertEQ(traps, 2, "Two uncommon traps with " 121 + "reason rtm_state_change should be fired."); 122 } else { 123 Asserts.assertEQ(traps, 0, "No uncommon traps with " 124 + "reason rtm_state_change should be fired."); 125 } 126 127 int rangeCheckTraps = RTMTestBase.firedUncommonTraps(logFile, 128 TestRTMAfterNonRTMDeopt.RANGE_CHECK); 129 130 Asserts.assertEQ(rangeCheckTraps, 1, 131 "One range_check uncommon trap should be fired."); 132 133 List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 134 test.getMethodWithLockName(), outputAnalyzer.getOutput()); 135 136 int expectedStatEntries = (useRTMDeopt ? 4 : 2); 137 138 Asserts.assertEQ(statistics.size(), expectedStatEntries, 139 String.format("VM output should contain %d RTM locking " 140 + "statistics entries.", expectedStatEntries)); 141 } 142 143 public static class Test implements CompilableTest { 144 // Following field have to be static in order to avoid escape analysis. 145 @SuppressWarnings("UnsuedDeclaration") 146 private static int field = 0; 147 private static final int ITERATIONS = 10000; 148 private static final int RANGE_CHECK_AT = ITERATIONS / 2; 149 private static final Unsafe UNSAFE = Utils.getUnsafe(); 150 private final Object monitor = new Object(); 151 152 @Override 153 public String getMethodWithLockName() { 154 return this.getClass().getName() + "::forceAbort"; 155 } 156 157 @Override 158 public String[] getMethodsToCompileNames() { 159 return new String[] { 160 getMethodWithLockName(), 161 sun.misc.Unsafe.class.getName() + "::forceAbort" 162 }; 163 } 164 165 public void forceAbort(int a[], boolean abort) { 166 try { 167 synchronized(monitor) { 168 a[0]++; 169 if (abort) { 170 Test.field = Test.UNSAFE.addressSize(); 171 } 172 } 173 } catch (Throwable t) { 174 // suppress any throwables 175 } 176 } 177 178 /** 179 * Usage: 180 * Test <inflate monitor> 181 */ 182 public static void main(String args[]) throws Throwable { 183 Test t = new Test(); 184 185 if (Boolean.valueOf(args[0])) { 186 AbortProvoker.inflateMonitor(t.monitor); 187 } 188 189 int tmp[] = new int[1]; 190 191 for (int i = 0; i < Test.ITERATIONS; i++ ) { 192 if (i == Test.RANGE_CHECK_AT) { 193 t.forceAbort(new int[0], false); 194 } else { 195 boolean isThreshold 196 = (i == TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD); 197 boolean isThresholdPlusRange 198 = (i == TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD 199 + Test.RANGE_CHECK_AT); 200 t.forceAbort(tmp, isThreshold || isThresholdPlusRange); 201 } 202 } 203 } 204 } 205 206 public static void main(String args[]) throws Throwable { 207 new TestRTMAfterNonRTMDeopt().test(); 208 } 209 } 210