Files
leleka/crates/model-format/examples/bulk_read_bench.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 40fc3eab2b Fast cold load: large-block, deep-queue reads for the sealed archive copy, the expert bank and the hash passes
The cold start of the 5090 server is dominated by reading two things: the 55 GB KAR archive (copied into the sealed memfd) and the 50 GB of
routed-expert tensors of shard 0 (copied into the page-locked bank). A plain read() loop asks the NVMe for one 128 KiB readahead window
at a time (queue depth 1-2; the drive's max_sectors_kb is 128): 8-35 MB/s after a reboot, 2003.8 s for the first holder
(docs/experiments/baseline-pair-5090.md section 9).

* model-format/bulk_read.rs (new, no unsafe): N threads each read blocks of 8 MiB. Direct (default): O_DIRECT through the open file's
  /proc/self/fd entry (no path lookup) into 4 KiB-aligned bounce buffers, so the page cache is never touched and nothing has to be evicted;
  Advise: buffered pread with POSIX_FADV_WILLNEED ahead and DONTNEED behind (a bounded window); Plain: large pread + DONTNEED. Direct falls
  back to Advise where O_DIRECT is refused. Memory is threads x 8 MiB. Switches for A/B in one binary: LELEKA_LOAD_READ=direct|advise|plain,
  LELEKA_LOAD_THREADS, LELEKA_LOAD_CHUNK_MIB, LELEKA_LOAD_AHEAD_MIB; LELEKA_LOAD_READ=legacy keeps the old sequential loop for the bank.
* deploy/sealed.rs: the memfd fill uses it (seal_file_report also returns how the file was read; seal_file_with_identity is unchanged).
* gguf.rs read_file_span_into: spans of 64 MiB and more (the expert tensors) use it; smaller spans and every failure mode are as before.
* readahead.rs (the SHA-256/CRC passes of a first verification): WILLNEED 512 MiB ahead of the reader thread.
* examples/bulk_read_bench.rs + bulk_read_digest.py: offline cold-read measurement with a reproducible digest (SHA-256 over the SHA-256 of
  consecutive blocks), device-level read/write accounting so a shared disk shows.

Measured on rnr5090 (Lexar NM610PRO, DRAM-less, HMB; kernel 7.0.0-38; another agent's jobs share the drive, so every figure carries its
"other" volume; page cache of the span evicted first with posix_fadvise(DONTNEED), never drop_caches):
* whole KAR file, 51.5 GiB, new default (direct, 32 x 8 MiB): 152.9 s (362 MB/s) and 159.8 s (346 MB/s, with the digest), nothing else on the
  drive; Cached 62 -> 59 GiB (no cache growth). Target < 5 min met.
* old patterns, same day: one thread, 16-32 MiB reads (the holder's pattern) 65, 86, 105, 154 MB/s on 2-8 GiB cold slices (55 GB in 6-14 min);
  8 threads x 32 MiB buffered (the old sealed.rs pattern) 240 MB/s while sharing the drive; the recorded cold-after-reboot holder 2003.8 s.
* strategy matrix (4 rounds x 7 configs, fresh 2 GiB regions, MB/s per round, median of 4): advise 16x16 MiB 230 574 194 1126 (402);
  direct 32x8 185 554 1133 113 (369); advise 32x8 277 454 229 510 (365); plain 16x16 385 257 979 207 (321); direct 16x8 180 311 351 174 (245);
  direct 8x32 149 317 271 138 (210); one thread 65 201 123 86 (105). The drive's speed varies 10x between regions and moments whatever the
  strategy, so the multi-thread configs are indistinguishable here; every one of them beats one thread. Direct is the default because it
  leaves the page cache untouched (nothing to evict, the machine's free-memory state is not changed), not because it is faster.
* identity: an 8 GiB slice read with the new code has the same block-digest chain as a plain sequential pass in Python
  (8b8b21b830cf938b1f0cec60a5a8127d2489e33e1f853cd792b18497b2b081a6, both); unit tests compare every strategy with the source for aligned and
  ragged spans. The engine's own SHA-256/CRC check of the sealed memfd and the replay gate are in the GPU run recorded in docs/unattended.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-02 20:54:28 +01:00

99 lines
3.5 KiB
Rust

//! Offline measurement of `leleka_model_format::bulk_read` on a real (cold) file; not part of the product.
//!
//! bulk_read_bench PATH START_MIB LEN_MIB STRATEGY THREADS CHUNK_MIB [--digest] [--no-evict-first] (LEN_MIB 0: to the end of the file)
//!
//! Evicts the span first (`posix_fadvise(DONTNEED)`, never `drop_caches`), reads it with the chosen strategy and prints the rate,
//! the device-level read volume (so a disk shared with another job is visible) and, with `--digest`, a SHA-256 over the
//! SHA-256 digests of the consecutive CHUNK_MIB blocks from START (reproducible by `bulk_read_digest.py`).
use leleka_model_format::bulk_read::{Plan, Strategy, evict, for_each_chunk};
use sha2::{Digest, Sha256};
use std::{fs::File, sync::Mutex};
/// (bytes read, bytes written) by the whole device so far.
fn device_bytes() -> Option<(u64, u64)> {
let text = std::fs::read_to_string("/sys/block/nvme0n1/stat").ok()?;
let field = |n: usize| -> Option<u64> {
Some(text.split_whitespace().nth(n)?.parse::<u64>().ok()? * 512)
};
Some((field(2)?, field(6)?))
}
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 7 {
eprintln!(
"usage: bulk_read_bench PATH START_MIB LEN_MIB direct|advise|plain THREADS CHUNK_MIB [--digest] [--no-evict-first]"
);
std::process::exit(2);
}
let file = File::open(&args[1]).expect("open");
let start = args[2].parse::<u64>().expect("START_MIB") << 20;
let len = match args[3].parse::<u64>().expect("LEN_MIB") {
0 => file.metadata().expect("metadata").len() - start,
mib => mib << 20,
};
let strategy = match args[4].as_str() {
"direct" => Strategy::Direct,
"advise" => Strategy::Advise,
_ => Strategy::Plain,
};
let chunk = args[6].parse::<usize>().expect("CHUNK_MIB") << 20;
let plan = Plan {
strategy,
threads: args[5].parse().expect("THREADS"),
chunk,
ahead: 1 << 30,
};
let digest = args.iter().any(|a| a == "--digest");
if !args.iter().any(|a| a == "--no-evict-first") {
evict(&file, start, len);
}
let digests: Mutex<Vec<(u64, [u8; 32])>> = Mutex::new(Vec::new());
let before = device_bytes();
let report = for_each_chunk(&file, start, len, plan, |offset, bytes| {
if digest {
let d: [u8; 32] = Sha256::digest(bytes).into();
digests.lock().unwrap().push((offset, d));
}
Ok(())
})
.expect("read");
let after = device_bytes();
let device = match (before, after) {
(Some(b), Some(a)) => format!(
" device {:.0} MB/s other-read {:.1} GB written {:.2} GB",
(a.0 - b.0) as f64 / report.elapsed.as_secs_f64() / 1e6,
(a.0 - b.0).saturating_sub(len) as f64 / 1e9,
(a.1 - b.1) as f64 / 1e9
),
_ => String::new(),
};
print!(
"{:7} threads {:3} chunk {:3} MiB: {:6.1} s {:7.0} MB/s{}",
report.strategy.as_str(),
plan.threads,
chunk >> 20,
report.elapsed.as_secs_f64(),
report.mb_per_s(),
device
);
if digest {
let mut list = digests.into_inner().unwrap();
list.sort();
let mut outer = Sha256::new();
for (_, d) in &list {
outer.update(d);
}
print!(
" digest {}",
outer
.finalize()
.iter()
.map(|b| format!("{b:02x}"))
.collect::<String>()
);
}
println!();
evict(&file, start, len);
}