1 /*
 2  * Copyright Amazon.com Inc. 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 /*
26  * @test
27  * @library /test/lib /
28  * @build jdk.test.whitebox.WhiteBox
29  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
30  * @run main/othervm -Xbootclasspath/a:. -Xbatch -XX:-TieredCompilation -XX:+SegmentedCodeCache -XX:+UnlockExperimentalVMOptions -XX:+HotCodeHeap
31  *                   -XX:+NMethodRelocation -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:HotCodeIntervalSeconds=0 -XX:HotCodeCallLevel=0
32  *                   -XX:HotCodeSampleSeconds=5 -XX:HotCodeStablePercent=-1 -XX:HotCodeSamplePercent=100 -XX:HotCodeStartupDelaySeconds=0
33  *                   -XX:CompileCommand=compileonly,compiler.hotcode.HotCodeCollectorMoveFunction::func
34  *                   compiler.hotcode.HotCodeCollectorMoveFunction
35  */
36 
37 package compiler.hotcode;
38 
39 import java.lang.reflect.Method;
40 
41 import jdk.test.lib.Asserts;
42 import jdk.test.whitebox.WhiteBox;
43 import jdk.test.whitebox.code.BlobType;
44 import jdk.test.whitebox.code.NMethod;
45 
46 public class HotCodeCollectorMoveFunction {
47 
48     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
49     private static final Method method;
50     private static final int C2_LEVEL = 4;
51     private static final int FUNC_RUN_MILLIS = 60_000;
52 
53     static {
54         try {
55             method = HotCodeCollectorMoveFunction.class.getMethod("func");
56         } catch (NoSuchMethodException e) {
57             throw new RuntimeException(e);
58         }
59     }
60 
61     public static void main(String[] args) throws Exception {
62         WHITE_BOX.testSetDontInlineMethod(method, true);
63 
64         compileFunc();
65 
66         // Call function so collector samples and relocates
67         func();
68 
69         // Function should now be in the Hot code heap after collector has had time to relocate
70         NMethod reloc_nm = NMethod.get(method, false);
71         Asserts.assertNotEquals(reloc_nm, null);
72         Asserts.assertEQ(reloc_nm.code_blob_type, BlobType.MethodHot);
73     }
74 
75     public static void compileFunc() {
76         WHITE_BOX.enqueueMethodForCompilation(method, C2_LEVEL);
77 
78         if (WHITE_BOX.getMethodCompilationLevel(method) != C2_LEVEL) {
79             throw new IllegalStateException("Method " + method + " is not compiled by C2.");
80         }
81     }
82 
83     public static void func() {
84         long start = System.currentTimeMillis();
85         while (System.currentTimeMillis() - start < FUNC_RUN_MILLIS) {}
86     }
87 
88 }