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