1 /*
   2  * Copyright (c) 2014 Oracle and/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 package org.openjdk.bench.java.lang.invoke;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Scope;
  30 import org.openjdk.jmh.annotations.State;
  31 
  32 import java.lang.invoke.CallSite;
  33 import java.lang.invoke.MethodHandle;
  34 import java.lang.invoke.MethodHandles;
  35 import java.lang.invoke.MethodType;
  36 import java.lang.invoke.MutableCallSite;
  37 import java.lang.invoke.VolatileCallSite;
  38 import java.util.concurrent.TimeUnit;
  39 
  40 /**
  41  * This benchmark evaluates INDY performance under dynamic target updates.
  42  */
  43 @BenchmarkMode(Mode.AverageTime)
  44 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  45 @State(Scope.Thread)
  46 public class CallSiteSetTargetSelf {
  47 
  48     /*
  49      * Implementation notes:
  50      *   - This test makes sense for mutable and volatile call sites only
  51      *   - Multiple threads are calling the same callsite, and each call is swapping the target.
  52      *   - Additional baseline includes "raw" test, calling callsite's MH directly
  53      *
  54      *   - NOTE: invalidating shared target callsite is very bad with multiple threads.
  55      *     I.e. this test is inherently non-scalable.
  56      */
  57 
  58     private static CallSite cs;
  59 
  60     private static MethodHandle doCall1;
  61     private static MethodHandle doCall2;
  62 
  63     static {
  64         try {
  65             doCall1 = MethodHandles.lookup().findVirtual(CallSiteSetTargetSelf.class, "call1", MethodType.methodType(int.class));
  66             doCall2 = MethodHandles.lookup().findVirtual(CallSiteSetTargetSelf.class, "call2", MethodType.methodType(int.class));
  67             cs = new MutableCallSite(doCall1);
  68         } catch (NoSuchMethodException | IllegalAccessException e) {
  69             throw new IllegalStateException(e);
  70         }
  71     }
  72 
  73     private int i1;
  74     private int i2;
  75 
  76     public int call1() {
  77         cs.setTarget(doCall2);
  78         return i1++;
  79     }
  80 
  81     public int call2() {
  82         cs.setTarget(doCall1);
  83         return i2++;
  84     }
  85 
  86     @Benchmark
  87     public int baselineRaw() throws Throwable {
  88         return (int) cs.getTarget().invokeExact(this);
  89     }
  90 
  91     @Benchmark
  92     public int testMutable() throws Throwable {
  93         return (int) INDY_Mutable().invokeExact(this);
  94     }
  95 
  96     @Benchmark
  97     public int testVolatile() throws Throwable {
  98         return (int) INDY_Volatile().invokeExact(this);
  99     }
 100 
 101     /* =========================== INDY TRAMPOLINES ============================== */
 102 
 103     private static MethodType MT_bsm() {
 104         shouldNotCallThis();
 105         return MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
 106     }
 107 
 108     private static MethodHandle MH_bsm_Mutable() throws ReflectiveOperationException {
 109         shouldNotCallThis();
 110         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Mutable", MT_bsm());
 111     }
 112 
 113     private static MethodHandle MH_bsm_Volatile() throws ReflectiveOperationException {
 114         shouldNotCallThis();
 115         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Volatile", MT_bsm());
 116     }
 117 
 118     private static MethodHandle INDY_Mutable() throws Throwable {
 119         shouldNotCallThis();
 120         return ((CallSite) MH_bsm_Mutable().invoke(MethodHandles.lookup(), "doCall1", MethodType.methodType(int.class, CallSiteSetTargetSelf.class))).dynamicInvoker();
 121     }
 122 
 123     private static MethodHandle INDY_Volatile() throws Throwable {
 124         shouldNotCallThis();
 125         return ((CallSite) MH_bsm_Volatile().invoke(MethodHandles.lookup(), "doCall1", MethodType.methodType(int.class, CallSiteSetTargetSelf.class))).dynamicInvoker();
 126     }
 127 
 128     public static CallSite bsm_Mutable(MethodHandles.Lookup lookup, String name, MethodType type) {
 129         synchronized (CallSiteSetTarget.class) {
 130             if (cs == null)
 131                 cs = new MutableCallSite(doCall1);
 132             return cs;
 133         }
 134     }
 135 
 136     public static CallSite bsm_Volatile(MethodHandles.Lookup lookup, String name, MethodType type) {
 137         synchronized (CallSiteSetTarget.class) {
 138             if (cs == null)
 139                 cs = new VolatileCallSite(doCall1);
 140             return cs;
 141         }
 142     }
 143 
 144     private static void shouldNotCallThis() {
 145         // if this gets called, the transformation has not taken place
 146         throw new AssertionError("this code should be statically transformed away by Indify");
 147     }
 148 
 149 }