HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
trace.hpp
1// Copyright Take Vos 2019-2020.
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
5#include "counters.hpp"
6#include "datum.hpp"
7#include "logger.hpp"
8#include "cpu_utc_clock.hpp"
9#include "required.hpp"
10#include "tagged_map.hpp"
11#include "wfree_message_queue.hpp"
12#include "fixed_string.hpp"
13#include <fmt/ostream.h>
14#include <fmt/format.h>
15#include <atomic>
16#include <array>
17#include <utility>
18#include <ostream>
19#include <typeinfo>
20#include <typeindex>
21
22#pragma once
23
24namespace tt {
25
26constexpr int MAX_NR_TRACES = 1024;
27
28
29inline std::atomic<int64_t> trace_id = 0;
30
34 int64_t top_trace_id = 0;
35
38 int8_t depth = 0;
39
42 int8_t record_depth = 0;
43
49 inline int64_t push() noexcept {
50 ttlet parent_id = top_trace_id;
51 top_trace_id = trace_id.fetch_add(1, std::memory_order_relaxed) + 1;
52 depth++;
53 return parent_id;
54 }
55
62 inline std::pair<int64_t, bool> pop(int64_t parent_id) noexcept {
63 bool is_recording = record_depth > --depth;
64 if (is_recording) {
66 }
67
68 ttlet id = top_trace_id;
69 top_trace_id = parent_id;
70 return {id, is_recording};
71 }
72};
73
74inline thread_local trace_stack_type trace_stack;
75
78void trace_record() noexcept;
79
80template<basic_fixed_string Tag, basic_fixed_string... InfoTags>
81struct trace_data {
85 int64_t parent_id;
86
90
91 tagged_map<sdatum, InfoTags...> info;
92
93 trace_data(typename cpu_counter_clock::time_point timestamp) :
94 timestamp(timestamp) {}
95
96 trace_data() = default;
97 ~trace_data() = default;
98 trace_data(trace_data const &other) = default;
99 trace_data &operator=(trace_data const &other) = default;
100 trace_data(trace_data &&other) = default;
101 trace_data &operator=(trace_data &&other) = default;
102
103 template<basic_fixed_string InfoTag>
104 sdatum &get() noexcept {
105 return info.template get<InfoTag>();
106 }
107
108 template<basic_fixed_string InfoTag>
109 sdatum const &get() const noexcept {
110 return info.template get<InfoTag>();
111 }
112};
113
114template<basic_fixed_string Tag, basic_fixed_string... InfoTags>
115std::ostream &operator<<(std::ostream &lhs, trace_data<Tag, InfoTags...> const &rhs) {
116 auto info_string = std::string{};
117
118 auto counter = 0;
119 for (size_t i = 0; i < rhs.info.size(); i++) {
120 if (counter++ > 0) {
121 info_string += ", ";
122 }
123 info_string += rhs.info.get_tag(i);
124 info_string += "=";
125 info_string += static_cast<std::string>(rhs.info[i]);
126 }
127
128 lhs << fmt::format("parent={} tag={} start={} {}",
129 rhs.parent_id,
130 std::type_index(typeid(Tag)).name(),
131 format_iso8601(cpu_utc_clock::convert(rhs.timestamp)),
132 info_string
133 );
134 return lhs;
135}
136
147private:
148 std::atomic<int64_t> count = 0;
152
153 // Variables used by logger.
154 int64_t prev_count = 0;
155 typename cpu_counter_clock::duration prev_duration = {};
156
157public:
162 // In the logging thread we can check if count and version are equal
163 // to read the statistics.
164 ttlet current_count = count.fetch_add(1, std::memory_order_acquire);
165
166 duration.fetch_add(d.count(), std::memory_order_relaxed);
167
168 auto prev_peak = peak_duration.load(std::memory_order_relaxed);
169 decltype(prev_peak) new_peak;
170 do {
171 new_peak = d.count() > prev_peak ? d.count() : prev_peak;
172 } while (!peak_duration.compare_exchange_weak(prev_peak, new_peak, std::memory_order_relaxed));
173
174 version.store(current_count + 1, std::memory_order_release);
175
176 return current_count == 0;
177 }
178
179 struct read_result {
180 int64_t count;
181 int64_t last_count;
182
183 typename cpu_counter_clock::duration duration;
184 typename cpu_counter_clock::duration last_duration;
185 typename cpu_counter_clock::duration peak_duration;
186 };
187
188 read_result read() {
189 read_result r;
190
191 r.peak_duration = {};
192 do {
193 r.count = count.load(std::memory_order_acquire);
194
195 r.duration = decltype(r.duration){duration.load(std::memory_order_relaxed)};
196
197 auto tmp = peak_duration.exchange(0, std::memory_order_relaxed);
198 if (tmp > r.peak_duration.count()) {
199 r.peak_duration = decltype(r.duration){tmp};
200 }
201
202 std::atomic_thread_fence(std::memory_order_release);
203 } while (r.count != version.load(std::memory_order_relaxed));
204
205 r.last_count = r.count - prev_count;
206 r.last_duration = r.duration - prev_duration;
207
208 prev_count = r.count;
209 prev_duration = r.duration;
210 return r;
211 }
212};
213
214template<basic_fixed_string Tag>
215inline trace_statistics_type trace_statistics;
216
217inline wfree_unordered_map<std::string,trace_statistics_type *,MAX_NR_TRACES> trace_statistics_map;
218
219
220template<basic_fixed_string Tag, basic_fixed_string... InfoTags>
221class trace final {
222 // If this pointer is not an volatile, clang will optimize it away and replacing it
223 // with direct access to the trace_stack variable. This trace_stack variable is in local storage,
224 // so a lot of instructions and memory accesses are emitted by the compiler multiple times.
225 trace_stack_type * volatile stack;
226
227 trace_data<Tag, InfoTags...> data;
228
229 tt_no_inline static void add_to_map() {
230 trace_statistics_map.insert(Tag, &trace_statistics<Tag>);
231 }
232
233public:
240 stack(&trace_stack), data(cpu_counter_clock::now())
241 {
242 // We don't need to know our own id, until the destructor is called.
243 // Our id will be at the top of the stack.
244 data.parent_id = stack->push();
245 }
246
247 ~trace() {
248 ttlet end_timestamp = cpu_counter_clock::now();
249
250 if(trace_statistics<Tag>.write(end_timestamp - data.timestamp)) {
251 [[unlikely]] add_to_map();
252 }
253
254 ttlet [id, is_recording] = stack->pop(data.parent_id);
255
256 // Send the log to the log thread.
257 if (is_recording) {
258 [[unlikely]] tt_log_trace("id={} {}", id, std::move(data));
259 }
260 }
261
262 trace(trace const &) = delete;
263 trace(trace &&) = delete;
264 trace &operator=(trace const &) = delete;
265 trace &operator=(trace &&) = delete;
266
267 template<basic_fixed_string InfoTag, typename T>
268 trace &set(T &&value) {
269 data.template get<InfoTag>() = std::forward<T>(value);
270 return *this;
271 }
272};
273
274
275}
Definition cpu_counter_clock.hpp:19
A fixed size (64 bits) class for a generic value type.
Definition datum.hpp:111
A static sized stack.
Definition stack.hpp:22
static time_point convert(typename fast_clock::time_point fast_time) noexcept
Definition sync_clock.hpp:253
Definition tagged_map.hpp:16
Definition trace.hpp:31
int64_t top_trace_id
Definition trace.hpp:34
std::pair< int64_t, bool > pop(int64_t parent_id) noexcept
Definition trace.hpp:62
int8_t depth
Definition trace.hpp:38
int64_t push() noexcept
Definition trace.hpp:49
int8_t record_depth
Definition trace.hpp:42
Definition trace.hpp:81
int64_t parent_id
Definition trace.hpp:85
cpu_counter_clock::time_point timestamp
Definition trace.hpp:89
Definition trace.hpp:146
bool write(cpu_counter_clock::duration const &d)
Definition trace.hpp:161
Definition trace.hpp:221
trace()
Definition trace.hpp:239
Definition version.hpp:11
T atomic_thread_fence(T... args)
T compare_exchange_weak(T... args)
T exchange(T... args)
T fetch_add(T... args)
T load(T... args)
T move(T... args)
T name(T... args)