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 UseRTMXendForLockBusy option affects
  29  *          method behaviour if lock is busy.
  30  * @library /testlibrary /../../test/lib /compiler/testlibrary
  31  * @build com.oracle.java.testlibrary.* TestUseRTMXendForLockBusy
  32  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  33  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  35  *                   -XX:+WhiteBoxAPI TestUseRTMXendForLockBusy
  36  */
  37 
  38 import java.util.List;
  39 
  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 with +UseRTMXendForLockBusy there will be no aborts
  49  * forced by the test.
  50  */
  51 public class TestUseRTMXendForLockBusy extends CommandLineOptionTest {
  52     private final static int LOCKING_TIME = 5000;
  53 
  54     private TestUseRTMXendForLockBusy() {
  55         super(new AndPredicate(new SupportedVM(), new SupportedCPU()));
  56     }
  57 
  58     @Override
  59     protected void runTestCases() throws Throwable {
  60         // inflated lock, xabort on lock busy
  61         verifyXendForLockBusy(true, false);
  62         // inflated lock, xend on lock busy
  63         verifyXendForLockBusy(true, true);
  64         // stack lock, xabort on lock busy
  65         verifyXendForLockBusy(false, false);
  66         // stack lock, xend on lock busy
  67         verifyXendForLockBusy(false, true);
  68     }
  69 
  70     private void verifyXendForLockBusy(boolean inflateMonitor,
  71             boolean useXend) throws Throwable {
  72         CompilableTest test = new BusyLock();
  73 
  74         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  75                 test,
  76                 CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
  77                         inflateMonitor),
  78                 CommandLineOptionTest.prepareBooleanFlag(
  79                         "UseRTMXendForLockBusy",
  80                         useXend),
  81                 "-XX:RTMRetryCount=0",
  82                 "-XX:RTMTotalCountIncrRate=1",
  83                 "-XX:+PrintPreciseRTMLockingStatistics",
  84                 BusyLock.class.getName(),
  85                 Boolean.toString(inflateMonitor),
  86                 Integer.toString(TestUseRTMXendForLockBusy.LOCKING_TIME)
  87         );
  88 
  89         outputAnalyzer.shouldHaveExitValue(0);
  90 
  91         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
  92                 test.getMethodWithLockName(), outputAnalyzer.getOutput());
  93 
  94         Asserts.assertEQ(statistics.size(), 1, "VM output should contain "
  95                 + "exactly one rtm locking statistics entry for method "
  96                 + test.getMethodWithLockName());
  97 
  98         long aborts = statistics.get(0).getAborts(AbortType.XABORT);
  99 
 100         if (useXend) {
 101             Asserts.assertEQ(aborts, 0L,
 102                     "Expected to get no aborts on busy lock");
 103         } else {
 104             Asserts.assertGT(aborts, 0L,
 105                     "Expected to get at least one abort on busy lock");
 106         }
 107     }
 108 
 109     public static void main(String args[]) throws Throwable {
 110         new TestUseRTMXendForLockBusy().test();
 111     }
 112 }