HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
counters.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2019, 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8#pragma once
9
10#include "utility/module.hpp"
11#include "concurrency/module.hpp"
12#include "time_stamp_count.hpp"
13#include "atomic.hpp"
14#include <span>
15#include <typeinfo>
16#include <typeindex>
17#include <string>
18#include <atomic>
19#include <map>
20#include <memory>
21#include <mutex>
22
23namespace hi::inline v1 {
24namespace detail {
25
26class counter {
27public:
34 [[nodiscard]] static counter *get_if(std::string const& name) noexcept
35 {
36 hilet lock = std::scoped_lock(_mutex);
37 hilet& map_ = _map.get_or_make();
38 hilet it = map_.find(name);
39 if (it == map_.cend()) {
40 return nullptr;
41 } else {
42 hi_assert(it->second);
43 return it->second;
44 }
45 }
46
47 counter(counter const&) = delete;
48 counter(counter&&) = delete;
49 counter& operator=(counter const&) = delete;
50 counter& operator=(counter&&) = delete;
51
52 constexpr counter() noexcept {}
53
54 operator uint64_t() const noexcept
55 {
56 return _total_count.load(std::memory_order::relaxed);
57 }
58
59 static void log() noexcept
60 {
61 hilet lock = std::scoped_lock(_mutex);
62 log_header();
63 for (hilet & [ string, counter ] : _map.get_or_make()) {
64 hi_assert(counter);
65 counter->log(string);
66 }
67 }
68
69 static void log_header() noexcept;
70
73 void log(std::string const& tag) noexcept;
74
75 uint64_t operator++() noexcept
76 {
77 return _total_count.fetch_add(1, std::memory_order::relaxed) + 1;
78 }
79
80 uint64_t operator++(int) noexcept
81 {
82 return _total_count.fetch_add(1, std::memory_order::relaxed);
83 }
84
85 uint64_t operator--() noexcept
86 {
87 return _total_count.fetch_sub(1, std::memory_order::relaxed) + 1;
88 }
89
90 uint64_t operator--(int) noexcept
91 {
92 return _total_count.fetch_sub(1, std::memory_order::relaxed);
93 }
94
97 void add_duration(uint64_t duration) noexcept
98 {
99 _total_count.fetch_add(1, std::memory_order::relaxed);
100 fetch_max(_duration_max, duration, std::memory_order::relaxed);
101 fetch_min(_duration_min, duration, std::memory_order::relaxed);
102
103 // Combine duration with count in a single atomic.
104 hi_axiom(duration <= (std::numeric_limits<uint64_t>::max() >> 10));
105 duration <<= 16;
106 ++duration;
107 _duration_avg.fetch_add(duration, std::memory_order::relaxed);
108 }
109
110protected:
111 using map_type = std::map<std::string, counter *>;
112
116 constinit static inline unfair_mutex_impl<false> _mutex;
117 constinit static inline atomic_unique_ptr<map_type> _map;
118
119 std::atomic<uint64_t> _total_count = 0;
120 std::atomic<uint64_t> _prev_count = 0;
121 std::atomic<uint64_t> _duration_max = 0;
123
129 std::atomic<uint64_t> _duration_avg = 0;
130};
131
132template<fixed_string Tag>
133class tagged_counter : public counter {
134public:
135 tagged_counter() noexcept : counter()
136 {
137 hilet lock = std::scoped_lock(_mutex);
138 _map.get_or_make()[std::string{Tag}] = this;
139 }
140};
141
142} // namespace detail
143
144template<fixed_string Tag>
145inline detail::tagged_counter<Tag> global_counter;
146
147[[nodiscard]] inline detail::counter *get_global_counter_if(std::string const& name)
148{
149 return detail::counter::get_if(name);
150}
151
152} // namespace hi::inline v1
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
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:22
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:36
Definition atomic.hpp:48
Definition counters.hpp:26
static counter * get_if(std::string const &name) noexcept
Get the named counter.
Definition counters.hpp:34
void add_duration(uint64_t duration) noexcept
Add a duration.
Definition counters.hpp:97
Definition counters.hpp:133
T max(T... args)