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