Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
section.hpp
Go to the documentation of this file.
1/**
2 * @file section.hpp
3 * @brief Per-section cycle profiling with EWMA smoothing
4 *
5 * Measures CPU cycles consumed by named code sections in audio callbacks.
6 * Uses integer EWMA (alpha = 1/128, shift-based) for low-overhead smoothing.
7 *
8 * Gated behind SBL_PROFILING_ENABLED. When disabled, SectionProfile::avg
9 * is a static constexpr zero so format strings compile without #ifdef.
10 *
11 * Usage:
12 * static sbl::profiling::SectionProfile s_prof_osc;
13 * static sbl::profiling::SectionProfile s_prof_svf;
14 *
15 * void audio_callback(...) {
16 * SBL_PROF_MARK(t);
17 * // ... oscillator rendering ...
18 * SBL_PROF_SECTION(s_prof_osc, t);
19 * // ... filter processing ...
20 * SBL_PROF_SECTION(s_prof_svf, t);
21 * }
22 */
23#pragma once
24#include <cstdint>
25
26#ifdef SBL_PROFILING_ENABLED
27#include <sbl/profiling/dwt.hpp>
28#endif
29
30namespace sbl::profiling {
31
32#ifdef SBL_PROFILING_ENABLED
33
34struct SectionProfile {
35 uint32_t avg = 0; // EWMA accumulator (alpha = 1/128)
36
37 void update(uint32_t cyc) {
38 avg = avg - (avg >> 7) + (cyc >> 7);
39 }
40};
41
42#else
43
45 static constexpr uint32_t avg = 0;
46
47 void update(uint32_t) {}
48};
49
50#endif
51
52} // namespace sbl::profiling
53
54/// Mark a timestamp variable for section profiling
55#ifdef SBL_PROFILING_ENABLED
56#define SBL_PROF_MARK(var) uint32_t var = sbl::profiling::cycles()
57#else
58#define SBL_PROF_MARK(var) (void)0
59#endif
60
61/// Measure cycles since 'start', update section profile, advance start
62#ifdef SBL_PROFILING_ENABLED
63#define SBL_PROF_SECTION(section, start) do { \
64 uint32_t _end = sbl::profiling::cycles(); \
65 section.update(_end - start); \
66 start = _end; \
67} while(0)
68#else
69#define SBL_PROF_SECTION(section, start) (void)0
70#endif
DWT cycle counter for ARM Cortex-M7.
CPU load monitoring.
static constexpr uint32_t avg
Definition section.hpp:45