< prev index next >

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

Print this page

  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.lang.invoke.VarHandle;
 42 import java.util.concurrent.TimeUnit;
 43 
 44 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 45 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;

 46 
 47 @BenchmarkMode(Mode.AverageTime)
 48 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 49 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 50 @State(org.openjdk.jmh.annotations.Scope.Thread)
 51 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 52 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 53 public class LoopOverPollutedSegments {
 54 
 55     static final int ELEM_SIZE = 1_000_000;
 56     static final int CARRIER_SIZE = (int) JAVA_INT.byteSize();
 57     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 58 
 59     static final Unsafe unsafe = Utils.unsafe;
 60 
 61     MemorySegment nativeSegment, heapSegmentBytes, heapSegmentFloats;
 62     byte[] arr;
 63     long addr;
 64 
 65     static final VarHandle intHandle = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(int.class, MemoryLayout.PathElement.sequenceElement());
 66 
 67 
 68     @Setup
 69     public void setup() {
 70         addr = unsafe.allocateMemory(ALLOC_SIZE);
 71         for (int i = 0; i < ELEM_SIZE; i++) {
 72             unsafe.putInt(addr + (i * 4), i);
 73         }
 74         arr = new byte[ALLOC_SIZE];
 75         nativeSegment = MemorySegment.allocateNative(ALLOC_SIZE, 4, ResourceScope.newConfinedScope());
 76         heapSegmentBytes = MemorySegment.ofArray(new byte[ALLOC_SIZE]);
 77         heapSegmentFloats = MemorySegment.ofArray(new float[ELEM_SIZE]);
 78 
 79         for (int rep = 0 ; rep < 5 ; rep++) {
 80             for (int i = 0; i < ELEM_SIZE; i++) {
 81                 unsafe.putInt(arr, Unsafe.ARRAY_BYTE_BASE_OFFSET + (i * 4), i);
 82                 MemoryAccess.setIntAtIndex(nativeSegment, i, i);
 83                 MemoryAccess.setFloatAtIndex(nativeSegment, i, i);
 84                 intHandle.set(nativeSegment, (long)i, i);
 85                 MemoryAccess.setIntAtIndex(heapSegmentBytes, i, i);
 86                 MemoryAccess.setFloatAtIndex(heapSegmentBytes, i, i);
 87                 intHandle.set(heapSegmentBytes, (long)i, i);
 88                 MemoryAccess.setIntAtIndex(heapSegmentFloats, i, i);
 89                 MemoryAccess.setFloatAtIndex(heapSegmentFloats, i, i);
 90                 intHandle.set(heapSegmentFloats, (long)i, i);
 91             }
 92         }
 93     }
 94 
 95     @TearDown
 96     public void tearDown() {
 97         nativeSegment.scope().close();
 98         heapSegmentBytes = null;
 99         heapSegmentFloats = null;
100         arr = null;
101         unsafe.freeMemory(addr);
102     }
103 
104     @Benchmark
105     public int native_segment_VH() {
106         int sum = 0;
107         for (int k = 0; k < ELEM_SIZE; k++) {
108             intHandle.set(nativeSegment, (long)k, k + 1);
109             int v = (int) intHandle.get(nativeSegment, (long)k);
110             sum += v;
111         }
112         return sum;
113     }
114 
115     @Benchmark
116     public int native_segment_static() {
117         int sum = 0;
118         for (int k = 0; k < ELEM_SIZE; k++) {
119             MemoryAccess.setIntAtOffset(nativeSegment, k, k + 1);
120             int v = MemoryAccess.getIntAtOffset(nativeSegment, k);
121             sum += v;
122         }
123         return sum;
124     }
125 
126     @Benchmark
127     public int heap_segment_ints_VH() {
128         int sum = 0;
129         for (int k = 0; k < ELEM_SIZE; k++) {
130             intHandle.set(heapSegmentBytes, (long)k, k + 1);
131             int v = (int) intHandle.get(heapSegmentBytes, (long)k);
132             sum += v;
133         }
134         return sum;
135     }
136 
137     @Benchmark
138     public int heap_segment_ints_static() {
139         int sum = 0;
140         for (int k = 0; k < ELEM_SIZE; k++) {
141             MemoryAccess.setIntAtOffset(heapSegmentBytes, k, k + 1);
142             int v = MemoryAccess.getIntAtOffset(heapSegmentBytes, k);
143             sum += v;
144         }
145         return sum;
146     }
147 
148     @Benchmark
149     public int heap_segment_floats_VH() {
150         int sum = 0;
151         for (int k = 0; k < ELEM_SIZE; k++) {
152             intHandle.set(heapSegmentFloats, (long)k, k + 1);
153             int v = (int)intHandle.get(heapSegmentFloats, (long)k);
154             sum += v;
155         }
156         return sum;
157     }
158 
159     @Benchmark
160     public int heap_segment_floats_static() {
161         int sum = 0;
162         for (int k = 0; k < ELEM_SIZE; k++) {
163             MemoryAccess.setIntAtOffset(heapSegmentFloats, k, k + 1);
164             int v = MemoryAccess.getIntAtOffset(heapSegmentFloats, k);
165             sum += v;
166         }
167         return sum;
168     }
169 
170     @Benchmark
171     public int heap_unsafe() {
172         int sum = 0;
173         for (int k = 0; k < ALLOC_SIZE; k += 4) {
174             unsafe.putInt(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET, k + 1);
175             int v = unsafe.getInt(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET);
176             sum += v;
177         }
178         return sum;
179     }
180 
181     @Benchmark
182     public int native_unsafe() {
183         int sum = 0;
184         for (int k = 0; k < ALLOC_SIZE; k += 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.util.concurrent.TimeUnit;
 42 
 43 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 44 import static jdk.incubator.foreign.ValueLayout.JAVA_FLOAT;
 45 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 46 
 47 @BenchmarkMode(Mode.AverageTime)
 48 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 49 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 50 @State(org.openjdk.jmh.annotations.Scope.Thread)
 51 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 52 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 53 public class LoopOverPollutedSegments {
 54 
 55     static final int ELEM_SIZE = 1_000_000;
 56     static final int CARRIER_SIZE = (int) JAVA_INT.byteSize();
 57     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 58 
 59     static final Unsafe unsafe = Utils.unsafe;
 60 
 61     MemorySegment nativeSegment, heapSegmentBytes, heapSegmentFloats;
 62     byte[] arr;
 63     long addr;
 64 
 65     static final VarHandle intHandle = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(MemoryLayout.PathElement.sequenceElement());
 66 
 67 
 68     @Setup
 69     public void setup() {
 70         addr = unsafe.allocateMemory(ALLOC_SIZE);
 71         for (int i = 0; i < ELEM_SIZE; i++) {
 72             unsafe.putInt(addr + (i * 4), i);
 73         }
 74         arr = new byte[ALLOC_SIZE];
 75         nativeSegment = MemorySegment.allocateNative(ALLOC_SIZE, 4, ResourceScope.newConfinedScope());
 76         heapSegmentBytes = MemorySegment.ofArray(new byte[ALLOC_SIZE]);
 77         heapSegmentFloats = MemorySegment.ofArray(new float[ELEM_SIZE]);
 78 
 79         for (int rep = 0 ; rep < 5 ; rep++) {
 80             for (int i = 0; i < ELEM_SIZE; i++) {
 81                 unsafe.putInt(arr, Unsafe.ARRAY_BYTE_BASE_OFFSET + (i * 4), i);
 82                 nativeSegment.setAtIndex(JAVA_INT, i, i);
 83                 nativeSegment.setAtIndex(JAVA_FLOAT, i, i);
 84                 intHandle.set(nativeSegment, (long)i, i);
 85                 heapSegmentBytes.setAtIndex(JAVA_INT, i, i);
 86                 heapSegmentBytes.setAtIndex(JAVA_FLOAT, i, i);
 87                 intHandle.set(heapSegmentBytes, (long)i, i);
 88                 heapSegmentFloats.setAtIndex(JAVA_INT, i, i);
 89                 heapSegmentFloats.setAtIndex(JAVA_FLOAT, i, i);
 90                 intHandle.set(heapSegmentFloats, (long)i, i);
 91             }
 92         }
 93     }
 94 
 95     @TearDown
 96     public void tearDown() {
 97         nativeSegment.scope().close();
 98         heapSegmentBytes = null;
 99         heapSegmentFloats = null;
100         arr = null;
101         unsafe.freeMemory(addr);
102     }
103 
104     @Benchmark
105     public int native_segment_VH() {
106         int sum = 0;
107         for (int k = 0; k < ELEM_SIZE; k++) {
108             intHandle.set(nativeSegment, (long)k, k + 1);
109             int v = (int) intHandle.get(nativeSegment, (long)k);
110             sum += v;
111         }
112         return sum;
113     }
114 
115     @Benchmark
116     public int native_segment_instance() {
117         int sum = 0;
118         for (int k = 0; k < ELEM_SIZE; k++) {
119             nativeSegment.setAtIndex(JAVA_INT, k, k + 1);
120             int v = nativeSegment.getAtIndex(JAVA_INT, k);
121             sum += v;
122         }
123         return sum;
124     }
125 
126     @Benchmark
127     public int heap_segment_ints_VH() {
128         int sum = 0;
129         for (int k = 0; k < ELEM_SIZE; k++) {
130             intHandle.set(heapSegmentBytes, (long)k, k + 1);
131             int v = (int) intHandle.get(heapSegmentBytes, (long)k);
132             sum += v;
133         }
134         return sum;
135     }
136 
137     @Benchmark
138     public int heap_segment_ints_instance() {
139         int sum = 0;
140         for (int k = 0; k < ELEM_SIZE; k++) {
141             heapSegmentBytes.setAtIndex(JAVA_INT, k, k + 1);
142             int v = heapSegmentBytes.getAtIndex(JAVA_INT, k);
143             sum += v;
144         }
145         return sum;
146     }
147 
148     @Benchmark
149     public int heap_segment_floats_VH() {
150         int sum = 0;
151         for (int k = 0; k < ELEM_SIZE; k++) {
152             intHandle.set(heapSegmentFloats, (long)k, k + 1);
153             int v = (int)intHandle.get(heapSegmentFloats, (long)k);
154             sum += v;
155         }
156         return sum;
157     }
158 
159     @Benchmark
160     public int heap_segment_floats_instance() {
161         int sum = 0;
162         for (int k = 0; k < ELEM_SIZE; k++) {
163             heapSegmentFloats.setAtIndex(JAVA_INT, k, k + 1);
164             int v = heapSegmentFloats.getAtIndex(JAVA_INT, k);
165             sum += v;
166         }
167         return sum;
168     }
169 
170     @Benchmark
171     public int heap_unsafe() {
172         int sum = 0;
173         for (int k = 0; k < ALLOC_SIZE; k += 4) {
174             unsafe.putInt(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET, k + 1);
175             int v = unsafe.getInt(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET);
176             sum += v;
177         }
178         return sum;
179     }
180 
181     @Benchmark
182     public int native_unsafe() {
183         int sum = 0;
184         for (int k = 0; k < ALLOC_SIZE; k += 4) {
< prev index next >