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 package org.openjdk.bench.java.lang.foreign; 24 25 import java.lang.foreign.Arena; 26 import java.lang.foreign.MemorySegment; 27 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.util.concurrent.TimeUnit; 41 42 import static java.lang.foreign.ValueLayout.*; 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(3) 50 public class LoopOverPollutedSegments extends JavaLayouts { 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 Unsafe unsafe = Utils.unsafe; 57 58 59 Arena confinedArena, sharedArena; 60 MemorySegment nativeSegment, nativeSharedSegment, heapSegmentBytes, heapSegmentFloats; 61 byte[] arr; 62 long addr; 63 64 @Setup 65 public void setup() { 66 addr = unsafe.allocateMemory(ALLOC_SIZE); 67 for (int i = 0; i < ELEM_SIZE; i++) { 68 unsafe.putInt(addr + (i * 4), i); 69 } 70 arr = new byte[ALLOC_SIZE]; 71 confinedArena = Arena.ofConfined(); 72 sharedArena = Arena.ofShared(); 73 Arena scope1 = confinedArena; 74 nativeSegment = scope1.allocate(ALLOC_SIZE, 4); 75 Arena scope = sharedArena; 76 nativeSharedSegment = scope.allocate(ALLOC_SIZE, 4); 77 heapSegmentBytes = MemorySegment.ofArray(new byte[ALLOC_SIZE]); 78 heapSegmentFloats = MemorySegment.ofArray(new float[ELEM_SIZE]); 79 80 for (int rep = 0 ; rep < 5 ; rep++) { 81 for (int i = 0; i < ELEM_SIZE; i++) { 82 unsafe.putInt(arr, Unsafe.ARRAY_BYTE_BASE_OFFSET + (i * 4), i); 83 nativeSegment.setAtIndex(JAVA_INT_UNALIGNED, i, i); 84 nativeSegment.setAtIndex(JAVA_FLOAT_UNALIGNED, i, i); 85 nativeSharedSegment.setAtIndex(JAVA_INT_UNALIGNED, i, i); 86 nativeSharedSegment.setAtIndex(JAVA_FLOAT_UNALIGNED, i, i); 87 VH_INT_UNALIGNED.set(nativeSegment, (long)i, i); 88 heapSegmentBytes.setAtIndex(JAVA_INT_UNALIGNED, i, i); 89 heapSegmentBytes.setAtIndex(JAVA_FLOAT_UNALIGNED, i, i); 90 VH_INT_UNALIGNED.set(heapSegmentBytes, (long)i, i); 91 heapSegmentFloats.setAtIndex(JAVA_INT_UNALIGNED, i, i); 92 heapSegmentFloats.setAtIndex(JAVA_FLOAT_UNALIGNED, i, i); 93 VH_INT_UNALIGNED.set(heapSegmentFloats, (long)i, i); 94 } 95 } 96 } 97 98 @TearDown 99 public void tearDown() { 100 confinedArena.close(); 101 sharedArena.close(); 102 heapSegmentBytes = null; 103 heapSegmentFloats = null; 104 arr = null; 105 unsafe.freeMemory(addr); 106 } 107 108 @Benchmark 109 public int native_segment_VH() { 110 int sum = 0; 111 for (int k = 0; k < ELEM_SIZE; k++) { 112 VH_INT_UNALIGNED.set(nativeSegment, (long)k, k + 1); 113 int v = (int) VH_INT_UNALIGNED.get(nativeSegment, (long)k); 114 sum += v; 115 } 116 return sum; 117 } 118 119 @Benchmark 120 public int native_segment_instance() { 121 int sum = 0; 122 for (int k = 0; k < ELEM_SIZE; k++) { 123 nativeSegment.setAtIndex(JAVA_INT_UNALIGNED, k, k + 1); 124 int v = nativeSegment.getAtIndex(JAVA_INT_UNALIGNED, k); 125 sum += v; 126 } 127 return sum; 128 } 129 130 @Benchmark 131 public int heap_segment_ints_VH() { 132 int sum = 0; 133 for (int k = 0; k < ELEM_SIZE; k++) { 134 VH_INT_UNALIGNED.set(heapSegmentBytes, (long)k, k + 1); 135 int v = (int) VH_INT_UNALIGNED.get(heapSegmentBytes, (long)k); 136 sum += v; 137 } 138 return sum; 139 } 140 141 @Benchmark 142 public int heap_segment_ints_instance() { 143 int sum = 0; 144 for (int k = 0; k < ELEM_SIZE; k++) { 145 heapSegmentBytes.setAtIndex(JAVA_INT_UNALIGNED, k, k + 1); 146 int v = heapSegmentBytes.getAtIndex(JAVA_INT_UNALIGNED, k); 147 sum += v; 148 } 149 return sum; 150 } 151 152 @Benchmark 153 public int heap_segment_floats_VH() { 154 int sum = 0; 155 for (int k = 0; k < ELEM_SIZE; k++) { 156 VH_INT_UNALIGNED.set(heapSegmentFloats, (long)k, k + 1); 157 int v = (int)VH_INT_UNALIGNED.get(heapSegmentFloats, (long)k); 158 sum += v; 159 } 160 return sum; 161 } 162 163 @Benchmark 164 public int heap_segment_floats_instance() { 165 int sum = 0; 166 for (int k = 0; k < ELEM_SIZE; k++) { 167 heapSegmentFloats.setAtIndex(JAVA_INT_UNALIGNED, k, k + 1); 168 int v = heapSegmentFloats.getAtIndex(JAVA_INT_UNALIGNED, k); 169 sum += v; 170 } 171 return sum; 172 } 173 174 @Benchmark 175 public int heap_unsafe() { 176 int sum = 0; 177 for (int k = 0; k < ALLOC_SIZE; k += 4) { 178 unsafe.putInt(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET, k + 1); 179 int v = unsafe.getInt(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET); 180 sum += v; 181 } 182 return sum; 183 } 184 185 @Benchmark 186 public int native_unsafe() { 187 int sum = 0; 188 for (int k = 0; k < ALLOC_SIZE; k += 4) { 189 unsafe.putInt(addr + k, k + 1); 190 int v = unsafe.getInt(addr + k); 191 sum += v; 192 } 193 return sum; 194 } 195 }