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