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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.bench.java.lang.invoke;
  26 
  27 import org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Mode;
  30 import org.openjdk.jmh.annotations.OutputTimeUnit;
  31 import org.openjdk.jmh.annotations.Scope;
  32 import org.openjdk.jmh.annotations.State;
  33 
  34 import java.lang.invoke.CallSite;
  35 import java.lang.invoke.MethodHandle;
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.MethodType;
  38 import java.lang.invoke.MutableCallSite;
  39 import java.lang.invoke.VolatileCallSite;
  40 import java.util.concurrent.TimeUnit;
  41 
  42 /**
  43  * This benchmark evaluates INDY performance under dynamic target updates.
  44  */
  45 @BenchmarkMode(Mode.AverageTime)
  46 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  47 @State(Scope.Thread)
  48 public class CallSiteSetTargetSelf {
  49 
  50     /*
  51      * Implementation notes:
  52      *   - This test makes sense for mutable and volatile call sites only
  53      *   - Multiple threads are calling the same callsite, and each call is swapping the target.
  54      *   - Additional baseline includes "raw" test, calling callsite's MH directly
  55      *
  56      *   - NOTE: invalidating shared target callsite is very bad with multiple threads.
  57      *     I.e. this test is inherently non-scalable.
  58      */
  59 
  60     private static CallSite cs;
  61 
  62     private static MethodHandle doCall1;
  63     private static MethodHandle doCall2;
  64 
  65     static {
  66         try {
  67             doCall1 = MethodHandles.lookup().findVirtual(CallSiteSetTargetSelf.class, "call1", MethodType.methodType(int.class));
  68             doCall2 = MethodHandles.lookup().findVirtual(CallSiteSetTargetSelf.class, "call2", MethodType.methodType(int.class));
  69             cs = new MutableCallSite(doCall1);
  70         } catch (NoSuchMethodException | IllegalAccessException e) {
  71             throw new IllegalStateException(e);
  72         }
  73     }
  74 
  75     private int i1;
  76     private int i2;
  77 
  78     public int call1() {
  79         cs.setTarget(doCall2);
  80         return i1++;
  81     }
  82 
  83     public int call2() {
  84         cs.setTarget(doCall1);
  85         return i2++;
  86     }
  87 
  88     @Benchmark
  89     public int baselineRaw() throws Throwable {
  90         return (int) cs.getTarget().invokeExact(this);
  91     }
  92 
  93     @Benchmark
  94     public int testMutable() throws Throwable {
  95         return (int) INDY_Mutable().invokeExact(this);
  96     }
  97 
  98     @Benchmark
  99     public int testVolatile() throws Throwable {
 100         return (int) INDY_Volatile().invokeExact(this);
 101     }
 102 
 103     /* =========================== INDY TRAMPOLINES ============================== */
 104 
 105     private static MethodType MT_bsm() {
 106         shouldNotCallThis();
 107         return MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
 108     }
 109 
 110     private static MethodHandle MH_bsm_Mutable() throws ReflectiveOperationException {
 111         shouldNotCallThis();
 112         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Mutable", MT_bsm());
 113     }
 114 
 115     private static MethodHandle MH_bsm_Volatile() throws ReflectiveOperationException {
 116         shouldNotCallThis();
 117         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Volatile", MT_bsm());
 118     }
 119 
 120     private static MethodHandle INDY_Mutable() throws Throwable {
 121         shouldNotCallThis();
 122         return ((CallSite) MH_bsm_Mutable().invoke(MethodHandles.lookup(), "doCall1", MethodType.methodType(int.class, CallSiteSetTargetSelf.class))).dynamicInvoker();
 123     }
 124 
 125     private static MethodHandle INDY_Volatile() throws Throwable {
 126         shouldNotCallThis();
 127         return ((CallSite) MH_bsm_Volatile().invoke(MethodHandles.lookup(), "doCall1", MethodType.methodType(int.class, CallSiteSetTargetSelf.class))).dynamicInvoker();
 128     }
 129 
 130     public static CallSite bsm_Mutable(MethodHandles.Lookup lookup, String name, MethodType type) {
 131         synchronized (CallSiteSetTarget.class) {
 132             if (cs == null)
 133                 cs = new MutableCallSite(doCall1);
 134             return cs;
 135         }
 136     }
 137 
 138     public static CallSite bsm_Volatile(MethodHandles.Lookup lookup, String name, MethodType type) {
 139         synchronized (CallSiteSetTarget.class) {
 140             if (cs == null)
 141                 cs = new VolatileCallSite(doCall1);
 142             return cs;
 143         }
 144     }
 145 
 146     private static void shouldNotCallThis() {
 147         // if this gets called, the transformation has not taken place
 148         throw new AssertionError("this code should be statically transformed away by Indify");
 149     }
 150 
 151 }