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