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