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  * @summary Ensure the HotCodeCollector detects a very hot method and relocates
 29  *          it to the HotCodeHeap. Sampling is best effort, so for reliability
 30  *          we spawn a seperate test process to manage the VM flags.
 31  * @requires vm.flagless
 32  * @requires vm.compiler2.enabled
 33  * @library /test/lib /
 34  * @build jdk.test.whitebox.WhiteBox
 35  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 36  * @run driver compiler.hotcode.HotCodeCollectorMoveFunction
 37  */
 38 
 39 package compiler.hotcode;
 40 
 41 import java.lang.reflect.Method;
 42 
 43 import jdk.test.lib.Asserts;
 44 import jdk.test.lib.process.OutputAnalyzer;
 45 import jdk.test.lib.process.ProcessTools;
 46 import jdk.test.whitebox.WhiteBox;
 47 import jdk.test.whitebox.code.BlobType;
 48 import jdk.test.whitebox.code.NMethod;
 49 
 50 public class HotCodeCollectorMoveFunction {
 51 
 52     public static void main(String[] args) throws Exception {
 53         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
 54             "-Xbootclasspath/a:.",
 55             "-Xbatch",
 56             "-XX:-TieredCompilation",
 57             "-XX:+SegmentedCodeCache",
 58             "-XX:+UnlockExperimentalVMOptions",
 59             "-XX:+HotCodeHeap",
 60             "-XX:+NMethodRelocation",
 61             "-XX:+UnlockDiagnosticVMOptions",
 62             "-XX:+WhiteBoxAPI",
 63             "-XX:HotCodeIntervalSeconds=0",
 64             "-XX:HotCodeCallLevel=0",
 65             "-XX:HotCodeSampleSeconds=5",
 66             "-XX:HotCodeStablePercent=-1",
 67             "-XX:HotCodeSamplePercent=100",
 68             "-XX:HotCodeStartupDelaySeconds=0",
 69             "-XX:CompileCommand=compileonly," + Runner.class.getName() + "::func",
 70             Runner.class.getName()
 71         );
 72 
 73         OutputAnalyzer out = new OutputAnalyzer(pb.start());
 74         out.shouldHaveExitValue(0);
 75     }
 76 
 77     static class Runner {
 78         private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 79         private static final Method method;
 80         private static final int C2_LEVEL = 4;
 81         private static final int FUNC_RUN_MILLIS = 60_000;
 82 
 83         static {
 84             try {
 85                 method = Runner.class.getMethod("func");
 86             } catch (NoSuchMethodException e) {
 87                 throw new RuntimeException(e);
 88             }
 89         }
 90 
 91         public static void main(String[] args) {
 92             WHITE_BOX.testSetDontInlineMethod(method, true);
 93 
 94             compileFunc();
 95 
 96             // Call function so collector samples and relocates
 97             func();
 98 
 99             // Function should now be in the Hot code heap after collector has had time to relocate
100             NMethod relocatedNMethod = NMethod.get(method, false);
101             Asserts.assertNotNull(relocatedNMethod);
102             Asserts.assertEQ(BlobType.MethodHot, relocatedNMethod.code_blob_type);
103         }
104 
105         private static void compileFunc() {
106             WHITE_BOX.enqueueMethodForCompilation(method, C2_LEVEL);
107 
108             if (WHITE_BOX.getMethodCompilationLevel(method) != C2_LEVEL) {
109                 throw new IllegalStateException("Method " + method + " is not compiled by C2.");
110             }
111         }
112 
113         public static void func() {
114             long start = System.currentTimeMillis();
115             while (System.currentTimeMillis() - start < FUNC_RUN_MILLIS) {}
116         }
117     }
118 }