forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

mkv_open_cost.rs101 lines, 3.9 KiBHistoryRawDownload
1//! What one open of a video file costs -- wall time and bytes read through the
2//! read syscalls (`/proc/self/io`'s `rchar`) -- and what it answers: the frame
3//! count, and where a seek to a given second lands.
4//!
5//! `cargo run --release -p engine --example mkv_open_cost -- <file> [secs ...] [all]`
6//!
7//! The point of the byte count: a Matroska open that walks every cluster reads
8//! the whole segment's element headers, which on a multi-gigabyte film is
9//! hundreds of megabytes of `pread` and seconds of wall clock. One that reads
10//! the header, the `SeekHead` and the `Cues` reads kilobytes. Same tool on both
11//! builds, so the two are comparable line for line.
12//!
13//! `all` walks every access unit of the file and prints a rolling hash of their
14//! bytes with the count: two builds that disagree by one block anywhere disagree
15//! on that number, which is what makes "the lazy index is the same index" a
16//! check rather than a claim. The per-second lines do the same for a seek --
17//! landing index and the hash of the access unit handed back there.
18
19use std::path::PathBuf;
20use std::time::Instant;
21
22/// Bytes this process has read through `read`/`pread` so far, page cache or
23/// disk -- what the walk really costs the kernel, independent of what was warm.
24fn rchar() -> u64 {
25 std::fs::read_to_string("/proc/self/io")
26 .ok()
27 .and_then(|io| {
28 io.lines()
29 .find_map(|l| l.strip_prefix("rchar: ")?.trim().parse().ok())
30 })
31 .unwrap_or(0)
32}
33
34/// FNV-1a over the bytes, so a run of access units collapses to one number a
35/// diff can compare.
36fn fnv(hash: &mut u64, bytes: &[u8]) {
37 for &b in bytes {
38 *hash ^= u64::from(b);
39 *hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
40 }
41}
42
43fn main() {
44 let mut args = std::env::args().skip(1);
45 let path = PathBuf::from(
46 args.next()
47 .expect("usage: mkv_open_cost <file> [secs ...] [all]"),
48 );
49 let rest: Vec<String> = args.collect();
50 let walk_all = rest.iter().any(|a| a == "all");
51 let seeks: Vec<f64> = rest.iter().filter_map(|a| a.parse().ok()).collect();
52
53 let before = rchar();
54 let started = Instant::now();
55 let (meta, mut demuxer) = engine::demux::Demuxer::open(&path).expect("open");
56 let open_ms = started.elapsed().as_secs_f64() * 1e3;
57 let open_bytes = rchar() - before;
58 println!("file {}", path.display());
59 println!("open {open_ms:.1} ms, {open_bytes} bytes read");
60 println!(
61 "meta {}x{} @ {:.6} fps, {} frames",
62 meta.width, meta.height, meta.frame_rate, meta.frame_count
63 );
64
65 for secs in seeks {
66 let frame = (secs * meta.frame_rate).round() as u32;
67 let before = rchar();
68 let started = Instant::now();
69 let landed = demuxer.seek_to_sync_at_or_before(frame);
70 let au = demuxer.next_access_unit().expect("read after seek");
71 let ms = started.elapsed().as_secs_f64() * 1e3;
72 let bytes = rchar() - before;
73 let mut hash = 0xcbf2_9ce4_8422_2325u64;
74 let len = au.as_ref().map_or(0, |au| au.len());
75 if let Some(au) = &au {
76 fnv(&mut hash, au);
77 }
78 println!(
79 "seek {secs}s -> frame {frame}: landed {landed}, au {len} bytes, hash {hash:016x}, \
80 {ms:.1} ms, {bytes} bytes read"
81 );
82 }
83
84 if walk_all {
85 let before = rchar();
86 let started = Instant::now();
87 demuxer.seek_to_sync_at_or_before(0);
88 let mut hash = 0xcbf2_9ce4_8422_2325u64;
89 let (mut count, mut bytes) = (0u64, 0u64);
90 while let Some(au) = demuxer.next_access_unit().expect("read") {
91 fnv(&mut hash, &au);
92 bytes += au.len() as u64;
93 count += 1;
94 }
95 println!(
96 "walk {count} access units, {bytes} bytes, hash {hash:016x}, {:.1} ms, {} bytes read",
97 started.elapsed().as_secs_f64() * 1e3,
98 rchar() - before
99 );
100 }
101}