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