MaterializeVirtualObjectTest.java revision 12365:d1a00bd8dcca
1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8136421
27 * @requires vm.jvmci
28 *         & (vm.compMode != "Xcomp" | vm.opt.TieredCompilation == null | vm.opt.TieredCompilation == true)
29 * @summary no "-Xcomp -XX:-TieredCompilation" combination allowed until JDK-8140018 is resolved
30 * @library / /test/lib
31 * @library ../common/patches
32 * @modules java.base/jdk.internal.misc
33 * @modules java.base/jdk.internal.org.objectweb.asm
34 *          java.base/jdk.internal.org.objectweb.asm.tree
35 *          jdk.vm.ci/jdk.vm.ci.hotspot
36 *          jdk.vm.ci/jdk.vm.ci.code
37 *          jdk.vm.ci/jdk.vm.ci.meta
38 *
39 * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper sun.hotspot.WhiteBox
40 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
41 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
42 * @run main/othervm -Xmixed -Xbootclasspath/a:.
43 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
44 *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
45 *                   -XX:CompileCommand=exclude,*::check
46 *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
47 *                   -Xbatch
48 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
49 *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
50 * @run main/othervm -Xmixed -Xbootclasspath/a:.
51 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
52 *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
53 *                   -XX:CompileCommand=exclude,*::check
54 *                   -XX:+DoEscapeAnalysis -XX:-UseCounterDecay
55 *                   -Xbatch
56 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
57 *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
58 */
59
60package compiler.jvmci.compilerToVM;
61
62import compiler.jvmci.common.CTVMUtilities;
63import compiler.testlibrary.CompilerUtils;
64import compiler.whitebox.CompilerWhiteBoxTest;
65import jdk.test.lib.Asserts;
66import jdk.vm.ci.hotspot.CompilerToVMHelper;
67import jdk.vm.ci.hotspot.HotSpotStackFrameReference;
68import jdk.vm.ci.meta.ResolvedJavaMethod;
69import sun.hotspot.WhiteBox;
70
71import java.lang.reflect.Method;
72
73public class MaterializeVirtualObjectTest {
74    private static final WhiteBox WB;
75    private static final Method METHOD;
76    private static final ResolvedJavaMethod RESOLVED_METHOD;
77    private static final boolean INVALIDATE;
78    private static final int COMPILE_THRESHOLD;
79
80    static {
81        WB = WhiteBox.getWhiteBox();
82        try {
83            METHOD = MaterializeVirtualObjectTest.class.getDeclaredMethod(
84                    "testFrame", String.class, int.class);
85        } catch (NoSuchMethodException e) {
86            throw new Error("Can't get executable for test method", e);
87        }
88        RESOLVED_METHOD = CTVMUtilities.getResolvedMethod(METHOD);
89        INVALIDATE = Boolean.getBoolean(
90                "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");
91        COMPILE_THRESHOLD = WB.getBooleanVMFlag("TieredCompilation")
92                ? CompilerWhiteBoxTest.THRESHOLD
93                : CompilerWhiteBoxTest.THRESHOLD * 2;
94    }
95
96    public static void main(String[] args) {
97        int levels[] = CompilerUtils.getAvailableCompilationLevels();
98        // we need compilation level 4 to use EscapeAnalysis
99        if (levels.length < 1 || levels[levels.length - 1] != 4) {
100            System.out.println("INFO: Test needs compilation level 4 to"
101                    + " be available. Skipping.");
102        } else {
103            new MaterializeVirtualObjectTest().test();
104        }
105    }
106
107    private static String getName() {
108        return "CASE: invalidate=" + INVALIDATE;
109    }
110
111    private void test() {
112        System.out.println(getName());
113        Asserts.assertFalse(WB.isMethodCompiled(METHOD), getName()
114                + " : method unexpectedly compiled");
115        /* need to trigger compilation by multiple method invocations
116           in order to have method profile data to be gathered */
117        for (int i = 0; i < COMPILE_THRESHOLD; i++) {
118            testFrame("someString", i);
119        }
120        Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
121                + "Method unexpectedly not compiled");
122        testFrame("someString", COMPILE_THRESHOLD);
123    }
124
125    private void testFrame(String str, int iteration) {
126        Helper helper = new Helper(str);
127        check(iteration);
128        Asserts.assertTrue((helper.string != null) && (this != null)
129                && (helper != null), getName() + " : some locals are null");
130    }
131
132    private void check(int iteration) {
133        // Materialize virtual objects on last invocation
134        if (iteration == COMPILE_THRESHOLD) {
135            HotSpotStackFrameReference hsFrame = CompilerToVMHelper
136                    .getNextStackFrame(/* topmost frame */ null,
137                            new ResolvedJavaMethod[]{
138                                RESOLVED_METHOD}, /* don't skip any */ 0);
139            Asserts.assertNotNull(hsFrame, getName() + " : got null frame");
140            Asserts.assertTrue(WB.isMethodCompiled(METHOD), getName()
141                    + "Test method should be compiled");
142            Asserts.assertTrue(hsFrame.hasVirtualObjects(), getName()
143                    + ": has no virtual object before materialization");
144            CompilerToVMHelper.materializeVirtualObjects(hsFrame, INVALIDATE);
145            Asserts.assertFalse(hsFrame.hasVirtualObjects(), getName()
146                    + " : has virtual object after materialization");
147            Asserts.assertEQ(WB.isMethodCompiled(METHOD), !INVALIDATE, getName()
148                    + " : unexpected compiled status");
149        }
150    }
151
152    private class Helper {
153        public String string;
154
155        public Helper(String s) {
156            this.string = s;
157        }
158    }
159}
160