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.MemoryAccess;
 26 import jdk.incubator.foreign.MemoryLayout;
 27 import jdk.incubator.foreign.MemorySegment;
 28 import jdk.incubator.foreign.ResourceScope;
 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.Setup;
 36 import org.openjdk.jmh.annotations.State;
 37 import org.openjdk.jmh.annotations.TearDown;
 38 import org.openjdk.jmh.annotations.Warmup;
 39 import sun.misc.Unsafe;
 40 
 41 import java.io.File;
 42 import java.io.IOException;
 43 import java.lang.invoke.VarHandle;
 44 import java.nio.ByteBuffer;
 45 import java.nio.ByteOrder;
 46 import java.nio.MappedByteBuffer;
 47 import java.nio.channels.FileChannel;
 48 import java.nio.file.Files;
 49 import java.nio.file.Path;
 50 import java.nio.file.StandardOpenOption;
 51 import java.util.concurrent.TimeUnit;
 52 
 53 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 54 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
 55 
 56 @BenchmarkMode(Mode.AverageTime)
 57 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 58 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 59 @State(org.openjdk.jmh.annotations.Scope.Thread)
 60 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 61 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 62 public class LoopOverNonConstantMapped {
 63 
 64     static final Unsafe unsafe = Utils.unsafe;
 65 
 66     static final int ELEM_SIZE = 1_000_000;
 67     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 68     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 69 
 70     static final Path tempPath;
 71 
 72     static {
 73         try {
 74             File file = File.createTempFile("buffer", "txt");
 75             file.deleteOnExit();
 76             tempPath = file.toPath();
 77             Files.write(file.toPath(), new byte[ALLOC_SIZE], StandardOpenOption.WRITE);
 78 
 79         } catch (IOException ex) {
 80             throw new ExceptionInInitializerError(ex);
 81         }
 82     }
 83 
 84     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(int.class, sequenceElement());
 85     MemorySegment segment;
 86     long unsafe_addr;
 87 
 88     ByteBuffer byteBuffer;
 89 
 90     @Setup
 91     public void setup() throws IOException {
 92         try (FileChannel channel = FileChannel.open(tempPath, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
 93             byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, ALLOC_SIZE).order(ByteOrder.nativeOrder());
 94             for (int i = 0; i < ELEM_SIZE; i++) {
 95                 byteBuffer.putInt(i * CARRIER_SIZE, i);
 96             }
 97             ((MappedByteBuffer)byteBuffer).force();
 98         }
 99         segment = MemorySegment.mapFile(tempPath, 0L, ALLOC_SIZE, FileChannel.MapMode.READ_WRITE, ResourceScope.newConfinedScope());
100         unsafe_addr = segment.address().toRawLongValue();
101     }
102 
103     @TearDown
104     public void tearDown() {
105         segment.scope().close();
106         unsafe.invokeCleaner(byteBuffer);
107     }
108 
109     @Benchmark
110     @OutputTimeUnit(TimeUnit.NANOSECONDS)
111     public int unsafe_get() {
112         return unsafe.getInt(unsafe_addr);
113     }
114 
115     @Benchmark
116     @OutputTimeUnit(TimeUnit.NANOSECONDS)
117     public int segment_get() {
118         return (int) VH_int.get(segment, 0L);
119     }
120 
121     @Benchmark
122     @OutputTimeUnit(TimeUnit.NANOSECONDS)
123     public int BB_get() {
124         return byteBuffer.getInt(0);
125     }
126 
127     @Benchmark
128     public int unsafe_loop() {
129         int res = 0;
130         for (int i = 0; i < ELEM_SIZE; i ++) {
131             res += unsafe.getInt(unsafe_addr + (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_static() {
147         int res = 0;
148         for (int i = 0; i < ELEM_SIZE; i ++) {
149             res += MemoryAccess.getIntAtIndex(segment, i);
150         }
151         return res;
152     }
153 
154     @Benchmark
155     public int segment_loop_slice() {
156         int sum = 0;
157         MemorySegment base = segment.asSlice(0, segment.byteSize());
158         for (int i = 0; i < ELEM_SIZE; i++) {
159             sum += (int) VH_int.get(base, (long) i);
160         }
161         return sum;
162     }
163 
164     @Benchmark
165     public int segment_loop_readonly() {
166         int sum = 0;
167         MemorySegment base = segment.asReadOnly();
168         for (int i = 0; i < ELEM_SIZE; i++) {
169             sum += (int) VH_int.get(base, (long) i);
170         }
171         return sum;
172     }
173 
174     @Benchmark
175     public int BB_loop() {
176         int sum = 0;
177         ByteBuffer bb = byteBuffer;
178         for (int i = 0; i < ELEM_SIZE; i++) {
179             sum += bb.getInt(i * CARRIER_SIZE);
180         }
181         return sum;
182     }
183 
184 }