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 SharedTrampolineTest id=C2
 27  * @summary Checks that trampolines can be shared for static method.
 28  * @bug 8280152
 29  * @library /test/lib
 30  *
 31  * @requires vm.compiler2.enabled
 32  * @requires vm.opt.TieredCompilation == null
 33  * @requires os.arch=="aarch64" | os.arch=="riscv64"
 34  * @requires vm.debug
 35  *
 36  * @run driver compiler.sharedstubs.SharedTrampolineTest -XX:-TieredCompilation
 37  */
 38 
 39 package compiler.sharedstubs;
 40 
 41 import java.util.ArrayList;
 42 import java.util.List;
 43 import java.util.regex.Pattern;
 44 import jdk.test.lib.process.OutputAnalyzer;
 45 import jdk.test.lib.process.ProcessTools;
 46 
 47 public class SharedTrampolineTest {
 48     private final static int ITERATIONS_TO_HEAT_LOOP = 20_000;
 49 
 50     private static void runTest(String compiler, String test) throws Exception {
 51         String testClassName = SharedTrampolineTest.class.getName() + "$" + test;
 52         ArrayList<String> command = new ArrayList<String>();
 53         command.add(compiler);
 54         command.add("-XX:+UnlockDiagnosticVMOptions");
 55         command.add("-Xbatch");
 56         command.add("-XX:+PrintRelocations");
 57         command.add("-XX:CompileCommand=compileonly," + testClassName + "::" + "test");
 58         command.add("-XX:CompileCommand=dontinline," + testClassName + "::" + "test");
 59         command.add("-XX:CompileCommand=dontinline," + testClassName + "::" + "log");
 60         command.add(testClassName);
 61         command.add("a");
 62 
 63         ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(command);
 64 
 65         OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
 66 
 67         analyzer.shouldHaveExitValue(0);
 68 
 69         System.out.println(analyzer.getOutput());
 70 
 71         checkOutput(analyzer);
 72     }
 73 
 74     public static void main(String[] args) throws Exception {
 75         String[] tests = new String[] {"StaticMethodTest"};
 76         for (String test : tests) {
 77             runTest(args[0], test);
 78         }
 79     }
 80 
 81     private static void checkOutput(OutputAnalyzer output) {
 82         List<String> addrs = Pattern.compile("\\(trampoline_stub\\) addr=(\\w+) .*\\[trampoline owner")
 83             .matcher(output.getStdout())
 84             .results()
 85             .map(m -> m.group(1))
 86             .toList();
 87         if (addrs.stream().distinct().count() >= addrs.size()) {
 88             throw new RuntimeException("No runtime trampoline stubs reused: distinct " + addrs.stream().distinct().count() + ", in total " + addrs.size());
 89         }
 90     }
 91 
 92     public static class StaticMethodTest {
 93         private static void log(int i, String msg) {
 94         }
 95 
 96         static void test(int i, String[] args) {
 97             if (i % args.length == 0) {
 98                 log(i, "args[0] = " + args[0]);
 99             } else {
100                 log(i, "No args");
101             }
102         }
103 
104         public static void main(String[] args) {
105             for (int i = 1; i < ITERATIONS_TO_HEAT_LOOP; ++i) {
106                 test(i, args);
107             }
108         }
109     }
110 }