1 /* 2 * Copyright (c) 2020 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.jdk.incubator.foreign; 24 25 import jdk.incubator.foreign.CSupport; 26 import jdk.incubator.foreign.FunctionDescriptor; 27 import jdk.incubator.foreign.LibraryLookup; 28 import jdk.incubator.foreign.ForeignLinker; 29 import jdk.incubator.foreign.MemoryAddress; 30 import jdk.incubator.foreign.MemoryLayout; 31 import jdk.incubator.foreign.MemorySegment; 32 import org.openjdk.jmh.annotations.Benchmark; 33 import org.openjdk.jmh.annotations.BenchmarkMode; 34 import org.openjdk.jmh.annotations.Fork; 35 import org.openjdk.jmh.annotations.Measurement; 36 import org.openjdk.jmh.annotations.Mode; 37 import org.openjdk.jmh.annotations.OutputTimeUnit; 38 import org.openjdk.jmh.annotations.State; 39 import org.openjdk.jmh.annotations.Warmup; 40 41 import java.lang.invoke.MethodHandle; 42 import java.lang.invoke.MethodType; 43 import java.util.concurrent.TimeUnit; 44 45 import static jdk.incubator.foreign.CSupport.C_DOUBLE; 46 import static jdk.incubator.foreign.CSupport.C_INT; 47 import static jdk.incubator.foreign.CSupport.C_LONGLONG; 48 import static jdk.incubator.foreign.CSupport.C_POINTER; 49 50 @BenchmarkMode(Mode.AverageTime) 51 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 52 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 53 @State(org.openjdk.jmh.annotations.Scope.Thread) 54 @OutputTimeUnit(TimeUnit.NANOSECONDS) 55 @Fork(3) 56 public class CallOverhead { 57 58 static final ForeignLinker abi = CSupport.getSystemLinker(); 59 static final MethodHandle func; 60 static final MethodHandle identity; 61 static final MethodHandle identity_struct; 62 static final MethodHandle identity_memory_address; 63 static final MethodHandle args5; 64 static final MethodHandle args10; 65 66 static final MemoryLayout POINT_LAYOUT = MemoryLayout.ofStruct( 67 C_LONGLONG, C_LONGLONG 68 ); 69 70 static final MemorySegment point = MemorySegment.allocateNative(POINT_LAYOUT); 71 72 static { 73 System.loadLibrary("CallOverheadJNI"); 74 75 try { 76 LibraryLookup ll = LibraryLookup.ofLibrary("CallOverhead"); 77 func = abi.downcallHandle(ll.lookup("func"), 78 MethodType.methodType(void.class), 79 FunctionDescriptor.ofVoid()); 80 identity = abi.downcallHandle(ll.lookup("identity"), 81 MethodType.methodType(int.class, int.class), 82 FunctionDescriptor.of(C_INT, C_INT)); 83 identity_struct = abi.downcallHandle(ll.lookup("identity_struct"), 84 MethodType.methodType(MemorySegment.class, MemorySegment.class), 85 FunctionDescriptor.of(POINT_LAYOUT, POINT_LAYOUT)); 86 identity_memory_address = abi.downcallHandle(ll.lookup("identity_memory_address"), 87 MethodType.methodType(MemoryAddress.class, MemoryAddress.class), 88 FunctionDescriptor.of(C_POINTER, C_POINTER)); 89 args5 = abi.downcallHandle(ll.lookup("args5"), // just reuse identity 90 MethodType.methodType(void.class, long.class, double.class, long.class, double.class, long.class), 91 FunctionDescriptor.ofVoid(C_LONGLONG, C_DOUBLE, C_LONGLONG, C_DOUBLE, C_LONGLONG)); 92 args10 = abi.downcallHandle(ll.lookup("args10"), 93 MethodType.methodType(void.class, long.class, double.class, long.class, double.class, long.class, 94 double.class, long.class, double.class, long.class, double.class), 95 FunctionDescriptor.ofVoid(C_LONGLONG, C_DOUBLE, C_LONGLONG, C_DOUBLE, C_LONGLONG, 96 C_DOUBLE, C_LONGLONG, C_DOUBLE, C_LONGLONG, C_DOUBLE)); 97 } catch (NoSuchMethodException e) { 98 throw new BootstrapMethodError(e); 99 } 100 } 101 102 static native void blank(); 103 static native int identity(int x); 104 105 @Benchmark 106 public void jni_blank() throws Throwable { 107 blank(); 108 } 109 110 @Benchmark 111 public void panama_blank() throws Throwable { 112 func.invokeExact(); 113 } 114 115 @Benchmark 116 @Fork(jvmArgsAppend = "-Djdk.internal.foreign.ProgrammableInvoker.NO_SPEC=true") 117 public void panama_blank_NO_SPEC() throws Throwable { 118 func.invokeExact(); 119 } 120 121 @Benchmark 122 public int jni_identity() throws Throwable { 123 return identity(10); 124 } 125 126 @Benchmark 127 public int panama_identity() throws Throwable { 128 return (int) identity.invokeExact(10); 129 } 130 131 @Benchmark 132 @Fork(jvmArgsAppend = "-Djdk.internal.foreign.ProgrammableInvoker.NO_SPEC=true") 133 public int panama_identity_NO_SPEC() throws Throwable { 134 return (int) identity.invokeExact(10); 135 } 136 137 @Benchmark 138 public MemorySegment panama_identity_struct() throws Throwable { 139 return (MemorySegment) identity_struct.invokeExact(point); 140 } 141 142 @Benchmark 143 @Fork(jvmArgsAppend = "-Djdk.internal.foreign.ProgrammableInvoker.NO_SPEC=true") 144 public MemorySegment panama_identity_struct_NO_SPEC() throws Throwable { 145 return (MemorySegment) identity_struct.invokeExact(point); 146 } 147 148 @Benchmark 149 public MemoryAddress panama_identity_memory_address() throws Throwable { 150 return (MemoryAddress) identity_memory_address.invokeExact(MemoryAddress.NULL); 151 } 152 153 @Benchmark 154 @Fork(jvmArgsAppend = "-Djdk.internal.foreign.ProgrammableInvoker.NO_SPEC=true") 155 public MemoryAddress panama_identity_memory_address_NO_SPEC() throws Throwable { 156 return (MemoryAddress) identity_memory_address.invokeExact(MemoryAddress.NULL); 157 } 158 159 @Benchmark 160 public void panama_args5() throws Throwable { 161 args5.invokeExact(10L, 11D, 12L, 13D, 14L); 162 } 163 164 @Benchmark 165 @Fork(jvmArgsAppend = "-Djdk.internal.foreign.ProgrammableInvoker.NO_SPEC=true") 166 public void panama_args5_NO_SPEC() throws Throwable { 167 args5.invokeExact(10L, 11D, 12L, 13D, 14L); 168 } 169 170 @Benchmark 171 public void panama_args10() throws Throwable { 172 args10.invokeExact(10L, 11D, 12L, 13D, 14L, 173 15D, 16L, 17D, 18L, 19D); 174 } 175 176 @Benchmark 177 @Fork(jvmArgsAppend = "-Djdk.internal.foreign.ProgrammableInvoker.NO_SPEC=true") 178 public void panama_args10_NO_SPEC() throws Throwable { 179 args10.invokeExact(10L, 11D, 12L, 13D, 14L, 180 15D, 16L, 17D, 18L, 19D); 181 } 182 }