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