AllocateCompileIdTest.java revision 12158:0fe2815ffa74
113007Swpaul/*
213007Swpaul * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
313007Swpaul * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
413007Swpaul *
513007Swpaul * This code is free software; you can redistribute it and/or modify it
613007Swpaul * under the terms of the GNU General Public License version 2 only, as
713007Swpaul * published by the Free Software Foundation.
813007Swpaul *
913007Swpaul * This code is distributed in the hope that it will be useful, but WITHOUT
1013007Swpaul * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1113007Swpaul * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1213007Swpaul * version 2 for more details (a copy is included in the LICENSE file that
1313007Swpaul * accompanied this code).
1413007Swpaul *
1513007Swpaul * You should have received a copy of the GNU General Public License version
1613007Swpaul * 2 along with this work; if not, write to the Free Software Foundation,
1713007Swpaul * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1813007Swpaul *
1913007Swpaul * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2013007Swpaul * or visit www.oracle.com if you need additional information or have any
2113007Swpaul * questions.
2213007Swpaul */
2313007Swpaul
2413007Swpaul/**
2513007Swpaul * @test
2613007Swpaul * @bug 8136421
2713007Swpaul * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
2813007Swpaul * @library /test/lib /
2913007Swpaul * @library ../common/patches
3013007Swpaul * @modules java.base/jdk.internal.misc
3113007Swpaul * @modules java.base/jdk.internal.org.objectweb.asm
3231626Scharnier *          java.base/jdk.internal.org.objectweb.asm.tree
3331626Scharnier *          jdk.vm.ci/jdk.vm.ci.hotspot
3431626Scharnier *          jdk.vm.ci/jdk.vm.ci.code
3550476Speter *
3631626Scharnier * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
3731626Scharnier * @run driver ClassFileInstaller sun.hotspot.WhiteBox
3831626Scharnier *                                sun.hotspot.WhiteBox$WhiteBoxPermission
3913007Swpaul * @run main/othervm -Xbootclasspath/a:.
4013007Swpaul *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
4113007Swpaul *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
4213007Swpaul *                   -XX:-BackgroundCompilation
4331626Scharnier *                   compiler.jvmci.compilerToVM.AllocateCompileIdTest
4413007Swpaul */
4513007Swpaul
4613007Swpaulpackage compiler.jvmci.compilerToVM;
4713007Swpaul
4813007Swpaulimport compiler.jvmci.common.CTVMUtilities;
4913007Swpaulimport jdk.test.lib.Asserts;
5013376Swpaulimport jdk.test.lib.util.Pair;
5113007Swpaulimport jdk.test.lib.Utils;
5213007Swpaulimport jdk.vm.ci.hotspot.CompilerToVMHelper;
5313007Swpaulimport jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
5416132Swpaulimport sun.hotspot.code.NMethod;
5513007Swpaul
5613007Swpaulimport java.lang.reflect.Executable;
5713007Swpaulimport java.lang.reflect.Method;
5813007Swpaulimport java.util.ArrayList;
5913007Swpaulimport java.util.HashSet;
6013007Swpaulimport java.util.List;
6113007Swpaulimport java.util.stream.Collectors;
6213007Swpaulimport java.util.stream.Stream;
6313007Swpaul
6413007Swpaulpublic class AllocateCompileIdTest {
6513007Swpaul
6613007Swpaul    private static final int SOME_REPEAT_VALUE = 5;
6790298Sdes    private final HashSet<Integer> ids = new HashSet<>();
6890298Sdes
6913007Swpaul    public static void main(String[] args) {
7013007Swpaul        AllocateCompileIdTest test = new AllocateCompileIdTest();
7113007Swpaul        createTestCasesCorrectBci().forEach(test::runSanityCorrectTest);
7213007Swpaul        createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest);
7313007Swpaul    }
7413007Swpaul
7513007Swpaul    private static List<CompileCodeTestCase> createTestCasesCorrectBci() {
7613276Swpaul        List<CompileCodeTestCase> result = new ArrayList<>();
7713276Swpaul        try {
7813007Swpaul            Class<?> aClass = DummyClass.class;
7913007Swpaul            Method method = aClass.getMethod("withLoop");
8013007Swpaul            Object receiver = new DummyClass();
8113007Swpaul            result.add(new CompileCodeTestCase(receiver, method, 17));
8213007Swpaul            result.add(new CompileCodeTestCase(receiver, method, -1));
8346929Swpaul        } catch (NoSuchMethodException e) {
8413007Swpaul            throw new Error("TEST BUG : " + e, e);
8513007Swpaul        }
8613007Swpaul        return result;
8713007Swpaul    }
8813007Swpaul
8946929Swpaul    private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>>
9046929Swpaul            createTestCasesIncorrectBci() {
9113007Swpaul        List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result
9213007Swpaul                = new ArrayList<>();
9313007Swpaul        try {
9413007Swpaul            Class<?> aClass = DummyClass.class;
9513007Swpaul            Object receiver = new DummyClass();
9613007Swpaul            Method method = aClass.getMethod("dummyInstanceFunction");
9713007Swpaul            // greater than bytecode.length
9813007Swpaul            byte[] bytecode = CompilerToVMHelper.getBytecode(CTVMUtilities
9913007Swpaul                    .getResolvedMethod(method));
10013007Swpaul            Stream.of(
10113007Swpaul                    // greater than bytecode.length
10213007Swpaul                    bytecode.length + 4,
10313007Swpaul                    bytecode.length + 50,
10413007Swpaul                    bytecode.length + 200,
10513007Swpaul                    // negative cases
10613007Swpaul                    -4, -50, -200)
10713007Swpaul                    .map(bci -> new Pair<CompileCodeTestCase,
10813007Swpaul                            Class<? extends Throwable>>(
10990298Sdes                            new CompileCodeTestCase(receiver, method, bci),
11090298Sdes                            IllegalArgumentException.class))
11113007Swpaul                    .collect(Collectors.toList());
11213007Swpaul        } catch (NoSuchMethodException e) {
11313007Swpaul            throw new Error("TEST BUG : " + e.getMessage(), e);
11413007Swpaul        }
11531626Scharnier        return result;
11631626Scharnier    }
11731626Scharnier
11831626Scharnier    private void runSanityCorrectTest(CompileCodeTestCase testCase) {
11913007Swpaul        System.out.println(testCase);
12013007Swpaul        Executable aMethod = testCase.executable;
12113007Swpaul        // to generate ciTypeFlow
12213007Swpaul        testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes()));
12390298Sdes        int bci = testCase.bci;
12490298Sdes        HotSpotResolvedJavaMethod method = CTVMUtilities
12590298Sdes                .getResolvedMethod(aMethod);
12613007Swpaul        for (int i = 0; i < SOME_REPEAT_VALUE; ++i) {
12713007Swpaul            int wbCompileID = getWBCompileID(testCase);
12813007Swpaul            int id = CompilerToVMHelper.allocateCompileId(method, bci);
12913007Swpaul            Asserts.assertNE(id, 0, testCase + " : zero compile id");
13013007Swpaul            Asserts.assertGT(id, wbCompileID, testCase
13113007Swpaul                    + " : allocated 'compile id' not  greater than existed");
13230008Swpaul            Asserts.assertTrue(ids.add(wbCompileID), testCase
13330008Swpaul                    + " : vm compilation allocated existing id " + id);
13430008Swpaul            Asserts.assertTrue(ids.add(id), testCase
13530008Swpaul                    + " : allocateCompileId returned existing id " + id);
13630008Swpaul        }
13730008Swpaul    }
13830008Swpaul
13930008Swpaul    private void runSanityIncorrectTest(
14030008Swpaul            Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) {
14130008Swpaul        System.out.println(testCase);
14230008Swpaul        Class<? extends Throwable> exception = testCase.second;
14330008Swpaul        Executable aMethod = testCase.first.executable;
14430008Swpaul        int bci = testCase.first.bci;
14530008Swpaul        HotSpotResolvedJavaMethod method = CTVMUtilities
14630008Swpaul                .getResolvedMethod(aMethod);
14730008Swpaul        Utils.runAndCheckException(
14830008Swpaul                () -> CompilerToVMHelper.allocateCompileId(method, bci),
14930008Swpaul                exception);
15030008Swpaul    }
15130008Swpaul
15213007Swpaul    private int getWBCompileID(CompileCodeTestCase testCase) {
15316132Swpaul        NMethod nm = testCase.deoptimizeAndCompile();
15413007Swpaul        if (nm == null || nm.compile_id <= 0) {
15513007Swpaul            throw new Error("TEST BUG : cannot compile method " + testCase);
15613007Swpaul        }
15713007Swpaul        return nm.compile_id;
15813007Swpaul    }
15931626Scharnier}
16090298Sdes