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