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 rtm locking is used for stack locks before
  29  *          inflation and after it used for inflated locks.
  30  * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary
  31  * @build TestUseRTMAfterLockInflation
  32  * @run main/othervm/bootclasspath -XX:+UnlockDiagnosticVMOptions
  33  *                   -XX:+WhiteBoxAPI TestUseRTMAfterLockInflation
  34  */
  35 
  36 import java.util.List;
  37 
  38 import com.oracle.java.testlibrary.*;
  39 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  40 import com.oracle.java.testlibrary.cli.predicate.AndPredicate;
  41 import rtm.*;
  42 import rtm.predicate.SupportedCPU;
  43 import rtm.predicate.SupportedVM;
  44 
  45 /**
  46  * Test verifies that RTM is used after lock inflation by executing compiled
  47  * method with RTM-based lock elision using stack lock first, then that lock
  48  * is inflated and the same compiled method invoked again.
  49  *
  50  * Compiled method invoked {@code AbortProvoker.DEFAULT_ITERATIONS} times before
  51  * lock inflation and the same amount of times after inflation.
  52  * As a result total locks count should be equal to
  53  * {@code 2*AbortProvoker.DEFAULT_ITERATIONS}.
  54  * It is a pretty strict assertion which could fail if some retriable abort
  55  * happened: it could be {@code AbortType.RETRIABLE} or
  56  * {@code AbortType.MEM_CONFLICT}, but unfortunately abort can has both these
  57  * reasons simultaneously. In order to avoid false negative failures related
  58  * to incorrect aborts counting, -XX:RTMRetryCount=0 is used.
  59  */
  60 public class TestUseRTMAfterLockInflation extends CommandLineOptionTest {
  61     private static final long EXPECTED_LOCKS
  62             = 2L * AbortProvoker.DEFAULT_ITERATIONS;
  63 
  64     private TestUseRTMAfterLockInflation() {
  65         super(new AndPredicate(new SupportedVM(), new SupportedCPU()));
  66     }
  67 
  68     @Override
  69     protected void runTestCases() throws Throwable {
  70         AbortProvoker provoker = AbortType.XABORT.provoker();
  71         long totalLocksCount = 0;
  72 
  73         OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
  74                 provoker,
  75                 "-XX:+UseRTMForStackLocks",
  76                 "-XX:RTMTotalCountIncrRate=1",
  77                 "-XX:RTMRetryCount=0",
  78                 "-XX:+PrintPreciseRTMLockingStatistics",
  79                 Test.class.getName(),
  80                 AbortType.XABORT.toString());
  81 
  82         outputAnalyzer.shouldHaveExitValue(0);
  83 
  84         List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
  85                 provoker.getMethodWithLockName(), outputAnalyzer.getOutput());
  86 
  87         Asserts.assertEQ(statistics.size(), 2,
  88                 "VM output should contain two rtm locking statistics entries "
  89                 + "for method " + provoker.getMethodWithLockName());
  90 
  91         for (RTMLockingStatistics s : statistics) {
  92             totalLocksCount += s.getTotalLocks();
  93         }
  94 
  95         Asserts.assertEQ(totalLocksCount,
  96                 TestUseRTMAfterLockInflation.EXPECTED_LOCKS,
  97                 "Total lock count should be greater or equal to "
  98                 + TestUseRTMAfterLockInflation.EXPECTED_LOCKS);
  99     }
 100 
 101     public static class Test {
 102 
 103         /**
 104          * Usage:
 105          * Test &lt;provoker type&gt;
 106          */
 107         public static void main(String args[]) throws Throwable {
 108             Asserts.assertGT(args.length, 0,
 109                     "AbortType name is expected as first argument.");
 110 
 111             AbortProvoker provoker
 112                     = AbortType.lookup(Integer.valueOf(args[0])).provoker();
 113             for (int i = 0; i < AbortProvoker.DEFAULT_ITERATIONS; i++) {
 114                 provoker.forceAbort();
 115             }
 116             provoker.inflateMonitor();
 117             for (int i = 0; i < AbortProvoker.DEFAULT_ITERATIONS; i++) {
 118                 provoker.forceAbort();
 119             }
 120         }
 121     }
 122 
 123     public static void main(String args[]) throws Throwable {
 124         new TestUseRTMAfterLockInflation().test();
 125     }
 126 }