< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/foreign/BulkOps.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  */
 24 
 25 package org.openjdk.bench.jdk.incubator.foreign;
 26 
 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.State;

 35 import org.openjdk.jmh.annotations.Warmup;
 36 import sun.misc.Unsafe;
 37 
 38 import jdk.incubator.foreign.MemorySegment;
 39 import java.nio.ByteBuffer;

 40 import java.util.concurrent.TimeUnit;
 41 
 42 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
 43 
 44 @BenchmarkMode(Mode.AverageTime)
 45 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 46 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 47 @State(org.openjdk.jmh.annotations.Scope.Thread)
 48 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 49 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 50 public class BulkOps {
 51 
 52     static final Unsafe unsafe = Utils.unsafe;
 53 
 54     static final int ELEM_SIZE = 1_000_000;
 55     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 56     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 57 
 58     static final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 59     static final MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newConfinedScope());
 60 
 61     static final int[] bytes = new int[ELEM_SIZE];
 62     static final MemorySegment bytesSegment = MemorySegment.ofArray(bytes);
 63     static final int UNSAFE_INT_OFFSET = unsafe.arrayBaseOffset(int[].class);




 64 
 65     // large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized
 66     static final int SIZE_WITH_TAIL = (1024 * 1024) + 7;
 67     static final MemorySegment mismatchSegmentLarge1 = MemorySegment.allocateNative(SIZE_WITH_TAIL, ResourceScope.newConfinedScope());
 68     static final MemorySegment mismatchSegmentLarge2 = MemorySegment.allocateNative(SIZE_WITH_TAIL, ResourceScope.newConfinedScope());
 69     static final ByteBuffer mismatchBufferLarge1 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
 70     static final ByteBuffer mismatchBufferLarge2 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
 71 
 72     // mismatch at first byte
 73     static final MemorySegment mismatchSegmentSmall1 = MemorySegment.allocateNative(7, ResourceScope.newConfinedScope());
 74     static final MemorySegment mismatchSegmentSmall2 = MemorySegment.allocateNative(7, ResourceScope.newConfinedScope());
 75     static final ByteBuffer mismatchBufferSmall1 = ByteBuffer.allocateDirect(7);
 76     static final ByteBuffer mismatchBufferSmall2 = ByteBuffer.allocateDirect(7);
 77     static {


 78         mismatchSegmentSmall1.fill((byte) 0xFF);
 79         mismatchBufferSmall1.put((byte) 0xFF).clear();
 80         // verify expected mismatch indices
 81         long si = mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
 82         if (si != -1)
 83             throw new AssertionError("Unexpected mismatch index:" + si);
 84         int bi = mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
 85         if (bi != -1)
 86             throw new AssertionError("Unexpected mismatch index:" + bi);
 87         si = mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
 88         if (si != 0)
 89             throw new AssertionError("Unexpected mismatch index:" + si);
 90         bi = mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
 91         if (bi != 0)
 92             throw new AssertionError("Unexpected mismatch index:" + bi);
 93     }
 94 
 95     static {
 96         for (int i = 0 ; i < bytes.length ; i++) {
 97             bytes[i] = i;
 98         }
 99     }
100 





101     @Benchmark
102     @OutputTimeUnit(TimeUnit.NANOSECONDS)
103     public void unsafe_fill() {
104         unsafe.setMemory(unsafe_addr, ALLOC_SIZE, (byte)42);
105     }
106 
107     @Benchmark
108     @OutputTimeUnit(TimeUnit.NANOSECONDS)
109     public void segment_fill() {
110         segment.fill((byte)42);
111     }
112 
113     @Benchmark
114     @OutputTimeUnit(TimeUnit.NANOSECONDS)
115     public void unsafe_copy() {
116         unsafe.copyMemory(bytes, UNSAFE_INT_OFFSET, null, unsafe_addr, ALLOC_SIZE);
117     }
118 
119     @Benchmark
120     @OutputTimeUnit(TimeUnit.NANOSECONDS)
121     public void segment_copy() {
122         segment.copyFrom(bytesSegment);
123     }
124 












































125     @Benchmark
126     @OutputTimeUnit(TimeUnit.NANOSECONDS)
127     public long mismatch_large_segment() {
128         return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
129     }
130 
131     @Benchmark
132     @OutputTimeUnit(TimeUnit.NANOSECONDS)
133     public int mismatch_large_bytebuffer() {
134         return mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
135     }
136 
137     @Benchmark
138     @OutputTimeUnit(TimeUnit.NANOSECONDS)
139     public long mismatch_small_segment() {
140         return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
141     }
142 
143     @Benchmark
144     @OutputTimeUnit(TimeUnit.NANOSECONDS)

  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  */
 24 
 25 package org.openjdk.bench.jdk.incubator.foreign;
 26 
 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.CompilerControl;
 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 jdk.incubator.foreign.MemorySegment;
 42 import java.nio.ByteBuffer;
 43 import java.nio.IntBuffer;
 44 import java.util.concurrent.TimeUnit;
 45 
 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(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 54 public class BulkOps {
 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     final ResourceScope scope = ResourceScope.newConfinedScope();

 63 
 64     final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 65     final MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newConfinedScope());
 66     final IntBuffer buffer = IntBuffer.allocate(ELEM_SIZE);
 67 
 68     final int[] bytes = new int[ELEM_SIZE];
 69     final MemorySegment bytesSegment = MemorySegment.ofArray(bytes);
 70     final int UNSAFE_INT_OFFSET = unsafe.arrayBaseOffset(int[].class);
 71 
 72     // large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized
 73     static final int SIZE_WITH_TAIL = (1024 * 1024) + 7;
 74     final MemorySegment mismatchSegmentLarge1 = MemorySegment.allocateNative(SIZE_WITH_TAIL, scope);
 75     final MemorySegment mismatchSegmentLarge2 = MemorySegment.allocateNative(SIZE_WITH_TAIL, scope);
 76     final ByteBuffer mismatchBufferLarge1 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
 77     final ByteBuffer mismatchBufferLarge2 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
 78 
 79     // mismatch at first byte
 80     final MemorySegment mismatchSegmentSmall1 = MemorySegment.allocateNative(7, scope);
 81     final MemorySegment mismatchSegmentSmall2 = MemorySegment.allocateNative(7, scope);
 82     final ByteBuffer mismatchBufferSmall1 = ByteBuffer.allocateDirect(7);
 83     final ByteBuffer mismatchBufferSmall2 = ByteBuffer.allocateDirect(7);
 84 
 85     @Setup
 86     public void setup() {
 87         mismatchSegmentSmall1.fill((byte) 0xFF);
 88         mismatchBufferSmall1.put((byte) 0xFF).clear();
 89         // verify expected mismatch indices
 90         long si = mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
 91         if (si != -1)
 92             throw new AssertionError("Unexpected mismatch index:" + si);
 93         int bi = mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
 94         if (bi != -1)
 95             throw new AssertionError("Unexpected mismatch index:" + bi);
 96         si = mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
 97         if (si != 0)
 98             throw new AssertionError("Unexpected mismatch index:" + si);
 99         bi = mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
100         if (bi != 0)
101             throw new AssertionError("Unexpected mismatch index:" + bi);

102 

103         for (int i = 0 ; i < bytes.length ; i++) {
104             bytes[i] = i;
105         }
106     }
107 
108     @TearDown
109     public void tearDown() {
110         scope.close();
111     }
112 
113     @Benchmark
114     @OutputTimeUnit(TimeUnit.NANOSECONDS)
115     public void unsafe_fill() {
116         unsafe.setMemory(unsafe_addr, ALLOC_SIZE, (byte)42);
117     }
118 
119     @Benchmark
120     @OutputTimeUnit(TimeUnit.NANOSECONDS)
121     public void segment_fill() {
122         segment.fill((byte)42);
123     }
124 
125     @Benchmark
126     @OutputTimeUnit(TimeUnit.NANOSECONDS)
127     public void unsafe_copy() {
128         unsafe.copyMemory(bytes, UNSAFE_INT_OFFSET, null, unsafe_addr, ALLOC_SIZE);
129     }
130 
131     @Benchmark
132     @OutputTimeUnit(TimeUnit.NANOSECONDS)
133     public void segment_copy() {
134         segment.copyFrom(bytesSegment);
135     }
136 
137     @Benchmark
138     @OutputTimeUnit(TimeUnit.NANOSECONDS)
139     public void segment_copy_static() {
140         MemorySegment.copy(bytes, 0, segment, JAVA_INT, 0, bytes.length);
141     }
142 
143     @Benchmark
144     @OutputTimeUnit(TimeUnit.NANOSECONDS)
145     public void segment_copy_static_small() {
146         MemorySegment.copy(bytes, 0, segment, JAVA_INT, 0, 10);
147     }
148 
149     @Benchmark
150     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
151     @OutputTimeUnit(TimeUnit.NANOSECONDS)
152     public void segment_copy_static_small_dontinline() {
153         MemorySegment.copy(bytes, 0, segment, JAVA_INT, 0, 10);
154     }
155 
156     @Benchmark
157     @OutputTimeUnit(TimeUnit.NANOSECONDS)
158     public void unsafe_copy_small() {
159         unsafe.copyMemory(bytes, UNSAFE_INT_OFFSET, null, unsafe_addr, 10 * CARRIER_SIZE);
160     }
161 
162     @Benchmark
163     @OutputTimeUnit(TimeUnit.NANOSECONDS)
164     public void buffer_copy_small() {
165         buffer.put(0, bytes, 0, 10);
166     }
167 
168     @Benchmark
169     @OutputTimeUnit(TimeUnit.NANOSECONDS)
170     public void buffer_copy() {
171         buffer.put(0, bytes, 0, bytes.length);
172     }
173 
174     @Benchmark
175     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
176     @OutputTimeUnit(TimeUnit.NANOSECONDS)
177     public void segment_copy_static_dontinline() {
178         MemorySegment.copy(bytes, 0, segment, JAVA_INT, 0, bytes.length);
179     }
180 
181     @Benchmark
182     @OutputTimeUnit(TimeUnit.NANOSECONDS)
183     public long mismatch_large_segment() {
184         return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
185     }
186 
187     @Benchmark
188     @OutputTimeUnit(TimeUnit.NANOSECONDS)
189     public int mismatch_large_bytebuffer() {
190         return mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
191     }
192 
193     @Benchmark
194     @OutputTimeUnit(TimeUnit.NANOSECONDS)
195     public long mismatch_small_segment() {
196         return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
197     }
198 
199     @Benchmark
200     @OutputTimeUnit(TimeUnit.NANOSECONDS)
< prev index next >