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 <cstdint>
20#include <string>
21#include <mutex>
22#include <queue>
23#include <condition_variable>
24#include <thread>
25#include <atomic>
26#include <unordered_map>
27#include <memory>
28#include <vector>
29
30namespace yaets
31{
32
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__)
44#else
45 #define TRACE_EVENT(session) yaets::TraceGuard trace_guard(session, __FUNCTION__)
46#endif
47
48
58#define SHARED_TRACE_INIT(session, id) \
59 yaets::TraceRegistry::getInstance().registerTrace(id, session)
60
69#define SHARED_TRACE_START(id) \
70 yaets::TraceRegistry::getInstance().startTrace(id)
71
80#define SHARED_TRACE_END(id) \
81 yaets::TraceRegistry::getInstance().endTrace(id)
82
83
91{
92 std::string trace_name;
93 std::chrono::nanoseconds start_time;
94 std::chrono::nanoseconds end_time;
95};
96
105{
106public:
112 explicit TraceSession(const std::string & filename);
113
118
122 void stop();
123
134 void register_trace(
135 const std::string & trace_name,
136 const std::chrono::nanoseconds & start_time,
137 const std::chrono::nanoseconds & end_time);
138
139private:
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_;
147
154 void trace_consumer();
155};
156
165{
166public:
175 TraceGuard(TraceSession & session, const std::string & trace_name);
176
183 ~TraceGuard();
184
193 std::chrono::nanoseconds get_start_time() const
194 {
195 return start_time_;
196 }
197
198private:
199 TraceSession & session_;
200 std::string trace_name_;
201 std::chrono::nanoseconds start_time_;
202
203protected:
213 std::string extract_trace_name(const std::string & function_signature);
214};
215
216
225{
226public:
227 static const size_t TRACE_SIZE_INIT = 100;
228
238 NamedSharedTrace(TraceSession & session, const std::string & trace_name);
239
245 void start();
246
253 void end();
254
255private:
256 TraceSession & session_;
257 std::string trace_name_;
258 std::vector<std::chrono::nanoseconds> start_times_;
259
260 std::atomic<size_t> counter_push_;
261 std::atomic<size_t> counter_pop_;
262 size_t elements_;
264 std::mutex trace_mutex_;
265};
266
267
275class TraceRegistry
276{
277public:
286 static TraceRegistry & getInstance()
287 {
288 static TraceRegistry instance;
289 return instance;
290 }
291
301 void registerTrace(const std::string & id, TraceSession & session);
302
311 void startTrace(const std::string & id);
312
321 void endTrace(const std::string & id);
322
323private:
325 std::unordered_map<std::string, std::unique_ptr<NamedSharedTrace>> traces_;
326 std::mutex mutex_;
327
328 // Private constructors for singleton pattern
329 TraceRegistry() = default;
330 ~TraceRegistry() = default;
331
332 // Disable copy construction and assignment
333 TraceRegistry(const TraceRegistry &) = delete;
334 TraceRegistry & operator=(const TraceRegistry &) = delete;
335};
336
337} // namespace yaets
338
339#endif // YAETS__TRACING_HPP_
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