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 id=C1
 27  * @ignore
 28  * @bug 8316694
 29  * @requires vm.debug == true
 30  * @summary test that relocated nmethod is correctly deoptimized
 31  * @library /test/lib /
 32  * @modules java.base/jdk.internal.misc java.management
 33  *
 34  * @build jdk.test.whitebox.WhiteBox
 35  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 36  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=1
 37  * -XX:+SegmentedCodeCache -XX:-DeoptimizeRandom -XX:+DeoptimizeALot -XX:+UnlockExperimentalVMOptions -XX:+NMethodRelocation
 38  * compiler.whitebox.RelocateNMethodMultiplePaths
 39  */
 40 
 41 /*
 42  * @test id=C2
 43  * @ignore
 44  * @bug 8316694
 45  * @requires vm.debug == true
 46  * @summary test that relocated nmethod is correctly deoptimized
 47  * @library /test/lib /
 48  * @modules java.base/jdk.internal.misc java.management
 49  *
 50  * @build jdk.test.whitebox.WhiteBox
 51  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 52  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbatch -XX:+TieredCompilation
 53  * -XX:+SegmentedCodeCache -XX:-DeoptimizeRandom -XX:+DeoptimizeALot -XX:+UnlockExperimentalVMOptions -XX:+NMethodRelocation
 54  * compiler.whitebox.RelocateNMethodMultiplePaths
 55  */
 56 
 57 package compiler.whitebox;
 58 
 59 import compiler.whitebox.CompilerWhiteBoxTest;
 60 import java.lang.reflect.Method;
 61 import jdk.test.whitebox.WhiteBox;
 62 import jdk.test.whitebox.code.BlobType;
 63 import jdk.test.whitebox.code.NMethod;
 64 
 65 public class RelocateNMethodMultiplePaths {
 66 
 67     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 68 
 69     private static final int PATH_ONE_RESULT = 1;
 70     private static final int PATH_TWO_RESULT = 2;
 71 
 72     public static void main(String [] args) throws Exception {
 73         // Get method that will be relocated
 74         Method method = RelocateNMethodMultiplePaths.class.getMethod("function", boolean.class);
 75         WHITE_BOX.testSetDontInlineMethod(method, true);
 76 
 77         // Verify not initially compiled
 78         CompilerWhiteBoxTest.checkNotCompiled(method, false);
 79 
 80         // Call function enough to compile
 81         callFunction(true);
 82 
 83         // Verify now compiled
 84         CompilerWhiteBoxTest.checkCompiled(method, false);
 85 
 86         // Get newly created nmethod
 87         NMethod origNmethod = NMethod.get(method, false);
 88 
 89         // Relocate nmethod and mark old for cleanup
 90         WHITE_BOX.relocateNMethodFromMethod(method, BlobType.MethodNonProfiled.id);
 91 
 92         // Trigger GC to clean up old nmethod
 93         WHITE_BOX.fullGC();
 94 
 95         // Verify function still compiled after old was cleaned up
 96         CompilerWhiteBoxTest.checkCompiled(method, false);
 97 
 98         // Get new nmethod and verify it's actually new
 99         NMethod newNmethod = NMethod.get(method, false);
100         if (origNmethod.entry_point == newNmethod.entry_point) {
101             throw new RuntimeException("Did not create new nmethod");
102         }
103 
104         // Verify function still produces correct result
105         if (function(true) != PATH_ONE_RESULT) {
106             throw new RuntimeException("Relocated function produced incorrect result in path one");
107         }
108 
109         // Call function again with different path and verify result
110         if (function(false) != PATH_TWO_RESULT) {
111             throw new RuntimeException("Relocated function produced incorrect result in path two");
112         }
113 
114         // Verify function can be correctly deoptimized
115         WHITE_BOX.deoptimizeMethod(method);
116         CompilerWhiteBoxTest.checkNotCompiled(method, false);
117     }
118 
119     // Call function multiple times to trigger compilation
120     private static void callFunction(boolean pathOne) {
121         for (int i = 0; i < CompilerWhiteBoxTest.THRESHOLD; i++) {
122             function(pathOne);
123         }
124     }
125 
126     public static int function(boolean pathOne) {
127         if (pathOne) {
128             return PATH_ONE_RESULT;
129         } else {
130             return PATH_TWO_RESULT;
131         }
132     }
133 }