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