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