1/*
2 * Copyright (c) 2014, 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 OverloadCompileQueueTest
26 * @summary stressing code cache by overloading compile queues
27 * @library /test/lib /
28 * @modules java.base/jdk.internal.misc
29 *          java.management
30 *
31 * @build sun.hotspot.WhiteBox
32 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
33 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
34 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
35 *                   -XX:+WhiteBoxAPI
36 *                   -XX:CompileCommand=dontinline,compiler.codecache.stress.Helper$TestCase::method
37 *                   -XX:-SegmentedCodeCache
38 *                   compiler.codecache.stress.OverloadCompileQueueTest
39 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
40 *                   -XX:+WhiteBoxAPI
41 *                   -XX:CompileCommand=dontinline,compiler.codecache.stress.Helper$TestCase::method
42 *                   -XX:+SegmentedCodeCache
43 *                   compiler.codecache.stress.OverloadCompileQueueTest
44 */
45
46package compiler.codecache.stress;
47
48import jdk.test.lib.Platform;
49
50import java.lang.reflect.Method;
51import java.util.stream.IntStream;
52
53public class OverloadCompileQueueTest implements Runnable {
54    private static final int MAX_SLEEP = 10000;
55    private static final String METHOD_TO_ENQUEUE = "method";
56    private static final int LEVEL_SIMPLE = 1;
57    private static final int LEVEL_FULL_OPTIMIZATION = 4;
58    private static final boolean TIERED_COMPILATION
59            = Helper.WHITE_BOX.getBooleanVMFlag("TieredCompilation");
60    private static final int TIERED_STOP_AT_LEVEL
61            = Helper.WHITE_BOX.getIntxVMFlag("TieredStopAtLevel").intValue();
62    private static final int[] AVAILABLE_LEVELS;
63    static {
64        if (TIERED_COMPILATION) {
65            AVAILABLE_LEVELS = IntStream
66                    .rangeClosed(LEVEL_SIMPLE, TIERED_STOP_AT_LEVEL)
67                    .toArray();
68        } else if (Platform.isServer() && !Platform.isEmulatedClient()) {
69            AVAILABLE_LEVELS = new int[] { LEVEL_FULL_OPTIMIZATION };
70        } else if (Platform.isClient() || Platform.isMinimal() || Platform.isEmulatedClient()) {
71            AVAILABLE_LEVELS = new int[] { LEVEL_SIMPLE };
72        } else {
73            throw new Error("TESTBUG: unknown VM: " + Platform.vmName);
74        }
75    }
76
77    public static void main(String[] args) {
78        if (Platform.isInt()) {
79            throw new Error("TESTBUG: test can not be run in interpreter");
80        }
81        new CodeCacheStressRunner(new OverloadCompileQueueTest()).runTest();
82    }
83
84    public OverloadCompileQueueTest() {
85        Helper.startInfiniteLoopThread(this::lockUnlock, 100L);
86    }
87
88    @Override
89    public void run() {
90        Helper.TestCase obj = Helper.TestCase.get();
91        Class clazz = obj.getClass();
92        Method mEnqueue;
93        try {
94            mEnqueue = clazz.getMethod(METHOD_TO_ENQUEUE);
95        } catch (NoSuchMethodException | SecurityException e) {
96            throw new Error(String.format(
97                    "TESTBUG: cannot get method '%s' of class %s",
98                    METHOD_TO_ENQUEUE, clazz.getName()), e);
99        }
100        for (int compLevel : AVAILABLE_LEVELS) {
101            Helper.WHITE_BOX.enqueueMethodForCompilation(mEnqueue, compLevel);
102        }
103    }
104
105    private void lockUnlock() {
106        try {
107            int sleep = Helper.RNG.nextInt(MAX_SLEEP);
108            Helper.WHITE_BOX.lockCompilation();
109            Thread.sleep(sleep);
110        } catch (InterruptedException e) {
111            throw new Error("TESTBUG: lockUnlocker thread was unexpectedly interrupted", e);
112        } finally {
113            Helper.WHITE_BOX.unlockCompilation();
114        }
115    }
116
117}
118