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 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.State;
 35 import org.openjdk.jmh.annotations.Warmup;
 36 import sun.misc.Unsafe;
 37 
 38 import jdk.incubator.foreign.MemorySegment;
 39 import java.nio.ByteBuffer;
 40 import java.util.concurrent.TimeUnit;
 41 
 42 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
 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(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 50 public class BulkOps {
 51 
 52     static final Unsafe unsafe = Utils.unsafe;
 53 
 54     static final int ELEM_SIZE = 1_000_000;
 55     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 56     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 57 
 58     static final long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
 59     static final MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newConfinedScope());
 60 
 61     static final int[] bytes = new int[ELEM_SIZE];
 62     static final MemorySegment bytesSegment = MemorySegment.ofArray(bytes);
 63     static final int UNSAFE_INT_OFFSET = unsafe.arrayBaseOffset(int[].class);
 64 
 65     // large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized
 66     static final int SIZE_WITH_TAIL = (1024 * 1024) + 7;
 67     static final MemorySegment mismatchSegmentLarge1 = MemorySegment.allocateNative(SIZE_WITH_TAIL, ResourceScope.newConfinedScope());
 68     static final MemorySegment mismatchSegmentLarge2 = MemorySegment.allocateNative(SIZE_WITH_TAIL, ResourceScope.newConfinedScope());
 69     static final ByteBuffer mismatchBufferLarge1 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
 70     static final ByteBuffer mismatchBufferLarge2 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
 71 
 72     // mismatch at first byte
 73     static final MemorySegment mismatchSegmentSmall1 = MemorySegment.allocateNative(7, ResourceScope.newConfinedScope());
 74     static final MemorySegment mismatchSegmentSmall2 = MemorySegment.allocateNative(7, ResourceScope.newConfinedScope());
 75     static final ByteBuffer mismatchBufferSmall1 = ByteBuffer.allocateDirect(7);
 76     static final ByteBuffer mismatchBufferSmall2 = ByteBuffer.allocateDirect(7);
 77     static {
 78         mismatchSegmentSmall1.fill((byte) 0xFF);
 79         mismatchBufferSmall1.put((byte) 0xFF).clear();
 80         // verify expected mismatch indices
 81         long si = mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
 82         if (si != -1)
 83             throw new AssertionError("Unexpected mismatch index:" + si);
 84         int bi = mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
 85         if (bi != -1)
 86             throw new AssertionError("Unexpected mismatch index:" + bi);
 87         si = mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
 88         if (si != 0)
 89             throw new AssertionError("Unexpected mismatch index:" + si);
 90         bi = mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
 91         if (bi != 0)
 92             throw new AssertionError("Unexpected mismatch index:" + bi);
 93     }
 94 
 95     static {
 96         for (int i = 0 ; i < bytes.length ; i++) {
 97             bytes[i] = i;
 98         }
 99     }
100 
101     @Benchmark
102     @OutputTimeUnit(TimeUnit.NANOSECONDS)
103     public void unsafe_fill() {
104         unsafe.setMemory(unsafe_addr, ALLOC_SIZE, (byte)42);
105     }
106 
107     @Benchmark
108     @OutputTimeUnit(TimeUnit.NANOSECONDS)
109     public void segment_fill() {
110         segment.fill((byte)42);
111     }
112 
113     @Benchmark
114     @OutputTimeUnit(TimeUnit.NANOSECONDS)
115     public void unsafe_copy() {
116         unsafe.copyMemory(bytes, UNSAFE_INT_OFFSET, null, unsafe_addr, ALLOC_SIZE);
117     }
118 
119     @Benchmark
120     @OutputTimeUnit(TimeUnit.NANOSECONDS)
121     public void segment_copy() {
122         segment.copyFrom(bytesSegment);
123     }
124 
125     @Benchmark
126     @OutputTimeUnit(TimeUnit.NANOSECONDS)
127     public long mismatch_large_segment() {
128         return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
129     }
130 
131     @Benchmark
132     @OutputTimeUnit(TimeUnit.NANOSECONDS)
133     public int mismatch_large_bytebuffer() {
134         return mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
135     }
136 
137     @Benchmark
138     @OutputTimeUnit(TimeUnit.NANOSECONDS)
139     public long mismatch_small_segment() {
140         return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
141     }
142 
143     @Benchmark
144     @OutputTimeUnit(TimeUnit.NANOSECONDS)
145     public int mismatch_small_bytebuffer() {
146         return mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
147     }
148 }