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.*;
 26 
 27 import java.lang.invoke.MethodHandle;
 28 import java.lang.invoke.MethodType;
 29 
 30 import static java.lang.invoke.MethodHandles.insertArguments;
 31 
 32 public class CallOverheadHelper extends CLayouts {
 33 
 34     static final Linker abi = Linker.nativeLinker();
 35 
 36     static final MethodHandle func;
 37     static final MethodHandle func_critical;
 38     static final MethodHandle func_v;
 39     static final MethodHandle func_critical_v;
 40     static MemorySegment func_addr;
 41     static final MethodHandle identity;
 42     static final MethodHandle identity_critical;
 43     static final MethodHandle identity_v;
 44     static final MethodHandle identity_critical_v;
 45     static MemorySegment identity_addr;
 46     static final MethodHandle identity_struct;
 47     static final MethodHandle identity_struct_v;
 48     static MemorySegment identity_struct_addr;
 49     static final MethodHandle identity_struct_3;
 50     static final MethodHandle identity_struct_3_v;
 51     static MemorySegment identity_struct_3_addr;
 52     static final MethodHandle identity_memory_address;
 53     static final MethodHandle identity_memory_address_v;
 54     static MemorySegment identity_memory_address_addr;
 55     static final MethodHandle identity_memory_address_3;
 56     static final MethodHandle identity_memory_address_3_v;
 57     static MemorySegment identity_memory_address_3_addr;
 58     static final MethodHandle args1;
 59     static final MethodHandle args1_v;
 60     static MemorySegment args1_addr;
 61     static final MethodHandle args2;
 62     static final MethodHandle args2_v;
 63     static MemorySegment args2_addr;
 64     static final MethodHandle args3;
 65     static final MethodHandle args3_v;
 66     static MemorySegment args3_addr;
 67     static final MethodHandle args4;
 68     static final MethodHandle args4_v;
 69     static MemorySegment args4_addr;
 70     static final MethodHandle args5;
 71     static final MethodHandle args5_v;
 72     static MemorySegment args5_addr;
 73     static final MethodHandle args10;
 74     static final MethodHandle args10_v;
 75     static MemorySegment args10_addr;
 76 
 77     static final MemoryLayout POINT_LAYOUT = MemoryLayout.structLayout(
 78             C_INT, C_INT
 79     );
 80 
 81     static final MemorySegment sharedPoint;
 82 
 83     static {
 84         Arena scope = Arena.ofShared();
 85         sharedPoint = scope.allocate(POINT_LAYOUT);
 86     }
 87 
 88     static final MemorySegment confinedPoint;
 89 
 90     static {
 91         Arena scope = Arena.ofConfined();
 92         confinedPoint = scope.allocate(POINT_LAYOUT);
 93     }
 94 
 95     static final MemorySegment point;
 96 
 97     static {
 98         Arena scope = Arena.ofAuto();
 99         point = scope.allocate(POINT_LAYOUT);
100     }
101 
102     static final SegmentAllocator recycling_allocator;
103 
104     static {
105         Arena scope = Arena.ofAuto();
106         recycling_allocator = SegmentAllocator.prefixAllocator(scope.allocate(POINT_LAYOUT));
107         System.loadLibrary("CallOverheadJNI");
108 
109         System.loadLibrary("CallOverhead");
110         SymbolLookup loaderLibs = SymbolLookup.loaderLookup();
111         {
112             func_addr = loaderLibs.find("func").orElseThrow();
113             MethodType mt = MethodType.methodType(void.class);
114             FunctionDescriptor fd = FunctionDescriptor.ofVoid();
115             func_v = abi.downcallHandle(fd);
116             func_critical_v = abi.downcallHandle(fd, Linker.Option.critical());
117             func = insertArguments(func_v, 0, func_addr);
118             func_critical = insertArguments(func_critical_v, 0, func_addr);
119         }
120         {
121             identity_addr = loaderLibs.find("identity").orElseThrow();
122             FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT);
123             identity_v = abi.downcallHandle(fd);
124             identity_critical_v = abi.downcallHandle(fd, Linker.Option.critical());
125             identity = insertArguments(identity_v, 0, identity_addr);
126             identity_critical = insertArguments(identity_critical_v, 0, identity_addr);
127         }
128         identity_struct_addr = loaderLibs.find("identity_struct").orElseThrow();
129         identity_struct_v = abi.downcallHandle(
130                 FunctionDescriptor.of(POINT_LAYOUT, POINT_LAYOUT));
131         identity_struct = insertArguments(identity_struct_v, 0, identity_struct_addr);
132 
133         identity_struct_3_addr = loaderLibs.find("identity_struct_3").orElseThrow();
134         identity_struct_3_v = abi.downcallHandle(
135                 FunctionDescriptor.of(POINT_LAYOUT, POINT_LAYOUT, POINT_LAYOUT, POINT_LAYOUT));
136         identity_struct_3 = insertArguments(identity_struct_3_v, 0, identity_struct_3_addr);
137 
138         identity_memory_address_addr = loaderLibs.find("identity_memory_address").orElseThrow();
139         identity_memory_address_v = abi.downcallHandle(
140                 FunctionDescriptor.of(C_POINTER, C_POINTER));
141         identity_memory_address = insertArguments(identity_memory_address_v, 0, identity_memory_address_addr);
142 
143         identity_memory_address_3_addr = loaderLibs.find("identity_memory_address_3").orElseThrow();
144         identity_memory_address_3_v = abi.downcallHandle(
145                 FunctionDescriptor.of(C_POINTER, C_POINTER, C_POINTER, C_POINTER));
146         identity_memory_address_3 = insertArguments(identity_memory_address_3_v, 0, identity_memory_address_3_addr);
147 
148         args1_addr = loaderLibs.find("args1").orElseThrow();
149         args1_v = abi.downcallHandle(
150                 FunctionDescriptor.ofVoid(C_LONG_LONG));
151         args1 = insertArguments(args1_v, 0, args1_addr);
152 
153         args2_addr = loaderLibs.find("args2").orElseThrow();
154         args2_v = abi.downcallHandle(
155                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE));
156         args2 = insertArguments(args2_v, 0, args2_addr);
157 
158         args3_addr = loaderLibs.find("args3").orElseThrow();
159         args3_v = abi.downcallHandle(
160                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG));
161         args3 = insertArguments(args3_v, 0, args3_addr);
162 
163         args4_addr = loaderLibs.find("args4").orElseThrow();
164         args4_v = abi.downcallHandle(
165                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE));
166         args4 = insertArguments(args4_v, 0, args4_addr);
167 
168         args5_addr = loaderLibs.find("args5").orElseThrow();
169         args5_v = abi.downcallHandle(
170                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG));
171         args5 = insertArguments(args5_v, 0, args5_addr);
172 
173         args10_addr = loaderLibs.find("args10").orElseThrow();
174         args10_v = abi.downcallHandle(
175                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG,
176                                           C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE));
177         args10 = insertArguments(args10_v, 0, args10_addr);
178     }
179 
180     static native void blank();
181     static native int identity(int x);
182 }