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 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         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  79                 test,
  80                 "-XX:CompileThreshold=1",
  81                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  82                         useStackLock),
  83                 "-XX:+UseRTMDeopt",
  84                 "-XX:RTMTotalCountIncrRate=1",
  85                 "-XX:RTMRetryCount=0",
  86                 CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold",
  87                         TestRTMLockingThreshold.ABORT_THRESHOLD),
  88                 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",
  89                         lockingThreshold),
  90                 "-XX:RTMAbortRatio=100",
  91                 "-XX:+PrintPreciseRTMLockingStatistics",
  92                 Test.class.getName(),
  93                 Boolean.toString(!useStackLock),
  94                 Integer.toString(lockingThreshold)
  95         );
  96 
  97         outputAnalyzer.shouldHaveExitValue(0);
  98 
  99         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
 100                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
 101 
 102         Asserts.assertEQ(statistics.size(), 2, "VM output should contain two "
 103                 + "RTM locking statistics entries.");
 104 
 105         /**
 106          * We force abort on each odd iteration, so if RTMLockingThreshold==0,
 107          * then we have to make 1 call without abort to avoid rtm state
 108          * transition to NoRTM (otherwise actual abort ratio will be 100%),
 109          * and after that make 1 call with abort to force deoptimization.
 110          * This leads us to two locks for threshold 0.
 111          * For other threshold values we have to make RTMLockingThreshold + 1
 112          * locks if locking threshold is even, or + 0 if odd.
 113          */
 114         long expectedValue = lockingThreshold +
 115                 (lockingThreshold == 0L ? 2L : lockingThreshold % 2L);
 116 
 117         RTMLockingStatistics statBeforeDeopt = null;
 118         for (RTMLockingStatistics s : statistics) {
 119             if (s.getTotalLocks() == expectedValue) {
 120                 Asserts.assertNull(statBeforeDeopt,
 121                         "Only one statistics entry should contain aborts");
 122                 statBeforeDeopt = s;
 123             }
 124         }
 125 
 126         Asserts.assertNotNull(statBeforeDeopt, "There should be exactly one "
 127                 + "statistics entry corresponding to ProfileRTM state.");
 128     }
 129 
 130     public static class Test implements CompilableTest {
 131         // Following field have to be static in order to avoid escape analysis.
 132         @SuppressWarnings("UnsuedDeclaration")
 133         private static int field = 0;
 134         private static final int TOTAL_ITERATIONS = 10000;
 135         private static final Unsafe UNSAFE = Utils.getUnsafe();
 136         private final Object monitor = new Object();
 137 
 138 
 139         @Override
 140         public String getMethodWithLockName() {
 141             return this.getClass().getName() + "::lock";
 142         }
 143 
 144         @Override
 145         public String[] getMethodsToCompileNames() {
 146             return new String[] {
 147                 getMethodWithLockName(),
 148                 sun.misc.Unsafe.class.getName() + "::addressSize"
 149             };
 150         }
 151 
 152         public void lock(boolean abort) {
 153             synchronized(monitor) {
 154                 if (abort) {
 155                     Test.field += Test.UNSAFE.addressSize();
 156                 }
 157             }
 158         }
 159 
 160         /**
 161          * Usage:
 162          * Test &lt;inflate monitor&gt;
 163          */
 164         public static void main(String args[]) throws Throwable {
 165             Asserts.assertGTE(args.length, 1, "One argument required.");
 166             Test t = new Test();
 167 
 168             if (Boolean.valueOf(args[0])) {
 169                 AbortProvoker.inflateMonitor(t.monitor);
 170             }
 171             for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) {
 172                 t.lock(i % 2 == 1);
 173             }
 174         }
 175     }
 176 
 177     public static void main(String args[]) throws Throwable {
 178         new TestRTMLockingThreshold().test();
 179     }
 180 }