1 /* 2 * Copyright (c) 2014, 2016, 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 on low abort ratio method will be recompiled. 29 * @library /test/lib / 30 * @modules java.base/jdk.internal.misc 31 * java.management 32 * @build sun.hotspot.WhiteBox 33 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 34 * sun.hotspot.WhiteBox$WhiteBoxPermission 35 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 36 * -XX:+WhiteBoxAPI 37 * compiler.rtm.locking.TestRTMDeoptOnLowAbortRatio 38 */ 39 40 package compiler.rtm.locking; 41 42 import compiler.testlibrary.rtm.AbortProvoker; 43 import compiler.testlibrary.rtm.CompilableTest; 44 import compiler.testlibrary.rtm.RTMLockingStatistics; 45 import compiler.testlibrary.rtm.RTMTestBase; 46 import compiler.testlibrary.rtm.predicate.SupportedCPU; 47 import compiler.testlibrary.rtm.predicate.SupportedOS; 48 import compiler.testlibrary.rtm.predicate.SupportedVM; 49 import jdk.internal.misc.Unsafe; 50 import jdk.test.lib.Asserts; 51 import jdk.test.lib.process.OutputAnalyzer; 52 import jdk.test.lib.unsafe.UnsafeHelper; 53 import jdk.test.lib.cli.CommandLineOptionTest; 54 import jdk.test.lib.cli.predicate.AndPredicate; 55 56 import java.util.List; 57 58 /** 59 * Test verifies that low abort ratio method will be deoptimized with 60 * <i>rtm_state_change</i> reason and will continue to use RTM-based lock 61 * elision after that. 62 * This test make asserts on total locks count done by compiled method, 63 * so in order to avoid issue with retriable locks -XX:RTMRetryCount=0 is used. 64 * For more details on that issue see {@link TestUseRTMAfterLockInflation}. 65 */ 66 public class TestRTMDeoptOnLowAbortRatio extends CommandLineOptionTest { 67 private static final long LOCKING_THRESHOLD = 100L; 68 private static final long ABORT_THRESHOLD = LOCKING_THRESHOLD / 2L; 69 70 private TestRTMDeoptOnLowAbortRatio() { 71 super(new AndPredicate(new SupportedCPU(), new SupportedOS(), new SupportedVM())); 72 } 73 74 @Override 75 protected void runTestCases() throws Throwable { 76 verifyRTMDeopt(false); 77 verifyRTMDeopt(true); 78 } 79 80 private void verifyRTMDeopt(boolean useStackLock) throws Throwable { 81 CompilableTest test = new Test(); 82 String logFileName = String.format("rtm_deopt_%s_stack_lock.xml", 83 useStackLock ? "use" : "no"); 84 85 OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 86 logFileName, 87 test, 88 "-XX:+UseRTMDeopt", 89 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks", 90 useStackLock), 91 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold", 92 TestRTMDeoptOnLowAbortRatio.LOCKING_THRESHOLD), 93 CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold", 94 TestRTMDeoptOnLowAbortRatio.ABORT_THRESHOLD), 95 "-XX:RTMAbortRatio=100", 96 "-XX:CompileThreshold=1", 97 "-XX:RTMRetryCount=0", 98 "-XX:RTMTotalCountIncrRate=1", 99 "-XX:+PrintPreciseRTMLockingStatistics", 100 Test.class.getName(), 101 Boolean.toString(!useStackLock) 102 ); 103 104 outputAnalyzer.shouldHaveExitValue(0); 105 106 int firedTraps = RTMTestBase.firedRTMStateChangeTraps(logFileName); 107 108 Asserts.assertEQ(firedTraps, 1, 109 "Expected to get only one deoptimization due to rtm" 110 + " state change"); 111 112 List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 113 test.getMethodWithLockName(), outputAnalyzer.getOutput()); 114 115 Asserts.assertEQ(statistics.size(), 2, 116 "VM output should contain two RTM locking " 117 + "statistics entries for method " 118 + test.getMethodWithLockName()); 119 120 RTMLockingStatistics statisticsBeforeDeopt = null; 121 122 for (RTMLockingStatistics s : statistics) { 123 if (s.getTotalLocks() 124 == TestRTMDeoptOnLowAbortRatio.LOCKING_THRESHOLD) { 125 Asserts.assertNull(statisticsBeforeDeopt, 126 "Only one abort was expected during test run"); 127 statisticsBeforeDeopt = s; 128 } 129 } 130 131 Asserts.assertNotNull(statisticsBeforeDeopt, 132 "After LockThreshold was reached, method should be recompiled " 133 + "with rtm lock eliding."); 134 } 135 136 public static class Test implements CompilableTest { 137 private static final Unsafe UNSAFE = UnsafeHelper.getUnsafe(); 138 private final Object monitor = new Object(); 139 140 @Override 141 public String getMethodWithLockName() { 142 return this.getClass().getName() + "::forceAbort"; 143 } 144 145 @Override 146 public String[] getMethodsToCompileNames() { 147 return new String[] { getMethodWithLockName() }; 148 } 149 150 public void forceAbort(boolean abort) { 151 synchronized(monitor) { 152 if (abort) { 153 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 boolean shouldBeInflated = Boolean.valueOf(args[0]); 166 if (shouldBeInflated) { 167 AbortProvoker.inflateMonitor(t.monitor); 168 } 169 for (int i = 0; i < AbortProvoker.DEFAULT_ITERATIONS; i++) { 170 AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated); 171 t.forceAbort(i >= TestRTMDeoptOnLowAbortRatio.ABORT_THRESHOLD); 172 } 173 } 174 } 175 176 public static void main(String args[]) throws Throwable { 177 new TestRTMDeoptOnLowAbortRatio().test(); 178 } 179 }