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 on low abort ratio method will be recompiled.
  29  * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary
  30  * @build TestRTMDeoptOnLowAbortRatio
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  33  *                   -XX:+WhiteBoxAPI TestRTMDeoptOnLowAbortRatio
  34  */
  35 
  36 import java.util.List;
  37 import com.oracle.java.testlibrary.*;
  38 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  39 import com.oracle.java.testlibrary.cli.predicate.AndPredicate;
  40 import rtm.*;
  41 import rtm.predicate.SupportedCPU;
  42 import rtm.predicate.SupportedVM;
  43 import sun.misc.Unsafe;
  44 
  45 /**
  46  * Test verifies that low abort ratio method will be deoptimized with
  47  * <i>rtm_state_change</i> reason and will continue to use RTM-based lock
  48  * elision after that.
  49  * This test make asserts on total locks count done by compiled method,
  50  * so in order to avoid issue with retriable locks -XX:RTMRetryCount=0 is used.
  51  * For more details on that issue see {@link TestUseRTMAfterLockInflation}.
  52  */
  53 public class TestRTMDeoptOnLowAbortRatio extends CommandLineOptionTest {
  54     private static final long LOCKING_THRESHOLD = 100L;
  55 
  56     private TestRTMDeoptOnLowAbortRatio() {
  57         super(new AndPredicate(new SupportedCPU(), new SupportedVM()));
  58     }
  59 
  60     @Override
  61     protected void runTestCases() throws Throwable {
  62         verifyRTMDeopt(false);
  63         verifyRTMDeopt(true);
  64     }
  65 
  66     private void verifyRTMDeopt(boolean useStackLock) throws Throwable {
  67         CompilableTest test = new Test();
  68         String logFileName = String.format("rtm_deopt_%s_stack_lock.xml",
  69                                            useStackLock ? "use" : "no");
  70 
  71         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  72                 logFileName,
  73                 test,
  74                 "-XX:+UseRTMDeopt",
  75                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  76                         useStackLock),
  77                 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",
  78                         TestRTMDeoptOnLowAbortRatio.LOCKING_THRESHOLD),
  79                 "-XX:RTMAbortThreshold=1",
  80                 "-XX:RTMAbortRatio=100",
  81                 "-XX:CompileThreshold=1",
  82                 "-XX:RTMRetryCount=0",
  83                 "-XX:RTMTotalCountIncrRate=1",
  84                 "-XX:+PrintPreciseRTMLockingStatistics",
  85                 Test.class.getName(),
  86                 Boolean.toString(!useStackLock)
  87         );
  88 
  89         outputAnalyzer.shouldHaveExitValue(0);
  90 
  91         int firedTraps = RTMTestBase.firedRTMStateChangeTraps(logFileName);
  92 
  93         Asserts.assertEQ(firedTraps, 1,
  94                         "Expected to get only one deoptimization due to rtm"
  95                         + " state change");
  96 
  97         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
  98                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
  99 
 100         Asserts.assertEQ(statistics.size(), 2,
 101                          "VM output should contain two RTM locking "
 102                          + "statistics entries for method "
 103                          + test.getMethodWithLockName());
 104 
 105         RTMLockingStatistics statisticsBeforeDeopt = null;
 106 
 107         for (RTMLockingStatistics s : statistics) {
 108             if (s.getTotalLocks()
 109                     == TestRTMDeoptOnLowAbortRatio.LOCKING_THRESHOLD + 1L) {
 110                 Asserts.assertNull(statisticsBeforeDeopt,
 111                         "Only one abort was expected during test run");
 112                 statisticsBeforeDeopt = s;
 113             }
 114         }
 115 
 116         Asserts.assertNotNull(statisticsBeforeDeopt,
 117                 "After LockThreshold was reached, method should be recompiled "
 118                 + "with rtm lock eliding.");
 119     }
 120 
 121     public static class Test implements CompilableTest {
 122         private static final Unsafe UNSAFE = Utils.getUnsafe();
 123         private final Object monitor = new Object();
 124 
 125         @Override
 126         public String getMethodWithLockName() {
 127             return this.getClass().getName() + "::forceAbort";
 128         }
 129 
 130         @Override
 131         public String[] getMethodsToCompileNames() {
 132             return new String[] {
 133                 getMethodWithLockName(),
 134                 sun.misc.Unsafe.class.getName() + "::addressSize"
 135             };
 136         }
 137 
 138         public void forceAbort(boolean abort) {
 139             synchronized(monitor) {
 140                 if (abort) {
 141                     Test.UNSAFE.addressSize();
 142                 }
 143             }
 144         }
 145 
 146         /**
 147          * Usage:
 148          * Test &lt;inflate monitor&gt;
 149          */
 150         public static void main(String args[]) throws Throwable {
 151             Asserts.assertGTE(args.length, 1, "One argument required.");
 152             Test t = new Test();
 153 
 154             if (Boolean.valueOf(args[0])) {
 155                 AbortProvoker.inflateMonitor(t.monitor);
 156             }
 157             for (int i = 0; i < AbortProvoker.DEFAULT_ITERATIONS; i++) {
 158                 t.forceAbort(
 159                         i == TestRTMDeoptOnLowAbortRatio.LOCKING_THRESHOLD);
 160             }
 161         }
 162     }
 163 
 164     public static void main(String args[]) throws Throwable {
 165         new TestRTMDeoptOnLowAbortRatio().test();
 166     }
 167 }