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