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.MemoryAddress;
 26 import jdk.incubator.foreign.MemorySegment;
 27 import org.openjdk.jmh.annotations.Benchmark;
 28 import org.openjdk.jmh.annotations.BenchmarkMode;
 29 import org.openjdk.jmh.annotations.Fork;
 30 import org.openjdk.jmh.annotations.Measurement;
 31 import org.openjdk.jmh.annotations.Mode;
 32 import org.openjdk.jmh.annotations.OutputTimeUnit;
 33 import org.openjdk.jmh.annotations.State;
 34 import org.openjdk.jmh.annotations.Warmup;
 35 
 36 import java.util.concurrent.TimeUnit;
 37 
 38 import static org.openjdk.bench.jdk.incubator.foreign.CallOverheadHelper.*;
 39 
 40 @BenchmarkMode(Mode.AverageTime)
 41 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 42 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 43 @State(org.openjdk.jmh.annotations.Scope.Thread)
 44 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 45 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 46 public class CallOverheadConstant {
 47 
 48     @Benchmark
 49     public void jni_blank() throws Throwable {
 50         blank();
 51     }
 52 
 53     @Benchmark
 54     public void panama_blank() throws Throwable {
 55         func.invokeExact();
 56     }
 57 
 58     @Benchmark
 59     public void panama_blank_trivial() throws Throwable {
 60         func_trivial.invokeExact();
 61     }
 62 
 63     @Benchmark
 64     public int jni_identity() throws Throwable {
 65         return identity(10);
 66     }
 67 
 68     @Benchmark
 69     public int panama_identity() throws Throwable {
 70         return (int) identity.invokeExact(10);
 71     }
 72 
 73     @Benchmark
 74     public int panama_identity_trivial() throws Throwable {
 75         return (int) identity_trivial.invokeExact(10);
 76     }
 77 
 78     @Benchmark
 79     public MemorySegment panama_identity_struct() throws Throwable {
 80         return (MemorySegment) identity_struct.invokeExact(recycling_allocator, point);
 81     }
 82 
 83     @Benchmark
 84     public MemoryAddress panama_identity_memory_address() throws Throwable {
 85         return (MemoryAddress) identity_memory_address.invokeExact(MemoryAddress.NULL);
 86     }
 87 
 88     @Benchmark
 89     public void panama_args_01() throws Throwable {
 90         args1.invokeExact(10L);
 91     }
 92 
 93     @Benchmark
 94     public void panama_args_02() throws Throwable {
 95         args2.invokeExact(10L, 11D);
 96     }
 97 
 98     @Benchmark
 99     public void panama_args_03() throws Throwable {
100         args3.invokeExact(10L, 11D, 12L);
101     }
102 
103     @Benchmark
104     public void panama_args_04() throws Throwable {
105         args4.invokeExact(10L, 11D, 12L, 13D);
106     }
107 
108     @Benchmark
109     public void panama_args_05() throws Throwable {
110         args5.invokeExact(10L, 11D, 12L, 13D, 14L);
111     }
112 
113     @Benchmark
114     public void panama_args_10() throws Throwable {
115         args10.invokeExact(10L, 11D, 12L, 13D, 14L,
116                            15D, 16L, 17D, 18L, 19D);
117     }
118 }