15#ifndef YAETS__TRACING_HPP_
16#define YAETS__TRACING_HPP_
22#include <condition_variable>
25#include <unordered_map>
38#if defined(__GNUC__) || defined(__clang__)
39 #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __PRETTY_FUNCTION__)
40#elif defined(_MSC_VER)
41 #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __FUNCSIG__)
43 #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __FUNCTION__)
56#define SHARED_TRACE_INIT(session, id) \
57 yaets::TraceRegistry::getInstance().registerTrace(id, session)
67#define SHARED_TRACE_START(id) \
68 yaets::TraceRegistry::getInstance().startTrace(id)
78#define SHARED_TRACE_END(id) \
79 yaets::TraceRegistry::getInstance().endTrace(id)
133 const std::string & trace_name,
134 const std::chrono::nanoseconds & start_time,
135 const std::chrono::nanoseconds & end_time);
138 std::queue<TraceEvent> trace_queue;
139 std::mutex queue_mutex;
140 std::condition_variable cv;
141 std::thread consumer_thread;
142 std::atomic<bool> running;
143 std::string filename_;
144 std::chrono::nanoseconds session_start_time_;
152 void trace_consumer();
198 std::string trace_name_;
199 std::chrono::nanoseconds start_time_;
255 std::string trace_name_;
256 std::vector<std::chrono::nanoseconds> start_times_;
258 std::atomic<size_t> counter_push_;
259 std::atomic<size_t> counter_pop_;
261 std::mutex trace_mutex_;
285 static TraceRegistry instance;
318 void endTrace(
const std::string &
id);
321 std::unordered_map<std::string, std::unique_ptr<NamedSharedTrace>> traces_;
void start()
Start a new trace event.
Definition tracing.cpp:135
static const size_t TRACE_SIZE_INIT
Initial size for the start_times_ vector.
Definition tracing.hpp:225
NamedSharedTrace(TraceSession &session, const std::string &trace_name)
Construct a new NamedSharedTrace object.
Definition tracing.cpp:128
void end()
End the current trace event.
Definition tracing.cpp:150
std::chrono::nanoseconds get_start_time() const
Retrieve the start time of the trace.
Definition tracing.hpp:191
~TraceGuard()
Destroy the TraceGuard object.
Definition tracing.cpp:103
TraceGuard(TraceSession &session, const std::string &trace_name)
Construct a new TraceGuard object.
Definition tracing.cpp:97
std::string extract_trace_name(const std::string &function_signature)
Extract the function name from the full function signature.
Definition tracing.cpp:111
Singleton class to manage global NamedSharedTrace instances.
Definition tracing.hpp:273
void endTrace(const std::string &id)
End a trace identified by a unique ID.
Definition tracing.cpp:182
void startTrace(const std::string &id)
Start a trace identified by a unique ID.
Definition tracing.cpp:173
static TraceRegistry & getInstance()
Retrieve the singleton instance of the TraceRegistry.
Definition tracing.hpp:283
void registerTrace(const std::string &id, TraceSession &session)
Register a new NamedSharedTrace with a unique identifier.
Definition tracing.cpp:166
Class to manage a trace session.
Definition tracing.hpp:103
TraceSession(const std::string &filename)
Construct a new TraceSession object.
Definition tracing.cpp:31
~TraceSession()
Destroy the TraceSession object and stop tracing.
Definition tracing.cpp:38
void stop()
Stop the trace session and flush remaining trace events to the file.
Definition tracing.cpp:44
void register_trace(const std::string &trace_name, const std::chrono::nanoseconds &start_time, const std::chrono::nanoseconds &end_time)
Register a new trace event.
Definition tracing.cpp:56
Definition tracing.hpp:29
Structure to represent a trace event.
Definition tracing.hpp:89
std::string trace_name
Name of the traced function.
Definition tracing.hpp:90
std::chrono::nanoseconds start_time
Start time of the function in nanoseconds.
Definition tracing.hpp:91
std::chrono::nanoseconds end_time
End time of the function in nanoseconds.
Definition tracing.hpp:92