forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

timeline.rs250 lines, 7.3 KiBHistoryRawDownload
1//! lane-timeline: `EC_AV1_TIMELINE=1` frame-granularity decode timeline.
2//!
3//! The threaded decoder (`stream.rs`) is bounded by *something* -- reference
4//! latency, the dispatcher's in-flight cap, the filter tail or the output
5//! path -- and no instrument could tell which: `EC_AV1_WAVE_STATS` counts
6//! superblocks, `perf` counts instructions, neither sees the dependency
7//! graph. This records ~10 timestamps per coded frame (submit, worker start,
8//! reference waits, parse, recon, filters, slot publish, grain, output) on
9//! one monotonic clock and dumps them as TSV at stream end;
10//! `scripts/av1-timeline-report.py` reads that and walks the critical path.
11//!
12//! Cost when off: one relaxed atomic load ([`env_flag!`]'s `LazyLock<bool>`)
13//! per event, at frame granularity -- ~10 loads per frame, nothing per block.
14
15use std::sync::Mutex;
16use std::time::Instant;
17
18/// Process start, the zero of every timestamp below.
19static START: std::sync::LazyLock<Instant> = std::sync::LazyLock::new(Instant::now);
20
21/// `EC_AV1_TIMELINE` -- one atomic load per call site after the first.
22pub(crate) fn on() -> bool {
23 crate::envflags::env_flag!("EC_AV1_TIMELINE")
24}
25
26fn now_us() -> u64 {
27 START.elapsed().as_micros() as u64
28}
29
30/// One coded frame's timestamps, in microseconds since [`START`]; 0 = never
31/// reached (a key frame decodes inline, so it has no submit/start/publish).
32#[derive(Clone, Default)]
33struct Rec {
34 used: bool,
35 ftype: char,
36 show: bool,
37 submit: u64,
38 start: u64,
39 refs_ready: u64,
40 hdr_done: u64,
41 tile_done: u64,
42 recon_done: u64,
43 filters_done: u64,
44 slot_fulfil: u64,
45 grain_done: u64,
46 output: u64,
47 /// Slot this frame blocked on longest, which frame produced it, and
48 /// for how long.
49 wait_slot: i32,
50 wait_owner: i64,
51 wait_us: u64,
52 /// The producing frame of every slot it waited on, for the DAG.
53 deps: Vec<usize>,
54 pipe: bool,
55}
56
57#[derive(Default)]
58struct Log {
59 frames: Vec<Rec>,
60 /// Dispatcher blocks at the in-flight bound: (kind, begin, end).
61 caps: Vec<(&'static str, u64, u64)>,
62}
63
64static LOG: Mutex<Option<Log>> = Mutex::new(None);
65
66fn with<F: FnOnce(&mut Log)>(f: F) {
67 let mut g = LOG.lock().expect("timeline");
68 f(g.get_or_insert_with(Log::default));
69}
70
71fn rec<F: FnOnce(&mut Rec)>(idx: usize, f: F) {
72 with(|log| {
73 if log.frames.len() <= idx {
74 log.frames.resize(idx + 1, Rec { wait_slot: -1, wait_owner: -1, ..Rec::default() });
75 }
76 let r = &mut log.frames[idx];
77 r.used = true;
78 f(r);
79 });
80}
81
82// The frame this thread is decoding, so `decode.rs`'s phase marks need no
83// new parameter threaded through the tile decoders.
84thread_local! {
85 static CUR: std::cell::Cell<usize> = const { std::cell::Cell::new(usize::MAX) };
86}
87
88/// Dispatch (parsing thread), before the job is queued.
89#[cold]
90pub(crate) fn submit(idx: usize, ftype: char, show: bool) {
91 if !on() {
92 return;
93 }
94 let t = now_us();
95 rec(idx, |r| {
96 r.ftype = ftype;
97 r.show = show;
98 r.submit = t;
99 });
100}
101
102/// A worker picked the job up. Also binds this thread to `idx`.
103#[cold]
104pub(crate) fn start(idx: usize) {
105 if !on() {
106 return;
107 }
108 CUR.with(|c| c.set(idx));
109 let t = now_us();
110 rec(idx, |r| r.start = t);
111}
112
113/// Binds a thread to `idx` without a start timestamp (the inline key-frame
114/// path, which is dispatched by nobody).
115#[cold]
116pub(crate) fn bind(idx: usize, ftype: char, show: bool) {
117 if !on() {
118 return;
119 }
120 CUR.with(|c| c.set(idx));
121 rec(idx, |r| {
122 r.ftype = ftype;
123 r.show = show;
124 });
125}
126
127/// One resolved `SlotPromise::wait`, with the slot and how long it blocked.
128#[cold]
129pub(crate) fn ref_wait(idx: usize, slot: usize, owner: usize, waited_us: u64) {
130 if !on() {
131 return;
132 }
133 rec(idx, |r| {
134 if owner != usize::MAX && !r.deps.contains(&owner) {
135 r.deps.push(owner);
136 }
137 if waited_us > r.wait_us {
138 r.wait_us = waited_us;
139 r.wait_slot = slot as i32;
140 r.wait_owner = if owner == usize::MAX { -1 } else { owner as i64 };
141 }
142 });
143}
144
145macro_rules! stamp {
146 ($name:ident, $field:ident) => {
147 #[doc = concat!("Marks `", stringify!($field), "` for the frame this thread is decoding.")]
148 #[cold]
149 pub(crate) fn $name() {
150 if !on() {
151 return;
152 }
153 let (idx, t) = (CUR.with(std::cell::Cell::get), now_us());
154 if idx != usize::MAX {
155 rec(idx, |r| r.$field = t);
156 }
157 }
158 };
159}
160stamp!(refs_ready, refs_ready);
161stamp!(hdr_done, hdr_done);
162stamp!(tile_done, tile_done);
163stamp!(recon_done, recon_done);
164stamp!(filters_done, filters_done);
165stamp!(slot_fulfil, slot_fulfil);
166stamp!(grain_done, grain_done);
167
168/// The picture reached the sink. Only a frame that finished on a worker
169/// (`grain_done`) and has not been emitted yet takes the stamp -- a
170/// `show_existing_frame` re-output reports a decode index that is not its
171/// own, and must not overwrite another frame's row.
172#[cold]
173pub(crate) fn output(idx: usize) {
174 if !on() {
175 return;
176 }
177 let t = now_us();
178 with(|log| {
179 if let Some(r) = log.frames.get_mut(idx)
180 && r.output == 0
181 // A frame that finished on a worker, or the inline key frame
182 // (never submitted) that has passed its filters.
183 && (r.grain_done != 0 || (r.submit == 0 && r.filters_done != 0))
184 {
185 r.output = t;
186 }
187 });
188}
189
190/// The parsing thread blocked at the in-flight bound.
191#[cold]
192pub(crate) fn cap_wait(kind: &'static str, begin: u64, end: u64) {
193 if !on() {
194 return;
195 }
196 with(|log| log.caps.push((kind, begin, end)));
197}
198
199/// `now_us` for the two call sites that need a begin timestamp of their own.
200pub(crate) fn now() -> u64 {
201 if !on() {
202 return 0;
203 }
204 now_us()
205}
206
207/// Writes every row to stderr as TSV and clears the log.
208#[cold]
209pub(crate) fn dump() {
210 if !on() {
211 return;
212 }
213 let taken = LOG.lock().expect("timeline").take();
214 let Some(log) = taken else { return };
215 let mut out = String::new();
216 out.push_str(
217 "TL_HDR\tidx\ttype\tshow\tsubmit\tstart\trefs_ready\thdr_done\ttile_done\trecon_done\tfilters_done\tslot_fulfil\tgrain_done\toutput\twait_slot\twait_owner\twait_us\tpipe\tdeps\n",
218 );
219 for (idx, r) in log.frames.iter().enumerate() {
220 if !r.used {
221 continue;
222 }
223 let deps: Vec<String> = r.deps.iter().map(std::string::ToString::to_string).collect();
224 out.push_str(&format!(
225 "TL\t{idx}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
226 r.ftype,
227 u8::from(r.show),
228 r.submit,
229 r.start,
230 r.refs_ready,
231 r.hdr_done,
232 r.tile_done,
233 r.recon_done,
234 r.filters_done,
235 r.slot_fulfil,
236 r.grain_done,
237 r.output,
238 r.wait_slot,
239 r.wait_owner,
240 r.wait_us,
241 u8::from(r.pipe),
242 if deps.is_empty() { "-".to_string() } else { deps.join(",") },
243 ));
244 }
245 for (kind, b, e) in &log.caps {
246 out.push_str(&format!("TL_CAP\t{kind}\t{b}\t{e}\n"));
247 }
248 out.push_str(&format!("TL_END\t{}\n", now_us()));
249 eprint!("{out}");
250}