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 CallOverheadVirtual {
 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_v.invokeExact(func_addr);
 57     }
 58 
 59     @Benchmark
 60     public int jni_identity() throws Throwable {
 61         return identity(10);
 62     }
 63 
 64     public MemorySegment panama_identity_struct_confined() throws Throwable {
 65         return (MemorySegment) identity_struct_v.invokeExact(identity_struct_addr, recycling_allocator, confinedPoint);
 66     }
 67 
 68     @Benchmark
 69     public MemorySegment panama_identity_struct_shared() throws Throwable {
 70         return (MemorySegment) identity_struct_v.invokeExact(identity_struct_addr, recycling_allocator, sharedPoint);
 71     }
 72 
 73     @Benchmark
 74     public MemorySegment panama_identity_struct_confined_3() throws Throwable {
 75         return (MemorySegment) identity_struct_v.invokeExact(identity_struct_addr, recycling_allocator, confinedPoint, confinedPoint, confinedPoint);
 76     }
 77 
 78     @Benchmark
 79     public MemorySegment panama_identity_struct_shared_3() throws Throwable {
 80         return (MemorySegment) identity_struct_v.invokeExact(identity_struct_addr, recycling_allocator, sharedPoint, sharedPoint, sharedPoint);
 81     }
 82 
 83     @Benchmark
 84     public MemoryAddress panama_identity_memory_address_shared() throws Throwable {
 85         return (MemoryAddress) identity_memory_address_v.invokeExact(identity_memory_address_addr, (Addressable)sharedPoint.address());
 86     }
 87 
 88     @Benchmark
 89     public MemoryAddress panama_identity_memory_address_confined() throws Throwable {
 90         return (MemoryAddress) identity_memory_address_v.invokeExact(identity_memory_address_addr, (Addressable)confinedPoint.address());
 91     }
 92 
 93     @Benchmark
 94     public MemoryAddress panama_identity_memory_address_shared_3() throws Throwable {
 95         return (MemoryAddress) identity_memory_address_3_v.invokeExact(identity_memory_address_3_addr, (Addressable)sharedPoint.address(), (Addressable)sharedPoint.address(), (Addressable)sharedPoint.address());
 96     }
 97 
 98     @Benchmark
 99     public MemoryAddress panama_identity_memory_address_confined_3() throws Throwable {
100         return (MemoryAddress) identity_memory_address_3_v.invokeExact(identity_memory_address_3_addr, (Addressable)confinedPoint.address(), (Addressable)confinedPoint.address(), (Addressable)confinedPoint.address());
101     }
102 
103     @Benchmark
104     public MemoryAddress panama_identity_struct_ref_shared() throws Throwable {
105         return (MemoryAddress) identity_memory_address_v.invokeExact((Addressable)sharedPoint);
106     }
107 
108     @Benchmark
109     public MemoryAddress panama_identity_struct_ref_confined() throws Throwable {
110         return (MemoryAddress) identity_memory_address_v.invokeExact((Addressable)confinedPoint);
111     }
112 
113     @Benchmark
114     public MemoryAddress panama_identity_struct_ref_shared_3() throws Throwable {
115         return (MemoryAddress) identity_memory_address_3_v.invokeExact((Addressable)sharedPoint, (Addressable)sharedPoint, (Addressable)sharedPoint);
116     }
117 
118     @Benchmark
119     public MemoryAddress panama_identity_struct_ref_confined_3() throws Throwable {
120         return (MemoryAddress) identity_memory_address_3_v.invokeExact((Addressable)confinedPoint, (Addressable)confinedPoint, (Addressable)confinedPoint);
121     }
122 
123     @Benchmark
124     public int panama_identity() throws Throwable {
125         return (int) identity_v.invokeExact(identity_addr, 10);
126     }
127 
128     @Benchmark
129     public MemorySegment panama_identity_struct() throws Throwable {
130         return (MemorySegment) identity_struct_v.invokeExact(identity_struct_addr, recycling_allocator, point);
131     }
132 
133     @Benchmark
134     public MemoryAddress panama_identity_memory_address_null() throws Throwable {
135         return (MemoryAddress) identity_memory_address_v.invokeExact(identity_memory_address_addr, MemoryAddress.NULL);
136     }
137 
138     @Benchmark
139     public void panama_args_01() throws Throwable {
140         args1_v.invokeExact(args1_addr, 10L);
141     }
142 
143     @Benchmark
144     public void panama_args_02() throws Throwable {
145         args2_v.invokeExact(args2_addr, 10L, 11D);
146     }
147 
148     @Benchmark
149     public void panama_args_03() throws Throwable {
150         args3_v.invokeExact(args3_addr, 10L, 11D, 12L);
151     }
152 
153     @Benchmark
154     public void panama_args_04() throws Throwable {
155         args4_v.invokeExact(args4_addr, 10L, 11D, 12L, 13D);
156     }
157 
158     @Benchmark
159     public void panama_args_05() throws Throwable {
160         args5_v.invokeExact(args5_addr, 10L, 11D, 12L, 13D, 14L);
161     }
162 
163     @Benchmark
164     public void panama_args_10() throws Throwable {
165         args10_v.invokeExact(args10_addr,
166                            10L, 11D, 12L, 13D, 14L,
167                            15D, 16L, 17D, 18L, 19D);
168     }
169 }