1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/compiler/rtm/locking/TestRTMAfterNonRTMDeopt.java Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,210 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +/** 1.29 + * @test 1.30 + * @bug 8031320 1.31 + * @summary Verify that if we use RTMDeopt, then deoptimization 1.32 + * caused by reason other then rtm_state_change will reset 1.33 + * method's RTM state. And if we don't use RTMDeopt, then 1.34 + * RTM state remain the same after such deoptimization. 1.35 + * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary 1.36 + * @build TestRTMAfterNonRTMDeopt 1.37 + * @run main ClassFileInstaller sun.hotspot.WhiteBox 1.38 + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 1.39 + * -XX:+WhiteBoxAPI TestRTMAfterNonRTMDeopt 1.40 + */ 1.41 + 1.42 +import java.util.List; 1.43 +import com.oracle.java.testlibrary.*; 1.44 +import com.oracle.java.testlibrary.cli.CommandLineOptionTest; 1.45 +import com.oracle.java.testlibrary.cli.predicate.AndPredicate; 1.46 +import rtm.*; 1.47 +import rtm.predicate.SupportedCPU; 1.48 +import rtm.predicate.SupportedVM; 1.49 +import sun.misc.Unsafe; 1.50 + 1.51 +/** 1.52 + * To verify that with +UseRTMDeopt method's RTM state will be 1.53 + * changed to ProfileRTM on deoptimization unrelated to 1.54 + * rtm_state_change following sequence of events is used: 1.55 + * <pre> 1.56 + * 1.57 + * rtm state ^ 1.58 + * | 1.59 + * UseRTM | ******| ****** 1.60 + * | | 1.61 + * ProfileRTM |******| |*****| 1.62 + * | | | | 1.63 + * 0-------|-----|-----|---------------------> time 1.64 + * | | \ force abort 1.65 + * | | 1.66 + * | \ force deoptimization 1.67 + * | 1.68 + * \ force xabort 1.69 + * </pre> 1.70 + * When xabort is forced by native method call method should 1.71 + * change it's state to UseRTM, because we use RTMAbortRatio=100 1.72 + * and low RTMLockingThreshold, so at this point actual abort 1.73 + * ratio will be below 100% and there should be enough lock 1.74 + * attempts to recompile method without RTM profiling. 1.75 + */ 1.76 +public class TestRTMAfterNonRTMDeopt extends CommandLineOptionTest { 1.77 + private static final int ABORT_THRESHOLD = 1000; 1.78 + private static final String RANGE_CHECK = "range_check"; 1.79 + 1.80 + private TestRTMAfterNonRTMDeopt() { 1.81 + super(new AndPredicate(new SupportedCPU(), new SupportedVM())); 1.82 + } 1.83 + 1.84 + @Override 1.85 + protected void runTestCases() throws Throwable { 1.86 + verifyRTMAfterDeopt(false, false); 1.87 + verifyRTMAfterDeopt(true, false); 1.88 + 1.89 + verifyRTMAfterDeopt(false, true); 1.90 + verifyRTMAfterDeopt(true, true); 1.91 + } 1.92 + 1.93 + private void verifyRTMAfterDeopt(boolean useStackLock, 1.94 + boolean useRTMDeopt) throws Throwable { 1.95 + CompilableTest test = new Test(); 1.96 + String logFile = String.format("rtm_%s_stack_lock_%s_deopt.xml", 1.97 + (useStackLock ? "use" : "no"), (useRTMDeopt ? "use" : "no")); 1.98 + 1.99 + OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 1.100 + logFile, 1.101 + test, 1.102 + "-XX:CompileThreshold=1", 1.103 + CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks", 1.104 + useStackLock), 1.105 + CommandLineOptionTest.prepareBooleanFlag("UseRTMDeopt", 1.106 + useRTMDeopt), 1.107 + "-XX:RTMAbortRatio=100", 1.108 + CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold", 1.109 + TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD), 1.110 + CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold", 1.111 + TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD / 2L), 1.112 + "-XX:RTMTotalCountIncrRate=1", 1.113 + "-XX:+PrintPreciseRTMLockingStatistics", 1.114 + Test.class.getName(), 1.115 + Boolean.toString(!useStackLock) 1.116 + ); 1.117 + 1.118 + outputAnalyzer.shouldHaveExitValue(0); 1.119 + 1.120 + int traps = RTMTestBase.firedRTMStateChangeTraps(logFile); 1.121 + 1.122 + if (useRTMDeopt) { 1.123 + Asserts.assertEQ(traps, 2, "Two uncommon traps with " 1.124 + + "reason rtm_state_change should be fired."); 1.125 + } else { 1.126 + Asserts.assertEQ(traps, 0, "No uncommon traps with " 1.127 + + "reason rtm_state_change should be fired."); 1.128 + } 1.129 + 1.130 + int rangeCheckTraps = RTMTestBase.firedUncommonTraps(logFile, 1.131 + TestRTMAfterNonRTMDeopt.RANGE_CHECK); 1.132 + 1.133 + Asserts.assertEQ(rangeCheckTraps, 1, 1.134 + "One range_check uncommon trap should be fired."); 1.135 + 1.136 + List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 1.137 + test.getMethodWithLockName(), outputAnalyzer.getOutput()); 1.138 + 1.139 + int expectedStatEntries = (useRTMDeopt ? 4 : 2); 1.140 + 1.141 + Asserts.assertEQ(statistics.size(), expectedStatEntries, 1.142 + String.format("VM output should contain %d RTM locking " 1.143 + + "statistics entries.", expectedStatEntries)); 1.144 + } 1.145 + 1.146 + public static class Test implements CompilableTest { 1.147 + // Following field have to be static in order to avoid escape analysis. 1.148 + @SuppressWarnings("UnsuedDeclaration") 1.149 + private static int field = 0; 1.150 + private static final int ITERATIONS = 10000; 1.151 + private static final int RANGE_CHECK_AT = ITERATIONS / 2; 1.152 + private static final Unsafe UNSAFE = Utils.getUnsafe(); 1.153 + private final Object monitor = new Object(); 1.154 + 1.155 + @Override 1.156 + public String getMethodWithLockName() { 1.157 + return this.getClass().getName() + "::forceAbort"; 1.158 + } 1.159 + 1.160 + @Override 1.161 + public String[] getMethodsToCompileNames() { 1.162 + return new String[] { 1.163 + getMethodWithLockName(), 1.164 + sun.misc.Unsafe.class.getName() + "::forceAbort" 1.165 + }; 1.166 + } 1.167 + 1.168 + public void forceAbort(int a[], boolean abort) { 1.169 + try { 1.170 + synchronized(monitor) { 1.171 + a[0]++; 1.172 + if (abort) { 1.173 + Test.field = Test.UNSAFE.addressSize(); 1.174 + } 1.175 + } 1.176 + } catch (Throwable t) { 1.177 + // suppress any throwables 1.178 + } 1.179 + } 1.180 + 1.181 + /** 1.182 + * Usage: 1.183 + * Test <inflate monitor> 1.184 + */ 1.185 + public static void main(String args[]) throws Throwable { 1.186 + Test t = new Test(); 1.187 + 1.188 + if (Boolean.valueOf(args[0])) { 1.189 + AbortProvoker.inflateMonitor(t.monitor); 1.190 + } 1.191 + 1.192 + int tmp[] = new int[1]; 1.193 + 1.194 + for (int i = 0; i < Test.ITERATIONS; i++ ) { 1.195 + if (i == Test.RANGE_CHECK_AT) { 1.196 + t.forceAbort(new int[0], false); 1.197 + } else { 1.198 + boolean isThreshold 1.199 + = (i == TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD); 1.200 + boolean isThresholdPlusRange 1.201 + = (i == TestRTMAfterNonRTMDeopt.ABORT_THRESHOLD 1.202 + + Test.RANGE_CHECK_AT); 1.203 + t.forceAbort(tmp, isThreshold || isThresholdPlusRange); 1.204 + } 1.205 + } 1.206 + } 1.207 + } 1.208 + 1.209 + public static void main(String args[]) throws Throwable { 1.210 + new TestRTMAfterNonRTMDeopt().test(); 1.211 + } 1.212 +} 1.213 +