Skip to content

Sequential read: 141 Store demands for 64 MiB on base (read-ahead re-opens windows under concurrent FUSE reads) #779

Backlog

Description

@mengmengmengqiang

Corrected 2026-11-24: The 141-demand measurement reproduces on the base on both engines, but the causal claim below is wrong: BATCH_BYTES is GC-mark-only and 1/4/8 MiB candidates all still measure 141/140. The read path re-opens the 8 MiB read-ahead window when the two FUSE receive workers deliver reads out of order. See the measurement and correction comment. The 20-demand figure at 910f686c is confirmed by its commit message.


What

mounted_sequential_read measures the Store demands of one 64 MiB sequential read through a real kernel mount. On the current layerfs_v2_0_0 (and on main) it takes about 141 Store demands, while the test's bound is 64 — so the test fails on the base itself, before any p4 work.

Why (base regression, not snapshot rollback)

d19d98f138 ("perf(gc): mark reads record headers, not pack bodies", 2026-11-17) is already in layerfs_v2_0_0 (verified: git merge-base --is-ancestor d19d98f138 up/release). It caps one object-demand call at

  • engine/crates/layerfs-persistence/src/history/objects/edges.rs:55 — BATCH_BYTES = 512 * 1024 (with BATCH_RANGES = 1024 at :53),

so a 64 MiB read is ~64 MiB / 512 KiB = 128 data demands plus ~13 metadata demands ≈ 141, on both SQLite and PostgreSQL. The earlier 20-demand figure belongs to 910f686c (2026-11-06), before that cap existed; the workspace client still asks for 8 MiB read-ahead streams, but the persistence path splits them at 512 KiB.

PR #770 only changed the test bound (64 → 192) and, after review, restored the release's 64 so the regression stays visible. It does not introduce the extra demands.

What to do

Pick one, with a measurement on both engines (the FUSE test needs a privileged host with /dev/fuse):

  1. Raise/adapt the batch: let one call carry a sequential read-ahead window (e.g. 4–8 MiB), or make BATCH_BYTES grow with the read-ahead stream while keeping BATCH_RANGES as the hard cap. Measure demands and RSS (MOUNTED_READ prints rss_before_kb/vm_hwm_kb); 4 MiB should land near 20–30 demands.
  2. Keep 512 KiB and lower the test bound to reflect the new batching, documenting the memory/latency trade (this changes the contract, so it needs the perf rationale from d19d98f138).

PR #770 keeps the 64 bound, so it will report this failure until the base is fixed; it is not a regression of that PR. Tracked here so the fix lands on the base rather than in the p4 PR.

References

Activity

  1. mengmengmengqiang commented on Nov 24, 2026

    @mengmengmengqiang
    ContributorAuthor

    Correction: 141 is real on the base, but the 512 KiB batch is not the cause

    I re-ran mounted_sequential_read on throwaway builds of 87e46c65 (up/release) and on base + one-constant candidates. 141 Store demands reproduce on both engines, but the stated mechanism does not: BATCH_BYTES is used only by the GC mark's read_ranges, and changing it does not move the count.

    Verified statically

    • up/release = 87e46c652a7c9caf80859f1ceb8dfddfaee10f36.
    • git merge-base --is-ancestor d19d98f138 87e46c65 -> 0: d19d98f138 is in the base.
    • Base edges.rs: BATCH_RANGES = 1024 (:53), BATCH_BYTES = 512 * 1024 (:55).
    • mounted_sequential_read.rs: MOST_DEMANDS = 64 (:31); "Store demands" is the delta of store.work().object_batches, i.e. one counted StorePorts::read_canonical_batch (one StoreReader.objects call); one kernel read is 128 KiB.
    • The 20-demand figure is recorded in the 910f686c commit message: 541 -> 20 on both engines.

    BATCH_BYTES is reached only from wave_edges (mark walk, walk.rs) and group_directories (kept-waste accounting, waste.rs). The mounted read path is BaseRead::emit -> CanonicalClient::read_through/read_ahead -> StorePorts::read_canonical_batch -> StoreReader.objects -> storage.read_objects; it never calls edges.rs, so the 8 MiB read-ahead stream is not split at 512 KiB.

    Measured (base 87e46c6)

    Container dsh:current, --privileged --network host, -v /home/dell/.dsh/layerfs/scratch:/scratch -v /home/dell/.cargo:/home/dell/.cargo, fresh CARGO_TARGET_DIR per tree, TMPDIR=/scratch/tmp779; PostgreSQL 17 on 127.0.0.1:55432.

    build (base 87e46c6 +) engine Store demands object ids SQL stmts rss_before_kb vm_hwm_kb <= 64
    none sqlite 141 3478 498 100308 174716 no
    none postgresql 141 3478 284 95456 169652 no
    BATCH_BYTES = 1 MiB sqlite 141 3478 497 100980 175400 no
    BATCH_BYTES = 4 MiB sqlite 141 3478 498 100356 173476 no
    BATCH_BYTES = 8 MiB sqlite 140 3478 499 100000 174568 no
    containment fix (below) sqlite 20 3478 314 100184 175532 yes
    containment fix (below) postgresql 27 3495 82 107832 182528 yes

    Base repeats: 141, 141, 141, 140, 134 (the 134 run had eprintln instrumentation only). Each variant was a real rebuild: the build log recompiles layerfs-persistence and every dependent crate.

    Actual cause

    Instrumented base: 127 read_through calls, every one with until == start; 127 fetch_ahead calls of 21-37 objects (~25 = the uncached tail of a re-opened 8 MiB window), upstream_batches 137-143. The mount has two receive workers (RECEIVE_SLOTS = 2; logs show ThreadId(11) and ThreadId(12)), so 128 KiB READs arrive out of order. Streams::follow only continues a stream on exact adjacency (next == start); an out-of-order continuation opens a fresh stream whose until is its own end, and the next read that matches it re-opens the full 8 MiB window. fetch_ahead filters already-cached chunks, so each re-opened window costs one demand for its ~25-object tail: 127 tails + ~13 metadata demands ~= 141.

    Recommendation

    Do not raise or adapt BATCH_BYTES: no constant on that path is involved (1/4/8 MiB all measure 141/140). Fix the stream match instead: when no exact continuation exists, follow a window of the same root that already contains start (until > start) rather than opening a new one. A 12-line throwaway prototype on the base is memory-neutral (same 8 MiB READ_AHEAD, same 16 MiB READ_AHEAD_CACHE, same 64 MiB object cache) and lands at 20 SQLite / 27 PostgreSQL demands - the 910f686c range - with SQL statements also down (498 -> 314 SQLite, 284 -> 82 PostgreSQL). RSS/VmHWM stay in the same 100/175 MB band on SQLite (PostgreSQL +~13 MB in the single run; no cache or window size changed).

    The test bound stays 64. I changed no release ref and pushed nothing; the branch, worktree and container used for this are throwaway and have been removed.

  2. changed the title [-]Sequential read: 512 KiB object batches cost ~141 Store demands for 64 MiB (base regression since d19d98f138)[/-] [+]Sequential read: 141 Store demands for 64 MiB on base (read-ahead re-opens windows under concurrent FUSE reads)[/+] on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions