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 package org.openjdk.bench.jdk.incubator.foreign;
 24 
 25 import jdk.incubator.foreign.MemoryAccess;
 26 import jdk.incubator.foreign.MemoryLayout;
 27 import jdk.incubator.foreign.MemorySegment;
 28 import jdk.incubator.foreign.ResourceScope;
 29 import org.openjdk.jmh.annotations.Benchmark;
 30 import org.openjdk.jmh.annotations.BenchmarkMode;
 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.Param;
 36 import org.openjdk.jmh.annotations.Setup;
 37 import org.openjdk.jmh.annotations.State;
 38 import org.openjdk.jmh.annotations.TearDown;
 39 import org.openjdk.jmh.annotations.Warmup;
 40 import sun.misc.Unsafe;
 41 
 42 import java.lang.invoke.VarHandle;
 43 import java.lang.ref.Cleaner;
 44 import java.nio.ByteBuffer;
 45 import java.nio.ByteOrder;
 46 import java.util.concurrent.TimeUnit;
 47 
 48 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 49 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
 50 
 51 @BenchmarkMode(Mode.AverageTime)
 52 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 53 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 54 @State(org.openjdk.jmh.annotations.Scope.Thread)
 55 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 56 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 57 public class LoopOverNonConstantHeap {
 58 
 59     static final Unsafe unsafe = Utils.unsafe;
 60 
 61     static final int ELEM_SIZE = 1_000_000;
 62     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 63     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 64     static final int UNSAFE_BYTE_BASE = unsafe.arrayBaseOffset(byte[].class);
 65 
 66     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(int.class, sequenceElement());
 67     MemorySegment segment;
 68     byte[] base;
 69 
 70     ByteBuffer byteBuffer;
 71 
 72     @Param(value = {"false", "true"})
 73     boolean polluteProfile;
 74 
 75     @Setup
 76     public void setup() {
 77         if (polluteProfile) {
 78             MemorySegment intB = MemorySegment.ofArray(new byte[ALLOC_SIZE]);
 79             MemorySegment intI = MemorySegment.ofArray(new int[ALLOC_SIZE]);
 80             MemorySegment intD = MemorySegment.ofArray(new double[ALLOC_SIZE]);
 81             MemorySegment intF = MemorySegment.ofArray(new float[ALLOC_SIZE]);
 82             MemorySegment s = MemorySegment.allocateNative(ALLOC_SIZE, 1, ResourceScope.newConfinedScope(Cleaner.create()));
 83             for (int i = 0; i < ALLOC_SIZE; i++) {
 84                 MemoryAccess.setByteAtOffset(intB, i, (byte)i);
 85                 MemoryAccess.setIntAtIndex(intI, i, i);
 86                 MemoryAccess.setDoubleAtIndex(intD, i, i);
 87                 MemoryAccess.setFloatAtIndex(intF, i, i);
 88                 MemoryAccess.setByteAtOffset(s, i, (byte) i);
 89             }
 90         }
 91 
 92         base = new byte[ALLOC_SIZE];
 93         for (int i = 0; i < ELEM_SIZE; i++) {
 94             unsafe.putInt(base, UNSAFE_BYTE_BASE + (i * CARRIER_SIZE) , i);
 95         }
 96         segment = MemorySegment.ofArray(base);
 97         byteBuffer = ByteBuffer.wrap(base).order(ByteOrder.nativeOrder());
 98     }
 99 
100     @Benchmark
101     @OutputTimeUnit(TimeUnit.NANOSECONDS)
102     public int unsafe_get() {
103         return unsafe.getInt(base, UNSAFE_BYTE_BASE);
104     }
105 
106     @Benchmark
107     @OutputTimeUnit(TimeUnit.NANOSECONDS)
108     public int segment_get() {
109         return (int) VH_int.get(segment, 0L);
110     }
111 
112     @Benchmark
113     @OutputTimeUnit(TimeUnit.NANOSECONDS)
114     public int BB_get() {
115         return byteBuffer.getInt(0);
116     }
117 
118     @Benchmark
119     public int unsafe_loop() {
120         int res = 0;
121         for (int i = 0; i < ELEM_SIZE; i ++) {
122             res += unsafe.getInt(base, UNSAFE_BYTE_BASE + (i * CARRIER_SIZE));
123         }
124         return res;
125     }
126 
127     @Benchmark
128     public int segment_loop() {
129         int sum = 0;
130         for (int i = 0; i < ELEM_SIZE; i++) {
131             sum += (int) VH_int.get(segment, (long) i);
132         }
133         return sum;
134     }
135 
136     @Benchmark
137     public int segment_loop_static() {
138         int res = 0;
139         for (int i = 0; i < ELEM_SIZE; i ++) {
140             res += MemoryAccess.getIntAtIndex(segment, i);
141         }
142         return res;
143     }
144 
145     @Benchmark
146     public int segment_loop_slice() {
147         int sum = 0;
148         MemorySegment base = segment.asSlice(0, segment.byteSize());
149         for (int i = 0; i < ELEM_SIZE; i++) {
150             sum += (int) VH_int.get(base, (long) i);
151         }
152         return sum;
153     }
154 
155     @Benchmark
156     public int segment_loop_readonly() {
157         int sum = 0;
158         MemorySegment base = segment.asReadOnly();
159         for (int i = 0; i < ELEM_SIZE; i++) {
160             sum += (int) VH_int.get(base, (long) i);
161         }
162         return sum;
163     }
164 
165     @Benchmark
166     public int BB_loop() {
167         int sum = 0;
168         ByteBuffer bb = byteBuffer;
169         for (int i = 0; i < ELEM_SIZE; i++) {
170             sum += bb.getInt(i * CARRIER_SIZE);
171         }
172         return sum;
173     }
174 
175 }