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
counter.hpp
Go to the documentation of this file.
1/**
2 * @file counter.hpp
3 * @brief An event counter for the profiling registry
4 *
5 * Counts events whose cost a cycle average hides: an edge or a correction,
6 * a frame that took a slower path. A registered counter is an entry of the
7 * registry (registry.hpp): its total since the start and its count in the
8 * last window.
9 *
10 * Gated behind SBL_PROFILING_ENABLED. Without it the class is empty and its
11 * methods compile to nothing.
12 *
13 * Usage:
14 * @code
15 * static sbl::system::profiling::Counter edges;
16 * edges.register_as("edges"); // at startup
17 *
18 * if (crossed) {
19 * edges.add(); // in the measured code
20 * }
21 * @endcode
22 */
23
24#ifndef SBL_SYSTEM_PROFILING_COUNTER_HPP_
25#define SBL_SYSTEM_PROFILING_COUNTER_HPP_
26
27#include <cstdint>
29
30namespace sbl::system::profiling {
31
32#ifdef SBL_PROFILING_ENABLED
33
34class Counter {
35public:
36 /** Enter the registry under `name`. False if already registered or the table is full. Startup only. */
37 [[nodiscard]] bool register_as(const char* name) { return slot_.enroll(Kind::Counter, name); }
38
39 /** Count `events` more. */
40 void add(uint32_t events = 1) { slot_.stats().count += events; }
41
42 /** Take a total kept elsewhere, such as a count a library primitive keeps. */
43 void set(uint32_t total) { slot_.stats().count = total; }
44
45 /** Events since the start. Wraps at 2^32. */
46 uint32_t count() const { return slot_.stats().count; }
47
48 /** Forget the count. */
49 void reset() { slot_.stats().clear(); }
50
51private:
52 detail::Slot slot_{};
53};
54
55#else
56
57class Counter {
58public:
59 [[nodiscard]] static bool register_as(const char*) { return true; }
60 static void add(uint32_t = 1) {}
61 static void set(uint32_t) {}
62 static uint32_t count() { return 0; }
63 static void reset() {}
64};
65
66#endif
67
68} // namespace sbl::system::profiling
69
70#endif // SBL_SYSTEM_PROFILING_COUNTER_HPP_
Profiling: the cycle counter, the audio budget, section profiles and reports.
The profiling registry: every budget, section and counter in one table.