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.Addressable;
 29 import jdk.incubator.foreign.CLinker;
 30 import jdk.incubator.foreign.FunctionDescriptor;
 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.util.concurrent.TimeUnit;
 49 
 50 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 51 
 52 @BenchmarkMode(Mode.AverageTime)
 53 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 54 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 55 @State(org.openjdk.jmh.annotations.Scope.Thread)
 56 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 57 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 58 public class StrLenTest extends CLayouts {
 59 
 60     ResourceScope scope = ResourceScope.newImplicitScope();
 61 
 62     SegmentAllocator segmentAllocator;
 63     SegmentAllocator arenaAllocator = SegmentAllocator.newNativeArena(scope);
 64 
 65     @Param({"5", "20", "100"})
 66     public int size;
 67     public String str;
 68 
 69     static {
 70         System.loadLibrary("StrLen");
 71     }
 72 
 73     static final MethodHandle STRLEN;
 74 
 75     static {
 76         CLinker abi = CLinker.systemCLinker();
 77         STRLEN = abi.downcallHandle(abi.lookup("strlen").get(),
 78                 FunctionDescriptor.of(C_INT, C_POINTER));
 79     }
 80 
 81     @Setup
 82     public void setup() {
 83         str = makeString(size);
 84         segmentAllocator = SegmentAllocator.prefixAllocator(MemorySegment.allocateNative(size + 1, ResourceScope.newConfinedScope()));
 85     }
 86 
 87     @TearDown
 88     public void tearDown() {
 89         scope.close();
 90     }
 91 
 92     @Benchmark
 93     public int jni_strlen() throws Throwable {
 94         return strlen(str);
 95     }
 96 
 97     @Benchmark
 98     public int panama_strlen() throws Throwable {
 99         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
100             MemorySegment segment = MemorySegment.allocateNative(str.length() + 1, scope);
101             segment.setUtf8String(0, str);
102             return (int)STRLEN.invokeExact((Addressable)segment);
103         }
104     }
105 
106     @Benchmark
107     public int panama_strlen_arena() throws Throwable {
108         return (int)STRLEN.invokeExact((Addressable)arenaAllocator.allocateUtf8String(str));
109     }
110 
111     @Benchmark
112     public int panama_strlen_prefix() throws Throwable {
113         return (int)STRLEN.invokeExact((Addressable)segmentAllocator.allocateUtf8String(str));
114     }
115 
116     @Benchmark
117     public int panama_strlen_unsafe() throws Throwable {
118         MemoryAddress address = makeStringUnsafe(str);
119         int res = (int) STRLEN.invokeExact((Addressable)address);
120         freeMemory(address);
121         return res;
122     }
123 
124     static MemoryAddress makeStringUnsafe(String s) {
125         byte[] bytes = s.getBytes();
126         int len = bytes.length;
127         MemoryAddress address = allocateMemory(len + 1);
128         MemorySegment str = MemorySegment.ofAddressNative(address, len + 1, ResourceScope.globalScope());
129         str.copyFrom(MemorySegment.ofArray(bytes));
130         str.set(JAVA_BYTE, len, (byte)0);
131         return address;
132     }
133 
134     static native int strlen(String str);
135 
136     static String makeString(int size) {
137         String lorem = """
138                 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
139                  dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
140                  ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
141                  fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
142                  mollit anim id est laborum.
143                 """;
144         return lorem.substring(0, size);
145     }
146 }