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 jdk.incubator.foreign.MemorySegment;
 27 import jdk.incubator.foreign.ResourceScope;
 28 import org.openjdk.jmh.annotations.Benchmark;
 29 import org.openjdk.jmh.annotations.BenchmarkMode;
 30 import org.openjdk.jmh.annotations.Fork;
 31 import org.openjdk.jmh.annotations.Measurement;
 32 import org.openjdk.jmh.annotations.Mode;
 33 import org.openjdk.jmh.annotations.OutputTimeUnit;
 34 import org.openjdk.jmh.annotations.Setup;
 35 import org.openjdk.jmh.annotations.State;
 36 import org.openjdk.jmh.annotations.TearDown;
 37 import org.openjdk.jmh.annotations.Warmup;
 38 import sun.misc.Unsafe;
 39 
 40 import java.lang.invoke.VarHandle;
 41 import java.nio.ByteBuffer;
 42 import java.nio.ByteOrder;
 43 import java.util.concurrent.TimeUnit;
 44 
 45 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 46 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 47 
 48 @BenchmarkMode(Mode.AverageTime)
 49 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 50 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @State(org.openjdk.jmh.annotations.Scope.Thread)
 52 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 53 @Fork(3)
 54 public class LoopOverNonConstantShared {
 55 
 56     static final Unsafe unsafe = Utils.unsafe;
 57 
 58     static final int ELEM_SIZE = 1_000_000;
 59     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 60     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 61 
 62     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(sequenceElement());
 63     MemorySegment segment;
 64     long unsafe_addr;
 65 
 66     ByteBuffer byteBuffer;
 67 
 68     @Setup
 69     public void setup() {
 70         unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 71         for (int i = 0; i < ELEM_SIZE; i++) {
 72             unsafe.putInt(unsafe_addr + (i * CARRIER_SIZE) , i);
 73         }
 74         segment = MemorySegment.allocateNative(ALLOC_SIZE, CARRIER_SIZE, ResourceScope.newConfinedScope());
 75         for (int i = 0; i < ELEM_SIZE; i++) {
 76             VH_int.set(segment, (long) i, i);
 77         }
 78         byteBuffer = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
 79         for (int i = 0; i < ELEM_SIZE; i++) {
 80             byteBuffer.putInt(i * CARRIER_SIZE , i);
 81         }
 82     }
 83 
 84     @TearDown
 85     public void tearDown() {
 86         segment.scope().close();
 87         unsafe.invokeCleaner(byteBuffer);
 88         unsafe.freeMemory(unsafe_addr);
 89     }
 90 
 91     @Benchmark
 92     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 93     public int unsafe_get() {
 94         return unsafe.getInt(unsafe_addr);
 95     }
 96 
 97     @Benchmark
 98     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 99     public int segment_get() {
100         return (int) VH_int.get(segment, 0L);
101     }
102 
103     @Benchmark
104     @OutputTimeUnit(TimeUnit.NANOSECONDS)
105     public int BB_get() {
106         return byteBuffer.getInt(0);
107     }
108 
109     @Benchmark
110     public int unsafe_loop() {
111         int res = 0;
112         for (int i = 0; i < ELEM_SIZE; i ++) {
113             res += unsafe.getInt(unsafe_addr + (i * CARRIER_SIZE));
114         }
115         return res;
116     }
117 
118     @Benchmark
119     public int segment_loop_instance() {
120         int res = 0;
121         for (int i = 0; i < ELEM_SIZE; i ++) {
122             res += segment.get(JAVA_INT, i * CARRIER_SIZE);
123         }
124         return res;
125     }
126 
127     @Benchmark
128     public int segment_loop_instance_address() {
129         int res = 0;
130         for (int i = 0; i < ELEM_SIZE; i ++) {
131             res += segment.get(JAVA_INT, i * CARRIER_SIZE);
132         }
133         return res;
134     }
135 
136     @Benchmark
137     public int segment_loop() {
138         int sum = 0;
139         for (int i = 0; i < ELEM_SIZE; i++) {
140             sum += (int) VH_int.get(segment, (long) i);
141         }
142         return sum;
143     }
144 
145     @Benchmark
146     public int segment_loop_slice() {
147         int sum = 0;
148         MemorySegment base = segment.asSlice(0, segment.byteSize());
149         for (int i = 0; i < ELEM_SIZE; i++) {
150             sum += (int) VH_int.get(base, (long) i);
151         }
152         return sum;
153     }
154 
155     @Benchmark
156     public int segment_loop_readonly() {
157         int sum = 0;
158         MemorySegment base = segment.asReadOnly();
159         for (int i = 0; i < ELEM_SIZE; i++) {
160             sum += (int) VH_int.get(base, (long) i);
161         }
162         return sum;
163     }
164 
165     @Benchmark
166     public int BB_loop() {
167         int sum = 0;
168         ByteBuffer bb = byteBuffer;
169         for (int i = 0; i < ELEM_SIZE; i++) {
170             sum += bb.getInt(i * CARRIER_SIZE);
171         }
172         return sum;
173     }
174 
175 }