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