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 RTMAbortRatio affects amount of aborts before
  29  *          deoptimization.
  30  * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary
  31  * @build TestRTMAbortRatio
  32  * @run main/othervm/bootclasspath -XX:+UnlockDiagnosticVMOptions
  33  *                   -XX:+WhiteBoxAPI TestRTMAbortRatio
  34  */
  35 
  36 import java.util.List;
  37 import com.oracle.java.testlibrary.*;
  38 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  39 import com.oracle.java.testlibrary.cli.predicate.AndPredicate;
  40 import rtm.*;
  41 import rtm.predicate.SupportedCPU;
  42 import rtm.predicate.SupportedVM;
  43 import sun.misc.Unsafe;
  44 
  45 /**
  46  * Test verifies that method will be deoptimized on high abort ratio
  47  * as soon as abort ratio reaches RTMAbortRatio's value.
  48  */
  49 public class TestRTMAbortRatio extends CommandLineOptionTest {
  50     private TestRTMAbortRatio() {
  51         super(new AndPredicate(new SupportedCPU(), new SupportedVM()));
  52     }
  53 
  54     @Override
  55     protected void runTestCases() throws Throwable {
  56         verifyAbortRatio(0, false);
  57         verifyAbortRatio(10, false);
  58         verifyAbortRatio(50, false);
  59         verifyAbortRatio(100, false);
  60 
  61         verifyAbortRatio(0, true);
  62         verifyAbortRatio(10, true);
  63         verifyAbortRatio(50, true);
  64         verifyAbortRatio(100, true);
  65     }
  66 
  67     private void verifyAbortRatio(int abortRatio, boolean useStackLock)
  68             throws Throwable {
  69         CompilableTest test = new Test();
  70 
  71         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  72                 test,
  73                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  74                         useStackLock),
  75                 "-XX:+UseRTMDeopt",
  76                 "-XX:RTMTotalCountIncrRate=1",
  77                 "-XX:RTMAbortThreshold=0",
  78                 CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",
  79                         10 * Test.TOTAL_ITERATIONS),
  80                 CommandLineOptionTest.prepareNumericFlag("RTMAbortRatio",
  81                         abortRatio),
  82                 "-XX:+PrintPreciseRTMLockingStatistics",
  83                 test.getClass().getName(),
  84                 Boolean.toString(!useStackLock));
  85 
  86         outputAnalyzer.shouldHaveExitValue(0);
  87 
  88         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
  89                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
  90 
  91         Asserts.assertEQ(statistics.size(), 1, "VM output should contain "
  92                 + "exactly one RTM locking statistics entry.");
  93 
  94         RTMLockingStatistics lock = statistics.get(0);
  95         int actualRatio;
  96 
  97         if (lock.getTotalAborts() == 1L) {
  98             actualRatio = 0;
  99         } else {
 100             actualRatio = (int) (lock.getTotalLocks()
 101                     / (lock.getTotalAborts() - 1L));
 102         }
 103 
 104         Asserts.assertLTE(actualRatio, abortRatio, String.format(
 105                 "Actual abort ratio (%d) should lower or equal to "
 106                 + "specified (%d).", actualRatio, abortRatio));
 107     }
 108 
 109     /**
 110      * Force abort after {@code Test.WARMUP_ITERATIONS} is done.
 111      */
 112     public static class Test implements CompilableTest {
 113         private static final int TOTAL_ITERATIONS = 10000;
 114         private static final int WARMUP_ITERATIONS = 1000;
 115         private static final Unsafe UNSAFE = Utils.getUnsafe();
 116         private final Object monitor = new Object();
 117         // Following field have to be static in order to avoid escape analysis.
 118         @SuppressWarnings("UnsuedDeclaration")
 119         private static int field = 0;
 120 
 121         @Override
 122         public String getMethodWithLockName() {
 123              return this.getClass().getName() + "::lock";
 124          }
 125 
 126         @Override
 127         public String[] getMethodsToCompileNames() {
 128             return new String[] {
 129                     getMethodWithLockName(),
 130                     Unsafe.class.getName() + "::addressSize"
 131             };
 132         }
 133 
 134         public void lock(boolean abort) {
 135             synchronized(monitor) {
 136                 if (abort) {
 137                     Test.UNSAFE.addressSize();
 138                 }
 139             }
 140         }
 141 
 142         /**
 143          * Usage:
 144          * Test &lt;inflate monitor&gt;
 145          */
 146         public static void main(String args[]) throws Throwable {
 147             Asserts.assertGTE(args.length, 1, "One argument required.");
 148             Test t = new Test();
 149             if (Boolean.valueOf(args[0])) {
 150                 AbortProvoker.inflateMonitor(t.monitor);
 151             }
 152             for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) {
 153                 t.lock(i >= Test.WARMUP_ITERATIONS);
 154             }
 155         }
 156     }
 157 
 158     public static void main(String args[]) throws Throwable {
 159         new TestRTMAbortRatio().test();
 160     }
 161 }
 162