aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 8031320 aoqi@0: * @summary Verify that RTMAbortRatio affects amount of aborts before aoqi@0: * deoptimization. aoqi@0: * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary aoqi@0: * @build TestRTMAbortRatio aoqi@0: * @run main ClassFileInstaller sun.hotspot.WhiteBox aoqi@0: * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions aoqi@0: * -XX:+WhiteBoxAPI TestRTMAbortRatio aoqi@0: */ aoqi@0: aoqi@0: import java.util.List; aoqi@0: import com.oracle.java.testlibrary.*; aoqi@0: import com.oracle.java.testlibrary.cli.CommandLineOptionTest; aoqi@0: import com.oracle.java.testlibrary.cli.predicate.AndPredicate; aoqi@0: import rtm.*; aoqi@0: import rtm.predicate.SupportedCPU; aoqi@0: import rtm.predicate.SupportedVM; aoqi@0: import sun.misc.Unsafe; aoqi@0: aoqi@0: /** aoqi@0: * Test verifies that method will be deoptimized on high abort ratio aoqi@0: * as soon as abort ratio reaches RTMAbortRatio's value. aoqi@0: */ aoqi@0: public class TestRTMAbortRatio extends CommandLineOptionTest { aoqi@0: private TestRTMAbortRatio() { aoqi@0: super(new AndPredicate(new SupportedCPU(), new SupportedVM())); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: protected void runTestCases() throws Throwable { aoqi@0: verifyAbortRatio(0, false); aoqi@0: verifyAbortRatio(10, false); aoqi@0: verifyAbortRatio(50, false); aoqi@0: verifyAbortRatio(100, false); aoqi@0: aoqi@0: verifyAbortRatio(0, true); aoqi@0: verifyAbortRatio(10, true); aoqi@0: verifyAbortRatio(50, true); aoqi@0: verifyAbortRatio(100, true); aoqi@0: } aoqi@0: aoqi@0: private void verifyAbortRatio(int abortRatio, boolean useStackLock) aoqi@0: throws Throwable { aoqi@0: CompilableTest test = new Test(); aoqi@0: aoqi@0: OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest( aoqi@0: test, aoqi@0: CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks", aoqi@0: useStackLock), aoqi@0: "-XX:+UseRTMDeopt", aoqi@0: "-XX:RTMTotalCountIncrRate=1", aoqi@0: "-XX:RTMAbortThreshold=0", aoqi@0: CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold", aoqi@0: 10 * Test.TOTAL_ITERATIONS), aoqi@0: CommandLineOptionTest.prepareNumericFlag("RTMAbortRatio", aoqi@0: abortRatio), aoqi@0: "-XX:+PrintPreciseRTMLockingStatistics", aoqi@0: test.getClass().getName(), aoqi@0: Boolean.toString(!useStackLock)); aoqi@0: aoqi@0: outputAnalyzer.shouldHaveExitValue(0); aoqi@0: aoqi@0: List statistics = RTMLockingStatistics.fromString( aoqi@0: test.getMethodWithLockName(), outputAnalyzer.getOutput()); aoqi@0: aoqi@0: Asserts.assertEQ(statistics.size(), 1, "VM output should contain " aoqi@0: + "exactly one RTM locking statistics entry."); aoqi@0: aoqi@0: RTMLockingStatistics lock = statistics.get(0); aoqi@0: int actualRatio; aoqi@0: aoqi@0: if (lock.getTotalAborts() == 1L) { aoqi@0: actualRatio = 0; aoqi@0: } else { aoqi@0: actualRatio = (int) (lock.getTotalLocks() aoqi@0: / (lock.getTotalAborts() - 1L)); aoqi@0: } aoqi@0: aoqi@0: Asserts.assertLTE(actualRatio, abortRatio, String.format( aoqi@0: "Actual abort ratio (%d) should lower or equal to " aoqi@0: + "specified (%d).", actualRatio, abortRatio)); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Force abort after {@code Test.WARMUP_ITERATIONS} is done. aoqi@0: */ aoqi@0: public static class Test implements CompilableTest { aoqi@0: private static final int TOTAL_ITERATIONS = 10000; aoqi@0: private static final int WARMUP_ITERATIONS = 1000; aoqi@0: private static final Unsafe UNSAFE = Utils.getUnsafe(); aoqi@0: private final Object monitor = new Object(); aoqi@0: // Following field have to be static in order to avoid escape analysis. aoqi@0: @SuppressWarnings("UnsuedDeclaration") aoqi@0: private static int field = 0; aoqi@0: aoqi@0: @Override aoqi@0: public String getMethodWithLockName() { aoqi@0: return this.getClass().getName() + "::lock"; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String[] getMethodsToCompileNames() { aoqi@0: return new String[] { aoqi@0: getMethodWithLockName(), aoqi@0: Unsafe.class.getName() + "::addressSize" aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: public void lock(boolean abort) { aoqi@0: synchronized(monitor) { aoqi@0: if (abort) { aoqi@0: Test.UNSAFE.addressSize(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Usage: aoqi@0: * Test <inflate monitor> aoqi@0: */ aoqi@0: public static void main(String args[]) throws Throwable { aoqi@0: Asserts.assertGTE(args.length, 1, "One argument required."); aoqi@0: Test t = new Test(); aoqi@0: if (Boolean.valueOf(args[0])) { aoqi@0: AbortProvoker.inflateMonitor(t.monitor); aoqi@0: } aoqi@0: for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) { aoqi@0: t.lock(i >= Test.WARMUP_ITERATIONS); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static void main(String args[]) throws Throwable { aoqi@0: new TestRTMAbortRatio().test(); aoqi@0: } aoqi@0: } aoqi@0: