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