LevelTransitionTest.java 9.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.Callable;

/**
 * @test LevelTransitionTest
 * @library /testlibrary /../../test/lib /compiler/whitebox
32 33
 * @modules java.base/sun.misc
 *          java.management
34
 * @ignore 8067651
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
 * @build TransitionsTestExecutor LevelTransitionTest
 * @run main ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
 * @run main/othervm/timeout=240 -Xmixed -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
 *                   -XX:+WhiteBoxAPI -XX:+TieredCompilation
 *                   -XX:CompileCommand=compileonly,SimpleTestCase$Helper::*
 *                   -XX:CompileCommand=compileonly,ExtendedTestCase$CompileMethodHolder::*
 *                   TransitionsTestExecutor LevelTransitionTest
 * @summary Test the correctness of compilation level transitions for different methods
 */
public class LevelTransitionTest extends TieredLevelsTest {
    /** Shows if method was profiled by being executed on levels 2 or 3 */
    protected boolean isMethodProfiled;
    private int transitionCount;

    public static void main(String[] args) throws Throwable {
        assert (!CompilerWhiteBoxTest.skipOnTieredCompilation(false));

        CompilerWhiteBoxTest.main(LevelTransitionTest::new, args);
        // run extended test cases
        for (TestCase testCase : ExtendedTestCase.values()) {
            new LevelTransitionTest(testCase).runTest();
        }
    }

    protected LevelTransitionTest(TestCase testCase) {
        super(testCase);
        isMethodProfiled = testCase.isOsr(); // OSR methods were already profiled by warmup
        transitionCount = 0;
    }

    @Override
    protected void test() throws Exception {
        checkTransitions();
        deoptimize();
        printInfo();
        if (testCase.isOsr()) {
            // deoptimization makes the following transitions be unstable
            // methods go to level 3 before 4 because of uncommon_trap and reprofile
            return;
        }
        checkTransitions();
    }

    /**
     * Makes and verifies transitions between compilation levels
     */
    protected void checkTransitions() {
        checkNotCompiled();
        boolean finish = false;
        while (!finish) {
            System.out.printf("Level transition #%d%n", ++transitionCount);
            int newLevel;
            int current = getCompLevel();
            int expected = getNextLevel(current);
            if (current == expected) {
                // if we are on expected level, just execute it more
                // to ensure that the level won't change
                System.out.printf("Method %s is already on expected level %d%n", method, expected);
                compile();
                newLevel = getCompLevel();
                finish = true;
            } else {
                newLevel = changeCompLevel();
                finish = false;
            }
            System.out.printf("Method %s is compiled on level %d. Expected level is %d%n", method, newLevel, expected);
            checkLevel(expected, newLevel);
            printInfo();
        };
    }

    /**
     * Gets next expected level for the test case on each transition.
     *
     * @param currentLevel a level the test case is compiled on
     * @return expected compilation level
     */
    protected int getNextLevel(int currentLevel) {
        int nextLevel = currentLevel;
        switch (currentLevel) {
            case CompilerWhiteBoxTest.COMP_LEVEL_NONE:
                nextLevel = isMethodProfiled ? CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION
                        : CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE;
                break;
            case CompilerWhiteBoxTest.COMP_LEVEL_LIMITED_PROFILE:
            case CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE:
                nextLevel = CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION;
                isMethodProfiled = true;
                break;
        }
        nextLevel = isTrivial() ? CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE : nextLevel;
        return Math.min(nextLevel, CompilerWhiteBoxTest.TIERED_STOP_AT_LEVEL);
    }

    /**
     * Determines if tested method should be handled as trivial
     *
     * @return {@code true} for trivial methods, {@code false} otherwise
     */
    protected boolean isTrivial() {
        return testCase == ExtendedTestCase.ACCESSOR_TEST
                || testCase == SimpleTestCase.METHOD_TEST
                || testCase == SimpleTestCase.STATIC_TEST
                || (testCase == ExtendedTestCase.TRIVIAL_CODE_TEST && isMethodProfiled);
    }

    /**
     * Invokes {@linkplain #method} until its compilation level is changed.
     * Note that if the level won't change, it will be an endless loop
     *
     * @return compilation level the {@linkplain #method} was compiled on
     */
    protected int changeCompLevel() {
        int currentLevel = getCompLevel();
        int newLevel = currentLevel;
        int result = 0;
        while (currentLevel == newLevel) {
            result = compile(1);
            if (WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) {
                newLevel = getCompLevel();
            }
        }
        return newLevel;
    }

    protected static class Helper {
        /**
         * Gets method from a specified class using its name
         *
         * @param aClass type method belongs to
         * @param name   the name of the method
         * @return {@link Method} that represents corresponding class method
         */
        public static Method getMethod(Class<?> aClass, String name) {
            Method method;
            try {
                method = aClass.getDeclaredMethod(name);
            } catch (NoSuchMethodException e) {
                throw new Error("TESTBUG: Unable to get method " + name, e);
            }
            return method;
        }

        /**
         * Gets {@link Callable} that invokes given method from the given object
         *
         * @param object the object the specified method is invoked from
         * @param name   the name of the method
         */
        public static Callable<Integer> getCallable(Object object, String name) {
            Method method = getMethod(object.getClass(), name);
            return () -> {
                try {
                    return Objects.hashCode(method.invoke(object));
                } catch (ReflectiveOperationException e) {
                    throw new Error("TESTBUG: Invocation failure", e);
                }
            };
        }
    }
}

enum ExtendedTestCase implements CompilerWhiteBoxTest.TestCase {
    ACCESSOR_TEST("accessor"),
    NONTRIVIAL_METHOD_TEST("nonTrivialMethod"),
    TRIVIAL_CODE_TEST("trivialCode");

    private final Executable executable;
    private final Callable<Integer> callable;

    @Override
    public Executable getExecutable() {
        return executable;
    }

    @Override
    public Callable<Integer> getCallable() {
        return callable;
    }

    @Override
    public boolean isOsr() {
        return false;
    }

    private ExtendedTestCase(String methodName) {
        this.executable = LevelTransitionTest.Helper.getMethod(CompileMethodHolder.class, methodName);
        this.callable = LevelTransitionTest.Helper.getCallable(new CompileMethodHolder(), methodName);
    }

    private static class CompileMethodHolder {
        private final int iter = 10;
        private int field = 42;

        /** Non-trivial method for threshold policy: contains loops */
        public int nonTrivialMethod() {
            int acc = 0;
            for (int i = 0; i < iter; i++) {
                acc += i;
            }
            return acc;
        }

        /** Field accessor method */
        public int accessor() {
            return field;
        }

        /** Method considered as trivial by amount of code */
        public int trivialCode() {
            int var = 0xBAAD_C0DE;
            var *= field;
            return var;
        }
    }
}