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 RTMTotalCountIncrRate option affects 29 * RTM locking statistics. 30 * @library /testlibrary /test/lib / 31 * @modules java.base/jdk.internal.misc 32 * java.management 33 * @build compiler.rtm.locking.TestRTMTotalCountIncrRate 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.TestRTMTotalCountIncrRate 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 compiler.testlibrary.rtm.predicate.SupportedCPU; 48 import compiler.testlibrary.rtm.predicate.SupportedVM; 49 import jdk.internal.misc.Unsafe; 50 import jdk.test.lib.Asserts; 51 import jdk.test.lib.OutputAnalyzer; 52 import jdk.test.lib.Utils; 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 with RTMTotalCountIncrRate=1 RTM locking statistics 60 * contains precise information abort attempted locks and that with other values 61 * statistics contains information abort non-zero locking attempts. 62 * Since assert done for RTMTotalCountIncrRate=1 is pretty strict, test uses 63 * -XX:RTMRetryCount=0 to avoid issue with retriable aborts. For more details on 64 * that issue see {@link TestUseRTMAfterLockInflation}. 65 */ 66 public class TestRTMTotalCountIncrRate extends CommandLineOptionTest { 67 private TestRTMTotalCountIncrRate() { 68 super(new AndPredicate(new SupportedCPU(), new SupportedVM())); 69 } 70 71 @Override 72 protected void runTestCases() throws Throwable { 73 verifyLocksCount(1, false); 74 verifyLocksCount(64, false); 75 verifyLocksCount(128, false); 76 verifyLocksCount(1, true); 77 verifyLocksCount(64, true); 78 verifyLocksCount(128, true); 79 } 80 81 private void verifyLocksCount(int incrRate, boolean useStackLock) 82 throws Throwable{ 83 CompilableTest test = new Test(); 84 85 OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 86 test, 87 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks", 88 useStackLock), 89 CommandLineOptionTest.prepareNumericFlag( 90 "RTMTotalCountIncrRate", incrRate), 91 "-XX:RTMRetryCount=0", 92 "-XX:+PrintPreciseRTMLockingStatistics", 93 Test.class.getName(), 94 Boolean.toString(!useStackLock) 95 ); 96 97 outputAnalyzer.shouldHaveExitValue(0); 98 99 List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 100 test.getMethodWithLockName(), outputAnalyzer.getOutput()); 101 102 Asserts.assertEQ(statistics.size(), 1, "VM output should contain " 103 + "exactly one RTM locking statistics entry for method " 104 + test.getMethodWithLockName()); 105 106 RTMLockingStatistics lock = statistics.get(0); 107 if (incrRate == 1) { 108 Asserts.assertEQ(lock.getTotalLocks(), Test.TOTAL_ITERATIONS, 109 "Total locks should be exactly the same as amount of " 110 + "iterations."); 111 } 112 } 113 114 public static class Test implements CompilableTest { 115 private static final long TOTAL_ITERATIONS = 10000L; 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 forceAbort) { 133 synchronized(monitor) { 134 if (forceAbort) { 135 // We're calling native method in order to force 136 // abort. It's done by explicit xabort call emitted 137 // in SharedRuntime::generate_native_wrapper. 138 // If an actual JNI call will be replaced by 139 // intrinsic - we'll be in trouble, since xabort 140 // will be no longer called and test may fail. 141 UNSAFE.addressSize(); 142 } 143 Test.field++; 144 } 145 } 146 147 /** 148 * Usage: 149 * Test <inflate monitor> 150 */ 151 public static void main(String args[]) throws Throwable { 152 Asserts.assertGTE(args.length, 1, "One argument required."); 153 Test test = new Test(); 154 boolean shouldBeInflated = Boolean.valueOf(args[0]); 155 if (shouldBeInflated) { 156 AbortProvoker.inflateMonitor(test.monitor); 157 } 158 for (long i = 0L; i < Test.TOTAL_ITERATIONS; i++) { 159 AbortProvoker.verifyMonitorState(test.monitor, 160 shouldBeInflated); 161 // Force abort on first iteration to avoid rare case when 162 // there were no aborts and locks count was not incremented 163 // with RTMTotalCountIncrRate > 1 (in such case JVM won't 164 // print JVM locking statistics). 165 test.lock(i == 0); 166 } 167 } 168 } 169 170 public static void main(String args[]) throws Throwable { 171 new TestRTMTotalCountIncrRate().test(); 172 } 173 }