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 RTMLockingThreshold affects rtm state transition
  29  *          ProfileRTM => UseRTM.
  30  * @library /testlibrary /../../test/lib /compiler/testlibrary
  31  * @build TestRTMLockingThreshold
  32  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  33  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  35  *                   -XX:+WhiteBoxAPI TestRTMLockingThreshold
  36  */
  37 
  38 import java.util.List;
  39 import com.oracle.java.testlibrary.*;
  40 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  41 import com.oracle.java.testlibrary.cli.predicate.AndPredicate;
  42 import rtm.*;
  43 import rtm.predicate.SupportedCPU;
  44 import rtm.predicate.SupportedVM;
  45 import sun.misc.Unsafe;
  46 
  47 /**
  48  * Test verifies that RTMLockingThreshold option actually affects how soon
  49  * method will be deoptimized on low abort ratio.
  50  */
  51 public class TestRTMLockingThreshold extends CommandLineOptionTest {
  52     private TestRTMLockingThreshold() {
  53         super(new AndPredicate(new SupportedVM(), new SupportedCPU()));
  54     }
  55 
  56     /**
  57      * We use non-zero abort threshold to avoid abort related to
  58      * interrupts, VMM calls, etc. during first lock attempt.
  59      *
  60      */
  61     private static final int MIN_ABORT_THRESHOLD = 10;
  62 
  63     @Override
  64     protected void runTestCases() throws Throwable {
  65         verifyLockingThreshold(0, false);
  66         verifyLockingThreshold(100, false);
  67         verifyLockingThreshold(1000, false);
  68 
  69         verifyLockingThreshold(0, true);
  70         verifyLockingThreshold(100, true);
  71         verifyLockingThreshold(1000, true);
  72     }
  73 
  74     private void verifyLockingThreshold(int lockingThreshold,
  75             boolean useStackLock) throws Throwable {
  76         CompilableTest test = new Test();
  77 
  78         int abortThreshold = Math.max(lockingThreshold / 2,
  79                 TestRTMLockingThreshold.MIN_ABORT_THRESHOLD);
  80 
  81         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  82                 test,
  83                 "-XX:CompileThreshold=1",
  84                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  85                         useStackLock),
  86                 "-XX:+UseRTMDeopt",
  87                 "-XX:RTMTotalCountIncrRate=1",
  88                 "-XX:RTMRetryCount=0",
  89                 CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold",
  90                         abortThreshold),
  91                 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",
  92                         lockingThreshold),
  93                 "-XX:RTMAbortRatio=100",
  94                 "-XX:+PrintPreciseRTMLockingStatistics",
  95                 Test.class.getName(),
  96                 Boolean.toString(!useStackLock),
  97                 Integer.toString(lockingThreshold)
  98         );
  99 
 100         outputAnalyzer.shouldHaveExitValue(0);
 101 
 102         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
 103                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
 104 
 105         Asserts.assertEQ(statistics.size(), 2, "VM output should contain two "
 106                 + "RTM locking statistics entries.");
 107 
 108         /**
 109          * If RTMLockingThreshold==0, then we have to make at least 1 call.
 110          */
 111         long expectedValue = lockingThreshold;
 112         if (expectedValue == 0) {
 113             expectedValue++;
 114         }
 115 
 116         RTMLockingStatistics statBeforeDeopt = null;
 117         for (RTMLockingStatistics s : statistics) {
 118             if (s.getTotalLocks() == expectedValue) {
 119                 Asserts.assertNull(statBeforeDeopt,
 120                         "Only one statistics entry should contain aborts");
 121                 statBeforeDeopt = s;
 122             }
 123         }
 124 
 125         Asserts.assertNotNull(statBeforeDeopt, "There should be exactly one "
 126                 + "statistics entry corresponding to ProfileRTM state.");
 127     }
 128 
 129     public static class Test implements CompilableTest {
 130         // Following field have to be static in order to avoid escape analysis.
 131         @SuppressWarnings("UnsuedDeclaration")
 132         private static int field = 0;
 133         private static final int TOTAL_ITERATIONS = 10000;
 134         private static final Unsafe UNSAFE = Utils.getUnsafe();
 135         private final Object monitor = new Object();
 136 
 137 
 138         @Override
 139         public String getMethodWithLockName() {
 140             return this.getClass().getName() + "::lock";
 141         }
 142 
 143         @Override
 144         public String[] getMethodsToCompileNames() {
 145             return new String[] { getMethodWithLockName() };
 146         }
 147 
 148         public void lock(boolean abort) {
 149             synchronized(monitor) {
 150                 if (abort) {
 151                     Test.field += Test.UNSAFE.addressSize();
 152                 }
 153             }
 154         }
 155 
 156         /**
 157          * Usage:
 158          * Test &lt;inflate monitor&gt;
 159          */
 160         public static void main(String args[]) throws Throwable {
 161             Asserts.assertGTE(args.length, 2, "Two arguments required.");
 162             Test t = new Test();
 163             boolean shouldBeInflated = Boolean.valueOf(args[0]);
 164             int lockingThreshold = Integer.valueOf(args[1]);
 165             if (shouldBeInflated) {
 166                 AbortProvoker.inflateMonitor(t.monitor);
 167             }
 168             for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) {
 169                 AbortProvoker.verifyMonitorState(t.monitor, shouldBeInflated);
 170                 t.lock(i >= lockingThreshold / 2);
 171             }
 172         }
 173     }
 174 
 175     public static void main(String args[]) throws Throwable {
 176         new TestRTMLockingThreshold().test();
 177     }
 178 }