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