8#include "cpu_utc_clock.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>
26constexpr int MAX_NR_TRACES = 1024;
49 inline int64_t
push() noexcept {
70 return {id, is_recording};
74inline thread_local trace_stack_type trace_stack;
78void trace_record() noexcept;
80template<basic_fixed_string Tag, basic_fixed_string... InfoTags>
94 timestamp(timestamp) {}
103 template<basic_fixed_
string InfoTag>
105 return info.template get<InfoTag>();
108 template<basic_fixed_
string InfoTag>
109 sdatum
const &get() const noexcept {
110 return info.template get<InfoTag>();
114template<basic_fixed_string Tag, basic_fixed_string... InfoTags>
119 for (
size_t i = 0; i < rhs.info.size(); i++) {
123 info_string += rhs.info.get_tag(i);
125 info_string +=
static_cast<std::string>(rhs.info[i]);
128 lhs << fmt::format(
"parent={} tag={} start={} {}",
154 int64_t prev_count = 0;
164 ttlet current_count = count.fetch_add(1, std::memory_order_acquire);
168 auto prev_peak = peak_duration.
load(std::memory_order_relaxed);
169 decltype(prev_peak) new_peak;
171 new_peak = d.
count() > prev_peak ? d.
count() : prev_peak;
174 version.store(current_count + 1, std::memory_order_release);
176 return current_count == 0;
191 r.peak_duration = {};
193 r.count = count.load(std::memory_order_acquire);
195 r.duration =
decltype(r.duration){duration.
load(std::memory_order_relaxed)};
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};
203 }
while (r.count != version.
load(std::memory_order_relaxed));
205 r.last_count = r.count - prev_count;
206 r.last_duration = r.duration - prev_duration;
208 prev_count = r.count;
209 prev_duration = r.duration;
214template<basic_fixed_
string Tag>
215inline trace_statistics_type trace_statistics;
217inline wfree_unordered_map<std::string,trace_statistics_type *,MAX_NR_TRACES> trace_statistics_map;
220template<basic_fixed_string Tag, basic_fixed_string... InfoTags>
229 tt_no_inline
static void add_to_map() {
230 trace_statistics_map.insert(Tag, &trace_statistics<Tag>);
248 ttlet end_timestamp = cpu_counter_clock::now();
250 if(trace_statistics<Tag>.write(end_timestamp - data.
timestamp)) {
251 [[unlikely]] add_to_map();
254 ttlet [id, is_recording] = stack->pop(data.
parent_id);
258 [[unlikely]] tt_log_trace(
"id={} {}",
id,
std::move(data));
267 template<basic_fixed_
string InfoTag,
typename T>
268 trace &set(T &&value) {
269 data.template get<InfoTag>() = std::forward<T>(value);
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
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
int64_t parent_id
Definition trace.hpp:85
cpu_counter_clock::time_point timestamp
Definition trace.hpp:89
bool write(cpu_counter_clock::duration const &d)
Definition trace.hpp:161
trace()
Definition trace.hpp:239
Definition version.hpp:11
T atomic_thread_fence(T... args)
T compare_exchange_weak(T... args)