1 /* 2 * Copyright (c) 2020, 2022, 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.java.lang.foreign; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.BenchmarkMode; 29 import org.openjdk.jmh.annotations.CompilerControl; 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.foreign.Arena; 41 import java.lang.foreign.MemorySegment; 42 import java.nio.ByteBuffer; 43 import java.nio.IntBuffer; 44 import java.util.concurrent.TimeUnit; 45 46 import static java.lang.foreign.ValueLayout.JAVA_INT; 47 import static java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED; 48 49 @BenchmarkMode(Mode.AverageTime) 50 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 51 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 52 @State(org.openjdk.jmh.annotations.Scope.Thread) 53 @OutputTimeUnit(TimeUnit.MILLISECONDS) 54 @Fork(value = 3, jvmArgsAppend = "--enable-preview") 55 public class BulkOps { 56 57 static final Unsafe unsafe = Utils.unsafe; 58 59 static final int ELEM_SIZE = 1_000_000; 60 static final int CARRIER_SIZE = (int)JAVA_INT.byteSize(); 61 static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE; 62 63 final Arena arena = Arena.ofShared(); 64 65 final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE); 66 final MemorySegment segment = arena.allocate(ALLOC_SIZE, 1); 67 68 final IntBuffer buffer = IntBuffer.allocate(ELEM_SIZE); 69 70 final int[] ints = new int[ELEM_SIZE]; 71 final MemorySegment bytesSegment = MemorySegment.ofArray(ints); 72 final int UNSAFE_INT_OFFSET = unsafe.arrayBaseOffset(int[].class); 73 74 // large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized 75 static final int SIZE_WITH_TAIL = (1024 * 1024) + 7; 76 final MemorySegment mismatchSegmentLarge1; 77 78 { 79 mismatchSegmentLarge1 = arena.allocate(SIZE_WITH_TAIL, 1); 80 } 81 82 final MemorySegment mismatchSegmentLarge2 = arena.allocate(SIZE_WITH_TAIL, 1); 83 final ByteBuffer mismatchBufferLarge1 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL); 84 final ByteBuffer mismatchBufferLarge2 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL); 85 86 // mismatch at first byte 87 final MemorySegment mismatchSegmentSmall1 = arena.allocate(7, 1); 88 final MemorySegment mismatchSegmentSmall2 = arena.allocate(7, 1); 89 final ByteBuffer mismatchBufferSmall1 = ByteBuffer.allocateDirect(7); 90 final ByteBuffer mismatchBufferSmall2 = ByteBuffer.allocateDirect(7); 91 92 @Setup 93 public void setup() { 94 mismatchSegmentSmall1.fill((byte) 0xFF); 95 mismatchBufferSmall1.put((byte) 0xFF).clear(); 96 // verify expected mismatch indices 97 long si = mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2); 98 if (si != -1) 99 throw new AssertionError("Unexpected mismatch index:" + si); 100 int bi = mismatchBufferLarge1.mismatch(mismatchBufferLarge2); 101 if (bi != -1) 102 throw new AssertionError("Unexpected mismatch index:" + bi); 103 si = mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2); 104 if (si != 0) 105 throw new AssertionError("Unexpected mismatch index:" + si); 106 bi = mismatchBufferSmall1.mismatch(mismatchBufferSmall2); 107 if (bi != 0) 108 throw new AssertionError("Unexpected mismatch index:" + bi); 109 110 for (int i = 0; i < ints.length ; i++) { 111 ints[i] = i; 112 } 113 } 114 115 @TearDown 116 public void tearDown() { 117 arena.close(); 118 } 119 120 @Benchmark 121 @OutputTimeUnit(TimeUnit.NANOSECONDS) 122 public void unsafe_fill() { 123 unsafe.setMemory(unsafe_addr, ALLOC_SIZE, (byte)42); 124 } 125 126 @Benchmark 127 @OutputTimeUnit(TimeUnit.NANOSECONDS) 128 public void segment_fill() { 129 segment.fill((byte)42); 130 } 131 132 @Benchmark 133 @OutputTimeUnit(TimeUnit.NANOSECONDS) 134 public void unsafe_copy() { 135 unsafe.copyMemory(ints, UNSAFE_INT_OFFSET, null, unsafe_addr, ALLOC_SIZE); 136 } 137 138 @Benchmark 139 @OutputTimeUnit(TimeUnit.NANOSECONDS) 140 public void segment_copy() { 141 segment.copyFrom(bytesSegment); 142 } 143 144 @Benchmark 145 @OutputTimeUnit(TimeUnit.NANOSECONDS) 146 public void segment_copy_static() { 147 MemorySegment.copy(ints, 0, segment, JAVA_INT_UNALIGNED, 0, ints.length); 148 } 149 150 @Benchmark 151 @OutputTimeUnit(TimeUnit.NANOSECONDS) 152 public void segment_copy_static_small() { 153 MemorySegment.copy(ints, 0, segment, JAVA_INT_UNALIGNED, 0, 10); 154 } 155 156 @Benchmark 157 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 158 @OutputTimeUnit(TimeUnit.NANOSECONDS) 159 public void segment_copy_static_small_dontinline() { 160 MemorySegment.copy(ints, 0, segment, JAVA_INT_UNALIGNED, 0, 10); 161 } 162 163 @Benchmark 164 @OutputTimeUnit(TimeUnit.NANOSECONDS) 165 public void unsafe_copy_small() { 166 unsafe.copyMemory(ints, UNSAFE_INT_OFFSET, null, unsafe_addr, 10 * CARRIER_SIZE); 167 } 168 169 @Benchmark 170 @OutputTimeUnit(TimeUnit.NANOSECONDS) 171 public void buffer_copy_small() { 172 buffer.put(0, ints, 0, 10); 173 } 174 175 @Benchmark 176 @OutputTimeUnit(TimeUnit.NANOSECONDS) 177 public void buffer_copy() { 178 buffer.put(0, ints, 0, ints.length); 179 } 180 181 @Benchmark 182 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 183 @OutputTimeUnit(TimeUnit.NANOSECONDS) 184 public void segment_copy_static_dontinline() { 185 MemorySegment.copy(ints, 0, segment, JAVA_INT_UNALIGNED, 0, ints.length); 186 } 187 188 @Benchmark 189 @OutputTimeUnit(TimeUnit.NANOSECONDS) 190 public long mismatch_large_segment() { 191 return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2); 192 } 193 194 @Benchmark 195 @OutputTimeUnit(TimeUnit.NANOSECONDS) 196 public int mismatch_large_bytebuffer() { 197 return mismatchBufferLarge1.mismatch(mismatchBufferLarge2); 198 } 199 200 @Benchmark 201 @OutputTimeUnit(TimeUnit.NANOSECONDS) 202 public long mismatch_small_segment() { 203 return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2); 204 } 205 206 @Benchmark 207 @OutputTimeUnit(TimeUnit.NANOSECONDS) 208 public int mismatch_small_bytebuffer() { 209 return mismatchBufferSmall1.mismatch(mismatchBufferSmall2); 210 } 211 }