1 /*
2 * Copyright (c) 2020, 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.CSupport;
26 import jdk.incubator.foreign.ForeignLinker;
27 import jdk.incubator.foreign.FunctionDescriptor;
28 import jdk.incubator.foreign.LibraryLookup;
29 import org.openjdk.jmh.annotations.Benchmark;
30 import org.openjdk.jmh.annotations.BenchmarkMode;
31 import org.openjdk.jmh.annotations.Fork;
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.lang.invoke.MethodType;
40 import java.util.concurrent.TimeUnit;
41
42 import static jdk.incubator.foreign.CSupport.C_DOUBLE;
43 import static jdk.incubator.foreign.CSupport.C_INT;
44 import static jdk.incubator.foreign.CSupport.C_LONGLONG;
45 import static jdk.incubator.foreign.CSupport.Win64.asVarArg;
46
47 @BenchmarkMode(Mode.AverageTime)
48 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
49 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
50 @State(org.openjdk.jmh.annotations.Scope.Thread)
51 @OutputTimeUnit(TimeUnit.NANOSECONDS)
52 @Fork(3)
53 public class VaList {
54
55 static final ForeignLinker linker = CSupport.getSystemLinker();
56 static final LibraryLookup lookup = LibraryLookup.ofLibrary("VaList");
57
58 static final MethodHandle MH_ellipsis;
59 static final MethodHandle MH_vaList;
60
61 static {
62 try {
63 MH_ellipsis = linker.downcallHandle(lookup.lookup("ellipsis"),
64 MethodType.methodType(void.class, int.class, int.class, double.class, long.class),
65 FunctionDescriptor.ofVoid(C_INT, asVarArg(C_INT), asVarArg(C_DOUBLE), asVarArg(C_LONGLONG)));
66 MH_vaList = linker.downcallHandle(lookup.lookup("vaList"),
67 MethodType.methodType(void.class, int.class, CSupport.VaList.class),
68 FunctionDescriptor.ofVoid(C_INT, CSupport.C_VA_LIST));
69 } catch (NoSuchMethodException e) {
70 throw new InternalError(e);
71 }
72 }
73
74 @Benchmark
75 public void ellipsis() throws Throwable {
76 MH_ellipsis.invokeExact(3,
77 1, 2D, 3L);
78 }
79
80 @Benchmark
81 public void vaList() throws Throwable {
82 try (CSupport.VaList vaList = CSupport.VaList.make(b ->
83 b.vargFromInt(C_INT, 1)
84 .vargFromDouble(C_DOUBLE, 2D)
85 .vargFromLong(C_LONGLONG, 3L)
86 )) {
87 MH_vaList.invokeExact(3,
88 vaList);
89 }
90 }
91 }