1 /*
  2  * Copyright (c) 2020, 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.Arena;
 26 import java.lang.foreign.MemorySegment;
 27 
 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.io.File;
 41 import java.io.IOException;
 42 import java.nio.ByteBuffer;
 43 import java.nio.ByteOrder;
 44 import java.nio.MappedByteBuffer;
 45 import java.nio.channels.FileChannel;
 46 import java.nio.file.Files;
 47 import java.nio.file.Path;
 48 import java.nio.file.StandardOpenOption;
 49 import java.util.concurrent.TimeUnit;
 50 
 51 import static java.lang.foreign.ValueLayout.*;
 52 
 53 @BenchmarkMode(Mode.AverageTime)
 54 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 55 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 56 @State(org.openjdk.jmh.annotations.Scope.Thread)
 57 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 58 @Fork(3)
 59 public class LoopOverNonConstantMapped extends JavaLayouts {
 60 
 61     static final Unsafe unsafe = Utils.unsafe;
 62 
 63     static final int ELEM_SIZE = 1_000_000;
 64     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 65     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 66 
 67     static final Path tempPath;
 68 
 69     static {
 70         try {
 71             File file = File.createTempFile("buffer", "txt");
 72             file.deleteOnExit();
 73             tempPath = file.toPath();
 74             Files.write(file.toPath(), new byte[ALLOC_SIZE], StandardOpenOption.WRITE);
 75 
 76         } catch (IOException ex) {
 77             throw new ExceptionInInitializerError(ex);
 78         }
 79     }
 80 
 81     FileChannel fileChannel;
 82     Arena arena;
 83     MemorySegment segment;
 84     long unsafe_addr;
 85 
 86     ByteBuffer byteBuffer;
 87 
 88     @Setup
 89     public void setup() throws IOException {
 90         try (FileChannel channel = FileChannel.open(tempPath, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
 91             byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, ALLOC_SIZE).order(ByteOrder.nativeOrder());
 92             for (int i = 0; i < ELEM_SIZE; i++) {
 93                 byteBuffer.putInt(i * CARRIER_SIZE, i);
 94             }
 95             ((MappedByteBuffer)byteBuffer).force();
 96         }
 97         fileChannel = FileChannel.open(tempPath, StandardOpenOption.READ, StandardOpenOption.WRITE);
 98         arena = Arena.ofConfined();
 99         segment = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, ALLOC_SIZE, arena);
100         unsafe_addr = segment.address();
101     }
102 
103     @TearDown
104     public void tearDown() throws IOException {
105         fileChannel.close();
106         arena.close();
107         unsafe.invokeCleaner(byteBuffer);
108     }
109 
110     @Benchmark
111     @OutputTimeUnit(TimeUnit.NANOSECONDS)
112     public int unsafe_get() {
113         return unsafe.getInt(unsafe_addr);
114     }
115 
116     @Benchmark
117     @OutputTimeUnit(TimeUnit.NANOSECONDS)
118     public int segment_get() {
119         return (int) VH_INT.get(segment, 0L);
120     }
121 
122     @Benchmark
123     @OutputTimeUnit(TimeUnit.NANOSECONDS)
124     public int BB_get() {
125         return byteBuffer.getInt(0);
126     }
127 
128     @Benchmark
129     public int unsafe_loop() {
130         int res = 0;
131         for (int i = 0; i < ELEM_SIZE; i ++) {
132             res += unsafe.getInt(unsafe_addr + (i * CARRIER_SIZE));
133         }
134         return res;
135     }
136 
137     @Benchmark
138     public int segment_loop() {
139         int sum = 0;
140         for (int i = 0; i < ELEM_SIZE; i++) {
141             sum += (int) VH_INT.get(segment, (long) i);
142         }
143         return sum;
144     }
145 
146     @Benchmark
147     public int segment_loop_instance() {
148         int res = 0;
149         for (int i = 0; i < ELEM_SIZE; i ++) {
150             res += segment.get(JAVA_INT, i * CARRIER_SIZE);
151         }
152         return res;
153     }
154 
155     @Benchmark
156     public int segment_loop_slice() {
157         int sum = 0;
158         MemorySegment base = segment.asSlice(0, segment.byteSize());
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 segment_loop_readonly() {
167         int sum = 0;
168         MemorySegment base = segment.asReadOnly();
169         for (int i = 0; i < ELEM_SIZE; i++) {
170             sum += (int) VH_INT.get(base, (long) i);
171         }
172         return sum;
173     }
174 
175     @Benchmark
176     public int BB_loop() {
177         int sum = 0;
178         ByteBuffer bb = byteBuffer;
179         for (int i = 0; i < ELEM_SIZE; i++) {
180             sum += bb.getInt(i * CARRIER_SIZE);
181         }
182         return sum;
183     }
184 }