AllocateCompileIdTest.java revision 9287:40bd4478a362
190075Sobrien/*
2169689Skan * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3132718Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
490075Sobrien *
590075Sobrien * This code is free software; you can redistribute it and/or modify it
690075Sobrien * under the terms of the GNU General Public License version 2 only, as
7132718Skan * published by the Free Software Foundation.
890075Sobrien *
9132718Skan * This code is distributed in the hope that it will be useful, but WITHOUT
1090075Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1190075Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1290075Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1390075Sobrien * accompanied this code).
14132718Skan *
1590075Sobrien * You should have received a copy of the GNU General Public License version
1690075Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1790075Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1890075Sobrien *
1990075Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20132718Skan * or visit www.oracle.com if you need additional information or have any
21169689Skan * questions.
22169689Skan *
2390075Sobrien */
2490075Sobrien/**
2590075Sobrien * @test
2690075Sobrien * @bug 8136421
2790075Sobrien * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
2890075Sobrien * @library /testlibrary /../../test/lib /
2990075Sobrien * @compile ../common/CompilerToVMHelper.java
3090075Sobrien * @build sun.hotspot.WhiteBox
3190075Sobrien * @run main ClassFileInstaller sun.hotspot.WhiteBox
3290075Sobrien *                              sun.hotspot.WhiteBox$WhiteBoxPermission
3390075Sobrien *                              jdk.vm.ci.hotspot.CompilerToVMHelper
3490075Sobrien * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
3590075Sobrien *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
3690075Sobrien *      -XX:-BackgroundCompilation
3790075Sobrien        -XX:+LogCompilation
3890075Sobrien *      compiler.jvmci.compilerToVM.AllocateCompileIdTest
3990075Sobrien */
4090075Sobrien
4190075Sobrienpackage compiler.jvmci.compilerToVM;
4290075Sobrien
4390075Sobrienimport compiler.jvmci.common.CTVMUtilities;
4490075Sobrien
4590075Sobrienimport java.lang.reflect.Executable;
4690075Sobrienimport java.lang.reflect.Method;
4790075Sobrienimport java.util.ArrayList;
4890075Sobrienimport java.util.HashMap;
4990075Sobrienimport java.util.List;
5090075Sobrienimport java.util.Map;
5190075Sobrienimport java.util.HashSet;
52117395Skan
53117395Skanimport compiler.jvmci.common.testcases.TestCase;
54117395Skanimport jdk.vm.ci.hotspot.CompilerToVMHelper;
55117395Skanimport jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
56117395Skanimport jdk.test.lib.Asserts;
57117395Skanimport jdk.test.lib.Pair;
58117395Skanimport jdk.test.lib.Utils;
59169689Skanimport sun.hotspot.WhiteBox;
60117395Skanimport sun.hotspot.code.NMethod;
61117395Skan
62117395Skanpublic class AllocateCompileIdTest {
63117395Skan
64117395Skan    private final HashSet<Integer> ids = new HashSet<>();
65117395Skan
66117395Skan    public static void main(String[] args) {
67117395Skan        AllocateCompileIdTest test = new AllocateCompileIdTest();
68132718Skan        createTestCasesCorrectBci().forEach(test::runSanityCorrectTest);
69117395Skan        createTestCasesIncorrectBci().forEach(test::runSanityIncorrectTest);
70117395Skan    }
71117395Skan
72117395Skan
73117395Skan    private static List<CompileCodeTestCase> createTestCasesCorrectBci() {
74117395Skan        List<CompileCodeTestCase> result = new ArrayList<>();
75117395Skan        try {
76117395Skan            Class<?> aClass = DummyClass.class;
77117395Skan            Method method = aClass.getMethod("withLoop");
78122180Skan            Object receiver = new DummyClass();
79132718Skan            result.add(new CompileCodeTestCase(receiver, method, 17));
80132718Skan            result.add(new CompileCodeTestCase(receiver, method, -1));
81169689Skan        } catch (NoSuchMethodException e) {
82169689Skan            throw new Error("TEST BUG : " + e, e);
83169689Skan        }
84169689Skan        return result;
85169689Skan    }
86169689Skan
87169689Skan
88169689Skan    private static List<Pair<CompileCodeTestCase, Class<? extends Throwable>>>
89169689Skan            createTestCasesIncorrectBci() {
90117395Skan        List<Pair<CompileCodeTestCase, Class<? extends Throwable>>> result
91117395Skan                = new ArrayList<>();
92117395Skan
93117395Skan        try {
94117395Skan            Class<?> aClass = DummyClass.class;
95117395Skan            Object receiver = new DummyClass();
96117395Skan            Method method = aClass.getMethod("dummyInstanceFunction");
97117395Skan            // greater than bytecode.length
98117395Skan            int[] bcis = new int[] {30, 50, 200};
99117395Skan            for (int bci : bcis) {
100117395Skan                result.add(new Pair<>(
101117395Skan                        new CompileCodeTestCase(receiver, method, bci),
102169689Skan                        IllegalArgumentException.class));
103169689Skan            }
10490075Sobrien            bcis = new int[] {-4, -50, -200};
10590075Sobrien            for (int bci : bcis) {
10690075Sobrien                result.add(new Pair<>(
10790075Sobrien                        new CompileCodeTestCase(receiver, method, bci),
10890075Sobrien                        IllegalArgumentException.class));
10990075Sobrien            }
11090075Sobrien        } catch (NoSuchMethodException e) {
111132718Skan            throw new Error("TEST BUG : " + e.getMessage(), e);
112132718Skan        }
113132718Skan        return result;
114132718Skan    }
11590075Sobrien
11690075Sobrien    private void runSanityCorrectTest(CompileCodeTestCase testCase) {
11790075Sobrien        System.out.println(testCase);
11890075Sobrien        Executable aMethod = testCase.executable;
11990075Sobrien        // to generate ciTypeFlow
12090075Sobrien        System.out.println(testCase.invoke(Utils.getNullValues(aMethod.getParameterTypes())));
12190075Sobrien        int bci = testCase.bci;
12290075Sobrien        HotSpotResolvedJavaMethod method = CTVMUtilities
12390075Sobrien                .getResolvedMethod(aMethod);
12490075Sobrien        int wbCompileID = getWBCompileID(testCase);
12590075Sobrien        int id = CompilerToVMHelper.allocateCompileId(method, bci);
12690075Sobrien        Asserts.assertNE(id, 0, testCase + " : zero compile id");
12790075Sobrien
12890075Sobrien        if (wbCompileID > 0) {
12990075Sobrien            Asserts.assertGT(id, wbCompileID, testCase
13090075Sobrien                    + " : allocated 'compile id' not  greater than existed");
131117395Skan            if (!ids.add(wbCompileID)) {
132169689Skan                throw new AssertionError(String.format(
133169689Skan                        "%s : vm compilation allocated existed id -- %d",
134169689Skan                        testCase, id));
135169689Skan            }
136117395Skan        }
13790075Sobrien        if (!ids.add(id)) {
138169689Skan            throw new AssertionError(String.format(
13990075Sobrien                    "%s : allocateCompileId returned existed id %d",
140169689Skan                    testCase, id));
14190075Sobrien        }
14290075Sobrien    }
14390075Sobrien
144169689Skan    private void runSanityIncorrectTest(
145169689Skan            Pair<CompileCodeTestCase, Class<? extends Throwable>> testCase) {
146169689Skan        System.out.println(testCase);
14790075Sobrien        Class<? extends Throwable> exception = testCase.second;
14890075Sobrien        Executable aMethod = testCase.first.executable;
14990075Sobrien        int bci = testCase.first.bci;
15090075Sobrien        HotSpotResolvedJavaMethod method = CTVMUtilities
15190075Sobrien                .getResolvedMethod(aMethod);
15290075Sobrien        Utils.runAndCheckException(
15390075Sobrien                () -> CompilerToVMHelper.allocateCompileId(method, bci),
15490075Sobrien                exception);
15590075Sobrien    }
15690075Sobrien
15790075Sobrien    private int getWBCompileID(CompileCodeTestCase testCase) {
15890075Sobrien        NMethod nm = testCase.deoptimizeAndCompile();
15990075Sobrien        if (nm == null) {
16090075Sobrien            throw new Error("[TEST BUG] cannot compile method " + testCase);
16190075Sobrien        }
16290075Sobrien        return nm.compile_id;
16390075Sobrien    }
16490075Sobrien}
16590075Sobrien