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