1 /*
  2  * Copyright (c) 2020, 2021, 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.Addressable;
 26 import jdk.incubator.foreign.CLinker;
 27 import jdk.incubator.foreign.FunctionDescriptor;
 28 import jdk.incubator.foreign.NativeSymbol;
 29 import jdk.incubator.foreign.ResourceScope;
 30 import jdk.incubator.foreign.SymbolLookup;
 31 import org.openjdk.jmh.annotations.Benchmark;
 32 import org.openjdk.jmh.annotations.BenchmarkMode;
 33 import org.openjdk.jmh.annotations.Fork;
 34 import org.openjdk.jmh.annotations.Measurement;
 35 import org.openjdk.jmh.annotations.Mode;
 36 import org.openjdk.jmh.annotations.OutputTimeUnit;
 37 import org.openjdk.jmh.annotations.State;
 38 import org.openjdk.jmh.annotations.Warmup;
 39 
 40 import java.lang.invoke.MethodHandle;
 41 import java.lang.invoke.MethodType;
 42 import java.util.concurrent.TimeUnit;
 43 
 44 import static java.lang.invoke.MethodHandles.lookup;
 45 
 46 @BenchmarkMode(Mode.AverageTime)
 47 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 48 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 49 @State(org.openjdk.jmh.annotations.Scope.Thread)
 50 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 51 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 52 public class Upcalls extends CLayouts {
 53 
 54     static final CLinker abi = CLinker.systemCLinker();
 55     static final MethodHandle blank;
 56     static final MethodHandle identity;
 57     static final MethodHandle args5;
 58     static final MethodHandle args10;
 59 
 60     static final NativeSymbol cb_blank;
 61     static final NativeSymbol cb_identity;
 62     static final NativeSymbol cb_args5;
 63     static final NativeSymbol cb_args10;
 64 
 65     static final long cb_blank_jni;
 66     static final long cb_identity_jni;
 67     static final long cb_args5_jni;
 68     static final long cb_args10_jni;
 69 
 70     static {
 71         System.loadLibrary("UpcallsJNI");
 72 
 73         String className = "org/openjdk/bench/jdk/incubator/foreign/Upcalls";
 74         cb_blank_jni = JNICB.makeCB(className, "blank", "()V");
 75         cb_identity_jni = JNICB.makeCB(className, "identity", "(I)I");
 76         cb_args5_jni = JNICB.makeCB(className, "args5", "(JDJDJ)V");
 77         cb_args10_jni = JNICB.makeCB(className, "args10", "(JDJDJDJDJD)V");
 78 
 79         try {
 80             System.loadLibrary("Upcalls");
 81             {
 82                 String name = "blank";
 83                 MethodType mt = MethodType.methodType(void.class);
 84                 FunctionDescriptor fd = FunctionDescriptor.ofVoid();
 85 
 86                 blank = linkFunc(name, fd);
 87                 cb_blank = makeCB(name, mt, fd);
 88             }
 89             {
 90                 String name = "identity";
 91                 MethodType mt = MethodType.methodType(int.class, int.class);
 92                 FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT);
 93 
 94                 identity = linkFunc(name, fd);
 95                 cb_identity = makeCB(name, mt, fd);
 96             }
 97             {
 98                 String name = "args5";
 99                 MethodType mt = MethodType.methodType(void.class,
100                         long.class, double.class, long.class, double.class, long.class);
101                 FunctionDescriptor fd = FunctionDescriptor.ofVoid(
102                         C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG);
103 
104                 args5 = linkFunc(name, fd);
105                 cb_args5 = makeCB(name, mt, fd);
106             }
107             {
108                 String name = "args10";
109                 MethodType mt = MethodType.methodType(void.class,
110                         long.class, double.class, long.class, double.class, long.class,
111                         double.class, long.class, double.class, long.class, double.class);
112                 FunctionDescriptor fd = FunctionDescriptor.ofVoid(
113                         C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG,
114                         C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE);
115 
116                 args10 = linkFunc(name, fd);
117                 cb_args10 = makeCB(name, mt, fd);
118             }
119         } catch (ReflectiveOperationException e) {
120             throw new BootstrapMethodError(e);
121         }
122     }
123 
124     static MethodHandle linkFunc(String name, FunctionDescriptor baseDesc) {
125         return abi.downcallHandle(
126             SymbolLookup.loaderLookup().lookup(name).orElseThrow(),
127                 baseDesc.withAppendedArgumentLayouts(C_POINTER)
128         );
129     }
130 
131     static NativeSymbol makeCB(String name, MethodType mt, FunctionDescriptor fd) throws ReflectiveOperationException {
132         return abi.upcallStub(
133             lookup().findStatic(Upcalls.class, name, mt),
134             fd, ResourceScope.globalScope()
135         );
136     }
137 
138     static native void blank(long cb);
139     static native int identity(int x, long cb);
140     static native void args5(long a0, double a1, long a2, double a3, long a4, long cb);
141     static native void args10(long a0, double a1, long a2, double a3, long a4,
142                               double a5, long a6, double a7, long a8, double a9, long cb);
143 
144     @Benchmark
145     public void jni_blank() throws Throwable {
146         blank(cb_blank_jni);
147     }
148 
149     @Benchmark
150     public void panama_blank() throws Throwable {
151         blank.invokeExact((Addressable)cb_blank);
152     }
153 
154     @Benchmark
155     public int jni_identity() throws Throwable {
156         return identity(10, cb_identity_jni);
157     }
158 
159     @Benchmark
160     public void jni_args5() throws Throwable {
161         args5(1L, 2D, 3L, 4D, 5L, cb_args5_jni);
162     }
163 
164     @Benchmark
165     public void jni_args10() throws Throwable {
166         args10(1L, 2D, 3L, 4D, 5L, 6D, 7L, 8D, 9L, 10D, cb_args10_jni);
167     }
168 
169     @Benchmark
170     public int panama_identity() throws Throwable {
171         return (int) identity.invokeExact(10, (Addressable)cb_identity);
172     }
173 
174     @Benchmark
175     public void panama_args5() throws Throwable {
176         args5.invokeExact(1L, 2D, 3L, 4D, 5L, (Addressable)cb_args5);
177     }
178 
179     @Benchmark
180     public void panama_args10() throws Throwable {
181         args10.invokeExact(1L, 2D, 3L, 4D, 5L, 6D, 7L, 8D, 9L, 10D, (Addressable)cb_args10);
182     }
183 
184     static void blank() {}
185     static int identity(int x) { return x; }
186     static void args5(long a0, double a1, long a2, double a3, long a4) { }
187     static void args10(long a0, double a1, long a2, double a3, long a4,
188                        double a5, long a6, double a7, long a8, double a9) { }
189 }