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 org.openjdk.jmh.annotations.Benchmark;
28 import org.openjdk.jmh.annotations.BenchmarkMode;
29 import org.openjdk.jmh.annotations.Fork;
30 import org.openjdk.jmh.annotations.Measurement;
31 import org.openjdk.jmh.annotations.Mode;
32 import org.openjdk.jmh.annotations.OutputTimeUnit;
33 import org.openjdk.jmh.annotations.State;
34 import org.openjdk.jmh.annotations.Warmup;
35 import sun.misc.Unsafe;
36 
37 import jdk.incubator.foreign.MemorySegment;
38 import java.util.concurrent.TimeUnit;
39 
40 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
41 
42 @BenchmarkMode(Mode.AverageTime)
43 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
44 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
45 @State(org.openjdk.jmh.annotations.Scope.Thread)
46 @OutputTimeUnit(TimeUnit.MILLISECONDS)
47 @Fork(3)
48 public class BulkOps {
49 
50     static final Unsafe unsafe = Utils.unsafe;
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 long unsafe_addr = unsafe.allocateMemory(ALLOC_SIZE);
57     static final MemorySegment segment = MemorySegment.allocateNative(ALLOC_SIZE);
58 
59     static final int[] bytes = new int[ELEM_SIZE];
60     static final MemorySegment bytesSegment = MemorySegment.ofArray(bytes);
61     static final int UNSAFE_INT_OFFSET = unsafe.arrayBaseOffset(int[].class);
62 
63     static {
64         for (int i = 0 ; i < bytes.length ; i++) {
65             bytes[i] = i;
66         }
67     }
68 
69     @Benchmark
70     @OutputTimeUnit(TimeUnit.NANOSECONDS)
71     public void unsafe_fill() {
72         unsafe.setMemory(unsafe_addr, ALLOC_SIZE, (byte)0);
73     }
74 
75     @Benchmark
76     @OutputTimeUnit(TimeUnit.NANOSECONDS)
77     public void segment_fill() {
78         segment.fill((byte)0);
79     }
80 
81     @Benchmark
82     @OutputTimeUnit(TimeUnit.NANOSECONDS)
83     public void unsafe_copy() {
84         unsafe.copyMemory(bytes, UNSAFE_INT_OFFSET, null, unsafe_addr, ALLOC_SIZE);
85     }
86 
87     @Benchmark
88     @OutputTimeUnit(TimeUnit.NANOSECONDS)
89     public void segment_copy() {
90         segment.copyFrom(bytesSegment);
91     }
92 }