1 /*
2  * Copyright (c) 2014, 2021, 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 rtm locking statistics contain proper information
28  *          on overall aborts and locks count and count of aborts of
29  *          different types. Test also verify that VM output does not
30  *          contain rtm locking statistics when it should not.
31  * @library /test/lib /
32  * @modules java.base/jdk.internal.misc
33  *          java.management
34  * @requires vm.rtm.cpu & vm.rtm.compiler
35  * @build sun.hotspot.WhiteBox
36  * @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
37  * @run main/othervm/native -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
38  *                          -XX:+WhiteBoxAPI
39  *                          compiler.rtm.print.TestPrintPreciseRTMLockingStatistics
40  */
41 
42 
43 package compiler.rtm.print;
44 
45 import compiler.testlibrary.rtm.AbortProvoker;
46 import compiler.testlibrary.rtm.AbortType;
47 import compiler.testlibrary.rtm.RTMLockingStatistics;
48 import compiler.testlibrary.rtm.RTMTestBase;
49 import jdk.test.lib.Asserts;
50 import jdk.test.lib.process.OutputAnalyzer;
51 
52 import java.util.Collections;
53 import java.util.LinkedList;
54 import java.util.List;
55 
56 /**
57  * Test verifies that VM output does not contain RTM locking statistics when it
58  * should not (when PrintPreciseRTMLockingStatistics is off) and that with
59  * -XX:+PrintPreciseRTMLockingStatistics locking statistics contains sane
60  * total locks and aborts count as well as for specific abort types.
61  */
62 public class TestPrintPreciseRTMLockingStatistics {
63 
runTestCases()64     public void runTestCases() throws Throwable {
65         verifyNoStatistics();
66         verifyStatistics();
67     }
68 
69     // verify that VM output does not contain
70     // rtm locking statistics
verifyNoStatistics()71     private void verifyNoStatistics() throws Throwable {
72         verifyNoStatistics(AbortType.XABORT);
73 
74         verifyNoStatistics(AbortType.XABORT,
75                 "-XX:-PrintPreciseRTMLockingStatistics");
76 
77         verifyNoStatistics(AbortType.XABORT, "-XX:-UseRTMLocking",
78                 "-XX:+PrintPreciseRTMLockingStatistics");
79     }
80 
81     // verify that rtm locking statistics contain information
82     // about each type of aborts
verifyStatistics()83     private void verifyStatistics() throws Throwable {
84         verifyAbortsCount(AbortType.XABORT);
85         verifyAbortsCount(AbortType.MEM_CONFLICT);
86         verifyAbortsCount(AbortType.BUF_OVERFLOW);
87         verifyAbortsCount(AbortType.NESTED_ABORT);
88     }
89 
verifyNoStatistics(AbortType abortProvokerType, String... vmOpts)90     private void verifyNoStatistics(AbortType abortProvokerType,
91             String... vmOpts) throws Throwable {
92         AbortProvoker provoker = abortProvokerType.provoker();
93         List<String> finalVMOpts = new LinkedList<>();
94         Collections.addAll(finalVMOpts, vmOpts);
95         Collections.addAll(finalVMOpts, AbortProvoker.class.getName(),
96                 abortProvokerType.toString());
97 
98         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(provoker,
99                 finalVMOpts.toArray(new String[finalVMOpts.size()]));
100 
101         outputAnalyzer.shouldHaveExitValue(0);
102 
103         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
104                 outputAnalyzer.getOutput());
105 
106         Asserts.assertEQ(statistics.size(), 0, "VM output should not contain "
107                 + "any RTM locking statistics");
108     }
109 
verifyAbortsCount(AbortType abortType)110     private void verifyAbortsCount(AbortType abortType) throws Throwable {
111         AbortProvoker provoker = abortType.provoker();
112 
113         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
114                 provoker,
115                 "-XX:+PrintPreciseRTMLockingStatistics",
116                 AbortProvoker.class.getName(),
117                 abortType.toString());
118 
119         outputAnalyzer.shouldHaveExitValue(0);
120 
121         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
122                 provoker.getMethodWithLockName(),outputAnalyzer.getOutput());
123 
124         Asserts.assertGT(statistics.size(), 0, "VM output should contain one "
125                 + "rtm locking statistics entry for method "
126                 + provoker.getMethodWithLockName());
127 
128         RTMLockingStatistics lock = statistics.get(0);
129 
130         Asserts.assertGT(lock.getTotalAborts(), 0L,
131                 "RTM locking statistics should contain non zero total aborts "
132                 + "count");
133 
134         Asserts.assertGT(lock.getAborts(abortType), 0L, String.format(
135                 "RTM locking statistics should contain non zero aborts count "
136                 + "for abort reason %s", abortType));
137     }
138 
main(String args[])139     public static void main(String args[]) throws Throwable {
140         new TestPrintPreciseRTMLockingStatistics().runTestCases();
141     }
142 }
143