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 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 }