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.
  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.jdk.incubator.foreign;
 24 
 25 import jdk.incubator.foreign.Addressable;
 26 import jdk.incubator.foreign.CLinker;
 27 import jdk.incubator.foreign.FunctionDescriptor;
 28 import jdk.incubator.foreign.MemoryAddress;
 29 import jdk.incubator.foreign.MemoryLayout;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.NativeSymbol;
 32 import jdk.incubator.foreign.ResourceScope;
 33 import jdk.incubator.foreign.SymbolLookup;
 34 import org.openjdk.jmh.annotations.Benchmark;
 35 import org.openjdk.jmh.annotations.BenchmarkMode;
 36 import org.openjdk.jmh.annotations.Fork;
 37 import org.openjdk.jmh.annotations.Measurement;
 38 import org.openjdk.jmh.annotations.Mode;
 39 import org.openjdk.jmh.annotations.OutputTimeUnit;
 40 import org.openjdk.jmh.annotations.State;
 41 import org.openjdk.jmh.annotations.Warmup;
 42 
 43 import java.lang.invoke.MethodHandle;
 44 import java.lang.invoke.MethodType;
 45 import java.util.concurrent.TimeUnit;
 46 
 47 import static java.lang.invoke.MethodHandles.lookup;
 48 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 49 
 50 @BenchmarkMode(Mode.AverageTime)
 51 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 53 @State(org.openjdk.jmh.annotations.Scope.Thread)
 54 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 55 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 56 public class QSort extends CLayouts {
 57 
 58     static final CLinker abi = CLinker.systemCLinker();
 59     static final MethodHandle clib_qsort;
 60     static final NativeSymbol native_compar;
 61     static final NativeSymbol panama_upcall_compar;
 62     static final long jni_upcall_compar;
 63 
 64     static final int[] INPUT = { 5, 3, 2, 7, 8, 12, 1, 7 };
 65     static final MemorySegment INPUT_SEGMENT;
 66 
 67     static NativeSymbol qsort_addr = abi.lookup("qsort").get();
 68 
 69     static {
 70         INPUT_SEGMENT = MemorySegment.allocateNative(MemoryLayout.sequenceLayout(INPUT.length, JAVA_INT), ResourceScope.globalScope());
 71         INPUT_SEGMENT.copyFrom(MemorySegment.ofArray(INPUT));
 72 
 73         System.loadLibrary("QSortJNI");
 74         jni_upcall_compar = JNICB.makeCB("org/openjdk/bench/jdk/incubator/foreign/QSort", "jni_upcall_compar", "(II)I");
 75 
 76         try {
 77             clib_qsort = abi.downcallHandle(
 78                     qsort_addr,
 79                     FunctionDescriptor.ofVoid(C_POINTER, C_LONG_LONG, C_LONG_LONG, C_POINTER)
 80             );
 81             System.loadLibrary("QSort");
 82             native_compar = SymbolLookup.loaderLookup().lookup("compar").orElseThrow();
 83             panama_upcall_compar = abi.upcallStub(
 84                     lookup().findStatic(QSort.class,
 85                             "panama_upcall_compar",
 86                             MethodType.methodType(int.class, MemoryAddress.class, MemoryAddress.class)),
 87                     FunctionDescriptor.of(C_INT, C_POINTER, C_POINTER),
 88                     ResourceScope.globalScope()
 89             );
 90         } catch (ReflectiveOperationException e) {
 91             throw new BootstrapMethodError(e);
 92         }
 93     }
 94 
 95     static native void jni_qsort_optimized(int[] array, long cb);
 96     static native void jni_qsort_naive(int[] array);
 97 
 98     @FunctionalInterface
 99     interface JNIComparator {
100         int cmp(int e0, int e1);
101     }
102 
103     static final JNIComparator COMP = QSort::jni_upcall_compar;
104 
105     @Benchmark
106     public void native_qsort() throws Throwable {
107          clib_qsort.invokeExact((Addressable)INPUT_SEGMENT, (long) INPUT.length, JAVA_INT.byteSize(), (Addressable)native_compar);
108     }
109 
110     @Benchmark
111     public void jni_upcall_qsort_optimized() {
112         jni_qsort_optimized(INPUT, jni_upcall_compar);
113     }
114 
115     @Benchmark
116     public void jni_upcall_qsort_naive() {
117         jni_qsort_naive(INPUT);
118     }
119 
120     @Benchmark
121     public void panama_upcall_qsort() throws Throwable {
122         clib_qsort.invokeExact((Addressable)INPUT_SEGMENT, (long) INPUT.length, JAVA_INT.byteSize(), (Addressable)panama_upcall_compar);
123     }
124 
125     private static int getIntAbsolute(MemoryAddress addr) {
126         return addr.get(JAVA_INT, 0);
127     }
128 
129     static int panama_upcall_compar(MemoryAddress e0, MemoryAddress e1) {
130         return Integer.compare(getIntAbsolute(e0), getIntAbsolute(e1));
131     }
132 
133     static int jni_upcall_compar(int j0, int j1) {
134         return Integer.compare(j0, j1);
135     }
136 }