Sound Byte Libs
0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
audio_budget.hpp
Go to the documentation of this file.
1
/**
2
* @file audio_budget.hpp
3
* @brief Audio callback cycle budget tracker
4
*/
5
6
#ifndef SBL_SYSTEM_PROFILING_AUDIO_BUDGET_HPP_
7
#define SBL_SYSTEM_PROFILING_AUDIO_BUDGET_HPP_
8
9
#include <cstddef>
10
#include <cstdint>
11
#include <
sbl/system/profiling/registry.hpp
>
12
13
#ifdef SBL_PROFILING_ENABLED
14
#include <
sbl/system/profiling/cycles.hpp
>
15
#endif
16
17
namespace
sbl::system::profiling
{
18
19
/**
20
* @brief Cycle budget of the audio callback: load, peak, minimum, overruns and a load histogram
21
*
22
* Measures per-block cycle consumption in audio callbacks using the
23
* platform timing backend. Tracks average (EWMA), peak, min, overrun count,
24
* and a per-block load histogram (11 buckets, 10% each).
25
*
26
* A budget registered with register_as() is an entry of the profiling
27
* registry (registry.hpp): the reporter and a reader over SWD find it there,
28
* and the end of each of its blocks is where the registry closes a window a
29
* reader asked for. The histogram stays in the budget.
30
*
31
* Gated behind SBL_PROFILING_ENABLED. When the define is absent, all
32
* methods compile to no-ops and the class has no data members. An app's
33
* build defines it with the CMake option SBL_PROFILING
34
* (tools/cmake/SblHooks.cmake).
35
*
36
* The cycle counter has to be started before the budget measures anything:
37
* sbl::system::profiling::init() (cycles.hpp), once at startup after clock
38
* configuration. The budget does not start it. With the counter stopped,
39
* every block reads zero cycles.
40
*
41
* Usage:
42
* @code
43
* #include <sbl/system/profiling/audio_budget.hpp>
44
* #include <sbl/system/profiling/cycles.hpp>
45
*
46
* static sbl::system::profiling::AudioBudget budget;
47
*
48
* // At startup, after clock configuration:
49
* sbl::system::profiling::init(); // start the cycle counter
50
* budget.init(48000, 48);
51
* budget.register_as("audio"); // into the registry, for the reporter
52
*
53
* void audio_callback(int32_t* tx, const int32_t* rx, uint16_t frames) {
54
* budget.on_block_start();
55
* // ... DSP ...
56
* budget.on_block_end();
57
* }
58
*
59
* // In control loop:
60
* budget.avg_load() // [0.0, 1.0+]
61
* budget.peak_load() // worst-case since the last reset() or reset_window()
62
* budget.overruns() // count of callbacks that exceeded budget
63
* budget.reset_window() // not registered: peak, min and overruns start over
64
* @endcode
65
*/
66
class
AudioBudget
{
67
static
constexpr
size_t
HIST_BUCKETS = 11;
68
69
public
:
70
/**
71
* Configure budget from audio parameters.
72
* budget_cycles = (cycle_rate() / sample_rate) * block_size
73
*/
74
void
init
(uint32_t sample_rate, uint16_t block_size) {
75
#ifdef SBL_PROFILING_ENABLED
76
slot_.stats().limit = (
cycle_rate
() / sample_rate) * block_size;
77
reset
();
78
#else
79
(void)sample_rate;
80
(void)block_size;
81
#endif
82
}
83
84
/**
85
* Enter the registry under `name`, with the statistics so far. False if
86
* already registered or the table is full. Startup only.
87
*/
88
[[nodiscard]]
bool
register_as
(
const
char
* name) {
89
#ifdef SBL_PROFILING_ENABLED
90
return
slot_.enroll(Kind::Budget, name);
91
#else
92
(void)name;
93
return
true
;
94
#endif
95
}
96
97
/** Mark the start of audio processing (call first in callback) */
98
void
on_block_start
() {
99
#ifdef SBL_PROFILING_ENABLED
100
start_ =
cycles
();
101
#endif
102
}
103
104
/** Mark the end of audio processing (call last in callback) */
105
void
on_block_end
() {
106
#ifdef SBL_PROFILING_ENABLED
107
const
uint32_t elapsed =
cycles
() - start_;
108
Entry
& stats = slot_.stats();
109
stats.
measure
(elapsed);
110
if
(elapsed > stats.
limit
) {
111
++stats.
overruns
;
112
}
113
114
// Histogram: bucket index = load_percent / 10, clamped to [0, HIST_BUCKETS-1]
115
uint32_t pct = (elapsed * 100) / stats.
limit
;
116
uint32_t bucket = pct / 10;
117
if
(bucket >= HIST_BUCKETS) {
118
bucket = HIST_BUCKETS - 1;
119
}
120
hist_[bucket]++;
121
122
if
(slot_.registered()) {
123
window_point();
// a block is whole: close the window a reader asked for
124
}
125
#endif
126
}
127
128
// Queries (safe to call from control context)
129
130
/** Average load as fraction [0.0, 1.0+] */
131
float
avg_load
()
const
{
132
#ifdef SBL_PROFILING_ENABLED
133
return
slot_.stats().avg /
static_cast<
float
>
(slot_.stats().limit);
134
#else
135
return
0.0f;
136
#endif
137
}
138
139
/** Peak (worst-case) load since the last reset() or reset_window() */
140
float
peak_load
()
const
{
141
#ifdef SBL_PROFILING_ENABLED
142
return
static_cast<
float
>
(peak_cycles()) /
static_cast<
float
>
(slot_.stats().limit);
143
#else
144
return
0.0f;
145
#endif
146
}
147
148
/** Minimum load since the last reset() or reset_window(); 1 before the first block */
149
float
min_load
()
const
{
150
#ifdef SBL_PROFILING_ENABLED
151
return
static_cast<
float
>
(min_cycles()) /
static_cast<
float
>
(slot_.stats().limit);
152
#else
153
return
0.0f;
154
#endif
155
}
156
157
/** Raw cycle counts */
158
uint32_t
avg_cycles
()
const
{
159
#ifdef SBL_PROFILING_ENABLED
160
return
static_cast<
uint32_t
>
(slot_.stats().avg);
161
#else
162
return
0;
163
#endif
164
}
165
166
uint32_t peak_cycles()
const
{
167
#ifdef SBL_PROFILING_ENABLED
168
return
slot_.stats().max;
169
#else
170
return
0;
171
#endif
172
}
173
174
uint32_t min_cycles()
const
{
175
#ifdef SBL_PROFILING_ENABLED
176
return
slot_.stats().min ==
NO_MINIMUM
? slot_.stats().limit : slot_.stats().min;
177
#else
178
return
0;
179
#endif
180
}
181
182
uint32_t budget_cycles()
const
{
183
#ifdef SBL_PROFILING_ENABLED
184
return
slot_.stats().limit;
185
#else
186
return
0;
187
#endif
188
}
189
190
/** Number of callbacks that exceeded the cycle budget since the last reset() or reset_window() */
191
uint32_t
overruns
()
const
{
192
#ifdef SBL_PROFILING_ENABLED
193
return
slot_.stats().overruns;
194
#else
195
return
0;
196
#endif
197
}
198
199
/** Total callbacks measured */
200
uint32_t
count
()
const
{
201
#ifdef SBL_PROFILING_ENABLED
202
return
slot_.stats().count;
203
#else
204
return
0;
205
#endif
206
}
207
208
// Histogram (10% buckets: 0-10%, 10-20%, ..., 90-100%, >100%)
209
210
/** Number of histogram buckets */
211
static
constexpr
size_t
hist_size
() {
return
HIST_BUCKETS; }
212
213
/** Read histogram bucket count (0-indexed, last bucket is >100%) */
214
uint32_t
hist_bucket
(
size_t
i)
const
{
215
#ifdef SBL_PROFILING_ENABLED
216
return
(i < HIST_BUCKETS) ? hist_[i] : 0;
217
#else
218
(void)i;
219
return
0;
220
#endif
221
}
222
223
/**
224
* Start a new reporting window: the peak, the minimum and the overrun
225
* count start over. The average, the block count and the histogram
226
* carry on. For a budget that is not registered; the registry does it
227
* for one that is, at the end of a block (registry.hpp).
228
*/
229
void
reset_window
() {
230
#ifdef SBL_PROFILING_ENABLED
231
slot_.stats().open_window();
232
#endif
233
}
234
235
/** Reset statistics (budget preserved) */
236
void
reset
() {
237
#ifdef SBL_PROFILING_ENABLED
238
slot_.stats().clear();
239
for
(
auto
& b : hist_) {
240
b = 0;
241
}
242
#endif
243
}
244
245
private
:
246
#ifdef SBL_PROFILING_ENABLED
247
detail::Slot slot_{};
248
uint32_t start_ = 0;
249
uint32_t hist_[HIST_BUCKETS] = {};
250
#endif
251
};
252
253
}
// namespace sbl::system::profiling
254
255
#endif
// SBL_SYSTEM_PROFILING_AUDIO_BUDGET_HPP_
sbl::system::profiling::AudioBudget
Cycle budget of the audio callback: load, peak, minimum, overruns and a load histogram.
Definition
audio_budget.hpp:66
sbl::system::profiling::AudioBudget::init
void init(uint32_t sample_rate, uint16_t block_size)
Definition
audio_budget.hpp:74
sbl::system::profiling::AudioBudget::on_block_end
void on_block_end()
Definition
audio_budget.hpp:105
sbl::system::profiling::AudioBudget::register_as
bool register_as(const char *name)
Definition
audio_budget.hpp:88
sbl::system::profiling::AudioBudget::reset
void reset()
Definition
audio_budget.hpp:236
sbl::system::profiling::AudioBudget::hist_bucket
uint32_t hist_bucket(size_t i) const
Definition
audio_budget.hpp:214
sbl::system::profiling::AudioBudget::peak_load
float peak_load() const
Definition
audio_budget.hpp:140
sbl::system::profiling::AudioBudget::min_load
float min_load() const
Definition
audio_budget.hpp:149
sbl::system::profiling::AudioBudget::reset_window
void reset_window()
Definition
audio_budget.hpp:229
sbl::system::profiling::AudioBudget::hist_size
static constexpr size_t hist_size()
Definition
audio_budget.hpp:211
sbl::system::profiling::AudioBudget::overruns
uint32_t overruns() const
Definition
audio_budget.hpp:191
sbl::system::profiling::AudioBudget::count
uint32_t count() const
Definition
audio_budget.hpp:200
sbl::system::profiling::AudioBudget::avg_cycles
uint32_t avg_cycles() const
Definition
audio_budget.hpp:158
sbl::system::profiling::AudioBudget::avg_load
float avg_load() const
Definition
audio_budget.hpp:131
sbl::system::profiling::AudioBudget::on_block_start
void on_block_start()
Definition
audio_budget.hpp:98
cycles.hpp
The cycle counter behind the profiling tools.
sbl::system::profiling
Profiling: the cycle counter, the audio budget, section profiles and reports.
Definition
audio_budget.hpp:17
sbl::system::profiling::cycles
uint32_t cycles()
Definition
cycles.hpp:39
sbl::system::profiling::NO_MINIMUM
constexpr uint32_t NO_MINIMUM
Definition
registry.hpp:52
sbl::system::profiling::cycle_rate
constexpr uint32_t cycle_rate()
Definition
cycles.hpp:42
registry.hpp
The profiling registry: every budget, section and counter in one table.
sbl::system::profiling::Entry
Definition
registry.hpp:65
sbl::system::profiling::Entry::measure
void measure(uint32_t cycles)
Record one measurement: the count, the running average (alpha 1/128), the window's extremes.
Definition
registry.hpp:92
sbl::system::profiling::Entry::limit
uint32_t limit
Definition
registry.hpp:73
sbl::system::profiling::Entry::overruns
uint32_t overruns
Definition
registry.hpp:72
src
sbl
system
profiling
audio_budget.hpp
Generated by
1.9.8