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.Setup; 33 import org.openjdk.jmh.annotations.State; 34 import org.openjdk.jmh.annotations.TearDown; 35 36 import java.lang.invoke.CallSite; 37 import java.lang.invoke.MethodHandle; 38 import java.lang.invoke.MethodHandles; 39 import java.lang.invoke.MethodType; 40 import java.lang.invoke.MutableCallSite; 41 import java.lang.invoke.VolatileCallSite; 42 import java.util.concurrent.TimeUnit; 43 import java.util.concurrent.atomic.AtomicBoolean; 44 45 /** 46 * This benchmark evaluates INDY performance under dynamic target updates. 47 */ 48 @BenchmarkMode(Mode.AverageTime) 49 @OutputTimeUnit(TimeUnit.NANOSECONDS) 50 @State(Scope.Thread) 51 public class CallSiteSetTarget { 52 53 /* 54 * Implementation notes: 55 * - This test makes sense for mutable and volatile call sites only 56 * - Multiple threads are calling the same callsite, and invalidator thread tries to swap target on the fly. 57 * - Additional baseline includes "raw" test, calling callsite's MH directly 58 */ 59 60 private static volatile CallSite cs; 61 62 private static MethodHandle doCall1; 63 private static MethodHandle doCall2; 64 65 static { 66 try { 67 doCall1 = MethodHandles.lookup().findVirtual(CallSiteSetTarget.class, "call1", MethodType.methodType(int.class)); 68 doCall2 = MethodHandles.lookup().findVirtual(CallSiteSetTarget.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 return i1++; 80 } 81 82 public int call2() { 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, CallSiteSetTarget.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, CallSiteSetTarget.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 /* =========================== INVALIDATE LOGIC ============================== */ 150 151 private final static Invalidator invalidator = new Invalidator(); 152 153 @Setup 154 public void setup() { 155 invalidator.start(); 156 } 157 158 @TearDown 159 public void tearDown() throws InterruptedException { 160 invalidator.stop(); 161 } 162 163 public static class Invalidator implements Runnable { 164 165 private final long period = Integer.getInteger("period", 1000); 166 167 private final AtomicBoolean started = new AtomicBoolean(); 168 private volatile Thread thread; 169 170 @Override 171 public void run() { 172 try { 173 while(!Thread.interrupted()) { 174 if (cs != null) { 175 cs.setTarget(doCall1); 176 } 177 TimeUnit.MICROSECONDS.sleep(period); 178 179 if (cs != null) { 180 cs.setTarget(doCall2); 181 } 182 TimeUnit.MICROSECONDS.sleep(period); 183 } 184 } catch (InterruptedException e) { 185 // do nothing 186 } 187 } 188 189 public void start() { 190 if (started.compareAndSet(false, true)) { 191 thread = new Thread(this); 192 thread.setPriority(Thread.MAX_PRIORITY); 193 thread.start(); 194 } 195 } 196 197 public void stop() { 198 if (thread != null) { 199 thread.interrupt(); 200 try { 201 thread.join(); 202 } catch (InterruptedException e) { 203 Thread.currentThread().interrupt(); 204 } 205 started.set(false); 206 } 207 } 208 } 209 210 }