11#include "../utility/utility.hpp"
12#include "../concurrency/concurrency.hpp"
13#include "../time/module.hpp"
14#include "../macros.hpp"
39 hilet lock = std::scoped_lock(_mutex);
40 hilet& map_ = _map.get_or_make();
41 hilet it = map_.find(name);
42 if (it == map_.cend()) {
45 hi_assert(it->second);
57 operator uint64_t() const noexcept
59 return _total_count.load(std::memory_order::relaxed);
62 static void log() noexcept
64 hilet lock = std::scoped_lock(_mutex);
66 for (hilet & [
string, counter ] : _map.get_or_make()) {
72 static void log_header() noexcept
74 hi_log_statistics(
"");
75 hi_log_statistics(
"{:>18} {:>9} {:>10} {:>10} {:>10}",
"total",
"delta",
"min",
"max",
"mean");
76 hi_log_statistics(
"------------------ --------- ---------- ---------- ----------");
85 hilet total_count = _total_count.load(std::memory_order::relaxed);
86 hilet prev_count = _prev_count.exchange(_total_count, std::memory_order::relaxed);
87 hilet delta_count = total_count - prev_count;
88 if (delta_count != 0) {
89 hilet duration_max = time_stamp_count::duration_from_count(_duration_max.exchange(0, std::memory_order::relaxed));
90 hilet duration_min = time_stamp_count::duration_from_count(
93 hilet duration_avg = _duration_avg.exchange(0, std::memory_order::relaxed);
94 if (duration_avg == 0) {
95 hi_log_statistics(
"{:>18} {:>+9} {:10} {:10} {:10} {}", total_count, delta_count,
"",
"",
"", tag);
98 hilet avg_count = duration_avg & 0xffff;
99 hilet avg_sum = duration_avg >> 16;
100 hilet average = time_stamp_count::duration_from_count(avg_sum / avg_count);
103 "{:18d} {:+9d} {:>10} {:>10} {:>10} {}",
106 format_engineering(duration_min),
107 format_engineering(duration_max),
108 format_engineering(average),
114 uint64_t operator++() noexcept
116 return _total_count.fetch_add(1, std::memory_order::relaxed) + 1;
119 uint64_t operator++(
int)
noexcept
121 return _total_count.fetch_add(1, std::memory_order::relaxed);
124 uint64_t operator--() noexcept
126 return _total_count.fetch_sub(1, std::memory_order::relaxed) + 1;
129 uint64_t operator--(
int)
noexcept
131 return _total_count.fetch_sub(1, std::memory_order::relaxed);
138 _total_count.fetch_add(1, std::memory_order::relaxed);
139 fetch_max(_duration_max, duration, std::memory_order::relaxed);
140 fetch_min(_duration_min, duration, std::memory_order::relaxed);
146 _duration_avg.fetch_add(duration, std::memory_order::relaxed);
155 constinit static inline unfair_mutex_impl<false> _mutex;
171template<fixed_
string Tag>
176 hilet lock = std::scoped_lock(_mutex);
183template<fixed_
string Tag>
188 return detail::counter::get_if(name);
191inline void log::log_thread_main(std::stop_token
stop_token)
noexcept
193 using namespace std::chrono_literals;
196 hi_log_info(
"log thread started");
203 hilet now = std::chrono::utc_clock::now();
206 detail::counter::log();
212 hi_log_info(
"log thread finished");
void set_thread_name(std::string_view name) noexcept
Set the name of the current thread.
DOXYGEN BUG.
Definition algorithm.hpp:16
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
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Definition counters.hpp:29
static counter * get_if(std::string const &name) noexcept
Get the named counter.
Definition counters.hpp:37
void log(std::string const &tag) noexcept
Log the counter.
Definition counters.hpp:83
void add_duration(uint64_t duration) noexcept
Add a duration.
Definition counters.hpp:136
Definition counters.hpp:172
hi_no_inline void flush() noexcept
Flush all messages from the log_queue directly from this thread.
Definition log.hpp:145