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