|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | + |
| 24 | +package org.openjdk.bench.java.lang.foreign; |
| 25 | + |
| 26 | +import org.openjdk.jmh.annotations.*; |
| 27 | + |
| 28 | +import java.lang.foreign.*; |
| 29 | +import java.lang.invoke.*; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.function.Supplier; |
| 32 | + |
| 33 | +@BenchmarkMode(Mode.AverageTime) |
| 34 | +@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) |
| 35 | +@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) |
| 36 | +@State(org.openjdk.jmh.annotations.Scope.Thread) |
| 37 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 38 | +@Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED", "-Djava.library.path=micro/native" }) |
| 39 | +public class CallByRefHighArity { |
| 40 | + |
| 41 | + static { |
| 42 | + System.loadLibrary("CallByRefHighArity"); |
| 43 | + } |
| 44 | + |
| 45 | + @Param |
| 46 | + SegmentKind kind; |
| 47 | + |
| 48 | + public enum SegmentKind { |
| 49 | + CONFINED, |
| 50 | + SHARED, |
| 51 | + GLOBAL, |
| 52 | + HEAP |
| 53 | + } |
| 54 | + |
| 55 | + Supplier<MemorySegment> segmentSupplier; |
| 56 | + Arena arena; |
| 57 | + |
| 58 | + @Setup |
| 59 | + public void setup() { |
| 60 | + if (kind == SegmentKind.CONFINED) { |
| 61 | + arena = Arena.ofConfined(); |
| 62 | + MemorySegment segment = arena.allocateFrom(ValueLayout.JAVA_INT, 0); |
| 63 | + segmentSupplier = () -> segment; |
| 64 | + } else if (kind == SegmentKind.SHARED) { |
| 65 | + arena = Arena.ofShared(); |
| 66 | + MemorySegment segment = arena.allocateFrom(ValueLayout.JAVA_INT, 0); |
| 67 | + segmentSupplier = () -> segment; |
| 68 | + } else if (kind == SegmentKind.HEAP) { |
| 69 | + byte[] array = new byte[8]; |
| 70 | + MemorySegment segment = MemorySegment.ofArray(array); |
| 71 | + segmentSupplier = () -> segment; |
| 72 | + } else { // global |
| 73 | + segmentSupplier = () -> MemorySegment.ofAddress(0); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @TearDown |
| 78 | + public void tearDown() { |
| 79 | + if (arena != null) { |
| 80 | + arena.close(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // A shared library that exports the functions below |
| 85 | + private static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup(); |
| 86 | + |
| 87 | + // void noop_params0() {} |
| 88 | + private static final MethodHandle MH_NOOP_PARAMS0 = Linker.nativeLinker() |
| 89 | + .downcallHandle(FunctionDescriptor.ofVoid(), Linker.Option.critical(true)) |
| 90 | + .bindTo(LOOKUP.find("noop_params0").orElseThrow()); |
| 91 | + |
| 92 | + // void noop_params1(void *param0) {} |
| 93 | + private static final MethodHandle MH_NOOP_PARAMS1 = Linker.nativeLinker() |
| 94 | + .downcallHandle(FunctionDescriptor.ofVoid( |
| 95 | + ValueLayout.ADDRESS |
| 96 | + ), Linker.Option.critical(true)) |
| 97 | + .bindTo(LOOKUP.find("noop_params1").orElseThrow()); |
| 98 | + |
| 99 | + // void noop_params2(void *param0, void *param1) {} |
| 100 | + private static final MethodHandle MH_NOOP_PARAMS2 = Linker.nativeLinker() |
| 101 | + .downcallHandle(FunctionDescriptor.ofVoid( |
| 102 | + ValueLayout.ADDRESS, |
| 103 | + ValueLayout.ADDRESS |
| 104 | + ), Linker.Option.critical(true)) |
| 105 | + .bindTo(LOOKUP.find("noop_params2").orElseThrow()); |
| 106 | + |
| 107 | + // void noop_params3(void *param0, void *param1, void *param2) {} |
| 108 | + private static final MethodHandle MH_NOOP_PARAMS3 = Linker.nativeLinker() |
| 109 | + .downcallHandle(FunctionDescriptor.ofVoid( |
| 110 | + ValueLayout.ADDRESS, |
| 111 | + ValueLayout.ADDRESS, |
| 112 | + ValueLayout.ADDRESS |
| 113 | + ), Linker.Option.critical(true)) |
| 114 | + .bindTo(LOOKUP.find("noop_params3").orElseThrow()); |
| 115 | + |
| 116 | + // void noop_params4(void *param0, void *param1, void *param2, void *param3) {} |
| 117 | + private static final MethodHandle MH_NOOP_PARAMS4 = Linker.nativeLinker() |
| 118 | + .downcallHandle(FunctionDescriptor.ofVoid( |
| 119 | + ValueLayout.ADDRESS, |
| 120 | + ValueLayout.ADDRESS, |
| 121 | + ValueLayout.ADDRESS, |
| 122 | + ValueLayout.ADDRESS |
| 123 | + ), Linker.Option.critical(true)) |
| 124 | + .bindTo(LOOKUP.find("noop_params4").orElseThrow()); |
| 125 | + |
| 126 | + // void noop_params5(int param0, int param1, void *param2, void *param3, void *param4) {} |
| 127 | + private static final MethodHandle MH_NOOP_PARAMS5 = Linker.nativeLinker() |
| 128 | + .downcallHandle(FunctionDescriptor.ofVoid( |
| 129 | + ValueLayout.ADDRESS, |
| 130 | + ValueLayout.ADDRESS, |
| 131 | + ValueLayout.ADDRESS, |
| 132 | + ValueLayout.ADDRESS, |
| 133 | + ValueLayout.ADDRESS |
| 134 | + ), Linker.Option.critical(true)) |
| 135 | + .bindTo(LOOKUP.find("noop_params5").orElseThrow()); |
| 136 | + |
| 137 | + // void noop_params10(void *param0, void *param1, void *param2, void *param3, void *param4, |
| 138 | + // void *param5, void *param6, void *param7, void *param8, void *param9) {} |
| 139 | + private static final MethodHandle MH_NOOP_PARAMS10 = Linker.nativeLinker() |
| 140 | + .downcallHandle(FunctionDescriptor.ofVoid( |
| 141 | + ValueLayout.ADDRESS, |
| 142 | + ValueLayout.ADDRESS, |
| 143 | + ValueLayout.ADDRESS, |
| 144 | + ValueLayout.ADDRESS, |
| 145 | + ValueLayout.ADDRESS, |
| 146 | + ValueLayout.ADDRESS, |
| 147 | + ValueLayout.ADDRESS, |
| 148 | + ValueLayout.ADDRESS, |
| 149 | + ValueLayout.ADDRESS, |
| 150 | + ValueLayout.ADDRESS |
| 151 | + ), Linker.Option.critical(true)) |
| 152 | + .bindTo(LOOKUP.find("noop_params10").orElseThrow()); |
| 153 | + |
| 154 | + @Benchmark |
| 155 | + public void noop_params0() { |
| 156 | + try { |
| 157 | + MH_NOOP_PARAMS0.invokeExact(); |
| 158 | + } catch (Throwable t) { |
| 159 | + throw new AssertionError(t); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Benchmark |
| 164 | + public void noop_params1() { |
| 165 | + try { |
| 166 | + MH_NOOP_PARAMS1.invokeExact( |
| 167 | + segmentSupplier.get() |
| 168 | + ); |
| 169 | + } catch (Throwable t) { |
| 170 | + throw new AssertionError(t); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Benchmark |
| 175 | + public void noop_params2() { |
| 176 | + try { |
| 177 | + MH_NOOP_PARAMS2.invokeExact( |
| 178 | + segmentSupplier.get(), |
| 179 | + segmentSupplier.get() |
| 180 | + ); |
| 181 | + } catch (Throwable t) { |
| 182 | + throw new AssertionError(t); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Benchmark |
| 187 | + public void noop_params3() { |
| 188 | + try { |
| 189 | + MH_NOOP_PARAMS3.invokeExact( |
| 190 | + segmentSupplier.get(), |
| 191 | + segmentSupplier.get(), |
| 192 | + segmentSupplier.get() |
| 193 | + ); |
| 194 | + } catch (Throwable t) { |
| 195 | + throw new AssertionError(t); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Benchmark |
| 200 | + public void noop_params4() { |
| 201 | + try { |
| 202 | + MH_NOOP_PARAMS4.invokeExact( |
| 203 | + segmentSupplier.get(), |
| 204 | + segmentSupplier.get(), |
| 205 | + segmentSupplier.get(), |
| 206 | + segmentSupplier.get() |
| 207 | + ); |
| 208 | + } catch (Throwable t) { |
| 209 | + throw new AssertionError(t); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + @Benchmark |
| 214 | + public void noop_params5() { |
| 215 | + try { |
| 216 | + MH_NOOP_PARAMS5.invokeExact( |
| 217 | + segmentSupplier.get(), |
| 218 | + segmentSupplier.get(), |
| 219 | + segmentSupplier.get(), |
| 220 | + segmentSupplier.get(), |
| 221 | + segmentSupplier.get() |
| 222 | + ); |
| 223 | + } catch (Throwable t) { |
| 224 | + throw new AssertionError(t); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + @Benchmark |
| 229 | + public void noop_params10() { |
| 230 | + try { |
| 231 | + MH_NOOP_PARAMS10.invokeExact( |
| 232 | + segmentSupplier.get(), |
| 233 | + segmentSupplier.get(), |
| 234 | + segmentSupplier.get(), |
| 235 | + segmentSupplier.get(), |
| 236 | + segmentSupplier.get(), |
| 237 | + segmentSupplier.get(), |
| 238 | + segmentSupplier.get(), |
| 239 | + segmentSupplier.get(), |
| 240 | + segmentSupplier.get(), |
| 241 | + segmentSupplier.get() |
| 242 | + ); |
| 243 | + } catch (Throwable t) { |
| 244 | + throw new AssertionError(t); |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments