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