#[derive(Clone, Copy)]
#[repr(usize)]
pub enum Stage {
EncMdct,
EncPsy,
EncTns,
EncXpow,
EncEstimate,
EncQuant,
EncCodebook,
DecIcs,
DecSpectral,
DecDequant,
DecTools,
DecImdct,
DecSbrAnalysis,
DecSbrHf,
DecSbrSynthesis,
DecPs,
DecOutput,
}
pub const STAGES: [&str; 17] = [
"enc mdct (analyze_long/short)",
"enc psy (perceptual offsets)",
"enc tns analysis",
"enc xpow (|x|^0.75)",
"enc rate-loop estimate",
"enc quantize (code_core)",
"enc codebook select",
"dec ics side info (section/sf/tns)",
"dec spectral huffman",
"dec dequant (|q|^4/3 x gain, pns)",
"dec tools (ms/is/pred/ltp/tns)",
"dec imdct + window",
"dec sbr qmf analysis",
"dec sbr hf gen + adjust",
"dec sbr qmf synthesis",
"dec ps",
"dec output (interleave)",
];
#[derive(Clone, Copy)]
#[repr(usize)]
pub enum Kernel {
Quantize,
Xpow,
Fft,
FmulWindow,
QmfWindow,
}
pub const KERNELS: [&str; 5] = [
"quantize_band",
"xpow",
"radix2 fft",
"fmul_window",
"qmf_window",
];
#[derive(Clone, Copy)]
#[repr(usize)]
pub enum Counter {
ChannelFrames,
ShortFrames,
Codewords,
CodedCoeffs,
PnsCoeffs,
ZeroCoeffs,
TnsChannels,
SlowTuples,
EscTuples,
SymTuples,
}
pub const COUNTERS: [&str; 10] = [
"channel frames",
"eight-short channel frames",
"spectral codewords",
"huffman-coded coeffs",
"pns noise coeffs",
"zero/intensity coeffs",
"tns-active channel frames",
"spectral tuples via generic path",
"book-11 tuples with escapes",
" of the generic: codeword fits, signs do not",
];
#[cfg(feature = "profile")]
mod imp {
use super::{Counter, Kernel, Stage, COUNTERS, KERNELS, STAGES};
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
use std::time::Instant;
static NS: [AtomicU64; STAGES.len()] = [const { AtomicU64::new(0) }; STAGES.len()];
static CALLS: [AtomicU64; STAGES.len()] = [const { AtomicU64::new(0) }; STAGES.len()];
static SIMD: [AtomicU64; KERNELS.len()] = [const { AtomicU64::new(0) }; KERNELS.len()];
static SCALAR: [AtomicU64; KERNELS.len()] = [const { AtomicU64::new(0) }; KERNELS.len()];
thread_local! {
static COUNT: [std::cell::Cell<u64>; COUNTERS.len()] =
const { [const { std::cell::Cell::new(0) }; COUNTERS.len()] };
}
#[inline]
pub fn bump(c: Counter, n: usize) {
COUNT.with(|t| t[c as usize].set(t[c as usize].get() + n as u64));
}
#[must_use]
pub fn take_counters() -> Vec<(&'static str, u64)> {
COUNT.with(|t| {
(0..COUNTERS.len())
.map(|i| (COUNTERS[i], t[i].replace(0)))
.collect()
})
}
pub struct Scope(usize, Instant);
impl Drop for Scope {
fn drop(&mut self) {
NS[self.0].fetch_add(self.1.elapsed().as_nanos() as u64, Relaxed);
CALLS[self.0].fetch_add(1, Relaxed);
}
}
#[inline]
#[must_use]
pub fn scope(s: Stage) -> Scope {
Scope(s as usize, Instant::now())
}
#[inline]
pub fn count(k: Kernel, simd: bool, elems: usize) {
let t = if simd { &SIMD } else { &SCALAR };
t[k as usize].fetch_add(elems as u64, Relaxed);
}
pub type Row = (&'static str, u64, u64);
#[must_use]
pub fn take() -> (Vec<Row>, Vec<Row>) {
let stages = (0..STAGES.len())
.map(|i| (STAGES[i], NS[i].swap(0, Relaxed), CALLS[i].swap(0, Relaxed)))
.collect();
let kernels = (0..KERNELS.len())
.map(|i| {
(
KERNELS[i],
SIMD[i].swap(0, Relaxed),
SCALAR[i].swap(0, Relaxed),
)
})
.collect();
(stages, kernels)
}
}
#[cfg(not(feature = "profile"))]
mod imp {
use super::{Counter, Kernel, Stage};
pub struct Scope;
#[inline(always)]
pub fn bump(_: Counter, _: usize) {}
#[inline(always)]
pub fn scope(_: Stage) -> Scope {
Scope
}
#[inline(always)]
pub fn count(_: Kernel, _: bool, _: usize) {}
}
pub use imp::*;