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