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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package org.openjdk.bench.jdk.incubator.foreign;
 27 
 28 import jdk.incubator.foreign.CLinker;
 29 import jdk.incubator.foreign.FunctionDescriptor;
 30 import jdk.incubator.foreign.MemoryAccess;
 31 import jdk.incubator.foreign.MemoryAddress;
 32 import jdk.incubator.foreign.MemorySegment;
 33 import jdk.incubator.foreign.ResourceScope;
 34 import jdk.incubator.foreign.SegmentAllocator;
 35 import org.openjdk.jmh.annotations.Benchmark;
 36 import org.openjdk.jmh.annotations.BenchmarkMode;
 37 import org.openjdk.jmh.annotations.Fork;
 38 import org.openjdk.jmh.annotations.Setup;
 39 import org.openjdk.jmh.annotations.Param;
 40 import org.openjdk.jmh.annotations.TearDown;
 41 import org.openjdk.jmh.annotations.Measurement;
 42 import org.openjdk.jmh.annotations.Mode;
 43 import org.openjdk.jmh.annotations.OutputTimeUnit;
 44 import org.openjdk.jmh.annotations.State;
 45 import org.openjdk.jmh.annotations.Warmup;
 46 
 47 import java.lang.invoke.MethodHandle;
 48 import java.lang.invoke.MethodType;
 49 import java.util.concurrent.TimeUnit;
 50 
 51 import static jdk.incubator.foreign.CLinker.*;
 52 
 53 @BenchmarkMode(Mode.AverageTime)
 54 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 55 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 56 @State(org.openjdk.jmh.annotations.Scope.Thread)
 57 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 58 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 59 public class StrLenTest {
 60 
 61     ResourceScope scope = ResourceScope.newConfinedScope();
 62 
 63     SegmentAllocator segmentAllocator;
 64     SegmentAllocator arenaAllocator = SegmentAllocator.arenaAllocator(scope);
 65 
 66     @Param({"5", "20", "100"})
 67     public int size;
 68     public String str;
 69 
 70     static {
 71         System.loadLibrary("StrLen");
 72     }
 73 
 74     static final MethodHandle STRLEN;
 75     static final MethodHandle STRLEN_TRIVIAL;
 76     static final MethodHandle MALLOC_TRIVIAL;
 77     static final MethodHandle FREE_TRIVIAL;
 78 
 79     static {
 80         CLinker abi = CLinker.getInstance();
 81         STRLEN = abi.downcallHandle(CLinker.systemLookup().lookup("strlen").get(),
 82                 MethodType.methodType(int.class, MemoryAddress.class),
 83                 FunctionDescriptor.of(C_INT, C_POINTER));
 84         STRLEN_TRIVIAL = abi.downcallHandle(CLinker.systemLookup().lookup("strlen").get(),
 85                 MethodType.methodType(int.class, MemoryAddress.class),
 86                 FunctionDescriptor.of(C_INT, C_POINTER).withAttribute(FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME, true));
 87         MALLOC_TRIVIAL = abi.downcallHandle(CLinker.systemLookup().lookup("malloc").get(),
 88                 MethodType.methodType(MemoryAddress.class, long.class),
 89                 FunctionDescriptor.of(C_POINTER, C_LONG_LONG).withAttribute(FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME, true));
 90 
 91         FREE_TRIVIAL = abi.downcallHandle(CLinker.systemLookup().lookup("free").get(),
 92                 MethodType.methodType(void.class, MemoryAddress.class),
 93                 FunctionDescriptor.ofVoid(C_POINTER).withAttribute(FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME, true));
 94     }
 95 
 96     @Setup
 97     public void setup() {
 98         str = makeString(size);
 99         segmentAllocator = SegmentAllocator.ofSegment(MemorySegment.allocateNative(size + 1, ResourceScope.newImplicitScope()));
100     }
101 
102     @TearDown
103     public void tearDown() {
104         scope.close();
105     }
106 
107     @Benchmark
108     public int jni_strlen() throws Throwable {
109         return strlen(str);
110     }
111 
112     @Benchmark
113     public int panama_strlen() throws Throwable {
114         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
115             MemorySegment segment = CLinker.toCString(str, scope);
116             return (int)STRLEN.invokeExact(segment.address());
117         }
118     }
119 
120     @Benchmark
121     public int panama_strlen_arena() throws Throwable {
122         return (int)STRLEN.invokeExact(CLinker.toCString(str, arenaAllocator).address());
123     }
124 
125     @Benchmark
126     public int panama_strlen_prefix() throws Throwable {
127         return (int)STRLEN.invokeExact(CLinker.toCString(str, segmentAllocator).address());
128     }
129 
130     @Benchmark
131     public int panama_strlen_unsafe() throws Throwable {
132         MemoryAddress address = makeStringUnsafe(str);
133         int res = (int) STRLEN.invokeExact(address);
134         CLinker.freeMemory(address);
135         return res;
136     }
137 
138     @Benchmark
139     public int panama_strlen_unsafe_trivial() throws Throwable {
140         MemoryAddress address = makeStringUnsafeTrivial(str);
141         int res = (int) STRLEN_TRIVIAL.invokeExact(address);
142         FREE_TRIVIAL.invokeExact(address);
143         return res;
144     }
145 
146     static MemoryAddress makeStringUnsafe(String s) {
147         byte[] bytes = s.getBytes();
148         int len = bytes.length;
149         MemoryAddress address = CLinker.allocateMemory(len + 1);
150         MemorySegment str = address.asSegment(len + 1, ResourceScope.globalScope());
151         str.copyFrom(MemorySegment.ofArray(bytes));
152         MemoryAccess.setByteAtOffset(str, len, (byte)0);
153         return address;
154     }
155 
156     static MemoryAddress makeStringUnsafeTrivial(String s) throws Throwable {
157         byte[] bytes = s.getBytes();
158         int len = bytes.length;
159         MemoryAddress address = (MemoryAddress)MALLOC_TRIVIAL.invokeExact((long)len + 1);
160         MemorySegment str = address.asSegment(len + 1, ResourceScope.globalScope());
161         str.copyFrom(MemorySegment.ofArray(bytes));
162         MemoryAccess.setByteAtOffset(str, len, (byte)0);
163         return address;
164     }
165 
166     static native int strlen(String str);
167 
168     static String makeString(int size) {
169         String lorem = """
170                 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
171                  dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
172                  ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
173                  fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
174                  mollit anim id est laborum.
175                 """;
176         return lorem.substring(0, size);
177     }
178 }