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" })
 45 public class CallOverheadConstant {
 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.invokeExact();
 55     }
 56 
 57     @Benchmark
 58     public void panama_blank_critical() throws Throwable {
 59         func_critical.invokeExact();
 60     }
 61 
 62     @Benchmark
 63     public int jni_identity() throws Throwable {
 64         return identity(10);
 65     }
 66 
 67     @Benchmark
 68     public int panama_identity() throws Throwable {
 69         return (int) identity.invokeExact(10);
 70     }
 71 
 72     @Benchmark
 73     public int panama_identity_critical() throws Throwable {
 74         return (int) identity_critical.invokeExact(10);
 75     }
 76 
 77     @Benchmark
 78     public MemorySegment panama_identity_struct_confined() throws Throwable {
 79         return (MemorySegment) identity_struct.invokeExact(recycling_allocator, confinedPoint);
 80     }
 81 
 82     @Benchmark
 83     public MemorySegment panama_identity_struct_shared() throws Throwable {
 84         return (MemorySegment) identity_struct.invokeExact(recycling_allocator, sharedPoint);
 85     }
 86 
 87     @Benchmark
 88     public MemorySegment panama_identity_struct_confined_3() throws Throwable {
 89         return (MemorySegment) identity_struct_3.invokeExact(recycling_allocator, confinedPoint, confinedPoint, confinedPoint);
 90     }
 91 
 92     @Benchmark
 93     public MemorySegment panama_identity_struct_shared_3() throws Throwable {
 94         return (MemorySegment) identity_struct_3.invokeExact(recycling_allocator, sharedPoint, sharedPoint, sharedPoint);
 95     }
 96 
 97     @Benchmark
 98     public MemorySegment panama_identity_memory_address_shared() throws Throwable {
 99         return (MemorySegment) identity_memory_address.invokeExact(sharedPoint);
100     }
101 
102     @Benchmark
103     public MemorySegment panama_identity_memory_address_confined() throws Throwable {
104         return (MemorySegment) identity_memory_address.invokeExact(confinedPoint);
105     }
106 
107     @Benchmark
108     public MemorySegment panama_identity_memory_address_shared_3() throws Throwable {
109         return (MemorySegment) identity_memory_address_3.invokeExact(sharedPoint, sharedPoint, sharedPoint);
110     }
111 
112     @Benchmark
113     public MemorySegment panama_identity_memory_address_confined_3() throws Throwable {
114         return (MemorySegment) identity_memory_address_3.invokeExact(confinedPoint, confinedPoint, confinedPoint);
115     }
116 
117     @Benchmark
118     public MemorySegment panama_identity_memory_address_null() throws Throwable {
119         return (MemorySegment) identity_memory_address.invokeExact(MemorySegment.NULL);
120     }
121 
122     @Benchmark
123     public MemorySegment panama_identity_memory_address_null_3() throws Throwable {
124         return (MemorySegment) identity_memory_address_3.invokeExact(MemorySegment.NULL, MemorySegment.NULL, MemorySegment.NULL);
125     }
126 
127     @Benchmark
128     public MemorySegment panama_identity_memory_address_null_non_exact() throws Throwable {
129         return (MemorySegment) identity_memory_address.invoke(MemorySegment.NULL);
130     }
131 
132     @Benchmark
133     public void panama_args_01() throws Throwable {
134         args1.invokeExact(10L);
135     }
136 
137     @Benchmark
138     public void panama_args_02() throws Throwable {
139         args2.invokeExact(10L, 11D);
140     }
141 
142     @Benchmark
143     public void panama_args_03() throws Throwable {
144         args3.invokeExact(10L, 11D, 12L);
145     }
146 
147     @Benchmark
148     public void panama_args_04() throws Throwable {
149         args4.invokeExact(10L, 11D, 12L, 13D);
150     }
151 
152     @Benchmark
153     public void panama_args_05() throws Throwable {
154         args5.invokeExact(10L, 11D, 12L, 13D, 14L);
155     }
156 
157     @Benchmark
158     public void panama_args_10() throws Throwable {
159         args10.invokeExact(10L, 11D, 12L, 13D, 14L,
160                            15D, 16L, 17D, 18L, 19D);
161     }
162 }