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