Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/CallSiteTest.java
47216 views
/*1* Copyright (c) 2011, 2012, 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*22*/2324/**25* @test26* @summary smoke tests for CallSite27*28* @build indify.Indify29* @compile CallSiteTest.java30* @run main/othervm/timeout=3600 -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies31* indify.Indify32* --expand-properties --classpath ${test.classes}33* --java test.java.lang.invoke.CallSiteTest34*/3536package test.java.lang.invoke;3738import java.io.*;3940import java.lang.invoke.*;41import static java.lang.invoke.MethodHandles.*;42import static java.lang.invoke.MethodType.*;4344public class CallSiteTest {45private final static Class<?> CLASS = CallSiteTest.class;4647private static CallSite mcs;48private static CallSite vcs;49private static MethodHandle mh_foo;50private static MethodHandle mh_bar;5152static {53try {54mh_foo = lookup().findStatic(CLASS, "foo", methodType(int.class, int.class, int.class));55mh_bar = lookup().findStatic(CLASS, "bar", methodType(int.class, int.class, int.class));56mcs = new MutableCallSite(mh_foo);57vcs = new VolatileCallSite(mh_foo);58} catch (Exception e) {59e.printStackTrace();60}61}6263public static void main(String... av) throws Throwable {64testMutableCallSite();65testVolatileCallSite();66}6768private final static int N = Integer.MAX_VALUE / 100;69private final static int RESULT1 = 762786192;70private final static int RESULT2 = -21474836;7172private static void assertEquals(int expected, int actual) {73if (expected != actual)74throw new AssertionError("expected: " + expected + ", actual: " + actual);75}7677private static void testMutableCallSite() throws Throwable {78// warm-up79for (int i = 0; i < 20000; i++) {80mcs.setTarget(mh_foo);81}82// run83for (int n = 0; n < 2; n++) {84mcs.setTarget(mh_foo);85for (int i = 0; i < 5; i++) {86assertEquals(RESULT1, runMutableCallSite());87}88mcs.setTarget(mh_bar);89for (int i = 0; i < 5; i++) {90assertEquals(RESULT2, runMutableCallSite());91}92}93}94private static void testVolatileCallSite() throws Throwable {95// warm-up96for (int i = 0; i < 20000; i++) {97vcs.setTarget(mh_foo);98}99// run100for (int n = 0; n < 2; n++) {101vcs.setTarget(mh_foo);102for (int i = 0; i < 5; i++) {103assertEquals(RESULT1, runVolatileCallSite());104}105vcs.setTarget(mh_bar);106for (int i = 0; i < 5; i++) {107assertEquals(RESULT2, runVolatileCallSite());108}109}110}111112private static int runMutableCallSite() throws Throwable {113int sum = 0;114for (int i = 0; i < N; i++) {115sum += (int) INDY_mcs().invokeExact(i, i+1);116}117return sum;118}119private static int runVolatileCallSite() throws Throwable {120int sum = 0;121for (int i = 0; i < N; i++) {122sum += (int) INDY_vcs().invokeExact(i, i+1);123}124return sum;125}126127static int foo(int a, int b) { return a + b; }128static int bar(int a, int b) { return a - b; }129130private static MethodType MT_bsm() {131shouldNotCallThis();132return methodType(CallSite.class, Lookup.class, String.class, MethodType.class);133}134135private static CallSite bsm_mcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {136return mcs;137}138private static MethodHandle MH_bsm_mcs() throws ReflectiveOperationException {139shouldNotCallThis();140return lookup().findStatic(lookup().lookupClass(), "bsm_mcs", MT_bsm());141}142private static MethodHandle INDY_mcs() throws Throwable {143shouldNotCallThis();144return ((CallSite) MH_bsm_mcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();145}146147private static CallSite bsm_vcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {148return vcs;149}150private static MethodHandle MH_bsm_vcs() throws ReflectiveOperationException {151shouldNotCallThis();152return lookup().findStatic(lookup().lookupClass(), "bsm_vcs", MT_bsm());153}154private static MethodHandle INDY_vcs() throws Throwable {155shouldNotCallThis();156return ((CallSite) MH_bsm_vcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();157}158159private static void shouldNotCallThis() {160// if this gets called, the transformation has not taken place161throw new AssertionError("this code should be statically transformed away by Indify");162}163}164165166