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