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 rtm locking is used for stack locks before 29 * inflation and after it used for inflated locks. 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.TestUseRTMAfterLockInflation 39 */ 40 41 package compiler.rtm.locking; 42 43 import compiler.testlibrary.rtm.AbortProvoker; 44 import compiler.testlibrary.rtm.AbortType; 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.test.lib.Asserts; 50 import jdk.test.lib.process.OutputAnalyzer; 51 import jdk.test.lib.cli.CommandLineOptionTest; 52 import jdk.test.lib.cli.predicate.AndPredicate; 53 54 import java.util.List; 55 56 /** 57 * Test verifies that RTM is used after lock inflation by executing compiled 58 * method with RTM-based lock elision using stack lock first, then that lock 59 * is inflated and the same compiled method invoked again. 60 * 61 * Compiled method invoked {@code AbortProvoker.DEFAULT_ITERATIONS} times before 62 * lock inflation and the same amount of times after inflation. 63 * As a result total locks count should be equal to 64 * {@code 2 * AbortProvoker.DEFAULT_ITERATIONS}. 65 * It is a pretty strict assertion which could fail if some retriable abort 66 * happened: it could be {@code AbortType.RETRIABLE} or 67 * {@code AbortType.MEM_CONFLICT}, but unfortunately abort can has both these 68 * reasons simultaneously. In order to avoid false negative failures related 69 * to incorrect aborts counting, -XX:RTMRetryCount=0 is used. 70 */ 71 public class TestUseRTMAfterLockInflation extends CommandLineOptionTest { 72 private static final long EXPECTED_LOCKS 73 = 2L * AbortProvoker.DEFAULT_ITERATIONS; 74 75 private TestUseRTMAfterLockInflation() { 76 super(new AndPredicate(new SupportedVM(), new SupportedCPU())); 77 } 78 79 @Override 80 protected void runTestCases() throws Throwable { 81 AbortProvoker provoker = AbortType.XABORT.provoker(); 82 long totalLocksCount = 0; 83 84 OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( 85 provoker, 86 "-XX:+UseRTMForStackLocks", 87 "-XX:RTMTotalCountIncrRate=1", 88 "-XX:RTMRetryCount=0", 89 "-XX:+PrintPreciseRTMLockingStatistics", 90 Test.class.getName(), 91 AbortType.XABORT.toString()); 92 93 outputAnalyzer.shouldHaveExitValue(0); 94 95 List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString( 96 provoker.getMethodWithLockName(), outputAnalyzer.getOutput()); 97 98 Asserts.assertEQ(statistics.size(), 2, 99 "VM output should contain two rtm locking statistics entries " 100 + "for method " + provoker.getMethodWithLockName()); 101 102 for (RTMLockingStatistics s : statistics) { 103 totalLocksCount += s.getTotalLocks(); 104 } 105 106 Asserts.assertEQ(totalLocksCount, 107 TestUseRTMAfterLockInflation.EXPECTED_LOCKS, 108 "Total lock count should be greater or equal to " 109 + TestUseRTMAfterLockInflation.EXPECTED_LOCKS); 110 } 111 112 public static class Test { 113 /** 114 * Usage: 115 * Test <provoker type> 116 */ 117 public static void main(String args[]) throws Throwable { 118 Asserts.assertGT(args.length, 0, 119 "AbortType name is expected as first argument."); 120 121 AbortProvoker provoker 122 = AbortType.lookup(Integer.valueOf(args[0])).provoker(); 123 for (int i = 0; i < AbortProvoker.DEFAULT_ITERATIONS; i++) { 124 AbortProvoker.verifyMonitorState(provoker, false /*deflated*/); 125 provoker.forceAbort(); 126 } 127 provoker.inflateMonitor(); 128 for (int i = 0; i < AbortProvoker.DEFAULT_ITERATIONS; i++) { 129 AbortProvoker.verifyMonitorState(provoker, true /*inflated*/); 130 provoker.forceAbort(); 131 } 132 } 133 } 134 135 public static void main(String args[]) throws Throwable { 136 new TestUseRTMAfterLockInflation().test(); 137 } 138 }