forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

prof.rs64 lines, 2.0 KiBHistoryRawDownload
1//! Phase timers for the decoder, enabled by `EC_VP9_PROFILE=1`.
2//!
3//! Each phase accumulates CPU nanoseconds across all threads (atomic adds), so with
4//! tile threads the sums can exceed wall time. Off by default: `start` is then one
5//! relaxed atomic load and no clock read.
6
7use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};
8use std::time::Instant;
9
10/// Phases, indexes into [`TOTALS`].
11#[derive(Clone, Copy)]
12pub(crate) enum Phase {
13 /// Whole tile walk (parse plus reconstruction).
14 Walk = 0,
15 /// Inter prediction (motion compensation) for all planes of a block.
16 Mc = 1,
17 /// Coefficient token decoding.
18 Tokens = 2,
19 /// Inverse transform and add.
20 Idct = 3,
21 /// Loop filter over the whole frame.
22 LoopFilter = 4,
23 /// Tile-thread overhead: worker state setup and copying columns back.
24 Threads = 5,
25}
26
27/// Names for [`report`], in [`Phase`] order.
28pub const NAMES: [&str; 6] = ["walk", "mc", "tokens", "idct", "loopfilter", "threads"];
29
30static TOTALS: [AtomicU64; 6] = [const { AtomicU64::new(0) }; 6];
31
32/// Whether `EC_VP9_PROFILE` turned the timers on (read once per process).
33pub fn enabled() -> bool {
34 static STATE: AtomicU8 = AtomicU8::new(0);
35 match STATE.load(Ordering::Relaxed) {
36 0 => {
37 let on = std::env::var_os("EC_VP9_PROFILE").is_some_and(|v| v != "0");
38 STATE.store(if on { 2 } else { 1 }, Ordering::Relaxed);
39 on
40 }
41 s => s == 2,
42 }
43}
44
45/// Start a timer; `None` when profiling is off.
46pub(crate) fn start() -> Option<Instant> {
47 if enabled() {
48 Some(Instant::now())
49 } else {
50 None
51 }
52}
53
54/// Add the time since `t` to `phase`.
55pub(crate) fn stop(phase: Phase, t: Option<Instant>) {
56 if let Some(t) = t {
57 TOTALS[phase as usize].fetch_add(t.elapsed().as_nanos() as u64, Ordering::Relaxed);
58 }
59}
60
61/// Accumulated seconds per phase, in [`NAMES`] order.
62pub fn totals() -> [f64; 6] {
63 std::array::from_fn(|i| TOTALS[i].load(Ordering::Relaxed) as f64 * 1e-9)
64}