1/*
2 * Copyright (c) 2014, 2015, 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
24package compiler.codecache.dtrace;
25
26import jdk.test.lib.Utils;
27import sun.hotspot.WhiteBox;
28
29import java.lang.reflect.Executable;
30import java.util.ArrayList;
31import java.util.Arrays;
32import java.util.Collections;
33import java.util.List;
34
35public class SegmentedCodeCacheDtraceTestWorker {
36
37    private static final String METHOD1_NAME = "foo";
38    private static final String METHOD2_NAME = "bar";
39    private static final String METHOD3_NAME = "baz";
40    public static final List<Executable> TESTED_METHODS_LIST;
41    private final WhiteBox wb;
42    private final int compLevels[];
43
44    static {
45        List<Executable> methods = new ArrayList<>();
46        try {
47            // method order is important. Need to place methods in call order,
48            // to be able to verify results later
49            methods.add(SegmentedCodeCacheDtraceTestWorker.class.getMethod(METHOD1_NAME));
50            methods.add(SegmentedCodeCacheDtraceTestWorker.class.getMethod(METHOD2_NAME));
51            methods.add(SegmentedCodeCacheDtraceTestWorker.class.getMethod(METHOD3_NAME));
52        } catch (NoSuchMethodException e) {
53            throw new Error("TESTBUG: no expected method found", e);
54        }
55        TESTED_METHODS_LIST = Collections.unmodifiableList(methods);
56    }
57
58    protected static final boolean BACKGROUND_COMPILATION
59            = WhiteBox.getWhiteBox().getBooleanVMFlag("BackgroundCompilation");
60
61    public static void main(String[] args) {
62        if (args.length != 2 * TESTED_METHODS_LIST.size()) {
63            throw new Error("Usage: java <thisClass> <fooCompLevel> <fooInlined>"
64                    + "<barCompLevel> <barInlined> "
65                    + "<bazCompLevel> <bazInlined>");
66        } else {
67            int compLevels[] = new int[TESTED_METHODS_LIST.size()];
68            boolean inlines[] = new boolean[TESTED_METHODS_LIST.size()];
69            for (int i = 0; i < TESTED_METHODS_LIST.size(); i++) {
70                compLevels[i] = Integer.parseInt(args[2 * i]);
71                inlines[i] = Boolean.parseBoolean(args[2 * i + 1]);
72            }
73            new SegmentedCodeCacheDtraceTestWorker(compLevels, inlines).test();
74        }
75    }
76
77    public SegmentedCodeCacheDtraceTestWorker(int compLevels[], boolean inlines[]) {
78        wb = WhiteBox.getWhiteBox();
79        this.compLevels = Arrays.copyOf(compLevels, compLevels.length);
80        for (int i = 0; i < compLevels.length; i++) {
81            if (inlines[i]) {
82                wb.testSetForceInlineMethod(TESTED_METHODS_LIST.get(i), true);
83            } else {
84                wb.testSetDontInlineMethod(TESTED_METHODS_LIST.get(i), true);
85            }
86        }
87    }
88
89    private void waitForCompilation(Executable executable, int compLevel) {
90        if (compLevel > 0) {
91            Utils.waitForCondition(() -> wb.isMethodCompiled(executable));
92        }
93    }
94
95    protected void test() {
96        for (int i = 0; i < TESTED_METHODS_LIST.size(); i++) {
97            Executable method = TESTED_METHODS_LIST.get(i);
98            int compLevel = compLevels[i];
99            wb.enqueueMethodForCompilation(method, compLevel);
100            waitForCompilation(method, compLevel);
101        }
102        foo();
103    }
104
105    public static void foo() {
106        bar();
107    }
108
109    public static void bar() {
110        baz();
111    }
112
113    public static void baz() {
114        System.out.println("Reached baz method");
115    }
116}
117