forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust376.5 MiBcbbd6e2Tahinli2 months ago2369 commits

timeline.rs262 lines, 7.5 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/// Whether this frame's filters ran pipelined per superblock row.
169#[cold]
170pub(crate) fn note_pipe(pipe: bool) {
171 if !on() {
172 return;
173 }
174 let idx = CUR.with(std::cell::Cell::get);
175 if idx != usize::MAX {
176 rec(idx, |r| r.pipe = pipe);
177 }
178}
179
180/// The picture reached the sink. Only a frame that finished on a worker
181/// (`grain_done`) and has not been emitted yet takes the stamp -- a
182/// `show_existing_frame` re-output reports a decode index that is not its
183/// own, and must not overwrite another frame's row.
184#[cold]
185pub(crate) fn output(idx: usize) {
186 if !on() {
187 return;
188 }
189 let t = now_us();
190 with(|log| {
191 if let Some(r) = log.frames.get_mut(idx)
192 && r.output == 0
193 // A frame that finished on a worker, or the inline key frame
194 // (never submitted) that has passed its filters.
195 && (r.grain_done != 0 || (r.submit == 0 && r.filters_done != 0))
196 {
197 r.output = t;
198 }
199 });
200}
201
202/// The parsing thread blocked at the in-flight bound.
203#[cold]
204pub(crate) fn cap_wait(kind: &'static str, begin: u64, end: u64) {
205 if !on() {
206 return;
207 }
208 with(|log| log.caps.push((kind, begin, end)));
209}
210
211/// `now_us` for the two call sites that need a begin timestamp of their own.
212pub(crate) fn now() -> u64 {
213 if !on() {
214 return 0;
215 }
216 now_us()
217}
218
219/// Writes every row to stderr as TSV and clears the log.
220#[cold]
221pub(crate) fn dump() {
222 if !on() {
223 return;
224 }
225 let taken = LOG.lock().expect("timeline").take();
226 let Some(log) = taken else { return };
227 let mut out = String::new();
228 out.push_str(
229 "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",
230 );
231 for (idx, r) in log.frames.iter().enumerate() {
232 if !r.used {
233 continue;
234 }
235 let deps: Vec<String> = r.deps.iter().map(std::string::ToString::to_string).collect();
236 out.push_str(&format!(
237 "TL\t{idx}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
238 r.ftype,
239 u8::from(r.show),
240 r.submit,
241 r.start,
242 r.refs_ready,
243 r.hdr_done,
244 r.tile_done,
245 r.recon_done,
246 r.filters_done,
247 r.slot_fulfil,
248 r.grain_done,
249 r.output,
250 r.wait_slot,
251 r.wait_owner,
252 r.wait_us,
253 u8::from(r.pipe),
254 if deps.is_empty() { "-".to_string() } else { deps.join(",") },
255 ));
256 }
257 for (kind, b, e) in &log.caps {
258 out.push_str(&format!("TL_CAP\t{kind}\t{b}\t{e}\n"));
259 }
260 out.push_str(&format!("TL_END\t{}\n", now_us()));
261 eprint!("{out}");
262}