jimczi commented on code in PR #16279:
URL: https://github.com/apache/lucene/pull/16279#discussion_r3486602086


##########
lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/RandomReadIOBenchmark.java:
##########
@@ -0,0 +1,358 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.benchmark.jmh;
+
+import java.io.IOException;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.nio.ByteBuffer;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+/** Benchmark comparing random read I/O strategies under varying concurrency 
and memory pressure. */
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Benchmark)
+@Warmup(iterations = 2, time = 3)
+@Measurement(iterations = 3, time = 5)
+@Fork(
+    value = 2,
+    jvmArgsPrepend = {"--enable-native-access=ALL-UNNAMED", "-Xms2g", 
"-Xmx2g"})
+public class RandomReadIOBenchmark extends AbstractReadIOBenchmark {
+
+  @Param({"4096"})
+  public int readSize;
+
+  @Param({"16"})
+  public int readsPerOp;
+
+  private long maxOffset;
+  private long maxAlignedOffset;
+
+  @Setup(Level.Trial)
+  public void validateParams() {
+    validateReadSize(readSize);
+    maxOffset = FILE_SIZE - readSize;
+    maxAlignedOffset = (maxOffset / ALIGNMENT) * ALIGNMENT;
+  }
+
+  // ======== mmap NORMAL ========
+
+  @Benchmark
+  @Threads(1)
+  public void mmap_T01(ThreadBuffers tb, Blackhole bh) {
+    doMmapNormalReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(4)
+  public void mmap_T04(ThreadBuffers tb, Blackhole bh) {
+    doMmapNormalReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(8)
+  public void mmap_T08(ThreadBuffers tb, Blackhole bh) {
+    doMmapNormalReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(16)
+  public void mmap_T16(ThreadBuffers tb, Blackhole bh) {
+    doMmapNormalReads(tb, bh);
+  }
+
+  // ======== mmap + MADV_RANDOM ========
+
+  @Benchmark
+  @Threads(1)
+  public void mmapMadvRandom_T01(ThreadBuffers tb, Blackhole bh) {
+    doMmapMadvRandomReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(4)
+  public void mmapMadvRandom_T04(ThreadBuffers tb, Blackhole bh) {
+    doMmapMadvRandomReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(8)
+  public void mmapMadvRandom_T08(ThreadBuffers tb, Blackhole bh) {
+    doMmapMadvRandomReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(16)
+  public void mmapMadvRandom_T16(ThreadBuffers tb, Blackhole bh) {
+    doMmapMadvRandomReads(tb, bh);
+  }
+
+  // ======== mmap NORMAL + batched MADV_WILLNEED ========
+
+  @Benchmark
+  @Threads(1)
+  public void mmapBatchedPrefetch_T01(ThreadBuffers tb, Blackhole bh) {
+    doMmapBatchedPrefetch(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(4)
+  public void mmapBatchedPrefetch_T04(ThreadBuffers tb, Blackhole bh) {
+    doMmapBatchedPrefetch(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(8)
+  public void mmapBatchedPrefetch_T08(ThreadBuffers tb, Blackhole bh) {
+    doMmapBatchedPrefetch(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(16)
+  public void mmapBatchedPrefetch_T16(ThreadBuffers tb, Blackhole bh) {
+    doMmapBatchedPrefetch(tb, bh);
+  }
+
+  // ======== mmap RANDOM + batched MADV_WILLNEED ========
+
+  @Benchmark
+  @Threads(1)
+  public void mmapMadvRandomBatchedPrefetch_T01(ThreadBuffers tb, Blackhole 
bh) {
+    doMmapMadvRandomBatchedPrefetch(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(4)
+  public void mmapMadvRandomBatchedPrefetch_T04(ThreadBuffers tb, Blackhole 
bh) {
+    doMmapMadvRandomBatchedPrefetch(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(8)
+  public void mmapMadvRandomBatchedPrefetch_T08(ThreadBuffers tb, Blackhole 
bh) {
+    doMmapMadvRandomBatchedPrefetch(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(16)
+  public void mmapMadvRandomBatchedPrefetch_T16(ThreadBuffers tb, Blackhole 
bh) {
+    doMmapMadvRandomBatchedPrefetch(tb, bh);
+  }
+
+  // ======== FFI pread ========
+
+  @Benchmark
+  @Threads(1)
+  public void ffiPread_T01(ThreadBuffers tb, Blackhole bh) {
+    doFfiReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(4)
+  public void ffiPread_T04(ThreadBuffers tb, Blackhole bh) {
+    doFfiReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(8)
+  public void ffiPread_T08(ThreadBuffers tb, Blackhole bh) {
+    doFfiReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(16)
+  public void ffiPread_T16(ThreadBuffers tb, Blackhole bh) {
+    doFfiReads(tb, bh);
+  }
+
+  // ======== FileChannel + DirectByteBuffer ========
+
+  @Benchmark
+  @Threads(1)
+  public void fileChannelDirectBuffer_T01(ThreadBuffers tb, Blackhole bh) 
throws IOException {
+    doFileChannelDirectReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(4)
+  public void fileChannelDirectBuffer_T04(ThreadBuffers tb, Blackhole bh) 
throws IOException {
+    doFileChannelDirectReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(8)
+  public void fileChannelDirectBuffer_T08(ThreadBuffers tb, Blackhole bh) 
throws IOException {
+    doFileChannelDirectReads(tb, bh);
+  }
+
+  @Benchmark
+  @Threads(16)
+  public void fileChannelDirectBuffer_T16(ThreadBuffers tb, Blackhole bh) 
throws IOException {
+    doFileChannelDirectReads(tb, bh);
+  }
+
+  // ======== FFI pread + O_DIRECT ========
+
+  // @Benchmark
+  @Threads(1)
+  public void ffiPreadDirectIO_T01(ThreadBuffers tb, Blackhole bh) {
+    doFfiDirectIoReads(tb, bh);
+  }
+
+  // @Benchmark
+  @Threads(4)
+  public void ffiPreadDirectIO_T04(ThreadBuffers tb, Blackhole bh) {
+    doFfiDirectIoReads(tb, bh);
+  }
+
+  // @Benchmark
+  @Threads(8)
+  public void ffiPreadDirectIO_T08(ThreadBuffers tb, Blackhole bh) {
+    doFfiDirectIoReads(tb, bh);
+  }
+
+  // @Benchmark
+  @Threads(16)
+  public void ffiPreadDirectIO_T16(ThreadBuffers tb, Blackhole bh) {
+    doFfiDirectIoReads(tb, bh);
+  }
+
+  // ======== Implementation ========
+
+  private void doMmapNormalReads(ThreadBuffers tb, Blackhole bh) {
+    ThreadLocalRandom rng = ThreadLocalRandom.current();
+    byte[] dst = tb.heapBuf.array();
+    for (int i = 0; i < readsPerOp; i++) {
+      long offset = rng.nextLong(maxOffset);
+      MemorySegment.copy(mmapSegmentNormal, ValueLayout.JAVA_BYTE, offset, 
dst, 0, readSize);
+      bh.consume(dst[0]);
+    }
+  }
+
+  private void doMmapMadvRandomReads(ThreadBuffers tb, Blackhole bh) {
+    ThreadLocalRandom rng = ThreadLocalRandom.current();
+    byte[] dst = tb.heapBuf.array();
+    for (int i = 0; i < readsPerOp; i++) {
+      long offset = rng.nextLong(maxOffset);
+      MemorySegment.copy(mmapSegmentMadvRandom, ValueLayout.JAVA_BYTE, offset, 
dst, 0, readSize);
+      bh.consume(dst[0]);
+    }
+  }
+
+  @SuppressWarnings("unused")
+  private void doMmapBatchedPrefetch(ThreadBuffers tb, Blackhole bh) {
+    ThreadLocalRandom rng = ThreadLocalRandom.current();
+    byte[] dst = tb.heapBuf.array();
+    long[] offsets = new long[readsPerOp];
+    try {
+      for (int i = 0; i < readsPerOp; i++) {
+        offsets[i] = rng.nextLong(maxOffset);
+      }
+      for (int i = 0; i < readsPerOp; i++) {
+        MemorySegment slice = mmapSegmentNormal.asSlice(offsets[i], readSize);
+        int rc = (int) POSIX_MADVISE.invokeExact(slice, (long) readSize, 
MADV_WILLNEED);

Review Comment:
   `madvise` needs a **page-aligned** start address, so passing the raw random 
offset here makes it return `EINVAL` and do nothing — and since `rc` is 
discarded, it fails silently. On a c6id.4xlarge `strace` shows ~5117/5181 of 
these calls returning `-1 EINVAL`, so the prefetch never actually runs and the 
prefetch rows end up identical to plain mmap. The real Directory avoids this 
because `MemorySegmentIndexInput#advise` rounds the start down to the page 
first.
   
   Suggested fix (mirrors what the Directory does):
   
   ```suggestion
           // madvise needs a page-aligned start address, otherwise it returns 
EINVAL and is a no-op.
           long offsetInPage = (mmapSegmentNormal.address() + offsets[i]) % 
ALIGNMENT;
           long aoff = offsets[i] - offsetInPage;
           long alen = readSize + offsetInPage;
           MemorySegment slice = mmapSegmentNormal.asSlice(aoff, alen);
           int rc = (int) POSIX_MADVISE.invokeExact(slice, alen, MADV_WILLNEED);
           assert rc == 0 : "posix_madvise failed: " + rc;
   ```
   
   Same change is needed in `doMmapMadvRandomBatchedPrefetch` (against 
`mmapSegmentMadvRandom`). With this, single-threaded mmap+prefetch goes from 
~0.15 → ~4.2 ops/ms cold (≈7× pread at T01, ~device saturation). Might be worth 
asserting on `rc` at the other madvise call sites too so this can't silently 
regress again.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to