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.MemoryLayout;
26 import org.openjdk.jmh.annotations.Benchmark;
27 import org.openjdk.jmh.annotations.BenchmarkMode;
28 import org.openjdk.jmh.annotations.CompilerControl;
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.Setup;
34 import org.openjdk.jmh.annotations.State;
35 import org.openjdk.jmh.annotations.TearDown;
36 import org.openjdk.jmh.annotations.Warmup;
37 import sun.misc.Unsafe;
38 
39 import jdk.incubator.foreign.MemoryAddress;
40 import jdk.incubator.foreign.MemorySegment;
41 import java.lang.invoke.VarHandle;
42 import java.nio.ByteBuffer;
43 import java.nio.ByteOrder;
44 import java.util.concurrent.TimeUnit;
45 
46 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
47 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
48 
49 @BenchmarkMode(Mode.AverageTime)
50 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
51 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
52 @State(org.openjdk.jmh.annotations.Scope.Thread)
53 @OutputTimeUnit(TimeUnit.MILLISECONDS)
54 @Fork(3)
55 public class LoopOverConstant {
56 
57     static final Unsafe unsafe = Utils.unsafe;
58 
59     static final int ELEM_SIZE = 1_000_000;
60     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
61     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
62 
63     static final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
64 
65     //setup unsafe address
66 
67     static {
68         for (int i = 0; i < ELEM_SIZE; i++) {
69             unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i);
70         }
71     }
72 
73     //setup native memory segment
74 
75     static final MemoryAddress segment_addr = MemorySegment.allocateNative(ALLOC_SIZE).baseAddress();
76     static final VarHandle VH_int = MemoryLayout.ofSequence(JAVA_INT).varHandle(int.class, sequenceElement());
77 
78     static {
79         for (int i = 0; i < ELEM_SIZE; i++) {
VH_int.set(segment_addr, (long) i, i)80             VH_int.set(segment_addr, (long) i, i);
81         }
82     }
83 
84     //setup direct buffer
85 
86     static final ByteBuffer bb = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
87 
88     static {
89         for (int i = 0; i < ELEM_SIZE; i++) {
bb.putInt(i * CARRIER_SIZE , i)90             bb.putInt(i * CARRIER_SIZE , i);
91         }
92     }
93 
94     @Benchmark
95     @OutputTimeUnit(TimeUnit.NANOSECONDS)
unsafe_get()96     public int unsafe_get() {
97         return unsafe.getInt(unsafe_addr);
98     }
99 
100     @Benchmark
101     @OutputTimeUnit(TimeUnit.NANOSECONDS)
segment_get()102     public int segment_get() {
103         return (int)VH_int.get(segment_addr, 0L);
104     }
105 
106     @Benchmark
107     @OutputTimeUnit(TimeUnit.NANOSECONDS)
BB_get()108     public int BB_get() {
109         return bb.getInt(0);
110     }
111 
112     @Benchmark
unsafe_loop()113     public int unsafe_loop() {
114         int res = 0;
115         for (int i = 0; i < ELEM_SIZE; i ++) {
116             res += unsafe.getInt(unsafe_addr + (i * CARRIER_SIZE));
117         }
118         return res;
119     }
120 
121     @Benchmark
segment_loop()122     public int segment_loop() {
123         int res = 0;
124         for (int i = 0; i < ELEM_SIZE; i++) {
125             res += (int) VH_int.get(segment_addr, (long)i);
126         }
127         return res;
128     }
129 
130     @Benchmark
BB_loop()131     public int BB_loop() {
132         int res = 0;
133         for (int i = 0; i < ELEM_SIZE; i++) {
134             res += bb.getInt(i * CARRIER_SIZE);
135         }
136         return res;
137     }
138 }
139