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 UseRTMLockEliding option could be applied to
  29  *          specified method and that such method will not be deoptimized
  30  *          on high abort ratio.
  31  * @library /testlibrary /../../test/lib /compiler/testlibrary
  32  * @build TestUseRTMLockElidingOption
  33  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  34  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  35  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  36  *                   -XX:+WhiteBoxAPI TestUseRTMLockElidingOption
  37  */
  38 
  39 import java.util.List;
  40 import com.oracle.java.testlibrary.*;
  41 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  42 import com.oracle.java.testlibrary.cli.predicate.AndPredicate;
  43 import rtm.*;
  44 import rtm.predicate.SupportedCPU;
  45 import rtm.predicate.SupportedVM;
  46 
  47 /**
  48  * Test verifies that method tagged with option <i>UseRTMLockElidingOption</i>
  49  * will use RTM-based lock elision, but will be never deoptimized with
  50  * <i>rtm_state_change reason</i>.
  51  * Test invokes compiled method and checks that no deoptimization with
  52  * <i>rtm_state_change</i> reason had happened and that that VM output
  53  * contains RTM locking statistics for compiled method and that total locks
  54  * count equals to method's invocations.
  55  * Since last assert is pretty strict, test uses -XX:RTMRetryCount=0 in order
  56  * to avoid issue with retriable aborts described in
  57  * {@link TestUseRTMAfterLockInflation}.
  58  */
  59 public class TestUseRTMLockElidingOption extends CommandLineOptionTest {
  60     private TestUseRTMLockElidingOption() {
  61         super(new AndPredicate(new SupportedCPU(), new SupportedVM()));
  62     }
  63 
  64     @Override
  65     public void runTestCases() throws Throwable {
  66         verifyOption(false);
  67         verifyOption(true);
  68     }
  69 
  70     public void verifyOption(boolean useStackLock) throws Throwable {
  71         AbortProvoker provoker = AbortType.XABORT.provoker();
  72         String logFileName = String.format("rtm_deopt_%s_stack_lock.xml",
  73                 (useStackLock ? "use" : "no"));
  74         String methodOption = String.format("-XX:CompileCommand=option," +
  75                 "%s,UseRTMLockEliding", provoker.getMethodWithLockName());
  76 
  77         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  78                 logFileName,
  79                 provoker,
  80                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  81                         useStackLock),
  82                 methodOption,
  83                 "-XX:RTMTotalCountIncrRate=1",
  84                 "-XX:RTMRetryCount=0",
  85                 "-XX:+UseRTMDeopt",
  86                 "-XX:+PrintPreciseRTMLockingStatistics",
  87                 provoker.getClass().getName(),
  88                 AbortType.XABORT.toString(),
  89                 Boolean.toString(!useStackLock)
  90         );
  91 
  92         outputAnalyzer.shouldHaveExitValue(0);
  93 
  94         int firedTraps = RTMTestBase.firedRTMStateChangeTraps(logFileName);
  95 
  96         Asserts.assertEQ(firedTraps, 0,
  97                 "Method deoptimization with rtm_state_change is unexpected");
  98 
  99         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
 100                 provoker.getMethodWithLockName(), outputAnalyzer.getOutput());
 101 
 102         Asserts.assertEQ(statistics.size(), 1,
 103                 "VM output should contain exactly one RTM locking "
 104                 + "statistics entry for method "
 105                 + provoker.getMethodWithLockName());
 106 
 107         RTMLockingStatistics lock = statistics.get(0);
 108 
 109         Asserts.assertEQ(lock.getTotalLocks(), AbortProvoker.DEFAULT_ITERATIONS,
 110                 "Expected to get total locks count equal to total amount of "
 111                 + "lock attempts.");
 112     }
 113 
 114     public static void main(String args[]) throws Throwable {
 115         new TestUseRTMLockElidingOption().test();
 116     }
 117 }