1 /*
2  * Copyright (c) 2019, 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 TestOverloadCompileQueues
26  * @bug 8163511 8230402
27  * @summary Test overloading the C1 and C2 compile queues with tasks.
28  * @requires !vm.graal.enabled
29  * @run main/othervm/timeout=300 -XX:-TieredCompilation -XX:CompileThreshold=2 -XX:CICompilerCount=1
30  *                   compiler.classUnloading.methodUnloading.TestOverloadCompileQueues
31  * @run main/othervm/timeout=300 -XX:TieredCompileTaskTimeout=1000 -XX:CompileThresholdScaling=0.001 -XX:CICompilerCount=2
32  *                   compiler.classUnloading.methodUnloading.TestOverloadCompileQueues
33  */
34 
35 package compiler.classUnloading.methodUnloading;
36 
37 import java.lang.reflect.Method;
38 import java.net.URL;
39 import java.net.URLClassLoader;
40 import java.util.Arrays;
41 
42 public class TestOverloadCompileQueues {
43     public static final int ITERS = 500; // Increase for longer stress testing
44     public static final int ITERS_A = 1000; // Increase for longer stress testing
45 
46     public static int iArr[] = new int[100];
47 
48     // Some methods to fill up the compile queue
test0()49     public static void test0() { }
test1()50     public static void test1() { }
test2()51     public static void test2() { }
test3()52     public static void test3() { }
test4()53     public static void test4() { }
test5()54     public static void test5() { }
test6()55     public static void test6() { }
test7()56     public static void test7() { }
test8()57     public static void test8() { }
test9()58     public static void test9() { }
test10()59     public static void test10() { }
test11()60     public static void test11() { }
test12()61     public static void test12() { }
test13()62     public static void test13() { }
test14()63     public static void test14() { }
test15()64     public static void test15() { }
test16()65     public static void test16() { }
test17()66     public static void test17() { }
test18()67     public static void test18() { }
test19()68     public static void test19() { }
69 
70     // More methods that do some more complex things. Therefore, the compiler needs to spend some more time compiling them.
71     // With 50 methods, a queue size of 10000 is also reached in the second run with TieredCompilation enabled.
testA0()72     public static void testA0()  { Arrays.sort(iArr); }
testA1()73     public static void testA1()  { Arrays.sort(iArr); }
testA2()74     public static void testA2()  { Arrays.sort(iArr); }
testA3()75     public static void testA3()  { Arrays.sort(iArr); }
testA4()76     public static void testA4()  { Arrays.sort(iArr); }
testA5()77     public static void testA5()  { Arrays.sort(iArr); }
testA6()78     public static void testA6()  { Arrays.sort(iArr); }
testA7()79     public static void testA7()  { Arrays.sort(iArr); }
testA8()80     public static void testA8()  { Arrays.sort(iArr); }
testA9()81     public static void testA9()  { Arrays.sort(iArr); }
testA10()82     public static void testA10() { Arrays.sort(iArr); }
testA11()83     public static void testA11() { Arrays.sort(iArr); }
testA12()84     public static void testA12() { Arrays.sort(iArr); }
testA13()85     public static void testA13() { Arrays.sort(iArr); }
testA14()86     public static void testA14() { Arrays.sort(iArr); }
testA15()87     public static void testA15() { Arrays.sort(iArr); }
testA16()88     public static void testA16() { Arrays.sort(iArr); }
testA17()89     public static void testA17() { Arrays.sort(iArr); }
testA18()90     public static void testA18() { Arrays.sort(iArr); }
testA19()91     public static void testA19() { Arrays.sort(iArr); }
testA20()92     public static void testA20() { Arrays.sort(iArr); }
testA21()93     public static void testA21() { Arrays.sort(iArr); }
testA22()94     public static void testA22() { Arrays.sort(iArr); }
testA23()95     public static void testA23() { Arrays.sort(iArr); }
testA24()96     public static void testA24() { Arrays.sort(iArr); }
testA25()97     public static void testA25() { Arrays.sort(iArr); }
testA26()98     public static void testA26() { Arrays.sort(iArr); }
testA27()99     public static void testA27() { Arrays.sort(iArr); }
testA28()100     public static void testA28() { Arrays.sort(iArr); }
testA29()101     public static void testA29() { Arrays.sort(iArr); }
testA30()102     public static void testA30() { Arrays.sort(iArr); }
testA31()103     public static void testA31() { Arrays.sort(iArr); }
testA32()104     public static void testA32() { Arrays.sort(iArr); }
testA33()105     public static void testA33() { Arrays.sort(iArr); }
testA34()106     public static void testA34() { Arrays.sort(iArr); }
testA35()107     public static void testA35() { Arrays.sort(iArr); }
testA36()108     public static void testA36() { Arrays.sort(iArr); }
testA37()109     public static void testA37() { Arrays.sort(iArr); }
testA38()110     public static void testA38() { Arrays.sort(iArr); }
testA39()111     public static void testA39() { Arrays.sort(iArr); }
testA40()112     public static void testA40() { Arrays.sort(iArr); }
testA41()113     public static void testA41() { Arrays.sort(iArr); }
testA42()114     public static void testA42() { Arrays.sort(iArr); }
testA43()115     public static void testA43() { Arrays.sort(iArr); }
testA44()116     public static void testA44() { Arrays.sort(iArr); }
testA45()117     public static void testA45() { Arrays.sort(iArr); }
testA46()118     public static void testA46() { Arrays.sort(iArr); }
testA47()119     public static void testA47() { Arrays.sort(iArr); }
testA48()120     public static void testA48() { Arrays.sort(iArr); }
testA49()121     public static void testA49() { Arrays.sort(iArr); }
122 
main(String[] args)123     public static void main(String[] args) throws Throwable {
124         run();
125         runA();
126     }
127 
run()128     public static void run() throws Throwable {
129         Class<?> thisClass = TestOverloadCompileQueues.class;
130         ClassLoader defaultLoader = thisClass.getClassLoader();
131         URL classesDir = thisClass.getProtectionDomain().getCodeSource().getLocation();
132 
133         for (int i = 0; i < ITERS; ++i) {
134             // Load test class with own class loader
135             URLClassLoader myLoader = URLClassLoader.newInstance(new URL[] {classesDir}, defaultLoader.getParent());
136             Class<?> testClass = Class.forName(thisClass.getCanonicalName(), true, myLoader);
137 
138             // Execute all test methods to trigger compilation and fill up compile queue
139             for (int j = 1; j < 20; ++j) {
140                 Method method = testClass.getDeclaredMethod("test" + j);
141                 method.invoke(null);
142                 method.invoke(null);
143             }
144 
145             // Unload dead classes from ealier iterations
146             System.gc();
147         }
148     }
149 
runA()150     public static void runA() throws Throwable {
151         Class<?> thisClass = TestOverloadCompileQueues.class;
152         ClassLoader defaultLoader = thisClass.getClassLoader();
153         URL classesDir = thisClass.getProtectionDomain().getCodeSource().getLocation();
154 
155         for (int i = 0; i < ITERS_A; ++i) {
156             // Load test class with own class loader
157             URLClassLoader myLoader = URLClassLoader.newInstance(new URL[] {classesDir}, defaultLoader.getParent());
158             Class<?> testClass = Class.forName(thisClass.getCanonicalName(), true, myLoader);
159 
160             // Execute all test methods to trigger compilation and fill up compile queue
161             for (int j = 0; j < 50; ++j) {
162                 Method method = testClass.getDeclaredMethod("testA" + j);
163                 method.invoke(null);
164                 method.invoke(null);
165             }
166         }
167     }
168 }
169