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 sun.hotspot.WhiteBox
  32  *                              sun.hotspot.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     private static final long ABORT_THRESHOLD = LOCKING_THRESHOLD / 2L;
  57 
  58     private TestRTMDeoptOnLowAbortRatio() {
  59         super(new AndPredicate(new SupportedCPU(), new SupportedVM()));
  60     }
  61 
  62     @Override
  63     protected void runTestCases() throws Throwable {
  64         verifyRTMDeopt(false);
  65         verifyRTMDeopt(true);
  66     }
  67 
  68     private void verifyRTMDeopt(boolean useStackLock) throws Throwable {
  69         CompilableTest test = new Test();
  70         String logFileName = String.format("rtm_deopt_%s_stack_lock.xml",
  71                                            useStackLock ? "use" : "no");
  72 
  73         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  74                 logFileName,
  75                 test,
  76                 "-XX:+UseRTMDeopt",
  77                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  78                         useStackLock),
  79                 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",
  80                         TestRTMDeoptOnLowAbortRatio.LOCKING_THRESHOLD),
  81                 CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold",
  82                         TestRTMDeoptOnLowAbortRatio.ABORT_THRESHOLD),
  83                 "-XX:RTMAbortRatio=100",
  84                 "-XX:CompileThreshold=1",
  85                 "-XX:RTMRetryCount=0",
  86                 "-XX:RTMTotalCountIncrRate=1",
  87                 "-XX:+PrintPreciseRTMLockingStatistics",
  88                 Test.class.getName(),
  89                 Boolean.toString(!useStackLock)
  90         );
  91 
  92         outputAnalyzer.shouldHaveExitValue(0);
  93 
  94         int firedTraps = RTMTestBase.firedRTMStateChangeTraps(logFileName);
  95 
  96         Asserts.assertEQ(firedTraps, 1,
  97                         "Expected to get only one deoptimization due to rtm"
  98                         + " state change");
  99 
 100         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
 101                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
 102 
 103         Asserts.assertEQ(statistics.size(), 2,
 104                          "VM output should contain two RTM locking "
 105                          + "statistics entries for method "
 106                          + test.getMethodWithLockName());
 107 
 108         RTMLockingStatistics statisticsBeforeDeopt = null;
 109 
 110         for (RTMLockingStatistics s : statistics) {
 111             if (s.getTotalLocks()
 112                     == TestRTMDeoptOnLowAbortRatio.LOCKING_THRESHOLD) {
 113                 Asserts.assertNull(statisticsBeforeDeopt,
 114                         "Only one abort was expected during test run");
 115                 statisticsBeforeDeopt = s;
 116             }
 117         }
 118 
 119         Asserts.assertNotNull(statisticsBeforeDeopt,
 120                 "After LockThreshold was reached, method should be recompiled "
 121                 + "with rtm lock eliding.");
 122     }
 123 
 124     public static class Test implements CompilableTest {
 125         private static final Unsafe UNSAFE = Utils.getUnsafe();
 126         private final Object monitor = new Object();
 127 
 128         @Override
 129         public String getMethodWithLockName() {
 130             return this.getClass().getName() + "::forceAbort";
 131         }
 132 
 133         @Override
 134         public String[] getMethodsToCompileNames() {
 135             return new String[] { getMethodWithLockName() };
 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             boolean shouldBeInflated = Boolean.valueOf(args[0]);
 154             if (shouldBeInflated) {
 155                 AbortProvoker.inflateMonitor(t.monitor);
 156             }
 157             for (int i = 0; i < AbortProvoker.DEFAULT_ITERATIONS; i++) {
 158                 AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated);
 159                 t.forceAbort(i >= TestRTMDeoptOnLowAbortRatio.ABORT_THRESHOLD);
 160             }
 161         }
 162     }
 163 
 164     public static void main(String args[]) throws Throwable {
 165         new TestRTMDeoptOnLowAbortRatio().test();
 166     }
 167 }