11#include "../utility/utility.hpp"
12#include "../concurrency/concurrency.hpp"
13#include "../time/module.hpp"
14#include "../macros.hpp"
26namespace hi::inline
v1 {
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) {
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;
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>
172class tagged_counter :
public counter {
174 tagged_counter() noexcept : counter()
176 hilet lock = std::scoped_lock(_mutex);
183template<fixed_
string Tag>
191inline void log::log_thread_main(std::stop_token stop_token)
noexcept
193 using namespace std::chrono_literals;
195 set_thread_name(
"log");
196 hi_log_info(
"log thread started");
198 auto counter_statistics_deadline = std::chrono::utc_clock::now() + 1min;
200 while (not stop_token.stop_requested()) {
203 hilet now = std::chrono::utc_clock::now();
204 if (now >= counter_statistics_deadline) {
205 counter_statistics_deadline = now + 1min;
206 detail::counter::log();
212 hi_log_info(
"log thread finished");
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
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
static std::chrono::nanoseconds duration_from_count(uint64_t count) noexcept
Convert a time-stamp count to a duration.
Definition time_stamp_count.hpp:124