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 RTMLockingThreshold affects rtm state transition
  29  *          ProfileRTM => UseRTM.
  30  * @library /testlibrary /test/lib /compiler/testlibrary
  31  * @modules java.base/sun.misc
  32  *          java.management
  33  * @build TestRTMLockingThreshold
  34  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  35  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  36  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  37  *                   -XX:+WhiteBoxAPI TestRTMLockingThreshold
  38  */
  39 
  40 import java.util.List;
  41 import jdk.test.lib.*;
  42 import jdk.test.lib.cli.CommandLineOptionTest;
  43 import jdk.test.lib.cli.predicate.AndPredicate;
  44 import rtm.*;
  45 import rtm.predicate.SupportedCPU;
  46 import rtm.predicate.SupportedVM;
  47 import sun.misc.Unsafe;
  48 
  49 /**
  50  * Test verifies that RTMLockingThreshold option actually affects how soon
  51  * method will be deoptimized on low abort ratio.
  52  */
  53 public class TestRTMLockingThreshold extends CommandLineOptionTest {
  54     private TestRTMLockingThreshold() {
  55         super(new AndPredicate(new SupportedVM(), new SupportedCPU()));
  56     }
  57 
  58     /**
  59      * We use non-zero abort threshold to avoid abort related to
  60      * interrupts, VMM calls, etc. during first lock attempt.
  61      *
  62      */
  63     private static final int MIN_ABORT_THRESHOLD = 10;
  64 
  65     @Override
  66     protected void runTestCases() throws Throwable {
  67         verifyLockingThreshold(0, false);
  68         verifyLockingThreshold(100, false);
  69         verifyLockingThreshold(1000, false);
  70 
  71         verifyLockingThreshold(0, true);
  72         verifyLockingThreshold(100, true);
  73         verifyLockingThreshold(1000, true);
  74     }
  75 
  76     private void verifyLockingThreshold(int lockingThreshold,
  77             boolean useStackLock) throws Throwable {
  78         CompilableTest test = new Test();
  79 
  80         int abortThreshold = Math.max(lockingThreshold / 2,
  81                 TestRTMLockingThreshold.MIN_ABORT_THRESHOLD);
  82 
  83         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  84                 test,
  85                 "-XX:CompileThreshold=1",
  86                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  87                         useStackLock),
  88                 "-XX:+UseRTMDeopt",
  89                 "-XX:RTMTotalCountIncrRate=1",
  90                 "-XX:RTMRetryCount=0",
  91                 CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold",
  92                         abortThreshold),
  93                 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",
  94                         lockingThreshold),
  95                 "-XX:RTMAbortRatio=100",
  96                 "-XX:+PrintPreciseRTMLockingStatistics",
  97                 Test.class.getName(),
  98                 Boolean.toString(!useStackLock),
  99                 Integer.toString(lockingThreshold)
 100         );
 101 
 102         outputAnalyzer.shouldHaveExitValue(0);
 103 
 104         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
 105                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
 106 
 107         Asserts.assertEQ(statistics.size(), 2, "VM output should contain two "
 108                 + "RTM locking statistics entries.");
 109 
 110         /**
 111          * If RTMLockingThreshold==0, then we have to make at least 1 call.
 112          */
 113         long expectedValue = lockingThreshold;
 114         if (expectedValue == 0) {
 115             expectedValue++;
 116         }
 117 
 118         RTMLockingStatistics statBeforeDeopt = null;
 119         for (RTMLockingStatistics s : statistics) {
 120             if (s.getTotalLocks() == expectedValue) {
 121                 Asserts.assertNull(statBeforeDeopt,
 122                         "Only one statistics entry should contain aborts");
 123                 statBeforeDeopt = s;
 124             }
 125         }
 126 
 127         Asserts.assertNotNull(statBeforeDeopt, "There should be exactly one "
 128                 + "statistics entry corresponding to ProfileRTM state.");
 129     }
 130 
 131     public static class Test implements CompilableTest {
 132         // Following field have to be static in order to avoid escape analysis.
 133         @SuppressWarnings("UnsuedDeclaration")
 134         private static int field = 0;
 135         private static final int TOTAL_ITERATIONS = 10000;
 136         private static final Unsafe UNSAFE = Utils.getUnsafe();
 137         private final Object monitor = new Object();
 138 
 139 
 140         @Override
 141         public String getMethodWithLockName() {
 142             return this.getClass().getName() + "::lock";
 143         }
 144 
 145         @Override
 146         public String[] getMethodsToCompileNames() {
 147             return new String[] { getMethodWithLockName() };
 148         }
 149 
 150         public void lock(boolean abort) {
 151             synchronized(monitor) {
 152                 if (abort) {
 153                     Test.field += Test.UNSAFE.addressSize();
 154                 }
 155             }
 156         }
 157 
 158         /**
 159          * Usage:
 160          * Test &lt;inflate monitor&gt;
 161          */
 162         public static void main(String args[]) throws Throwable {
 163             Asserts.assertGTE(args.length, 2, "Two arguments required.");
 164             Test t = new Test();
 165             boolean shouldBeInflated = Boolean.valueOf(args[0]);
 166             int lockingThreshold = Integer.valueOf(args[1]);
 167             if (shouldBeInflated) {
 168                 AbortProvoker.inflateMonitor(t.monitor);
 169             }
 170             for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) {
 171                 AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated);
 172                 t.lock(i >= lockingThreshold / 2);
 173             }
 174         }
 175     }
 176 
 177     public static void main(String args[]) throws Throwable {
 178         new TestRTMLockingThreshold().test();
 179     }
 180 }