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