Path: blob/master/test/hotspot/jtreg/compiler/jsr292/ContinuousCallSiteTargetChange.java
41149 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @library /test/lib /26* @requires vm.flagless27*28* @build sun.hotspot.WhiteBox29* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox30* @run driver compiler.jsr292.ContinuousCallSiteTargetChange31*/3233package compiler.jsr292;3435import jdk.test.lib.Asserts;36import jdk.test.lib.process.OutputAnalyzer;37import jdk.test.lib.process.ProcessTools;38import sun.hotspot.WhiteBox;3940import java.lang.invoke.CallSite;41import java.lang.invoke.MethodHandle;42import java.lang.invoke.MethodHandles;43import java.lang.invoke.MethodType;44import java.lang.invoke.MutableCallSite;45import java.util.ArrayList;46import java.util.Arrays;47import java.util.List;4849public class ContinuousCallSiteTargetChange {50static final int ITERATIONS = Integer.parseInt(System.getProperty("iterations", "50"));5152static void runTest(Class<?> test, String... extraArgs) throws Exception {53List<String> argsList = new ArrayList<>(54List.of("-XX:+IgnoreUnrecognizedVMOptions",55"-XX:PerBytecodeRecompilationCutoff=10", "-XX:PerMethodRecompilationCutoff=10",56"-XX:+PrintCompilation", "-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining"));5758argsList.addAll(Arrays.asList(extraArgs));5960argsList.add(test.getName());61argsList.add(Integer.toString(ITERATIONS));6263ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(argsList);6465OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());6667analyzer.shouldHaveExitValue(0);6869analyzer.shouldNotContain("made not compilable");70analyzer.shouldNotContain("decompile_count > PerMethodRecompilationCutoff");7172}7374static void testServer(Class<?> test, String... args) throws Exception {75List<String> extraArgsList = new ArrayList<>(76List.of("-server", "-XX:-TieredCompilation", "-Xbootclasspath/a:.",77"-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI"));78extraArgsList.addAll(Arrays.asList(args));7980runTest(test, extraArgsList.toArray(new String[extraArgsList.size()]));81}8283static void testClient(Class<?> test, String... args) throws Exception {84List<String> extraArgsList = new ArrayList<>(85List.of("-client", "-XX:+TieredCompilation", "-XX:TieredStopAtLevel=1",86"-Xbootclasspath/a:.", "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI"));87extraArgsList.addAll(Arrays.asList(args));8889runTest(test, extraArgsList.toArray(new String[extraArgsList.size()]));90}9192public static void main(String[] args) throws Exception {93testServer(RecompilationTest.class, "-Xbatch");94testClient(RecompilationTest.class, "-Xbatch");9596testServer(PingPongTest.class);97testClient(PingPongTest.class);98}99100static MethodHandle findStatic(Class<?> cls, String name, MethodType mt) {101try {102return MethodHandles.lookup().findStatic(cls, name, mt);103} catch (Exception e) {104throw new Error(e);105}106}107108static class RecompilationTest {109static final MethodType mt = MethodType.methodType(void.class);110static final CallSite cs = new MutableCallSite(mt);111112static final MethodHandle mh = cs.dynamicInvoker();113114static void f() {115}116117static void test1() throws Throwable {118mh.invokeExact();119}120121static void test2() throws Throwable {122cs.getTarget().invokeExact();123}124125static void iteration() throws Throwable {126MethodHandle mh1 = findStatic(RecompilationTest.class, "f", mt);127cs.setTarget(mh1);128for (int i = 0; i < 20_000; i++) {129test1();130test2();131}132}133134public static void main(String[] args) throws Throwable {135int iterations = Integer.parseInt(args[0]);136for (int i = 0; i < iterations; i++) {137iteration();138}139}140}141142static class PingPongTest {143static final MethodType mt = MethodType.methodType(void.class);144static final CallSite cs = new MutableCallSite(mt);145146static final MethodHandle mh = cs.dynamicInvoker();147148static final MethodHandle ping = findStatic(PingPongTest.class, "ping", mt);149static final MethodHandle pong = findStatic(PingPongTest.class, "pong", mt);150151static void ping() {152Asserts.assertEQ(cs.getTarget(), ping, "wrong call site target");153cs.setTarget(pong);154}155156static void pong() {157Asserts.assertEQ(cs.getTarget(), pong, "wrong call site target");158cs.setTarget(ping);159}160161static void iteration() throws Throwable {162cs.setTarget(ping);163for (int i = 0; i < 20_000; i++) {164mh.invokeExact();165}166}167168public static void main(String[] args) throws Throwable {169int iterations = Integer.parseInt(args[0]);170WhiteBox whiteBox = WhiteBox.getWhiteBox();171for (int i = 0; i < iterations; i++) {172iteration();173whiteBox.forceNMethodSweep();174}175}176}177}178179180