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