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