117 lines
4.2 KiB
Rust
117 lines
4.2 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, Report, 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)?))
|
|
}
|
|
|
|
/// What the command line asks for.
|
|
struct Job {
|
|
file: File,
|
|
start: u64,
|
|
len: u64,
|
|
plan: Plan,
|
|
digest: bool,
|
|
evict_first: bool,
|
|
}
|
|
|
|
fn parse_job(args: &[String]) -> Job {
|
|
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");
|
|
let evict_first = !args.iter().any(|a| a == "--no-evict-first");
|
|
Job { file, start, len, plan, digest, evict_first }
|
|
}
|
|
|
|
fn device_note(before: Option<(u64, u64)>, after: Option<(u64, u64)>, report: &Report, len: u64) -> String {
|
|
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(),
|
|
}
|
|
}
|
|
|
|
fn print_digest(digests: Mutex<Vec<(u64, [u8; 32])>>) {
|
|
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>());
|
|
}
|
|
|
|
fn print_rate(report: &Report, plan: Plan, device: &str) {
|
|
print!(
|
|
"{:7} threads {:3} chunk {:3} MiB: {:6.1} s {:7.0} MB/s{}",
|
|
report.strategy.as_str(),
|
|
plan.threads,
|
|
plan.chunk >> 20,
|
|
report.elapsed.as_secs_f64(),
|
|
report.mb_per_s(),
|
|
device
|
|
);
|
|
}
|
|
|
|
fn main() {
|
|
let args: Vec<String> = std::env::args().collect();
|
|
let job = parse_job(&args);
|
|
let (file, start, len, plan, digest) = (&job.file, job.start, job.len, job.plan, job.digest);
|
|
if job.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 = device_note(before, after, &report, len);
|
|
print_rate(&report, plan, &device);
|
|
if digest {
|
|
print_digest(digests);
|
|
}
|
|
println!();
|
|
evict(file, start, len);
|
|
}
|