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