30#ifndef SBL_SYSTEM_PROFILING_REGISTRY_HPP_
31#define SBL_SYSTEM_PROFILING_REGISTRY_HPP_
37#ifdef SBL_PROFILING_ENABLED
42#ifndef SBL_PROFILING_REGISTRY_CAPACITY
43#define SBL_PROFILING_REGISTRY_CAPACITY 16
54static_assert(
REGISTRY_CAPACITY > 0,
"SBL ERROR: SBL_PROFILING_REGISTRY_CAPACITY must be at least 1");
86 constexpr bool is(
Kind k)
const {
return kind ==
static_cast<uint32_t
>(k); }
233static_assert(
sizeof(std::atomic<uint32_t>) ==
sizeof(uint32_t) && std::atomic<uint32_t>::is_always_lock_free,
234 "SBL ERROR: the registry needs a lock-free 32-bit atomic the size of a uint32_t");
235static_assert(
sizeof(float) == 4,
"SBL ERROR: the registry's f32 fields are 4 bytes");
237static_assert(offsetof(
Entry, name) == 4 && offsetof(
Entry, count) == 16 && offsetof(
Entry, avg) == 20 &&
238 offsetof(
Entry, max) == 24 && offsetof(
Entry, min) == 28 && offsetof(
Entry, overruns) == 32 &&
239 offsetof(
Entry, limit) == 36,
240 "SBL ERROR: the registry entry's live fields are documented");
241static_assert(offsetof(
Entry, window_count) == 40 && offsetof(
Entry, window_avg) == 44 &&
242 offsetof(
Entry, window_max) == 48 && offsetof(
Entry, window_min) == 52 &&
243 offsetof(
Entry, window_overruns) == 56 && offsetof(
Entry, window_base) == 60,
244 "SBL ERROR: the registry entry's window fields are documented");
246 offsetof(
Registry, window_request) == 28 && offsetof(
Registry, window_seq) == 32 &&
248 "SBL ERROR: the registry header is documented");
250#ifdef SBL_PROFILING_ENABLED
254inline Registry sbl_profiling_registry = {
270inline Registry& registry() {
return sbl_profiling_registry; }
273inline uint32_t entry_count() {
return registry().
count; }
276inline const Entry* entry(uint32_t index) {
return index < registry().count ? ®istry().entries[index] :
nullptr; }
285inline Entry* add_entry(Kind kind,
const char* name,
const Entry& from) {
286 Registry& r = registry();
287 if (r.count >= REGISTRY_CAPACITY || name ==
nullptr) {
290 Entry& e = r.entries[r.count];
292 e.kind =
static_cast<uint32_t
>(kind);
294 for (; i <
NAME_SIZE - 1 && name[i] !=
'\0'; ++i) {
300 r.count = r.count + 1;
305inline constexpr uint32_t seq_for(uint32_t request) {
return request << 1; }
315 constexpr Slot() =
default;
316 Slot(
const Slot&) =
delete;
317 Slot& operator=(
const Slot&) =
delete;
320 bool enroll(Kind kind,
const char* name) {
324 Entry* e = add_entry(kind, name, *stats_);
333 Entry& stats() {
return *stats_; }
334 const Entry& stats()
const {
return *stats_; }
335 bool registered()
const {
return registered_; }
339 Entry* stats_ = &own_;
340 bool registered_ =
false;
349inline uint32_t request_window() {
350 Registry& r = registry();
351 const uint32_t request = r.window_request.load(std::memory_order_relaxed) + 1;
352 r.window_request.store(request, std::memory_order_release);
362inline bool window_ready(uint32_t request) {
363 const uint32_t seq = registry().window_seq.load(std::memory_order_acquire);
364 return (seq & 1u) == 0 &&
static_cast<int32_t
>(seq - detail::seq_for(request)) >= 0;
374inline void window_point() {
375 Registry& r = registry();
376 const uint32_t served = r.window_seq.load(std::memory_order_relaxed);
377 if (detail::seq_for(r.window_request.load(std::memory_order_relaxed)) == served) {
380 const uint32_t request = r.window_request.load(std::memory_order_acquire);
381 r.window_seq.store(detail::seq_for(request) - 1, std::memory_order_relaxed);
382 for (uint32_t i = 0; i < r.count; ++i) {
383 r.entries[i].close_window();
385 r.window_seq.store(detail::seq_for(request), std::memory_order_release);
395inline void clear_registry() {
396 Registry& r = registry();
398 for (Entry& e : r.entries) {
401 r.window_request.store(0, std::memory_order_relaxed);
402 r.window_seq.store(0, std::memory_order_relaxed);
The cycle counter behind the profiling tools.
Profiling: the cycle counter, the audio budget, section profiles and reports.
constexpr uint32_t REGISTRY_HEADER_SIZE
constexpr uint32_t REGISTRY_ENTRY_SIZE
constexpr size_t REGISTRY_CAPACITY
constexpr uint32_t NO_MINIMUM
constexpr uint32_t REGISTRY_MAGIC
constexpr uint32_t cycle_rate()
constexpr size_t NAME_SIZE
constexpr uint32_t REGISTRY_VERSION
#define SBL_PROFILING_REGISTRY_CAPACITY
void measure(uint32_t cycles)
Record one measurement: the count, the running average (alpha 1/128), the window's extremes.
constexpr bool is(Kind k) const
True if the entry is of this kind.
static constexpr float AVERAGE_ALPHA
The header and the table.
std::atomic< uint32_t > window_request
std::atomic< uint32_t > window_seq
Entry entries[REGISTRY_CAPACITY]