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 org.openjdk.jmh.annotations.Benchmark;
30 import org.openjdk.jmh.annotations.BenchmarkMode;
31 import org.openjdk.jmh.annotations.Fork;
32 import org.openjdk.jmh.annotations.Measurement;
33 import org.openjdk.jmh.annotations.Mode;
34 import org.openjdk.jmh.annotations.OutputTimeUnit;
35 import org.openjdk.jmh.annotations.State;
36 import org.openjdk.jmh.annotations.Warmup;
37 
38 import java.lang.invoke.MethodHandle;
39 import java.lang.invoke.MethodType;
40 import java.util.concurrent.TimeUnit;
41 
42 import static jdk.incubator.foreign.CSupport.C_INT;
43 
44 @BenchmarkMode(Mode.AverageTime)
45 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
46 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
47 @State(org.openjdk.jmh.annotations.Scope.Thread)
48 @OutputTimeUnit(TimeUnit.NANOSECONDS)
49 @Fork(3)
50 public class CallOverhead {
51 
52     static final ForeignLinker abi = CSupport.getSystemLinker();
53     static final MethodHandle func;
54     static final MethodHandle identity;
55 
56     static {
57         System.loadLibrary("CallOverheadJNI");
58 
59         try {
60             LibraryLookup ll = LibraryLookup.ofLibrary("CallOverhead");
61             func = abi.downcallHandle(ll.lookup("func"),
62                     MethodType.methodType(void.class),
63                     FunctionDescriptor.ofVoid());
64             identity = abi.downcallHandle(ll.lookup("identity"),
65                     MethodType.methodType(int.class, int.class),
66                     FunctionDescriptor.of(C_INT, C_INT));
67         } catch (NoSuchMethodException e) {
68             throw new BootstrapMethodError(e);
69         }
70     }
71 
72     static native void blank();
73     static native int identity(int x);
74 
75     @Benchmark
76     public void jni_blank() throws Throwable {
77         blank();
78     }
79 
80     @Benchmark
81     public void panama_blank() throws Throwable {
82         func.invokeExact();
83     }
84 
85     @Benchmark
86     public int jni_identity() throws Throwable {
87         return identity(10);
88     }
89 
90     @Benchmark
91     public int panama_identity() throws Throwable {
92         return (int) identity.invokeExact(10);
93     }
94 }