33#ifdef SBL_PROFILING_ENABLED
40 static constexpr size_t HIST_BUCKETS = 11;
45 void init(uint32_t sample_rate, uint16_t block_size, uint32_t cpu_hz) {
46#ifdef SBL_PROFILING_ENABLED
47 budget_ = (cpu_hz / sample_rate) * block_size;
50 (void)sample_rate; (void)block_size; (void)cpu_hz;
56#ifdef SBL_PROFILING_ENABLED
63#ifdef SBL_PROFILING_ENABLED
64 uint32_t elapsed =
cycles() - start_;
67 if (elapsed > max_) max_ = elapsed;
68 if (elapsed < min_) min_ = elapsed;
69 if (elapsed > budget_) ++overruns_;
72 avg_ += (
static_cast<float>(elapsed) - avg_) * (1.0f / 128.0f);
75 uint32_t pct = (elapsed * 100) / budget_;
76 uint32_t bucket = pct / 10;
77 if (bucket >= HIST_BUCKETS) bucket = HIST_BUCKETS - 1;
86#ifdef SBL_PROFILING_ENABLED
87 return avg_ /
static_cast<float>(budget_);
95#ifdef SBL_PROFILING_ENABLED
96 return static_cast<float>(max_) /
static_cast<float>(budget_);
104#ifdef SBL_PROFILING_ENABLED
105 return static_cast<float>(min_) /
static_cast<float>(budget_);
113#ifdef SBL_PROFILING_ENABLED
114 return static_cast<uint32_t
>(avg_);
121#ifdef SBL_PROFILING_ENABLED
129#ifdef SBL_PROFILING_ENABLED
137#ifdef SBL_PROFILING_ENABLED
146#ifdef SBL_PROFILING_ENABLED
155#ifdef SBL_PROFILING_ENABLED
165 static constexpr size_t hist_size() {
return HIST_BUCKETS; }
169#ifdef SBL_PROFILING_ENABLED
170 return (i < HIST_BUCKETS) ? hist_[i] : 0;
178#ifdef SBL_PROFILING_ENABLED
184 for (
auto& b : hist_) b = 0;
189#ifdef SBL_PROFILING_ENABLED
191 uint32_t budget_ = 0;
195 uint32_t overruns_ = 0;
198 uint32_t hist_[HIST_BUCKETS] = {};
float avg_load() const
Average CPU load as fraction [0.0, 1.0+].
uint32_t overruns() const
Number of callbacks that exceeded the cycle budget.
void on_block_start()
Mark the start of audio processing (call first in callback)
uint32_t min_cycles() const
uint32_t avg_cycles() const
Raw cycle counts.
uint32_t peak_cycles() const
static constexpr size_t hist_size()
Number of histogram buckets.
float min_load() const
Minimum CPU load since last reset.
float peak_load() const
Peak (worst-case) CPU load since last reset.
uint32_t count() const
Total callbacks measured.
void on_block_end()
Mark the end of audio processing (call last in callback)
void init(uint32_t sample_rate, uint16_t block_size, uint32_t cpu_hz)
void reset()
Reset statistics (budget preserved)
uint32_t budget_cycles() const
uint32_t hist_bucket(size_t i) const
Read histogram bucket count (0-indexed, last bucket is >100%)
DWT cycle counter for ARM Cortex-M7.