Files
leleka/crates/model-format/examples/bulk_read_bench.rs
T

85 lines
3.4 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);
}