< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/foreign/LoopOverNonConstantMapped.java

Print this page

  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() {

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);

  1 /*
  2  * Copyright (c) 2020, 2021, 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.io.File;
 41 import java.io.IOException;
 42 import java.lang.invoke.VarHandle;
 43 import java.nio.ByteBuffer;
 44 import java.nio.ByteOrder;
 45 import java.nio.MappedByteBuffer;
 46 import java.nio.channels.FileChannel;
 47 import java.nio.file.Files;
 48 import java.nio.file.Path;
 49 import java.nio.file.StandardOpenOption;
 50 import java.util.concurrent.TimeUnit;
 51 
 52 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 53 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 54 
 55 @BenchmarkMode(Mode.AverageTime)
 56 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 57 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 58 @State(org.openjdk.jmh.annotations.Scope.Thread)
 59 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 60 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 61 public class LoopOverNonConstantMapped {
 62 
 63     static final Unsafe unsafe = Utils.unsafe;
 64 
 65     static final int ELEM_SIZE = 1_000_000;
 66     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 67     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 68 
 69     static final Path tempPath;
 70 
 71     static {
 72         try {
 73             File file = File.createTempFile("buffer", "txt");
 74             file.deleteOnExit();
 75             tempPath = file.toPath();
 76             Files.write(file.toPath(), new byte[ALLOC_SIZE], StandardOpenOption.WRITE);
 77 
 78         } catch (IOException ex) {
 79             throw new ExceptionInInitializerError(ex);
 80         }
 81     }
 82 
 83     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(sequenceElement());
 84     MemorySegment segment;
 85     long unsafe_addr;
 86 
 87     ByteBuffer byteBuffer;
 88 
 89     @Setup
 90     public void setup() throws IOException {
 91         try (FileChannel channel = FileChannel.open(tempPath, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
 92             byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, ALLOC_SIZE).order(ByteOrder.nativeOrder());
 93             for (int i = 0; i < ELEM_SIZE; i++) {
 94                 byteBuffer.putInt(i * CARRIER_SIZE, i);
 95             }
 96             ((MappedByteBuffer)byteBuffer).force();
 97         }
 98         segment = MemorySegment.mapFile(tempPath, 0L, ALLOC_SIZE, FileChannel.MapMode.READ_WRITE, ResourceScope.newConfinedScope());
 99         unsafe_addr = segment.address().toRawLongValue();
100     }
101 
102     @TearDown
103     public void tearDown() {

125 
126     @Benchmark
127     public int unsafe_loop() {
128         int res = 0;
129         for (int i = 0; i < ELEM_SIZE; i ++) {
130             res += unsafe.getInt(unsafe_addr + (i * CARRIER_SIZE));
131         }
132         return res;
133     }
134 
135     @Benchmark
136     public int segment_loop() {
137         int sum = 0;
138         for (int i = 0; i < ELEM_SIZE; i++) {
139             sum += (int) VH_int.get(segment, (long) i);
140         }
141         return sum;
142     }
143 
144     @Benchmark
145     public int segment_loop_instance() {
146         int res = 0;
147         for (int i = 0; i < ELEM_SIZE; i ++) {
148             res += segment.get(JAVA_INT, i * CARRIER_SIZE);
149         }
150         return res;
151     }
152 
153     @Benchmark
154     public int segment_loop_instance_address() {
155         int res = 0;
156         for (int i = 0; i < ELEM_SIZE; i ++) {
157             res += segment.address().get(JAVA_INT, i * CARRIER_SIZE);
158         }
159         return res;
160     }
161 
162     @Benchmark
163     public int segment_loop_slice() {
164         int sum = 0;
165         MemorySegment base = segment.asSlice(0, segment.byteSize());
166         for (int i = 0; i < ELEM_SIZE; i++) {
167             sum += (int) VH_int.get(base, (long) i);
168         }
169         return sum;
170     }
171 
172     @Benchmark
173     public int segment_loop_readonly() {
174         int sum = 0;
175         MemorySegment base = segment.asReadOnly();
176         for (int i = 0; i < ELEM_SIZE; i++) {
177             sum += (int) VH_int.get(base, (long) i);
< prev index next >