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.Setup;
  31 import org.openjdk.jmh.annotations.State;
  32 import org.openjdk.jmh.annotations.TearDown;
  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 import java.util.concurrent.atomic.AtomicBoolean;
  42 
  43 /**
  44  * This benchmark evaluates INDY performance under dynamic target updates.
  45  */
  46 @BenchmarkMode(Mode.AverageTime)
  47 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  48 @State(Scope.Thread)
  49 public class CallSiteSetTarget {
  50 
  51     /*
  52      * Implementation notes:
  53      *   - This test makes sense for mutable and volatile call sites only
  54      *   - Multiple threads are calling the same callsite, and invalidator thread tries to swap target on the fly.
  55      *   - Additional baseline includes "raw" test, calling callsite's MH directly
  56      */
  57 
  58     private static volatile CallSite cs;
  59 
  60     private static MethodHandle doCall1;
  61     private static MethodHandle doCall2;
  62 
  63     static {
  64         try {
  65             doCall1 = MethodHandles.lookup().findVirtual(CallSiteSetTarget.class, "call1", MethodType.methodType(int.class));
  66             doCall2 = MethodHandles.lookup().findVirtual(CallSiteSetTarget.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         return i1++;
  78     }
  79 
  80     public int call2() {
  81         return i2++;
  82     }
  83 
  84     @Benchmark
  85     public int baselineRaw() throws Throwable {
  86         return (int) cs.getTarget().invokeExact(this);
  87     }
  88 
  89     @Benchmark
  90     public int testMutable() throws Throwable {
  91         return (int) INDY_Mutable().invokeExact(this);
  92     }
  93 
  94     @Benchmark
  95     public int testVolatile() throws Throwable {
  96         return (int) INDY_Volatile().invokeExact(this);
  97     }
  98 
  99     /* =========================== INDY TRAMPOLINES ============================== */
 100 
 101     private static MethodType MT_bsm() {
 102         shouldNotCallThis();
 103         return MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
 104     }
 105 
 106     private static MethodHandle MH_bsm_Mutable() throws ReflectiveOperationException {
 107         shouldNotCallThis();
 108         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Mutable", MT_bsm());
 109     }
 110 
 111     private static MethodHandle MH_bsm_Volatile() throws ReflectiveOperationException {
 112         shouldNotCallThis();
 113         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Volatile", MT_bsm());
 114     }
 115 
 116     private static MethodHandle INDY_Mutable() throws Throwable {
 117         shouldNotCallThis();
 118         return ((CallSite) MH_bsm_Mutable().invoke(MethodHandles.lookup(), "doCall1", MethodType.methodType(int.class, CallSiteSetTarget.class))).dynamicInvoker();
 119     }
 120 
 121     private static MethodHandle INDY_Volatile() throws Throwable {
 122         shouldNotCallThis();
 123         return ((CallSite) MH_bsm_Volatile().invoke(MethodHandles.lookup(), "doCall1", MethodType.methodType(int.class, CallSiteSetTarget.class))).dynamicInvoker();
 124     }
 125 
 126     public static CallSite bsm_Mutable(MethodHandles.Lookup lookup, String name, MethodType type) {
 127         synchronized (CallSiteSetTarget.class) {
 128             if (cs == null)
 129                 cs = new MutableCallSite(doCall1);
 130             return cs;
 131         }
 132     }
 133 
 134     public static CallSite bsm_Volatile(MethodHandles.Lookup lookup, String name, MethodType type) {
 135         synchronized (CallSiteSetTarget.class) {
 136             if (cs == null)
 137                 cs = new VolatileCallSite(doCall1);
 138             return cs;
 139         }
 140     }
 141 
 142     private static void shouldNotCallThis() {
 143         // if this gets called, the transformation has not taken place
 144         throw new AssertionError("this code should be statically transformed away by Indify");
 145     }
 146 
 147     /* =========================== INVALIDATE LOGIC ============================== */
 148 
 149     private final static Invalidator invalidator = new Invalidator();
 150 
 151     @Setup
 152     public void setup() {
 153         invalidator.start();
 154     }
 155 
 156     @TearDown
 157     public void tearDown() throws InterruptedException {
 158         invalidator.stop();
 159     }
 160 
 161     public static class Invalidator implements Runnable {
 162 
 163         private final long period = Integer.getInteger("period", 1000);
 164 
 165         private final AtomicBoolean started = new AtomicBoolean();
 166         private volatile Thread thread;
 167 
 168         @Override
 169         public void run() {
 170             try {
 171                 while(!Thread.interrupted()) {
 172                     if (cs != null) {
 173                         cs.setTarget(doCall1);
 174                     }
 175                     TimeUnit.MICROSECONDS.sleep(period);
 176 
 177                     if (cs != null) {
 178                         cs.setTarget(doCall2);
 179                     }
 180                     TimeUnit.MICROSECONDS.sleep(period);
 181                 }
 182             } catch (InterruptedException e) {
 183                 // do nothing
 184             }
 185         }
 186 
 187         public void start() {
 188             if (started.compareAndSet(false, true)) {
 189                 thread = new Thread(this);
 190                 thread.setPriority(Thread.MAX_PRIORITY);
 191                 thread.start();
 192             }
 193         }
 194 
 195         public void stop() {
 196             if (thread != null) {
 197                 thread.interrupt();
 198                 try {
 199                     thread.join();
 200                 } catch (InterruptedException e) {
 201                     Thread.currentThread().interrupt();
 202                 }
 203                 started.set(false);
 204             }
 205         }
 206     }
 207 
 208 }