1 /*
  2  * Copyright (c) 2021, 2022, 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.java.lang.foreign;
 24 
 25 import java.lang.foreign.MemorySegment;
 26 import org.openjdk.jmh.annotations.Benchmark;
 27 import org.openjdk.jmh.annotations.BenchmarkMode;
 28 import org.openjdk.jmh.annotations.Fork;
 29 import org.openjdk.jmh.annotations.Measurement;
 30 import org.openjdk.jmh.annotations.Mode;
 31 import org.openjdk.jmh.annotations.OutputTimeUnit;
 32 import org.openjdk.jmh.annotations.State;
 33 import org.openjdk.jmh.annotations.Warmup;
 34 
 35 import java.util.concurrent.TimeUnit;
 36 
 37 import static org.openjdk.bench.java.lang.foreign.CallOverheadHelper.*;
 38 
 39 @BenchmarkMode(Mode.AverageTime)
 40 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 41 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 42 @State(org.openjdk.jmh.annotations.Scope.Thread)
 43 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 44 @Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED", "--enable-preview" })
 45 public class CallOverheadVirtual {
 46 
 47     @Benchmark
 48     public void jni_blank() throws Throwable {
 49         blank();
 50     }
 51 
 52     @Benchmark
 53     public void panama_blank() throws Throwable {
 54         func_v.invokeExact(func_addr);
 55     }
 56 
 57     @Benchmark
 58     public void panama_blank_trivial() throws Throwable {
 59         func_trivial_v.invokeExact(func_addr);
 60     }
 61 
 62     @Benchmark
 63     public int jni_identity() throws Throwable {
 64         return identity(10);
 65     }
 66 
 67     public MemorySegment panama_identity_struct_confined() throws Throwable {
 68         return (MemorySegment) identity_struct_v.invokeExact(identity_struct_addr, recycling_allocator, confinedPoint);
 69     }
 70 
 71     @Benchmark
 72     public MemorySegment panama_identity_struct_shared() throws Throwable {
 73         return (MemorySegment) identity_struct_v.invokeExact(identity_struct_addr, recycling_allocator, sharedPoint);
 74     }
 75 
 76     @Benchmark
 77     public MemorySegment panama_identity_struct_confined_3() throws Throwable {
 78         return (MemorySegment) identity_struct_3_v.invokeExact(identity_struct_3_addr, recycling_allocator, confinedPoint, confinedPoint, confinedPoint);
 79     }
 80 
 81     @Benchmark
 82     public MemorySegment panama_identity_struct_shared_3() throws Throwable {
 83         return (MemorySegment) identity_struct_3_v.invokeExact(identity_struct_3_addr, recycling_allocator, sharedPoint, sharedPoint, sharedPoint);
 84     }
 85 
 86     @Benchmark
 87     public MemorySegment panama_identity_memory_address_shared() throws Throwable {
 88         return (MemorySegment) identity_memory_address_v.invokeExact(identity_memory_address_addr, sharedPoint);
 89     }
 90 
 91     @Benchmark
 92     public MemorySegment panama_identity_memory_address_confined() throws Throwable {
 93         return (MemorySegment) identity_memory_address_v.invokeExact(identity_memory_address_addr, confinedPoint);
 94     }
 95 
 96     @Benchmark
 97     public MemorySegment panama_identity_memory_address_shared_3() throws Throwable {
 98         return (MemorySegment) identity_memory_address_3_v.invokeExact(identity_memory_address_3_addr, sharedPoint, sharedPoint, sharedPoint);
 99     }
100 
101     @Benchmark
102     public MemorySegment panama_identity_memory_address_confined_3() throws Throwable {
103         return (MemorySegment) identity_memory_address_3_v.invokeExact(identity_memory_address_3_addr, confinedPoint, confinedPoint, confinedPoint);
104     }
105 
106     @Benchmark
107     public int panama_identity() throws Throwable {
108         return (int) identity_v.invokeExact(identity_addr, 10);
109     }
110 
111     @Benchmark
112     public int panama_identity_trivial() throws Throwable {
113         return (int) identity_trivial_v.invokeExact(identity_addr, 10);
114     }
115 
116     @Benchmark
117     public MemorySegment panama_identity_struct() throws Throwable {
118         return (MemorySegment) identity_struct_v.invokeExact(identity_struct_addr, recycling_allocator, point);
119     }
120 
121     @Benchmark
122     public MemorySegment panama_identity_memory_address_null() throws Throwable {
123         return (MemorySegment) identity_memory_address_v.invokeExact(identity_memory_address_addr, MemorySegment.NULL);
124     }
125 
126     @Benchmark
127     public void panama_args_01() throws Throwable {
128         args1_v.invokeExact(args1_addr, 10L);
129     }
130 
131     @Benchmark
132     public void panama_args_02() throws Throwable {
133         args2_v.invokeExact(args2_addr, 10L, 11D);
134     }
135 
136     @Benchmark
137     public void panama_args_03() throws Throwable {
138         args3_v.invokeExact(args3_addr, 10L, 11D, 12L);
139     }
140 
141     @Benchmark
142     public void panama_args_04() throws Throwable {
143         args4_v.invokeExact(args4_addr, 10L, 11D, 12L, 13D);
144     }
145 
146     @Benchmark
147     public void panama_args_05() throws Throwable {
148         args5_v.invokeExact(args5_addr, 10L, 11D, 12L, 13D, 14L);
149     }
150 
151     @Benchmark
152     public void panama_args_10() throws Throwable {
153         args10_v.invokeExact(args10_addr,
154                            10L, 11D, 12L, 13D, 14L,
155                            15D, 16L, 17D, 18L, 19D);
156     }
157 }