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
report.hpp
Go to the documentation of this file.
1/**
2 * @file report.hpp
3 * @brief The profiling registry as tagged `#PROF` lines
4 *
5 * TaggedReporter writes every entry of the registry (registry.hpp), one
6 * window per period, so an app does not list what it measures. Each period
7 * it asks the registry for a window, and once the measured side has closed
8 * it, writes the window:
9 *
10 * @code
11 * #PROF cpu_avg=20.5 cpu_peak=21.1 cpu_min=17.3 overruns=0 blocks=48000
12 * #PROF.OSC cycles=85119 min=84012 max=91700 n=1000
13 * #PROF.EDGES count=1423 total=68310
14 * @endcode
15 *
16 * - The first budget is the `#PROF` line: the running average, and the
17 * window's peak and minimum, as percentages of its limit; the window's
18 * overruns; the blocks since the start. A further budget writes the same
19 * fields as `#PROF.<NAME>`.
20 * - A section writes `#PROF.<NAME>`: its running average in cycles, the
21 * window's smallest and largest pass, and the passes in the window.
22 * - A counter writes `#PROF.<NAME>`: the events in the window and since the
23 * start.
24 *
25 * The name is the registered name in upper case. The lines are
26 * tagged-output lines and go to the log sink, so the reporter
27 * belongs to the main loop.
28 *
29 * format_tagged_histogram() writes a budget's load histogram as a `#HIST`
30 * line.
31 *
32 * Gated behind SBL_PROFILING_ENABLED: without it poll() does nothing and
33 * returns false.
34 *
35 * Usage:
36 * @code
37 * static sbl::system::profiling::TaggedReporter reporter; // a line a second
38 *
39 * while (true) { // main loop
40 * reporter.poll<SBL_LOG_OUTPUT>(sbl::system::timer::millis());
41 * }
42 * @endcode
43 */
44#ifndef SBL_SYSTEM_PROFILING_REPORT_HPP_
45#define SBL_SYSTEM_PROFILING_REPORT_HPP_
46
47#include <cmath>
48#include <cstddef>
49#include <cstdint>
54
55namespace sbl::system::profiling {
56
57#ifdef SBL_PROFILING_ENABLED
58
59namespace detail {
60
61inline constexpr size_t REPORT_LINE_SIZE = 128;
62inline constexpr float PERCENT = 100.0f;
63inline constexpr uint8_t LOAD_DECIMALS = 1;
64inline constexpr char TAG[] = "PROF";
65
66/** "PROF.<NAME>": the entry's name in upper case under the profile tag. */
67inline void entry_tag(const Entry& e, char* tag, size_t size) {
68 size_t n = 0;
69 for (const char* p = TAG; *p != '\0' && n + 2 < size; ++p) {
70 tag[n++] = *p;
71 }
72 tag[n++] = '.';
73 for (size_t i = 0; i < NAME_SIZE && e.name[i] != '\0' && n + 1 < size; ++i) {
74 const char c = e.name[i];
75 tag[n++] = (c >= 'a' && c <= 'z') ? static_cast<char>(c - 'a' + 'A') : c;
76 }
77 tag[n] = '\0';
78}
79
80/** A running average in cycles, to the nearest cycle. */
81inline uint32_t rounded_cycles(float cycles) { return static_cast<uint32_t>(std::lround(cycles)); }
82
83/** A cycle count as a percentage of a budget's limit. */
84inline float load_percent(float cycles, uint32_t limit) {
85 return limit > 0 ? PERCENT * cycles / static_cast<float>(limit) : 0.0f;
86}
87
88} // namespace detail
89
90/**
91 * @brief Write one entry's window as its `#PROF` line
92 *
93 * The caller holds the window: window_ready() has returned true and no
94 * later window is asked for.
95 *
96 * @param e The entry
97 * @param summary True for the budget that is the app's load: the line is `#PROF` itself
98 */
99template<typename Output>
100void write_tagged_entry(const Entry& e, bool summary) {
101 char tag[sizeof(detail::TAG) + NAME_SIZE] = {};
102 detail::entry_tag(e, tag, sizeof(tag));
103 sbl::log::TaggedLine<detail::REPORT_LINE_SIZE> line{summary ? detail::TAG : tag};
104 if (e.is(Kind::Budget)) {
105 line.field("cpu_avg", detail::load_percent(e.window_avg, e.limit), detail::LOAD_DECIMALS)
106 .field("cpu_peak", detail::load_percent(static_cast<float>(e.window_max), e.limit), detail::LOAD_DECIMALS)
107 .field("cpu_min", detail::load_percent(static_cast<float>(e.window_min), e.limit), detail::LOAD_DECIMALS)
108 .field("overruns", e.window_overruns)
109 .field("blocks", e.window_base);
110 } else if (e.is(Kind::Section)) {
111 line.field("cycles", detail::rounded_cycles(e.window_avg))
112 .field("min", e.window_min)
113 .field("max", e.window_max)
114 .field("n", e.window_count);
115 } else if (e.is(Kind::Counter)) {
116 line.field("count", e.window_count).field("total", e.window_base);
117 } else {
118 return;
119 }
120 line.template write<Output>();
121}
122
123/**
124 * @brief Write the last window of every entry, in registration order
125 *
126 * The caller holds the window, as for write_tagged_entry().
127 */
128template<typename Output>
129void write_tagged_report() {
130 bool summary_written = false;
131 for (uint32_t i = 0; i < entry_count(); ++i) {
132 const Entry& e = *entry(i);
133 const bool summary = !summary_written && e.is(Kind::Budget);
134 write_tagged_entry<Output>(e, summary);
135 summary_written = summary_written || summary;
136 }
137}
138
139/**
140 * @brief The registry as `#PROF` lines, one window per period
141 *
142 * @context Main loop only.
143 *
144 * The first report comes a period after the first poll. A
145 * report waits for the measured side to close the window, so nothing is
146 * written while no block ends.
147 */
148class TaggedReporter {
149public:
150 /** A report a second. */
151 static constexpr uint32_t DEFAULT_PERIOD_MS = 1000;
152
153 explicit constexpr TaggedReporter(uint32_t period_ms = DEFAULT_PERIOD_MS) : period_ms_(period_ms) {}
154
155 void set_period_ms(uint32_t period_ms) { period_ms_ = period_ms; }
156
157 /**
158 * @brief Ask for a window when the period has passed; write it once it is closed
159 *
160 * @param now_ms A millisecond clock; wraparound is handled
161 * @return true if a report was written
162 */
163 template<typename Output>
164 bool poll(uint32_t now_ms) {
165 if (!started_) {
166 started_ = true;
167 last_ms_ = now_ms;
168 }
169 if (!waiting_) {
170 if (now_ms - last_ms_ < period_ms_) {
171 return false;
172 }
173 last_ms_ = now_ms;
174 request_ = request_window();
175 waiting_ = true;
176 }
177 if (!window_ready(request_)) {
178 return false;
179 }
180 waiting_ = false;
181 write_tagged_report<Output>();
182 return true;
183 }
184
185private:
186 uint32_t period_ms_;
187 uint32_t last_ms_ = 0;
188 uint32_t request_ = 0;
189 bool started_ = false;
190 bool waiting_ = false;
191};
192
193#else
194
196public:
197 static constexpr uint32_t DEFAULT_PERIOD_MS = 1000;
198
199 explicit constexpr TaggedReporter(uint32_t = DEFAULT_PERIOD_MS) {}
200
201 static void set_period_ms(uint32_t) {}
202
203 template<typename Output>
204 static bool poll(uint32_t) {
205 return false;
206 }
207};
208
209#endif
210
211/**
212 * @brief Format CPU load histogram as a tagged output line
213 *
214 * Emits a single `#HIST` line with 11 bucket counts (10% each, last is >100%).
215 *
216 * @return Number of characters written
217 */
218inline int format_tagged_histogram(char* buf, size_t size, const AudioBudget& budget) {
219#ifdef SBL_PROFILING_ENABLED
220 static const char* labels[] = {"b0_10", "b10_20", "b20_30", "b30_40", "b40_50", "b50_60",
221 "b60_70", "b70_80", "b80_90", "b90_100", "over"};
222
223 int pos = sbl::log::format(buf, size, "#HIST");
224
225 for (size_t i = 0; i < budget.hist_size() && static_cast<size_t>(pos) < size - 1; ++i) {
226 size_t remaining = size - static_cast<size_t>(pos);
227 pos += sbl::log::format(buf + pos, remaining, " %s=%u", labels[i], budget.hist_bucket(i));
228 }
229
230 if (static_cast<size_t>(pos) < size - 1) {
231 size_t remaining = size - static_cast<size_t>(pos);
232 pos += sbl::log::format(buf + pos, remaining, "\r\n");
233 }
234
235 return pos;
236#else
237 (void)budget;
238 if (size > 0) {
239 buf[0] = '\0';
240 }
241 return 0;
242#endif
243}
244
245} // namespace sbl::system::profiling
246
247#endif // SBL_SYSTEM_PROFILING_REPORT_HPP_
Audio callback cycle budget tracker.
One #TAG key=value ... line in a fixed buffer.
Definition tagged.hpp:59
TaggedLine & field(const char *key, float value, uint8_t decimals=3)
A float field, rounded to decimals places.
Definition tagged.hpp:82
Cycle budget of the audio callback: load, peak, minimum, overruns and a load histogram.
uint32_t hist_bucket(size_t i) const
static constexpr size_t hist_size()
Minimal safe string formatting for embedded systems.
int format(char *buf, size_t size, const char *fmt,...)
Format a string into a buffer (snprintf-style)
Definition format.hpp:265
Profiling: the cycle counter, the audio budget, section profiles and reports.
int format_tagged_histogram(char *buf, size_t size, const AudioBudget &budget)
Format CPU load histogram as a tagged output line.
Definition report.hpp:218
constexpr size_t NAME_SIZE
Definition registry.hpp:51
The profiling registry: every budget, section and counter in one table.
Tagged output lines: a builder and a throttled value report.