test/compiler/rtm/locking/TestRTMAbortRatio.java

changeset 0
f90c822e73f8
child 7798
9041e030d11f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/rtm/locking/TestRTMAbortRatio.java	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,163 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +/**
    1.29 + * @test
    1.30 + * @bug 8031320
    1.31 + * @summary Verify that RTMAbortRatio affects amount of aborts before
    1.32 + *          deoptimization.
    1.33 + * @library /testlibrary /testlibrary/whitebox /compiler/testlibrary
    1.34 + * @build TestRTMAbortRatio
    1.35 + * @run main ClassFileInstaller sun.hotspot.WhiteBox
    1.36 + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
    1.37 + *                   -XX:+WhiteBoxAPI TestRTMAbortRatio
    1.38 + */
    1.39 +
    1.40 +import java.util.List;
    1.41 +import com.oracle.java.testlibrary.*;
    1.42 +import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
    1.43 +import com.oracle.java.testlibrary.cli.predicate.AndPredicate;
    1.44 +import rtm.*;
    1.45 +import rtm.predicate.SupportedCPU;
    1.46 +import rtm.predicate.SupportedVM;
    1.47 +import sun.misc.Unsafe;
    1.48 +
    1.49 +/**
    1.50 + * Test verifies that method will be deoptimized on high abort ratio
    1.51 + * as soon as abort ratio reaches RTMAbortRatio's value.
    1.52 + */
    1.53 +public class TestRTMAbortRatio extends CommandLineOptionTest {
    1.54 +    private TestRTMAbortRatio() {
    1.55 +        super(new AndPredicate(new SupportedCPU(), new SupportedVM()));
    1.56 +    }
    1.57 +
    1.58 +    @Override
    1.59 +    protected void runTestCases() throws Throwable {
    1.60 +        verifyAbortRatio(0, false);
    1.61 +        verifyAbortRatio(10, false);
    1.62 +        verifyAbortRatio(50, false);
    1.63 +        verifyAbortRatio(100, false);
    1.64 +
    1.65 +        verifyAbortRatio(0, true);
    1.66 +        verifyAbortRatio(10, true);
    1.67 +        verifyAbortRatio(50, true);
    1.68 +        verifyAbortRatio(100, true);
    1.69 +    }
    1.70 +
    1.71 +    private void verifyAbortRatio(int abortRatio, boolean useStackLock)
    1.72 +            throws Throwable {
    1.73 +        CompilableTest test = new Test();
    1.74 +
    1.75 +        OutputAnalyzer outputAnalyzer = RTMTestBase.executeRTMTest(
    1.76 +                test,
    1.77 +                CommandLineOptionTest.prepareBooleanFlag("UseRTMForStackLocks",
    1.78 +                        useStackLock),
    1.79 +                "-XX:+UseRTMDeopt",
    1.80 +                "-XX:RTMTotalCountIncrRate=1",
    1.81 +                "-XX:RTMAbortThreshold=0",
    1.82 +                CommandLineOptionTest.prepareNumericFlag("RTMLockingThreshold",
    1.83 +                        10 * Test.TOTAL_ITERATIONS),
    1.84 +                CommandLineOptionTest.prepareNumericFlag("RTMAbortRatio",
    1.85 +                        abortRatio),
    1.86 +                "-XX:+PrintPreciseRTMLockingStatistics",
    1.87 +                test.getClass().getName(),
    1.88 +                Boolean.toString(!useStackLock));
    1.89 +
    1.90 +        outputAnalyzer.shouldHaveExitValue(0);
    1.91 +
    1.92 +        List<RTMLockingStatistics> statistics = RTMLockingStatistics.fromString(
    1.93 +                test.getMethodWithLockName(), outputAnalyzer.getOutput());
    1.94 +
    1.95 +        Asserts.assertEQ(statistics.size(), 1, "VM output should contain "
    1.96 +                + "exactly one RTM locking statistics entry.");
    1.97 +
    1.98 +        RTMLockingStatistics lock = statistics.get(0);
    1.99 +        int actualRatio;
   1.100 +
   1.101 +        if (lock.getTotalAborts() == 1L) {
   1.102 +            actualRatio = 0;
   1.103 +        } else {
   1.104 +            actualRatio = (int) (lock.getTotalLocks()
   1.105 +                    / (lock.getTotalAborts() - 1L));
   1.106 +        }
   1.107 +
   1.108 +        Asserts.assertLTE(actualRatio, abortRatio, String.format(
   1.109 +                "Actual abort ratio (%d) should lower or equal to "
   1.110 +                + "specified (%d).", actualRatio, abortRatio));
   1.111 +    }
   1.112 +
   1.113 +    /**
   1.114 +     * Force abort after {@code Test.WARMUP_ITERATIONS} is done.
   1.115 +     */
   1.116 +    public static class Test implements CompilableTest {
   1.117 +        private static final int TOTAL_ITERATIONS = 10000;
   1.118 +        private static final int WARMUP_ITERATIONS = 1000;
   1.119 +        private static final Unsafe UNSAFE = Utils.getUnsafe();
   1.120 +        private final Object monitor = new Object();
   1.121 +        // Following field have to be static in order to avoid escape analysis.
   1.122 +        @SuppressWarnings("UnsuedDeclaration")
   1.123 +        private static int field = 0;
   1.124 +
   1.125 +        @Override
   1.126 +        public String getMethodWithLockName() {
   1.127 +             return this.getClass().getName() + "::lock";
   1.128 +         }
   1.129 +
   1.130 +        @Override
   1.131 +        public String[] getMethodsToCompileNames() {
   1.132 +            return new String[] {
   1.133 +                    getMethodWithLockName(),
   1.134 +                    Unsafe.class.getName() + "::addressSize"
   1.135 +            };
   1.136 +        }
   1.137 +
   1.138 +        public void lock(boolean abort) {
   1.139 +            synchronized(monitor) {
   1.140 +                if (abort) {
   1.141 +                    Test.UNSAFE.addressSize();
   1.142 +                }
   1.143 +            }
   1.144 +        }
   1.145 +
   1.146 +        /**
   1.147 +         * Usage:
   1.148 +         * Test &lt;inflate monitor&gt;
   1.149 +         */
   1.150 +        public static void main(String args[]) throws Throwable {
   1.151 +            Asserts.assertGTE(args.length, 1, "One argument required.");
   1.152 +            Test t = new Test();
   1.153 +            if (Boolean.valueOf(args[0])) {
   1.154 +                AbortProvoker.inflateMonitor(t.monitor);
   1.155 +            }
   1.156 +            for (int i = 0; i < Test.TOTAL_ITERATIONS; i++) {
   1.157 +                t.lock(i >= Test.WARMUP_ITERATIONS);
   1.158 +            }
   1.159 +        }
   1.160 +    }
   1.161 +
   1.162 +    public static void main(String args[]) throws Throwable {
   1.163 +        new TestRTMAbortRatio().test();
   1.164 +    }
   1.165 +}
   1.166 +

mercurial