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