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