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
section.hpp
Go to the documentation of this file.
1/**
2 * @file section.hpp
3 * @brief Per-section cycle profiling: a running average and a window's extremes
4 *
5 * Measures the cycles a named stretch of code takes each pass: a running
6 * average (EWMA, alpha 1/128), and the smallest and the largest pass of the
7 * window. A registered section is an entry of the registry (registry.hpp),
8 * which closes its windows; one that is not registered restarts its
9 * extremes with reset_window().
10 *
11 * Gated behind SBL_PROFILING_ENABLED. Without it the class is empty, its
12 * methods compile to nothing and the macros expand to nothing.
13 *
14 * Usage:
15 * @code
16 * static sbl::system::profiling::SectionProfile prof_osc;
17 * static sbl::system::profiling::SectionProfile prof_svf;
18 *
19 * // At startup:
20 * prof_osc.register_as("osc");
21 * prof_svf.register_as("svf");
22 *
23 * void audio_callback(...) {
24 * SBL_PROF_MARK(t);
25 * // ... oscillator rendering ...
26 * SBL_PROF_SECTION(prof_osc, t);
27 * // ... filter processing ...
28 * SBL_PROF_SECTION(prof_svf, t);
29 * }
30 * @endcode
31 */
32#ifndef SBL_SYSTEM_PROFILING_SECTION_HPP_
33#define SBL_SYSTEM_PROFILING_SECTION_HPP_
34
35#include <cmath>
36#include <cstdint>
38
39namespace sbl::system::profiling {
40
41#ifdef SBL_PROFILING_ENABLED
42
43class SectionProfile {
44public:
45 /** Enter the registry under `name`. False if already registered or the table is full. Startup only. */
46 [[nodiscard]] bool register_as(const char* name) { return slot_.enroll(Kind::Section, name); }
47
48 /** One pass of the section, in cycles. */
49 void update(uint32_t cycles) { slot_.stats().measure(cycles); }
50
51 /** The running average, to the nearest cycle. */
52 uint32_t avg() const { return static_cast<uint32_t>(std::lround(slot_.stats().avg)); }
53
54 /** The largest pass of the open window, in cycles. */
55 uint32_t max() const { return slot_.stats().max; }
56
57 /** The smallest pass of the open window, in cycles; 0 before the first. */
58 uint32_t min() const { return slot_.stats().min == NO_MINIMUM ? 0 : slot_.stats().min; }
59
60 /** Passes since the start. */
61 uint32_t count() const { return slot_.stats().count; }
62
63 /** Start the extremes over. For a section that is not registered; the registry does it for one that is. */
64 void reset_window() { slot_.stats().open_window(); }
65
66 /** Forget every statistic. */
67 void reset() { slot_.stats().clear(); }
68
69private:
70 detail::Slot slot_{};
71};
72
73#else
74
76public:
77 [[nodiscard]] static bool register_as(const char*) { return true; }
78 static void update(uint32_t) {}
79 static uint32_t avg() { return 0; }
80 static uint32_t max() { return 0; }
81 static uint32_t min() { return 0; }
82 static uint32_t count() { return 0; }
83 static void reset_window() {}
84 static void reset() {}
85};
86
87#endif
88
89} // namespace sbl::system::profiling
90
91/** Mark a timestamp variable for section profiling */
92#ifdef SBL_PROFILING_ENABLED
93#define SBL_PROF_MARK(var) uint32_t var = sbl::system::profiling::cycles()
94#else
95#define SBL_PROF_MARK(var) (void)0
96#endif
97
98/** Measure cycles since 'start', update section profile, advance start */
99#ifdef SBL_PROFILING_ENABLED
100#define SBL_PROF_SECTION(section, start) \
101 do { \
102 uint32_t _end = sbl::system::profiling::cycles(); \
103 (section).update(_end - (start)); \
104 (start) = _end; \
105 } while (0)
106#else
107#define SBL_PROF_SECTION(section, start) (void)0
108#endif
109
110#endif // SBL_SYSTEM_PROFILING_SECTION_HPP_
Profiling: the cycle counter, the audio budget, section profiles and reports.
constexpr uint32_t NO_MINIMUM
Definition registry.hpp:52
The profiling registry: every budget, section and counter in one table.