28#ifndef SBL_COMMON_DIAGNOSTICS_AUDIO_BUDGET_HPP_
29#define SBL_COMMON_DIAGNOSTICS_AUDIO_BUDGET_HPP_
35#ifdef SBL_PROFILING_ENABLED
42 static constexpr size_t HIST_BUCKETS = 11;
47 void init(uint32_t sample_rate, uint16_t block_size) {
48#ifdef SBL_PROFILING_ENABLED
49 budget_ = (
tick_rate() / sample_rate) * block_size;
52 (void)sample_rate; (void)block_size;
58#ifdef SBL_PROFILING_ENABLED
65#ifdef SBL_PROFILING_ENABLED
66 uint32_t elapsed =
ticks() - start_;
69 if (elapsed > max_) max_ = elapsed;
70 if (elapsed < min_) min_ = elapsed;
71 if (elapsed > budget_) ++overruns_;
74 avg_ += (
static_cast<float>(elapsed) - avg_) * (1.0f / 128.0f);
77 uint32_t pct = (elapsed * 100) / budget_;
78 uint32_t bucket = pct / 10;
79 if (bucket >= HIST_BUCKETS) bucket = HIST_BUCKETS - 1;
88#ifdef SBL_PROFILING_ENABLED
89 return avg_ /
static_cast<float>(budget_);
97#ifdef SBL_PROFILING_ENABLED
98 return static_cast<float>(max_) /
static_cast<float>(budget_);
106#ifdef SBL_PROFILING_ENABLED
107 return static_cast<float>(min_) /
static_cast<float>(budget_);
115#ifdef SBL_PROFILING_ENABLED
116 return static_cast<uint32_t
>(avg_);
123#ifdef SBL_PROFILING_ENABLED
131#ifdef SBL_PROFILING_ENABLED
139#ifdef SBL_PROFILING_ENABLED
148#ifdef SBL_PROFILING_ENABLED
157#ifdef SBL_PROFILING_ENABLED
167 static constexpr size_t hist_size() {
return HIST_BUCKETS; }
171#ifdef SBL_PROFILING_ENABLED
172 return (i < HIST_BUCKETS) ? hist_[i] : 0;
180#ifdef SBL_PROFILING_ENABLED
186 for (
auto& b : hist_) b = 0;
191#ifdef SBL_PROFILING_ENABLED
193 uint32_t budget_ = 0;
197 uint32_t overruns_ = 0;
200 uint32_t hist_[HIST_BUCKETS] = {};
void init(uint32_t sample_rate, uint16_t block_size)
uint32_t overruns() const
Number of callbacks that exceeded the tick budget.
void reset()
Reset statistics (budget preserved)
void on_block_start()
Mark the start of audio processing (call first in callback)
void on_block_end()
Mark the end of audio processing (call last in callback)
uint32_t budget_ticks() const
static constexpr size_t hist_size()
Number of histogram buckets.
uint32_t count() const
Total callbacks measured.
float avg_load() const
Average load as fraction [0.0, 1.0+].
uint32_t avg_ticks() const
Raw tick counts.
float min_load() const
Minimum load since last reset.
uint32_t peak_ticks() const
float peak_load() const
Peak (worst-case) load since last reset.
uint32_t min_ticks() const
uint32_t hist_bucket(size_t i) const
Read histogram bucket count (0-indexed, last bucket is >100%)
Diagnostics and profiling.
uint32_t ticks()
Read current tick count (free-running, wraps at 2^32).
constexpr uint32_t tick_rate()
Tick rate in Hz (ticks per second).
Tick counter interface for profiling.