1 /*
   2  * Copyright (c) 2014, 2019, 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  * @test
  26  * @bug 8031320
  27  * @summary Verify that RTMTotalCountIncrRate option affects
  28  *          RTM locking statistics.
  29  * @library /test/lib /
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @requires vm.rtm.cpu & vm.rtm.compiler
  33  * @build sun.hotspot.WhiteBox
  34  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  35  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  36  * @run main/othervm/native -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  37  *                          -XX:+WhiteBoxAPI
  38  *                          compiler.rtm.locking.TestRTMTotalCountIncrRate
  39  */
  40 
  41 package compiler.rtm.locking;
  42 
  43 import compiler.testlibrary.rtm.AbortProvoker;
  44 import compiler.testlibrary.rtm.XAbortProvoker;
  45 import compiler.testlibrary.rtm.CompilableTest;
  46 import compiler.testlibrary.rtm.RTMLockingStatistics;
  47 import compiler.testlibrary.rtm.RTMTestBase;
  48 import jdk.test.lib.Asserts;
  49 import jdk.test.lib.process.OutputAnalyzer;
  50 import jdk.test.lib.cli.CommandLineOptionTest;
  51 
  52 import java.util.List;
  53 
  54 /**
  55  * Test verifies that with RTMTotalCountIncrRate=1 RTM locking statistics
  56  * contains precise information abort attempted locks and that with other values
  57  * statistics contains information abort non-zero locking attempts.
  58  * Since assert done for RTMTotalCountIncrRate=1 is pretty strict, test uses
  59  * -XX:RTMRetryCount=0 to avoid issue with retriable aborts. For more details on
  60  * that issue see {@link TestUseRTMAfterLockInflation}.
  61  */
  62 public class TestRTMTotalCountIncrRate {
  63     protected void runTestCases() throws Throwable {
  64         verifyLocksCount(1, false);
  65         verifyLocksCount(64, false);
  66         verifyLocksCount(128, false);
  67         verifyLocksCount(1, true);
  68         verifyLocksCount(64, true);
  69         verifyLocksCount(128, true);
  70     }
  71 
  72     private void verifyLocksCount(int incrRate, boolean useStackLock)
  73             throws Throwable{
  74         CompilableTest test = new Test();
  75 
  76         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  77                 test,
  78                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  79                         useStackLock),
  80                 CommandLineOptionTest.prepareNumericFlag(
  81                         "RTMTotalCountIncrRate", incrRate),
  82                 "-XX:RTMRetryCount=0",
  83                 "-XX:+PrintPreciseRTMLockingStatistics",
  84                 Test.class.getName(),
  85                 Boolean.toString(!useStackLock)
  86         );
  87 
  88         outputAnalyzer.shouldHaveExitValue(0);
  89 
  90         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
  91                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
  92 
  93         Asserts.assertEQ(statistics.size(), 1, "VM output should contain "
  94                 + "exactly one RTM locking statistics entry for method "
  95                 + test.getMethodWithLockName());
  96 
  97         RTMLockingStatistics lock = statistics.get(0);
  98         if (incrRate == 1) {
  99             Asserts.assertEQ(lock.getTotalLocks(), Test.TOTAL_ITERATIONS,
 100                     "Total locks should be exactly the same as amount of "
 101                     + "iterations.");
 102         }
 103     }
 104 
 105     public static class Test implements CompilableTest {
 106         private static final long TOTAL_ITERATIONS = 10000L;
 107         private final XAbortProvoker xabort = new XAbortProvoker();
 108         private final Object monitor = new Object();
 109         // Following field have to be static in order to avoid escape analysis.
 110         @SuppressWarnings("UnsuedDeclaration")
 111         private static int field = 0;
 112 
 113         @Override
 114         public String getMethodWithLockName() {
 115             return this.getClass().getName() + "::lock";
 116         }
 117 
 118         @Override
 119         public String[] getMethodsToCompileNames() {
 120             return new String[] { getMethodWithLockName(), "*.doAbort" };
 121         }
 122 
 123         public void lock(boolean forceAbort) {
 124             synchronized(monitor) {
 125                 if (forceAbort) {
 126                     // We're calling native method in order to force
 127                     // abort. It's done by explicit xabort call emitted
 128                     // in SharedRuntime::generate_native_wrapper.
 129                     // If an actual JNI call will be replaced by
 130                     // intrinsic - we'll be in trouble, since xabort
 131                     // will be no longer called and test may fail.
 132                     xabort.doAbort();
 133                 }
 134                 Test.field++;
 135             }
 136         }
 137 
 138         /**
 139          * Usage:
 140          * Test &lt;inflate monitor&gt;
 141          */
 142         public static void main(String args[]) throws Throwable {
 143             Asserts.assertGTE(args.length, 1, "One argument required.");
 144             Test test = new Test();
 145             boolean shouldBeInflated = Boolean.valueOf(args[0]);
 146             if (shouldBeInflated) {
 147                 AbortProvoker.inflateMonitor(test.monitor);
 148             }
 149             for (long i = 0L; i < Test.TOTAL_ITERATIONS; i++) {
 150                 AbortProvoker.verifyMonitorState(test.monitor,
 151                         shouldBeInflated);
 152                 // Force abort on first iteration to avoid rare case when
 153                 // there were no aborts and locks count was not incremented
 154                 // with RTMTotalCountIncrRate > 1 (in such case JVM won't
 155                 // print JVM locking statistics).
 156                 test.lock(i == 0);
 157             }
 158         }
 159     }
 160 
 161     public static void main(String args[]) throws Throwable {
 162         new TestRTMTotalCountIncrRate().runTestCases();
 163     }
 164 }