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