1 /*
  2  * Copyright (c) 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.MemoryAddress;
 27 import jdk.incubator.foreign.MemorySegment;
 28 import org.openjdk.jmh.annotations.Benchmark;
 29 import org.openjdk.jmh.annotations.BenchmarkMode;
 30 import org.openjdk.jmh.annotations.Fork;
 31 import org.openjdk.jmh.annotations.Measurement;
 32 import org.openjdk.jmh.annotations.Mode;
 33 import org.openjdk.jmh.annotations.OutputTimeUnit;
 34 import org.openjdk.jmh.annotations.State;
 35 import org.openjdk.jmh.annotations.Warmup;
 36 
 37 import java.util.concurrent.TimeUnit;
 38 
 39 import static org.openjdk.bench.jdk.incubator.foreign.CallOverheadHelper.*;
 40 
 41 @BenchmarkMode(Mode.AverageTime)
 42 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 43 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 44 @State(org.openjdk.jmh.annotations.Scope.Thread)
 45 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 46 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 47 public class CallOverheadConstant {
 48 
 49     @Benchmark
 50     public void jni_blank() throws Throwable {
 51         blank();
 52     }
 53 
 54     @Benchmark
 55     public void panama_blank() throws Throwable {
 56         func.invokeExact();
 57     }
 58 
 59     @Benchmark
 60     public int jni_identity() throws Throwable {
 61         return identity(10);
 62     }
 63 
 64     @Benchmark
 65     public int panama_identity() throws Throwable {
 66         return (int) identity.invokeExact(10);
 67     }
 68 
 69     @Benchmark
 70     public MemorySegment panama_identity_struct_confined() throws Throwable {
 71         return (MemorySegment) identity_struct.invokeExact(recycling_allocator, confinedPoint);
 72     }
 73 
 74     @Benchmark
 75     public MemorySegment panama_identity_struct_shared() throws Throwable {
 76         return (MemorySegment) identity_struct.invokeExact(recycling_allocator, sharedPoint);
 77     }
 78 
 79     @Benchmark
 80     public MemorySegment panama_identity_struct_confined_3() throws Throwable {
 81         return (MemorySegment) identity_struct_3.invokeExact(recycling_allocator, confinedPoint, confinedPoint, confinedPoint);
 82     }
 83 
 84     @Benchmark
 85     public MemorySegment panama_identity_struct_shared_3() throws Throwable {
 86         return (MemorySegment) identity_struct_3.invokeExact(recycling_allocator, sharedPoint, sharedPoint, sharedPoint);
 87     }
 88 
 89     @Benchmark
 90     public MemoryAddress panama_identity_memory_address_shared() throws Throwable {
 91         return (MemoryAddress) identity_memory_address.invokeExact((Addressable)sharedPoint.address());
 92     }
 93 
 94     @Benchmark
 95     public MemoryAddress panama_identity_memory_address_confined() throws Throwable {
 96         return (MemoryAddress) identity_memory_address.invokeExact((Addressable)confinedPoint.address());
 97     }
 98 
 99     @Benchmark
100     public MemoryAddress panama_identity_memory_address_shared_3() throws Throwable {
101         return (MemoryAddress) identity_memory_address_3.invokeExact((Addressable)sharedPoint.address(), (Addressable)sharedPoint.address(), (Addressable)sharedPoint.address());
102     }
103 
104     @Benchmark
105     public MemoryAddress panama_identity_memory_address_confined_3() throws Throwable {
106         return (MemoryAddress) identity_memory_address_3.invokeExact((Addressable)confinedPoint.address(), (Addressable)confinedPoint.address(), (Addressable)confinedPoint.address());
107     }
108 
109     @Benchmark
110     public MemoryAddress panama_identity_struct_ref_shared() throws Throwable {
111         return (MemoryAddress) identity_memory_address.invokeExact((Addressable)sharedPoint);
112     }
113 
114     @Benchmark
115     public MemoryAddress panama_identity_struct_ref_confined() throws Throwable {
116         return (MemoryAddress) identity_memory_address.invokeExact((Addressable)confinedPoint);
117     }
118 
119     @Benchmark
120     public MemoryAddress panama_identity_struct_ref_shared_3() throws Throwable {
121         return (MemoryAddress) identity_memory_address_3.invokeExact((Addressable)sharedPoint, (Addressable)sharedPoint, (Addressable)sharedPoint);
122     }
123 
124     @Benchmark
125     public MemoryAddress panama_identity_struct_ref_confined_3() throws Throwable {
126         return (MemoryAddress) identity_memory_address_3.invokeExact((Addressable)confinedPoint, (Addressable)confinedPoint, (Addressable)confinedPoint);
127     }
128 
129     @Benchmark
130     public MemoryAddress panama_identity_memory_address_null() throws Throwable {
131         return (MemoryAddress) identity_memory_address.invokeExact((Addressable)MemoryAddress.NULL);
132     }
133 
134     @Benchmark
135     public MemoryAddress panama_identity_memory_address_null_non_exact() throws Throwable {
136         return (MemoryAddress) identity_memory_address.invoke(MemoryAddress.NULL);
137     }
138 
139     @Benchmark
140     public void panama_args_01() throws Throwable {
141         args1.invokeExact(10L);
142     }
143 
144     @Benchmark
145     public void panama_args_02() throws Throwable {
146         args2.invokeExact(10L, 11D);
147     }
148 
149     @Benchmark
150     public void panama_args_03() throws Throwable {
151         args3.invokeExact(10L, 11D, 12L);
152     }
153 
154     @Benchmark
155     public void panama_args_04() throws Throwable {
156         args4.invokeExact(10L, 11D, 12L, 13D);
157     }
158 
159     @Benchmark
160     public void panama_args_05() throws Throwable {
161         args5.invokeExact(10L, 11D, 12L, 13D, 14L);
162     }
163 
164     @Benchmark
165     public void panama_args_10() throws Throwable {
166         args10.invokeExact(10L, 11D, 12L, 13D, 14L,
167                            15D, 16L, 17D, 18L, 19D);
168     }
169 }