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  * @ignore
28  * @bug 8316694
29  * @library /test/lib /
30  * @modules java.base/jdk.internal.misc java.management
31  * @requires vm.opt.DeoptimizeALot != true
32  * @requires vm.flavor == "server" & (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel == 4)
33  * @build jdk.test.whitebox.WhiteBox
34  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
35  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbatch -XX:+SegmentedCodeCache
36  * -XX:+UnlockExperimentalVMOptions -XX:+NMethodRelocation compiler.whitebox.DeoptimizeRelocatedNMethod
37  */
38 
39 package compiler.whitebox;
40 
41 import compiler.whitebox.CompilerWhiteBoxTest;
42 import java.lang.reflect.Method;
43 import jdk.test.whitebox.WhiteBox;
44 import jdk.test.whitebox.code.BlobType;
45 import jdk.test.whitebox.code.NMethod;
46 
47 public class DeoptimizeRelocatedNMethod {
48 
49     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
50     public static double FUNCTION_RESULT = 0;
51 
52     public static void main(String [] args) throws Exception {
53         // Get method that will be relocated
54         Method method = DeoptimizeRelocatedNMethod.class.getMethod("function");
55         WHITE_BOX.testSetDontInlineMethod(method, true);
56 
57         // Verify not initially compiled
58         CompilerWhiteBoxTest.checkNotCompiled(method, false);
59 
60         // Enqueue method for compilation. This will block since background compilation is disabled
61         WHITE_BOX.enqueueMethodForCompilation(method, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
62 
63         // Verify now compiled
64         CompilerWhiteBoxTest.checkCompiled(method, false);
65 
66         // Get newly created nmethod
67         NMethod origNmethod = NMethod.get(method, false);
68 
69         // Relocate nmethod and mark old for cleanup
70         WHITE_BOX.relocateNMethodFromMethod(method, BlobType.MethodNonProfiled.id);
71 
72         // Trigger GC to clean up old nmethod
73         WHITE_BOX.fullGC();
74 
75         // Verify function still compiled after old was cleaned up
76         CompilerWhiteBoxTest.checkCompiled(method, false);
77 
78         // Get new nmethod and verify it's actually new
79         NMethod newNmethod = NMethod.get(method, false);
80         if (origNmethod.entry_point == newNmethod.entry_point) {
81             throw new RuntimeException("Did not create new nmethod");
82         }
83 
84         // Call to verify everything still works
85         function();
86 
87         // Deoptimized method
88         WHITE_BOX.deoptimizeMethod(method);
89 
90         CompilerWhiteBoxTest.checkNotCompiled(method, false);
91 
92         // Call to verify everything still works
93         function();
94     }
95 
96     public static void function() {
97         FUNCTION_RESULT = Math.random();
98     }
99 }