1 /*
  2  * Copyright (c) 2021, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package org.openjdk.bench.jdk.incubator.foreign;
 27 
 28 import jdk.incubator.foreign.ResourceScope;
 29 import jdk.incubator.foreign.SegmentAllocator;
 30 import org.openjdk.jmh.annotations.Benchmark;
 31 import org.openjdk.jmh.annotations.BenchmarkMode;
 32 import org.openjdk.jmh.annotations.Fork;
 33 import org.openjdk.jmh.annotations.Measurement;
 34 import org.openjdk.jmh.annotations.Mode;
 35 import org.openjdk.jmh.annotations.Param;
 36 import org.openjdk.jmh.annotations.OutputTimeUnit;
 37 import org.openjdk.jmh.annotations.Setup;
 38 import org.openjdk.jmh.annotations.State;
 39 import org.openjdk.jmh.annotations.TearDown;
 40 import org.openjdk.jmh.annotations.Warmup;
 41 import sun.misc.Unsafe;
 42 
 43 import jdk.incubator.foreign.MemorySegment;
 44 import java.nio.ByteBuffer;
 45 import java.util.concurrent.TimeUnit;
 46 import java.util.function.Supplier;
 47 
 48 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
 49 
 50 @BenchmarkMode(Mode.AverageTime)
 51 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 53 @State(org.openjdk.jmh.annotations.Scope.Thread)
 54 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 55 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 56 public class BulkMismatchAcquire {
 57 
 58     public enum ScopeKind {
 59         CONFINED(ResourceScope::newConfinedScope),
 60         SHARED(ResourceScope::newSharedScope),
 61         IMPLICIT(ResourceScope::newImplicitScope);
 62 
 63         final Supplier<ResourceScope> scopeFactory;
 64 
 65         ScopeKind(Supplier<ResourceScope> scopeFactory) {
 66             this.scopeFactory = scopeFactory;
 67         }
 68 
 69         ResourceScope makeScope() {
 70             return scopeFactory.get();
 71         }
 72     }
 73 
 74     @Param({"CONFINED", "SHARED", "IMPLICIT"})
 75     public ScopeKind scopeKind;
 76 
 77     // large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized
 78     static final int SIZE_WITH_TAIL = (1024 * 1024) + 7;
 79 
 80     ResourceScope scope;
 81     MemorySegment mismatchSegmentLarge1;
 82     MemorySegment mismatchSegmentLarge2;
 83     ByteBuffer mismatchBufferLarge1;
 84     ByteBuffer mismatchBufferLarge2;
 85     MemorySegment mismatchSegmentSmall1;
 86     MemorySegment mismatchSegmentSmall2;
 87     ByteBuffer mismatchBufferSmall1;
 88     ByteBuffer mismatchBufferSmall2;
 89 
 90     @Setup
 91     public void setup() {
 92         scope = scopeKind.makeScope();
 93         mismatchSegmentLarge1 = MemorySegment.allocateNative(SIZE_WITH_TAIL, scope);
 94         mismatchSegmentLarge2 = MemorySegment.allocateNative(SIZE_WITH_TAIL, scope);
 95         mismatchBufferLarge1 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
 96         mismatchBufferLarge2 = ByteBuffer.allocateDirect(SIZE_WITH_TAIL);
 97 
 98         // mismatch at first byte
 99         mismatchSegmentSmall1 = MemorySegment.allocateNative(7, scope);
100         mismatchSegmentSmall2 = MemorySegment.allocateNative(7, scope);
101         mismatchBufferSmall1 = ByteBuffer.allocateDirect(7);
102         mismatchBufferSmall2 = ByteBuffer.allocateDirect(7);
103         {
104             mismatchSegmentSmall1.fill((byte) 0xFF);
105             mismatchBufferSmall1.put((byte) 0xFF).clear();
106             // verify expected mismatch indices
107             long si = mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
108             if (si != -1)
109                 throw new AssertionError("Unexpected mismatch index:" + si);
110             int bi = mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
111             if (bi != -1)
112                 throw new AssertionError("Unexpected mismatch index:" + bi);
113             si = mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
114             if (si != 0)
115                 throw new AssertionError("Unexpected mismatch index:" + si);
116             bi = mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
117             if (bi != 0)
118                 throw new AssertionError("Unexpected mismatch index:" + bi);
119         }
120     }
121 
122     @TearDown
123     public void tearDown() {
124         if (!scope.isImplicit())
125             scope.close();
126     }
127 
128     @Benchmark
129     @OutputTimeUnit(TimeUnit.NANOSECONDS)
130     public long mismatch_large_segment() {
131         return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
132     }
133 
134     @Benchmark
135     @OutputTimeUnit(TimeUnit.NANOSECONDS)
136     public long mismatch_large_segment_acquire() {
137         var handle = mismatchSegmentLarge1.scope().acquire();
138         try {
139             return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
140         } finally {
141             mismatchSegmentLarge1.scope().release(handle);
142         }
143     }
144 
145     @Benchmark
146     @OutputTimeUnit(TimeUnit.NANOSECONDS)
147     public int mismatch_large_bytebuffer() {
148         return mismatchBufferLarge1.mismatch(mismatchBufferLarge2);
149     }
150 
151     @Benchmark
152     @OutputTimeUnit(TimeUnit.NANOSECONDS)
153     public long mismatch_small_segment() {
154         return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
155     }
156 
157     @Benchmark
158     @OutputTimeUnit(TimeUnit.NANOSECONDS)
159     public long mismatch_small_segment_acquire() {
160         var handle = mismatchSegmentLarge1.scope().acquire();
161         try {
162             return mismatchSegmentSmall1.mismatch(mismatchSegmentSmall2);
163         } finally {
164             mismatchSegmentLarge1.scope().release(handle);
165         }
166     }
167 
168     @Benchmark
169     @OutputTimeUnit(TimeUnit.NANOSECONDS)
170     public int mismatch_small_bytebuffer() {
171         return mismatchBufferSmall1.mismatch(mismatchBufferSmall2);
172     }
173 }