1 /*
   2  * Copyright (c) 2014, 2019, 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  * @test
  26  * @bug 8031320
  27  * @summary Verify that on low abort ratio method will be recompiled.
  28  * @library /test/lib /
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  * @requires vm.rtm.cpu & vm.rtm.compiler
  32  * @build sun.hotspot.WhiteBox
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  34  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  35  * @run main/othervm/native -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  36  *                          -XX:+WhiteBoxAPI
  37  *                          compiler.rtm.locking.TestRTMDeoptOnLowAbortRatio
  38  */
  39 
  40 package compiler.rtm.locking;
  41 
  42 import compiler.testlibrary.rtm.AbortProvoker;
  43 import compiler.testlibrary.rtm.XAbortProvoker;
  44 import compiler.testlibrary.rtm.CompilableTest;
  45 import compiler.testlibrary.rtm.RTMLockingStatistics;
  46 import compiler.testlibrary.rtm.RTMTestBase;
  47 import jdk.test.lib.Asserts;
  48 import jdk.test.lib.process.OutputAnalyzer;
  49 import jdk.test.lib.cli.CommandLineOptionTest;
  50 
  51 import java.util.List;
  52 
  53 /**
  54  * Test verifies that low abort ratio method will be deoptimized with
  55  * <i>rtm_state_change</i> reason and will continue to use RTM-based lock
  56  * elision after that.
  57  * This test make asserts on total locks count done by compiled method,
  58  * so in order to avoid issue with retriable locks -XX:RTMRetryCount=0 is used.
  59  * For more details on that issue see {@link TestUseRTMAfterLockInflation}.
  60  */
  61 public class TestRTMDeoptOnLowAbortRatio {
  62     private static final long LOCKING_THRESHOLD = 100L;
  63     private static final long ABORT_THRESHOLD = LOCKING_THRESHOLD / 2L;
  64 
  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 final XAbortProvoker xabort = new XAbortProvoker();
 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                                   XAbortProvoker.class.getName() + "::doAbort" };
 139         }
 140 
 141         public void forceAbort(boolean abort) {
 142             synchronized(monitor) {
 143                 if (abort) {
 144                     xabort.doAbort();
 145                 }
 146             }
 147         }
 148 
 149         /**
 150          * Usage:
 151          * Test &lt;inflate monitor&gt;
 152          */
 153         public static void main(String args[]) throws Throwable {
 154             Asserts.assertGTE(args.length, 1, "One argument required.");
 155             Test t = new Test();
 156             boolean shouldBeInflated = Boolean.valueOf(args[0]);
 157             if (shouldBeInflated) {
 158                 AbortProvoker.inflateMonitor(t.monitor);
 159             }
 160             for (int i = 0; i < AbortProvoker.DEFAULT_ITERATIONS; i++) {
 161                 AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated);
 162                 t.forceAbort(i >= TestRTMDeoptOnLowAbortRatio.ABORT_THRESHOLD);
 163             }
 164         }
 165     }
 166 
 167     public static void main(String args[]) throws Throwable {
 168         new TestRTMDeoptOnLowAbortRatio().runTestCases();
 169     }
 170 }