1 /*
   2  * Copyright (c) 2014, 2015, 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 /../../test/lib /compiler/testlibrary
  33  * @modules java.base/sun.misc
  34  *          java.management
  35  * @build TestPrintPreciseRTMLockingStatistics
  36  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  37  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  38  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  39  *                   -XX:+WhiteBoxAPI TestPrintPreciseRTMLockingStatistics
  40  */
  41 
  42 import java.util.*;
  43 
  44 import jdk.test.lib.*;
  45 import jdk.test.lib.cli.CommandLineOptionTest;
  46 import jdk.test.lib.cli.predicate.AndPredicate;
  47 import rtm.*;
  48 import rtm.predicate.SupportedCPU;
  49 import rtm.predicate.SupportedVM;
  50 
  51 /**
  52  * Test verifies that VM output does not contain RTM locking statistics when it
  53  * should not (when PrintPreciseRTMLockingStatistics is off) and that with
  54  * -XX:+PrintPreciseRTMLockingStatistics locking statistics contains sane
  55  * total locks and aborts count as well as for specific abort types.
  56  */
  57 public class TestPrintPreciseRTMLockingStatistics
  58         extends CommandLineOptionTest {
  59     private TestPrintPreciseRTMLockingStatistics() {
  60         super(new AndPredicate(new SupportedCPU(), new SupportedVM()));
  61     }
  62 
  63     @Override
  64     public void runTestCases() throws Throwable {
  65         verifyNoStatistics();
  66         verifyStatistics();
  67     }
  68 
  69     // verify that VM output does not contain
  70     // rtm locking statistics
  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
  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 
  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 
 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 
 139     public static void main(String args[]) throws Throwable {
 140         new TestPrintPreciseRTMLockingStatistics().test();
 141     }
 142 }