1 /* 2 * Copyright (c) 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 24 package org.openjdk.bench.java.lang.foreign; 25 26 import java.lang.foreign.*; 27 28 import org.openjdk.jmh.annotations.Benchmark; 29 import org.openjdk.jmh.annotations.BenchmarkMode; 30 import org.openjdk.jmh.annotations.Fork; 31 import org.openjdk.jmh.annotations.TearDown; 32 import org.openjdk.jmh.annotations.Measurement; 33 import org.openjdk.jmh.annotations.Mode; 34 import org.openjdk.jmh.annotations.OutputTimeUnit; 35 import org.openjdk.jmh.annotations.State; 36 import org.openjdk.jmh.annotations.Warmup; 37 38 import java.lang.invoke.MethodHandle; 39 import java.util.concurrent.TimeUnit; 40 41 @BenchmarkMode(Mode.AverageTime) 42 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 43 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 44 @State(org.openjdk.jmh.annotations.Scope.Thread) 45 @OutputTimeUnit(TimeUnit.NANOSECONDS) 46 @Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" }) 47 public class PointerInvoke extends CLayouts { 48 49 Arena arena = Arena.ofConfined(); 50 MemorySegment segment = arena.allocate(100, 1); 51 52 static { 53 System.loadLibrary("Ptr"); 54 } 55 56 static final MethodHandle F_LONG_LONG, F_PTR_LONG, F_LONG_PTR, F_PTR_PTR; 57 58 static { 59 Linker abi = Linker.nativeLinker(); 60 SymbolLookup loaderLibs = SymbolLookup.loaderLookup(); 61 F_LONG_LONG = abi.downcallHandle(loaderLibs.find("id_long_long").get(), 62 FunctionDescriptor.of(C_LONG_LONG, C_LONG_LONG)); 63 F_PTR_LONG = abi.downcallHandle(loaderLibs.find("id_ptr_long").get(), 64 FunctionDescriptor.of(C_LONG_LONG, C_POINTER)); 65 F_LONG_PTR = abi.downcallHandle(loaderLibs.find("id_long_ptr").get(), 66 FunctionDescriptor.of(C_POINTER, C_LONG_LONG)); 67 F_PTR_PTR = abi.downcallHandle(loaderLibs.find("id_ptr_ptr").get(), 68 FunctionDescriptor.of(C_POINTER, C_POINTER)); 69 } 70 71 @TearDown 72 public void tearDown() { 73 arena.close(); 74 } 75 76 @Benchmark 77 public long long_to_long() throws Throwable { 78 return (long)F_LONG_LONG.invokeExact(segment.address()); 79 } 80 81 @Benchmark 82 public long ptr_to_long() throws Throwable { 83 return (long)F_PTR_LONG.invokeExact(segment); 84 } 85 86 @Benchmark 87 public long ptr_to_long_new_segment() throws Throwable { 88 MemorySegment newSegment = segment.reinterpret(100, arena, null); 89 return (long)F_PTR_LONG.invokeExact(newSegment); 90 } 91 92 @Benchmark 93 public long long_to_ptr() throws Throwable { 94 return ((MemorySegment)F_LONG_PTR.invokeExact(segment.address())).address(); 95 } 96 97 @Benchmark 98 public long ptr_to_ptr() throws Throwable { 99 return ((MemorySegment)F_PTR_PTR.invokeExact(segment)).address(); 100 } 101 102 @Benchmark 103 public long ptr_to_ptr_new_segment() throws Throwable { 104 MemorySegment newSegment = segment.reinterpret(100, arena, null); 105 return ((MemorySegment)F_PTR_PTR.invokeExact(newSegment)).address(); 106 } 107 }