forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

alloc_timing.rs105 lines, 3.8 KiBHistoryRawDownload
1//! Decode wall time and minor page faults for one `EC_AV1_PLANE_ALLOC` arm.
2//!
3//! The three closures lane-uballoc had to choose between (see
4//! `lanes/av1uballoc.report.md`) are selected by `EC_AV1_PLANE_ALLOC` inside
5//! the crate, not by a build flag -- see [`ec_av1`]'s `fresh_plane`. Measuring
6//! them that way means all arms share one binary, one allocator state and one
7//! machine, which three separate builds cannot do.
8//!
9//! ```text
10//! EC_AV1_PLANE_ALLOC=zeroed cargo run -p ec-av1 --release --example alloc_timing -- s.obu
11//! ```
12//!
13//! One process does ONE decode and prints ONE line, so the harness interleaves
14//! arms across processes instead of trusting one process's warm-up history:
15//!
16//! ```text
17//! ALLOC_TIMING arm=zeroed frames=24 wall_ms=1234.567 minflt=61234 majflt=0 hash=8f2a...
18//! ```
19//!
20//! `hash` is FNV-1a over every picture's Y/U/V samples in decode order. It is
21//! the cross-arm determinism evidence: identical `hash` on every arm means the
22//! closure chosen does not move a single output sample.
23use std::time::Instant;
24
25const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
26const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
27
28/// Minor/major faults from `/proc/self/stat` fields 10 and 12, read around the
29/// decode. A zeroed-from-the-kernel allocation and an explicit memset differ in
30/// how many pages they actually touch, and that is the only cheap proxy for it
31/// that does not need `perf`.
32fn faults() -> (u64, u64) {
33 let stat = std::fs::read_to_string("/proc/self/stat").unwrap_or_default();
34 // Everything before the last ')' is pid + comm, and comm may hold spaces.
35 let Some(rest) = stat.rsplit_once(')').map(|(_, r)| r) else {
36 return (0, 0);
37 };
38 let f: Vec<&str> = rest.split_whitespace().collect();
39 // `rest` starts at field 3 (state); minflt is field 10, majflt field 12.
40 (
41 f.get(7).and_then(|v| v.parse().ok()).unwrap_or(0),
42 f.get(9).and_then(|v| v.parse().ok()).unwrap_or(0),
43 )
44}
45
46fn fnv1a(mut h: u64, bytes: &[u8]) -> u64 {
47 for &b in bytes {
48 h ^= u64::from(b);
49 h = h.wrapping_mul(FNV_PRIME);
50 }
51 h
52}
53
54fn main() {
55 let mut args = std::env::args().skip(1);
56 let Some(path) = args.next() else {
57 eprintln!("usage: alloc_timing <stream.obu>");
58 std::process::exit(2);
59 };
60 let arm = std::env::var("EC_AV1_PLANE_ALLOC").unwrap_or_else(|_| "<unset>".into());
61 let data = match std::fs::read(&path) {
62 Ok(d) => d,
63 Err(e) => {
64 eprintln!("{path}: {e}");
65 std::process::exit(2);
66 }
67 };
68
69 // One warm-up decode is NOT done here on purpose: the plane allocations are
70 // freed back to the allocator between frames anyway, and a discarded decode
71 // would only hand one arm a warmer arena than another. The harness runs
72 // each arm many times and reports the median.
73 let (minflt0, majflt0) = faults();
74 let t0 = Instant::now();
75 let mut frames = 0usize;
76 let mut hash = FNV_OFFSET;
77 let r = ec_av1::stream::decode_stream_with(&data, |pic, _idx, shown| {
78 if !shown {
79 return Ok(());
80 }
81 frames += 1;
82 for p in [&pic.y, &pic.u, &pic.v] {
83 let mut buf = Vec::with_capacity(p.len() * 2);
84 for &s in p.iter() {
85 buf.extend_from_slice(&s.to_le_bytes());
86 }
87 hash = fnv1a(hash, &buf);
88 }
89 Ok(())
90 });
91 let wall = t0.elapsed();
92 let (minflt1, majflt1) = faults();
93 match r {
94 Ok(()) => println!(
95 "ALLOC_TIMING arm={arm} frames={frames} wall_ms={:.3} minflt={} majflt={} hash={hash:016x}",
96 wall.as_secs_f64() * 1e3,
97 minflt1.saturating_sub(minflt0),
98 majflt1.saturating_sub(majflt0),
99 ),
100 Err(e) => {
101 eprintln!("{path}: decode refused: {e}");
102 std::process::exit(3);
103 }
104 }
105}