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