11#include "../utility/utility.hpp"
12#include "../concurrency/concurrency.hpp"
15#include "../time/time.hpp"
16#include "../macros.hpp"
26hi_export_module(hikogui.telemetry : counters);
29hi_export
namespace hi::inline
v1 {
42 auto const lock = std::scoped_lock(_mutex);
43 auto const& map_ = _map.get_or_make();
44 auto const it = map_.find(name);
45 if (it == map_.cend()) {
48 hi_assert(it->second);
60 operator uint64_t() const noexcept
62 return _total_count.load(std::memory_order::relaxed);
65 static void log() noexcept
67 auto const lock = std::scoped_lock(_mutex);
69 for (
auto const & [
string, counter ] : _map.get_or_make()) {
75 static void log_header() noexcept
77 hi_log_statistics(
"");
78 hi_log_statistics(
"{:>18} {:>9} {:>10} {:>10} {:>10}",
"total",
"delta",
"min",
"max",
"mean");
79 hi_log_statistics(
"------------------ --------- ---------- ---------- ----------");
88 auto const total_count = _total_count.load(std::memory_order::relaxed);
89 auto const prev_count = _prev_count.exchange(_total_count, std::memory_order::relaxed);
90 auto const delta_count = total_count - prev_count;
91 if (delta_count != 0) {
92 auto const duration_max = time_stamp_count::duration_from_count(_duration_max.exchange(0, std::memory_order::relaxed));
93 auto const duration_min = time_stamp_count::duration_from_count(
96 auto const duration_avg = _duration_avg.exchange(0, std::memory_order::relaxed);
97 if (duration_avg == 0) {
98 hi_log_statistics(
"{:>18} {:>+9} {:10} {:10} {:10} {}", total_count, delta_count,
"",
"",
"", tag);
101 auto const avg_count = duration_avg & 0xffff;
102 auto const avg_sum = duration_avg >> 16;
103 auto const average = time_stamp_count::duration_from_count(avg_sum / avg_count);
106 "{:18d} {:+9d} {:>10} {:>10} {:>10} {}",
109 format_engineering(duration_min),
110 format_engineering(duration_max),
111 format_engineering(average),
123 hi_axiom(count >= 0);
124 _total_count.store(count, std::memory_order::relaxed);
128 uint64_t operator++() noexcept
130 return _total_count.fetch_add(1, std::memory_order::relaxed) + 1;
133 uint64_t operator++(
int)
noexcept
135 return _total_count.fetch_add(1, std::memory_order::relaxed);
138 uint64_t operator--() noexcept
140 return _total_count.fetch_sub(1, std::memory_order::relaxed) + 1;
143 uint64_t operator--(
int)
noexcept
145 return _total_count.fetch_sub(1, std::memory_order::relaxed);
152 _total_count.fetch_add(1, std::memory_order::relaxed);
153 fetch_max(_duration_max, duration, std::memory_order::relaxed);
154 fetch_min(_duration_min, duration, std::memory_order::relaxed);
160 _duration_avg.fetch_add(duration, std::memory_order::relaxed);
169 constinit static inline unfair_mutex_impl<false> _mutex;
185template<fixed_
string Tag>
188 using counter::operator=;
192 auto const lock = std::scoped_lock(_mutex);
199template<fixed_
string Tag>
204 return detail::counter::get_if(name);
207inline void log::log_thread_main(std::stop_token stop_token)
noexcept
209 using namespace std::chrono_literals;
212 hi_log_info(
"log thread started");
214 auto counter_statistics_deadline = std::chrono::utc_clock::now() + 1min;
216 while (not stop_token.stop_requested()) {
219 auto const now = std::chrono::utc_clock::now();
220 if (now >= counter_statistics_deadline) {
221 counter_statistics_deadline = now + 1min;
222 detail::counter::log();
228 hi_log_info(
"log thread finished");
Functions and types for accessing operating system threads.
Definition of the unfair_mutex.
void set_thread_name(std::string_view name) noexcept
Set the name of the current thread.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
T fetch_max(std::atomic< T > &lhs, T rhs, std::memory_order order) noexcept
Lock-free fetch-then-max operation on an atomic.
Definition atomic.hpp:25
T fetch_min(std::atomic< T > &lhs, T rhs, std::memory_order order) noexcept
Lock-free fetch-then-min operation on an atomic.
Definition atomic.hpp:39
Definition counters.hpp:32
static counter * get_if(std::string const &name) noexcept
Get the named counter.
Definition counters.hpp:40
void log(std::string const &tag) noexcept
Log the counter.
Definition counters.hpp:86
void add_duration(uint64_t duration) noexcept
Add a duration.
Definition counters.hpp:150
counter & operator=(std::integral auto count) noexcept
Rest the counter.
Definition counters.hpp:121
Definition counters.hpp:186
hi_no_inline void flush() noexcept
Flush all messages from the log_queue directly from this thread.
Definition log.hpp:147