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