Skip to content

fix(bench): hoist io_uring writer to prevent MEMLOCK exhaustion - #3576

Merged
oferchen merged 1 commit into
masterfrom
fix/io-uring-bench-writer-hoist
May 3, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/io-uring-bench-writer-hoist

Conversation

@oferchen

@oferchen oferchen commented May 3, 2026

Copy link
Copy Markdown
Owner

Summary

The io_uring write benchmark allocated a fresh ring inside b.iter() via IoUringWriter::create, which on CI runners exhausts RLIMIT_MEMLOCK (each ring + registered buffers locks ~512 KB). When the writer bench runs immediately after the reader bench, the kernel has not yet reclaimed the reader's MEMLOCK pages, and the very first writer iteration panics with ENOMEM from io_uring_setup():

thread 'main' panicked at crates/fast_io/benches/io_optimizations.rs:274:80:
called `Result::unwrap()` on an `Err` value: Custom { kind: Other, error:
"io_uring init failed: Cannot allocate memory (os error 12)" }

(See run 25279998712 -- failed at the FIRST iter of io_uring_writes/io_uring/64kb warm-up, 14ms in.)

Fix

Hoist IoUringWriter::create outside b.iter() and reuse the ring across iterations via writer.write_all_batched(data, 0). This public method takes an explicit offset and bypasses the internal write cursor, so writing to offset 0 every iteration overwrites the file in place rather than growing it -- cross-iter reuse is safe.

Same pattern as PR #3575 used for the reader.

Why the defensive bounds from PR #3575 are gone

PR #3575 added sample_size(10) / warm_up_time(500ms) / measurement_time(2s) because it (correctly) believed cross-iter writer reuse would grow the file unboundedly. That was true only for the buffered Write::write_all + flush path which uses the internal bytes_written cursor as the next-write offset.

write_all_batched(&mut self, data: &[u8], offset: u64) takes an explicit offset and never touches bytes_written -- so we can reuse the writer across iterations with offset=0 and the file is overwritten in place. This invalidates the "cross-iter reuse would grow the file" concern, so the defensive bounds are no longer needed.

Test plan

  • CI: cargo bench --workspace on ubuntu-24.04 (will be exercised by the Benchmark and Criterion Benchmarks workflows)
  • After merge: re-dispatch Criterion Benchmarks against v0.6.1 via gh workflow run "Criterion Benchmarks" --ref master -f target_tag=v0.6.1
  • Verify the v0.6.1 release notes get the Criterion Microbenchmarks section + criterion_output.txt asset

The io_uring write benchmark allocated a fresh ring inside `b.iter()`
via `IoUringWriter::create`, which on CI runners exhausts RLIMIT_MEMLOCK
(each ring + registered buffers locks ~512 KB). When the writer bench
runs immediately after the reader bench, the kernel has not yet
reclaimed the reader's MEMLOCK pages, and the very first writer
iteration panics with `ENOMEM` from `io_uring_setup()`.

Hoist `IoUringWriter::create` outside `b.iter()` and reuse the ring
across iterations via `writer.write_all_batched(data, 0)`. This public
method takes an explicit offset and bypasses the internal write cursor,
so writing to offset 0 every iteration overwrites the file in place
rather than growing it -- so cross-iter reuse is safe. Same pattern as
PR #3575 used for the reader.

Removes the defensive `sample_size`/`warm_up_time`/`measurement_time`
bounds from PR #3575 since they are no longer needed: with one ring per
sub-benchmark we are now within RLIMIT_MEMLOCK regardless of iter count.

Unblocks the Criterion Benchmarks workflow on the v0.6.1 release.
@github-actions github-actions Bot added the bug Something isn't working label May 3, 2026
@oferchen
oferchen merged commit 4373b1b into master May 3, 2026
38 checks passed
@oferchen
oferchen deleted the fix/io-uring-bench-writer-hoist branch May 3, 2026 15:24
oferchen added a commit that referenced this pull request May 5, 2026
The io_uring write benchmark allocated a fresh ring inside `b.iter()`
via `IoUringWriter::create`, which on CI runners exhausts RLIMIT_MEMLOCK
(each ring + registered buffers locks ~512 KB). When the writer bench
runs immediately after the reader bench, the kernel has not yet
reclaimed the reader's MEMLOCK pages, and the very first writer
iteration panics with `ENOMEM` from `io_uring_setup()`.

Hoist `IoUringWriter::create` outside `b.iter()` and reuse the ring
across iterations via `writer.write_all_batched(data, 0)`. This public
method takes an explicit offset and bypasses the internal write cursor,
so writing to offset 0 every iteration overwrites the file in place
rather than growing it -- so cross-iter reuse is safe. Same pattern as
PR #3575 used for the reader.

Removes the defensive `sample_size`/`warm_up_time`/`measurement_time`
bounds from PR #3575 since they are no longer needed: with one ring per
sub-benchmark we are now within RLIMIT_MEMLOCK regardless of iter count.

Unblocks the Criterion Benchmarks workflow on the v0.6.1 release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant