1 /*
   2  * Copyright (c) 2014, 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 /testlibrary/whitebox /compiler/testlibrary
  31  * @build TestRTMTotalCountIncrRate
  32  * @run main/othervm/bootclasspath -XX:+UnlockDiagnosticVMOptions
  33  *                   -XX:+WhiteBoxAPI TestRTMTotalCountIncrRate
  34  */
  35 
  36 import java.util.List;
  37 
  38 import com.oracle.java.testlibrary.*;
  39 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  40 import com.oracle.java.testlibrary.cli.predicate.AndPredicate;
  41 import rtm.*;
  42 import rtm.predicate.SupportedCPU;
  43 import rtm.predicate.SupportedVM;
  44 
  45 /**
  46  * Test verifies that with RTMTotalCountIncrRate=1 RTM locking statistics
  47  * contains precise information abort attempted locks and that with other values
  48  * statistics contains information abort non-zero locking attempts.
  49  * Since assert done for RTMTotalCountIncrRate=1 is pretty strict, test uses
  50  * -XX:RTMRetryCount=0 to avoid issue with retriable aborts. For more details on
  51  * that issue see {@link TestUseRTMAfterLockInflation}.
  52  */
  53 public class TestRTMTotalCountIncrRate extends CommandLineOptionTest {
  54     private TestRTMTotalCountIncrRate() {
  55         super(new AndPredicate(new SupportedCPU(), new SupportedVM()));
  56     }
  57 
  58     @Override
  59     protected void runTestCases() throws Throwable {
  60         verifyLocksCount(1, false);
  61         verifyLocksCount(64, false);
  62         verifyLocksCount(128, false);
  63         verifyLocksCount(1, true);
  64         verifyLocksCount(64, true);
  65         verifyLocksCount(128, true);
  66     }
  67 
  68     private void verifyLocksCount(int incrRate, boolean useStackLock)
  69             throws Throwable{
  70         CompilableTest test = new Test();
  71 
  72         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  73                 test,
  74                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  75                         useStackLock),
  76                 CommandLineOptionTest.prepareNumericFlag(
  77                         "RTMTotalCountIncrRate", incrRate),
  78                 "-XX:RTMRetryCount=0",
  79                 "-XX:+PrintPreciseRTMLockingStatistics",
  80                 Test.class.getName(),
  81                 Boolean.toString(!useStackLock)
  82         );
  83 
  84         outputAnalyzer.shouldHaveExitValue(0);
  85 
  86         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
  87                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
  88 
  89         Asserts.assertEQ(statistics.size(), 1, "VM output should contain "
  90                 + "exactly one RTM locking statistics entry for method "
  91                 + test.getMethodWithLockName());
  92 
  93         RTMLockingStatistics lock = statistics.get(0);
  94         if (incrRate == 1) {
  95             Asserts.assertEQ(lock.getTotalLocks(), Test.TOTAL_ITERATIONS,
  96                     "Total locks should be exactly the same as amount of "
  97                     + "iterations.");
  98         } else {
  99             Asserts.assertGT(lock.getTotalLocks(), 0L, "RTM statistics "
 100                     + "should contain information for at least on lock.");
 101         }
 102     }
 103 
 104     public static class Test implements CompilableTest {
 105         private static final long TOTAL_ITERATIONS = 10000L;
 106         private final Object monitor = new Object();
 107         // Following field have to be static in order to avoid escape analysis.
 108         @SuppressWarnings("UnsuedDeclaration")
 109         private static int field = 0;
 110 
 111         @Override
 112         public String getMethodWithLockName() {
 113             return this.getClass().getName() + "::lock";
 114         }
 115 
 116         @Override
 117         public String[] getMethodsToCompileNames() {
 118             return new String[] {
 119                 getMethodWithLockName()
 120             };
 121         }
 122 
 123         public void lock() {
 124             synchronized(monitor) {
 125                 Test.field++;
 126             }
 127         }
 128 
 129         /**
 130          * Usage:
 131          * Test &lt;inflate monitor&gt;
 132          */
 133         public static void main(String args[]) throws Throwable {
 134             Asserts.assertGTE(args.length, 1, "One argument required.");
 135             Test test = new Test();
 136 
 137             if (Boolean.valueOf(args[0])) {
 138                 AbortProvoker.inflateMonitor(test.monitor);
 139             }
 140             for (long i = 0L; i < Test.TOTAL_ITERATIONS; i++) {
 141                 test.lock();
 142             }
 143         }
 144     }
 145 
 146     public static void main(String args[]) throws Throwable {
 147         new TestRTMTotalCountIncrRate().test();
 148     }
 149 }