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