Yaets
Loading...
Searching...
No Matches
tracing.hpp
Go to the documentation of this file.
1// Copyright 2024 Intelligent Robotics Lab
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef YAETS__TRACING_HPP_
16#define YAETS__TRACING_HPP_
17
18#include <chrono>
19#include <string>
20#include <mutex>
21#include <queue>
22#include <condition_variable>
23#include <thread>
24#include <atomic>
25#include <unordered_map>
26#include <memory>
27
28namespace yaets
29{
30
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__)
42#else
43 #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __FUNCTION__)
44#endif
45
46
56#define SHARED_TRACE_INIT(session, id) \
57 yaets::TraceRegistry::getInstance().registerTrace(id, session)
58
67#define SHARED_TRACE_START(id) \
68 yaets::TraceRegistry::getInstance().startTrace(id)
69
78#define SHARED_TRACE_END(id) \
79 yaets::TraceRegistry::getInstance().endTrace(id)
80
81
89{
90 std::string trace_name;
91 std::chrono::nanoseconds start_time;
92 std::chrono::nanoseconds end_time;
93};
94
103{
104public:
110 explicit TraceSession(const std::string & filename);
111
116
120 void stop();
121
132 void register_trace(
133 const std::string & trace_name,
134 const std::chrono::nanoseconds & start_time,
135 const std::chrono::nanoseconds & end_time);
136
137private:
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_;
145
152 void trace_consumer();
153};
154
163{
164public:
173 TraceGuard(TraceSession & session, const std::string & trace_name);
174
181 ~TraceGuard();
182
191 std::chrono::nanoseconds get_start_time() const
192 {
193 return start_time_;
194 }
195
196private:
197 TraceSession & session_;
198 std::string trace_name_;
199 std::chrono::nanoseconds start_time_;
200
201protected:
211 std::string extract_trace_name(const std::string & function_signature);
212};
213
214
223{
224public:
225 static const size_t TRACE_SIZE_INIT = 100;
226
236 NamedSharedTrace(TraceSession & session, const std::string & trace_name);
237
243 void start();
244
251 void end();
252
253private:
254 TraceSession & session_;
255 std::string trace_name_;
256 std::vector<std::chrono::nanoseconds> start_times_;
257
258 std::atomic<size_t> counter_push_;
259 std::atomic<size_t> counter_pop_;
260 size_t elements_;
261 std::mutex trace_mutex_;
262};
263
264
272class TraceRegistry
273{
274public:
283 static TraceRegistry & getInstance()
284 {
285 static TraceRegistry instance;
286 return instance;
287 }
288
298 void registerTrace(const std::string & id, TraceSession & session);
299
308 void startTrace(const std::string & id);
309
318 void endTrace(const std::string & id);
319
320private:
321 std::unordered_map<std::string, std::unique_ptr<NamedSharedTrace>> traces_;
322 std::mutex mutex_;
323
324 // Private constructors for singleton pattern
325 TraceRegistry() = default;
326 ~TraceRegistry() = default;
327
328 // Disable copy construction and assignment
329 TraceRegistry(const TraceRegistry &) = delete;
330 TraceRegistry & operator=(const TraceRegistry &) = delete;
331};
332
333} // namespace yaets
334
335#endif // YAETS__TRACING_HPP_
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