15#ifndef YAETS__TRACING_HPP_
16#define YAETS__TRACING_HPP_
23#include <condition_variable>
26#include <unordered_map>
40#if defined(__GNUC__) || defined(__clang__)
41 #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __PRETTY_FUNCTION__)
42#elif defined(_MSC_VER)
43 #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __FUNCSIG__)
45 #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __FUNCTION__)
58#define SHARED_TRACE_INIT(session, id) \
59 yaets::TraceRegistry::getInstance().registerTrace(id, session)
69#define SHARED_TRACE_START(id) \
70 yaets::TraceRegistry::getInstance().startTrace(id)
80#define SHARED_TRACE_END(id) \
81 yaets::TraceRegistry::getInstance().endTrace(id)
135 const std::string & trace_name,
136 const std::chrono::nanoseconds & start_time,
137 const std::chrono::nanoseconds & end_time);
140 std::queue<TraceEvent> trace_queue;
141 std::mutex queue_mutex;
142 std::condition_variable cv;
143 std::thread consumer_thread;
144 std::atomic<bool> running;
145 std::string filename_;
146 std::chrono::nanoseconds session_start_time_;
154 void trace_consumer();
200 std::string trace_name_;
201 std::chrono::nanoseconds start_time_;
257 std::string trace_name_;
258 std::vector<std::chrono::nanoseconds> start_times_;
260 std::atomic<size_t> counter_push_;
261 std::atomic<size_t> counter_pop_;
264 std::mutex trace_mutex_;
288 static TraceRegistry instance;
321 void endTrace(
const std::string &
id);
325 std::unordered_map<std::string, std::unique_ptr<NamedSharedTrace>> traces_;
void start()
Start a new trace event.
Definition tracing.cpp:147
static const size_t TRACE_SIZE_INIT
Initial size for the start_times_ vector.
Definition tracing.hpp:227
NamedSharedTrace(TraceSession &session, const std::string &trace_name)
Construct a new NamedSharedTrace object.
Definition tracing.cpp:140
void end()
End the current trace event.
Definition tracing.cpp:162
std::chrono::nanoseconds get_start_time() const
Retrieve the start time of the trace.
Definition tracing.hpp:193
TraceGuard(TraceSession &session, const std::string &trace_name)
Construct a new TraceGuard object.
Definition tracing.cpp:109
std::string extract_trace_name(const std::string &function_signature)
Extract the function name from the full function signature.
Definition tracing.cpp:123
Singleton class to manage global NamedSharedTrace instances.
Definition tracing.hpp:276
void endTrace(const std::string &id)
End a trace identified by a unique ID.
Definition tracing.cpp:194
void startTrace(const std::string &id)
Start a trace identified by a unique ID.
Definition tracing.cpp:185
static TraceRegistry & getInstance()
Retrieve the singleton instance of the TraceRegistry.
Definition tracing.hpp:286
void registerTrace(const std::string &id, TraceSession &session)
Register a new NamedSharedTrace with a unique identifier.
Definition tracing.cpp:178
Class to manage a trace session.
Definition tracing.hpp:105
TraceSession(const std::string &filename)
Construct a new TraceSession object.
Definition tracing.cpp:31
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:60
Definition tracing.hpp:31
Structure to represent a trace event.
Definition tracing.hpp:91
std::string trace_name
Name of the traced function.
Definition tracing.hpp:92
std::chrono::nanoseconds start_time
Start time of the function in nanoseconds.
Definition tracing.hpp:93
std::chrono::nanoseconds end_time
End time of the function in nanoseconds.
Definition tracing.hpp:94