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 RTMAbortRatio affects amount of aborts before 29 * deoptimization. 30 * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary 31 * @build TestRTMAbortRatio 32 * @run main ClassFileInstaller sun.hotspot.WhiteBox 33 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 34 * -XX:+WhiteBoxAPI TestRTMAbortRatio 35 */ 36 37 import java.util.List; 38 import com.oracle.java.testlibrary.*; 39 import com.oracle.java.testlibrary.cli.CommandLineOptionTest; 40 import com.oracle.java.testlibrary.cli.predicate.AndPredicate; 41 import rtm.*; 42 import rtm.predicate.SupportedCPU; 43 import rtm.predicate.SupportedVM; 44 import sun.misc.Unsafe; 45 46 /** 47 * Test verifies that method will be deoptimized on high abort ratio 48 * as soon as abort ratio reaches RTMAbortRatio's value. 49 */ 50 public class TestRTMAbortRatio extends CommandLineOptionTest { 51 private TestRTMAbortRatio() { 52 super(new AndPredicate(new SupportedCPU(), new SupportedVM())); 53 } 54 55 @Override 56 protected void runTestCases() throws Throwable { 57 verifyAbortRatio(0, false); 58 verifyAbortRatio(10, false); 59 verifyAbortRatio(50, false); 60 verifyAbortRatio(100, false); 61 62 verifyAbortRatio(0, true); 63 verifyAbortRatio(10, true); 64 verifyAbortRatio(50, true); 65 verifyAbortRatio(100, true); 66 } 67 68 private void verifyAbortRatio(int abortRatio, boolean useStackLock) 69 throws Throwable { 70 CompilableTest test = new Test(); 71 72 OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 73 test, 74 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks", 75 useStackLock), 76 "-XX:+UseRTMDeopt", 77 "-XX:RTMTotalCountIncrRate=1", 78 "-XX:RTMAbortThreshold=0", 79 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold", 80 10 * Test.TOTAL_ITERATIONS), 81 CommandLineOptionTest.prepareNumericFlag("RTMAbortRatio", 82 abortRatio), 83 "-XX:+PrintPreciseRTMLockingStatistics", 84 test.getClass().getName(), 85 Boolean.toString(!useStackLock)); 86 87 outputAnalyzer.shouldHaveExitValue(0); 88 89 List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 90 test.getMethodWithLockName(), outputAnalyzer.getOutput()); 91 92 Asserts.assertEQ(statistics.size(), 1, "VM output should contain " 93 + "exactly one RTM locking statistics entry."); 94 95 RTMLockingStatistics lock = statistics.get(0); 96 int actualRatio; 97 98 if (lock.getTotalAborts() == 1L) { 99 actualRatio = 0; 100 } else { 101 actualRatio = (int) (lock.getTotalLocks() 102 / (lock.getTotalAborts() - 1L)); 103 } 104 105 Asserts.assertLTE(actualRatio, abortRatio, String.format( 106 "Actual abort ratio (%d) should lower or equal to " 107 + "specified (%d).", actualRatio, abortRatio)); 108 } 109 110 /** 111 * Force abort after {@code Test.WARMUP_ITERATIONS} is done. 112 */ 113 public static class Test implements CompilableTest { 114 private static final int TOTAL_ITERATIONS = 10000; 115 private static final int WARMUP_ITERATIONS = 1000; 116 private static final Unsafe UNSAFE = Utils.getUnsafe(); 117 private final Object monitor = new Object(); 118 // Following field have to be static in order to avoid escape analysis. 119 @SuppressWarnings("UnsuedDeclaration") 120 private static int field = 0; 121 122 @Override 123 public String getMethodWithLockName() { 124 return this.getClass().getName() + "::lock"; 125 } 126 127 @Override 128 public String[] getMethodsToCompileNames() { 129 return new String[] { getMethodWithLockName() }; 130 } 131 132 public void lock(boolean abort) { 133 synchronized(monitor) { 134 if (abort) { 135 Test.UNSAFE.addressSize(); 136 } 137 } 138 } 139 140 /** 141 * Usage: 142 * Test <inflate monitor> 143 */ 144 public static void main(String args[]) throws Throwable { 145 Asserts.assertGTE(args.length, 1, "One argument required."); 146 Test t = new Test(); 147 boolean shouldBeInflated = Boolean.valueOf(args[0]); 148 if (shouldBeInflated) { 149 AbortProvoker.inflateMonitor(t.monitor); 150 } 151 for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) { 152 AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated); 153 t.lock(i >= Test.WARMUP_ITERATIONS); 154 } 155 } 156 } 157 158 public static void main(String args[]) throws Throwable { 159 new TestRTMAbortRatio().test(); 160 } 161 } 162