scripts/dev/split_module.py (lexer-based, with 16 self-tests) moves the top-level fn and impl items of 59 oversize module files in control-plane, desktop-core, model-format, gateway-text, leleka-memory, experimental-ref, context, serving-policy, tokenizer, backend-api, quant-core, harmony, igpu, libc-shim, npu-service and runtime-client into foo/<name>.rs groups of about 250 lines; types, constants, macros, nested modules and tests stay, every path stays valid through pub use re-exports, restricted visibilities keep their scope one level down, include paths and super:: paths are shifted. Not touched: leleka-runtime, cuda-driver, native-cpu-expert, sampler-ref and cpu-* (hot path, next wave) and the files kar-off-ab edits. The baseline drops from 311 to 263 oversize files and from 273,600 to about 233,000 lines; the path-include ratchet ignores dir/name.rs child declarations.
96 lines
3.3 KiB
Rust
96 lines
3.3 KiB
Rust
//! Large-block, high-queue-depth reads of a cold file, with the page cache kept out of the way.
|
|
//!
|
|
//! A cold start reads two things that are tens of gigabytes long: the KAR archive (copied once into a sealed memfd) and
|
|
//! the routed-expert tensors of shard 0 (copied into the page-locked bank). A plain `read()` loop asks the device for
|
|
//! one 128 KiB readahead window at a time (queue depth 1-2); on the 5090 server's NVMe that ran at 8-35 MB/s while
|
|
//! the same drive does well over 1 GB/s when many requests are in flight (`docs/unattended.md`, "Fast cold load").
|
|
//! Here several threads each read large aligned blocks, so the device sees a deep queue.
|
|
//!
|
|
//! Three strategies, one result (the bytes are the file's, in every strategy):
|
|
//!
|
|
//! * [`Strategy::Direct`]: `O_DIRECT` into 4 KiB-aligned bounce buffers. The page cache is never touched, so nothing
|
|
//! has to be evicted afterwards and the machine's free-memory state (huge-page blocks) is left alone.
|
|
//! * [`Strategy::Advise`]: buffered `pread` with `posix_fadvise(WILLNEED)` kept ahead of the readers and
|
|
//! `posix_fadvise(DONTNEED)` behind them, so the cache holds a bounded window, never the file.
|
|
//! * [`Strategy::Plain`]: buffered `pread` of large blocks and eviction behind (the fallback where the other two are
|
|
//! refused, and the reference for tests).
|
|
//!
|
|
//! `Direct` falls back to `Advise` when the file system refuses `O_DIRECT` (tmpfs, some network and FUSE mounts); the
|
|
//! fall-back is reported in [`Report::strategy`]. Memory is bounded by `threads * (chunk + 8 KiB)`. No `unsafe`.
|
|
//! Nothing here decides what a digest means: a short read that is not the end of the file, or a file that ends early,
|
|
//! is an error, as with `read_exact`.
|
|
use std::{
|
|
fs::File,
|
|
io,
|
|
sync::{
|
|
Mutex,
|
|
atomic::{AtomicU64, Ordering},
|
|
},
|
|
thread,
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
/// Alignment of `O_DIRECT` offsets, lengths and buffers (the logical block size is never larger on supported devices).
|
|
const ALIGN: usize = 4096;
|
|
|
|
/// Spans below this are not worth the threads: callers keep their ordinary read for them.
|
|
pub const MIN_PARALLEL_BYTES: u64 = 64 << 20;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Strategy {
|
|
Direct,
|
|
Advise,
|
|
Plain,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub struct Plan {
|
|
pub strategy: Strategy,
|
|
/// Reader threads (each has one block in flight).
|
|
pub threads: usize,
|
|
/// Bytes per block (a multiple of 4096).
|
|
pub chunk: usize,
|
|
/// `Advise` only: how far ahead of the readers `WILLNEED` runs.
|
|
pub ahead: u64,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub struct Report {
|
|
/// The strategy that actually ran (`Direct` becomes `Advise` where `O_DIRECT` is refused).
|
|
pub strategy: Strategy,
|
|
pub bytes: u64,
|
|
pub elapsed: Duration,
|
|
}
|
|
|
|
/// A byte buffer whose start is 4 KiB-aligned, without `unsafe`.
|
|
struct Aligned {
|
|
raw: Vec<u8>,
|
|
start: usize,
|
|
len: usize,
|
|
}
|
|
|
|
struct Shared<'a, F> {
|
|
file: &'a File,
|
|
direct: Option<File>,
|
|
strategy: Strategy,
|
|
start: u64,
|
|
len: u64,
|
|
file_len: u64,
|
|
chunk: u64,
|
|
ahead: u64,
|
|
next: AtomicU64,
|
|
advised_to: AtomicU64,
|
|
failure: Mutex<Option<io::Error>>,
|
|
sink: &'a F,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
mod impl_strategy;
|
|
#[allow(unused_imports)]
|
|
pub use impl_strategy::*;
|
|
mod for_each_chunk;
|
|
#[allow(unused_imports)]
|
|
pub use for_each_chunk::*;
|