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