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