44#ifndef SBL_SYSTEM_PROFILING_REPORT_HPP_
45#define SBL_SYSTEM_PROFILING_REPORT_HPP_
57#ifdef SBL_PROFILING_ENABLED
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";
67inline void entry_tag(
const Entry& e,
char* tag,
size_t size) {
69 for (
const char* p = TAG; *p !=
'\0' && n + 2 < size; ++p) {
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;
81inline uint32_t rounded_cycles(
float cycles) {
return static_cast<uint32_t
>(std::lround(cycles)); }
84inline float load_percent(
float cycles, uint32_t limit) {
85 return limit > 0 ? PERCENT *
cycles /
static_cast<float>(limit) : 0.0f;
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));
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);
120 line.template write<Output>();
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;
148class TaggedReporter {
151 static constexpr uint32_t DEFAULT_PERIOD_MS = 1000;
153 explicit constexpr TaggedReporter(uint32_t period_ms = DEFAULT_PERIOD_MS) : period_ms_(period_ms) {}
155 void set_period_ms(uint32_t period_ms) { period_ms_ = period_ms; }
163 template<
typename Output>
164 bool poll(uint32_t now_ms) {
170 if (now_ms - last_ms_ < period_ms_) {
174 request_ = request_window();
177 if (!window_ready(request_)) {
181 write_tagged_report<Output>();
187 uint32_t last_ms_ = 0;
188 uint32_t request_ = 0;
189 bool started_ =
false;
190 bool waiting_ =
false;
197 static constexpr uint32_t DEFAULT_PERIOD_MS = 1000;
199 explicit constexpr TaggedReporter(uint32_t = DEFAULT_PERIOD_MS) {}
201 static void set_period_ms(uint32_t) {}
203 template<
typename Output>
204 static bool poll(uint32_t) {
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"};
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);
230 if (
static_cast<size_t>(pos) < size - 1) {
231 size_t remaining = size -
static_cast<size_t>(pos);
Audio callback cycle budget tracker.
One #TAG key=value ... line in a fixed buffer.
TaggedLine & field(const char *key, float value, uint8_t decimals=3)
A float field, rounded to decimals places.
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()
int format(char *buf, size_t size, const char *fmt,...)
Format a string into a buffer (snprintf-style)
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.
constexpr size_t NAME_SIZE
The profiling registry: every budget, section and counter in one table.
Tagged output lines: a builder and a throttled value report.