< prev index next >

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

Print this page
M BulkOps.java


  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 org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Fork;
  30 import org.openjdk.jmh.annotations.Measurement;
  31 import org.openjdk.jmh.annotations.Mode;
  32 import org.openjdk.jmh.annotations.OutputTimeUnit;
  33 import org.openjdk.jmh.annotations.State;
  34 import org.openjdk.jmh.annotations.Warmup;
  35 import sun.misc.Unsafe;
  36 
  37 import jdk.incubator.foreign.MemorySegment;

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






























  63     static {
  64         for (int i = 0 ; i < bytes.length ; i++) {
  65             bytes[i] = i;
  66         }
  67     }
  68 
  69     @Benchmark
  70     @OutputTimeUnit(TimeUnit.NANOSECONDS)
  71     public void unsafe_fill() {
  72         unsafe.setMemory(unsafe_addr, ALLOC_SIZE, (byte)42);
  73     }
  74 
  75     @Benchmark
  76     @OutputTimeUnit(TimeUnit.NANOSECONDS)
  77     public void segment_fill() {
  78         segment.fill((byte)42);
  79     }
  80 
  81     @Benchmark
  82     @OutputTimeUnit(TimeUnit.NANOSECONDS)
  83     public void unsafe_copy() {
  84         unsafe.copyMemory(bytes, UNSAFE_INT_OFFSET, null, unsafe_addr, ALLOC_SIZE);
  85     }
  86 
  87     @Benchmark
  88     @OutputTimeUnit(TimeUnit.NANOSECONDS)
  89     public void segment_copy() {
  90         segment.copyFrom(bytesSegment);
  91     }
























  92 }


  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 org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Fork;
  30 import org.openjdk.jmh.annotations.Measurement;
  31 import org.openjdk.jmh.annotations.Mode;
  32 import org.openjdk.jmh.annotations.OutputTimeUnit;
  33 import org.openjdk.jmh.annotations.State;
  34 import org.openjdk.jmh.annotations.Warmup;
  35 import sun.misc.Unsafe;
  36 
  37 import jdk.incubator.foreign.MemorySegment;
  38 import java.nio.ByteBuffer;
  39 import java.util.concurrent.TimeUnit;
  40 
  41 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
  42 
  43 @BenchmarkMode(Mode.AverageTime)
  44 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
  45 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
  46 @State(org.openjdk.jmh.annotations.Scope.Thread)
  47 @OutputTimeUnit(TimeUnit.MILLISECONDS)
  48 @Fork(3)
  49 public class BulkOps {
  50 
  51     static final Unsafe unsafe = Utils.unsafe;
  52 
  53     static final int ELEM_SIZE = 1_000_000;
  54     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
  55     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
  56 
  57     static final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
  58     static final MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE);
  59 
  60     static final int[] bytes = new int[ELEM_SIZE];
  61     static final MemorySegment bytesSegment = MemorySegment.ofArray(bytes);
  62     static final int UNSAFE_INT_OFFSET = unsafe.arrayBaseOffset(int[].class);
  63 
  64     // large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized
  65     static final int SIZE_WITH_TAIL = ALLOC_SIZE + 7;
  66     static final MemorySegment mismatchSegmentLarge1 = MemorySegment.allocateNative(SIZE_WITH_TAIL);
  67     static final MemorySegment mismatchSegmentLarge2 = MemorySegment.allocateNative(SIZE_WITH_TAIL);
  68     static final ByteBuffer mismatchBufferLarge1 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
  69     static final ByteBuffer mismatchBufferLarge2 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
  70 
  71     // mismatch at first byte
  72     static final MemorySegment mismatchSegmentSmall1 = MemorySegment.allocateNative(7);
  73     static final MemorySegment mismatchSegmentSmall2 = MemorySegment.allocateNative(7);
  74     static final ByteBuffer mismatchBufferSmall1 = ByteBuffer.allocateDirect(7);
  75     static final ByteBuffer mismatchBufferSmall2 = ByteBuffer.allocateDirect(7);
  76     static {
  77         mismatchSegmentSmall1.fill((byte) 0xFF);
  78         mismatchBufferSmall1.put((byte) 0xFF).clear();
  79         // verify expected mismatch indices
  80         long si = mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
  81         if (si != -1)
  82             throw new AssertionError("Unexpected mismatch index:" + si);
  83         int bi = mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
  84         if (bi != -1)
  85             throw new AssertionError("Unexpected mismatch index:" + bi);
  86         si = mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
  87         if (si != 0)
  88             throw new AssertionError("Unexpected mismatch index:" + si);
  89         bi = mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
  90         if (bi != 0)
  91             throw new AssertionError("Unexpected mismatch index:" + bi);
  92     }
  93 
  94     static {
  95         for (int i = 0 ; i < bytes.length ; i++) {
  96             bytes[i] = i;
  97         }
  98     }
  99 
 100     @Benchmark
 101     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 102     public void unsafe_fill() {
 103         unsafe.setMemory(unsafe_addr, ALLOC_SIZE, (byte)42);
 104     }
 105 
 106     @Benchmark
 107     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 108     public void segment_fill() {
 109         segment.fill((byte)42);
 110     }
 111 
 112     @Benchmark
 113     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 114     public void unsafe_copy() {
 115         unsafe.copyMemory(bytes, UNSAFE_INT_OFFSET, null, unsafe_addr, ALLOC_SIZE);
 116     }
 117 
 118     @Benchmark
 119     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 120     public void segment_copy() {
 121         segment.copyFrom(bytesSegment);
 122     }
 123 
 124     @Benchmark
 125     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 126     public long mismatch_large_segment() {
 127         return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
 128     }
 129 
 130     @Benchmark
 131     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 132     public int mismatch_large_bytebuffer() {
 133         return mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
 134     }
 135 
 136     @Benchmark
 137     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 138     public long mismatch_small_segment() {
 139         return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
 140     }
 141 
 142     @Benchmark
 143     @OutputTimeUnit(TimeUnit.NANOSECONDS)
 144     public int mismatch_small_bytebuffer() {
 145         return mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
 146     }
 147 }
< prev index next >