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