hotspot/test/compiler/rtm/locking/TestRTMLockingThreshold.java
changeset 24006 54399106938f
child 25958 8dc85547d6d6
equal deleted inserted replaced
24005:6841a4be0faa 24006:54399106938f
       
     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 /testlibrary/whitebox /compiler/testlibrary
       
    31  * @build TestRTMLockingThreshold
       
    32  * @run main ClassFileInstaller sun.hotspot.WhiteBox
       
    33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
       
    34  *                   -XX:+WhiteBoxAPI TestRTMLockingThreshold
       
    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 RTMLockingThreshold option actually affects how soon
       
    48  * method will be deoptimized on low abort ratio.
       
    49  */
       
    50 public class TestRTMLockingThreshold extends CommandLineOptionTest {
       
    51     private TestRTMLockingThreshold() {
       
    52         super(new AndPredicate(new SupportedVM(), new SupportedCPU()));
       
    53     }
       
    54 
       
    55     /**
       
    56      * We use non-zero abort threshold to avoid abort related to
       
    57      * interrupts, VMM calls, etc. during first lock attempt.
       
    58      *
       
    59      */
       
    60     private static final int ABORT_THRESHOLD = 10;
       
    61 
       
    62     @Override
       
    63     protected void runTestCases() throws Throwable {
       
    64         verifyLockingThreshold(0, false);
       
    65         verifyLockingThreshold(100, false);
       
    66         verifyLockingThreshold(1000, false);
       
    67 
       
    68         verifyLockingThreshold(0, true);
       
    69         verifyLockingThreshold(100, true);
       
    70         verifyLockingThreshold(1000, true);
       
    71     }
       
    72 
       
    73     private void verifyLockingThreshold(int lockingThreshold,
       
    74             boolean useStackLock) throws Throwable {
       
    75         CompilableTest test = new Test();
       
    76 
       
    77         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
       
    78                 test,
       
    79                 "-XX:CompileThreshold=1",
       
    80                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
       
    81                         useStackLock),
       
    82                 "-XX:+UseRTMDeopt",
       
    83                 "-XX:RTMTotalCountIncrRate=1",
       
    84                 "-XX:RTMRetryCount=0",
       
    85                 CommandLineOptionTest.prepareNumericFlag("RTMAbortThreshold",
       
    86                         TestRTMLockingThreshold.ABORT_THRESHOLD),
       
    87                 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",
       
    88                         lockingThreshold),
       
    89                 "-XX:RTMAbortRatio=100",
       
    90                 "-XX:+PrintPreciseRTMLockingStatistics",
       
    91                 Test.class.getName(),
       
    92                 Boolean.toString(!useStackLock),
       
    93                 Integer.toString(lockingThreshold)
       
    94         );
       
    95 
       
    96         outputAnalyzer.shouldHaveExitValue(0);
       
    97 
       
    98         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
       
    99                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
       
   100 
       
   101         Asserts.assertEQ(statistics.size(), 2, "VM output should contain two "
       
   102                 + "RTM locking statistics entries.");
       
   103 
       
   104         /**
       
   105          * We force abort on each odd iteration, so if RTMLockingThreshold==0,
       
   106          * then we have to make 1 call without abort to avoid rtm state
       
   107          * transition to NoRTM (otherwise actual abort ratio will be 100%),
       
   108          * and after that make 1 call with abort to force deoptimization.
       
   109          * This leads us to two locks for threshold 0.
       
   110          * For other threshold values we have to make RTMLockingThreshold + 1
       
   111          * locks if locking threshold is even, or + 0 if odd.
       
   112          */
       
   113         long expectedValue = lockingThreshold +
       
   114                 (lockingThreshold == 0L ? 2L : lockingThreshold % 2L);
       
   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[] {
       
   146                 getMethodWithLockName(),
       
   147                 sun.misc.Unsafe.class.getName() + "::addressSize"
       
   148             };
       
   149         }
       
   150 
       
   151         public void lock(boolean abort) {
       
   152             synchronized(monitor) {
       
   153                 if (abort) {
       
   154                     Test.field += Test.UNSAFE.addressSize();
       
   155                 }
       
   156             }
       
   157         }
       
   158 
       
   159         /**
       
   160          * Usage:
       
   161          * Test &lt;inflate monitor&gt;
       
   162          */
       
   163         public static void main(String args[]) throws Throwable {
       
   164             Asserts.assertGTE(args.length, 1, "One argument required.");
       
   165             Test t = new Test();
       
   166 
       
   167             if (Boolean.valueOf(args[0])) {
       
   168                 AbortProvoker.inflateMonitor(t.monitor);
       
   169             }
       
   170             for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) {
       
   171                 t.lock(i % 2 == 1);
       
   172             }
       
   173         }
       
   174     }
       
   175 
       
   176     public static void main(String args[]) throws Throwable {
       
   177         new TestRTMLockingThreshold().test();
       
   178     }
       
   179 }