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