EyeAI
Loading...
Searching...
No Matches
Profiling.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <concurrentqueue.h>
5#include <string_view>
6
7using moodycamel::ConcurrentQueue;
8
9using profile_clock = std::chrono::high_resolution_clock;
10
11class ProfilingFrame;
12
15 explicit ProfileScope(std::string_view name, ProfilingFrame& frame);
16 ~ProfileScope() noexcept;
17
18 ProfileScope(const ProfileScope&) = delete;
20 void operator=(const ProfileScope&) = delete;
21 void operator=(ProfileScope&&) = delete;
22
23 private:
24 std::string_view name;
25 int scope_depth = 0;
26 ProfilingFrame& frame;
27 profile_clock::time_point start;
28};
29
33 std::string_view name;
34 int scope_depth = 0;
35 profile_clock::time_point start;
36 profile_clock::duration duration = profile_clock::duration::zero();
37
38 explicit ProfileScopeRecord() = default;
40 std::string_view name,
41 int scope_depth,
42 profile_clock::time_point start,
43 profile_clock::duration duration
44 )
47
48 [[nodiscard]] std::string formatted() const;
49};
50
53 public:
54 explicit ProfilingFrame(std::string_view name) noexcept : name(name) {}
55
57 int start_scope() noexcept;
58
59 void end_scope(const ProfileScopeRecord& scope) noexcept;
60
63 std::string finish();
64
65 private:
66 std::string_view name;
67 profile_clock::time_point start = profile_clock::now();
68 ConcurrentQueue<ProfileScopeRecord> profile_scopes;
69 std::atomic_int current_frame_scope_depth = 0;
70};
71
77
79void set_last_depth_profiling_frame_formatted(std::string&& formatted);
82void set_last_camera_profiling_frame_formatted(std::string&& formatted);
85void set_last_object_profiling_frame_formatted(std::string&& formatted);
88void set_last_audio_profiling_frame_formatted(std::string&& formatted);
90
91#define FUNCTION_NAME() (static_cast<const char*>(__func__))
92
93#define COMBINE(x, y) x##y
94#define COMBINE2(x, y) COMBINE(x, y)
95
96#if EYE_AI_CORE_ENABLE_TRACY_PROFILER
97
98#include <tracy/Tracy.hpp>
99
100#define PROFILE_SCOPE(name, profiling_frame) \
101 ZoneScopedN(name); \
102 const ProfileScope COMBINE2(__profile_scope_, __LINE__)( \
103 name, profiling_frame \
104 );
105
106#define PROFILE_DEPTH_SCOPE(name) \
107 PROFILE_SCOPE(name, get_depth_profiling_frame())
108
109#define PROFILE_CAMERA_SCOPE(name) \
110 PROFILE_SCOPE(name, get_camera_profiling_frame())
111
112#define PROFILE_OBJECT_SCOPE(name) \
113 PROFILE_SCOPE(name, get_object_profiling_frame())
114
115#define PROFILE_AUDIO_SCOPE(name) \
116 PROFILE_SCOPE(name, get_audio_profiling_frame())
117
118#define PROFILE_FUNCTION(profiling_frame) \
119 ZoneScopedN(FUNCTION_NAME()); \
120 const ProfileScope COMBINE2(__profile_scope_, __LINE__)( \
121 FUNCTION_NAME(), profiling_frame \
122 );
123
124#define PROFILE_DEPTH_FUNCTION() PROFILE_FUNCTION(get_depth_profiling_frame())
125
126#define PROFILE_CAMERA_FUNCTION() PROFILE_FUNCTION(get_camera_profiling_frame())
127
128#define PROFILE_OBJECT_FUNCTION() PROFILE_FUNCTION(get_object_profiling_frame())
129
130#define PROFILE_AUDIO_FUNCTION() PROFILE_FUNCTION(get_audio_profiling_frame())
131
132#else
133
134#define PROFILE_SCOPE(name, profiling_frame) \
135 const ProfileScope COMBINE2(__profile_scope_, __LINE__)( \
136 name, profiling_frame \
137 );
138
139#define PROFILE_DEPTH_SCOPE(name) \
140 PROFILE_SCOPE(name, get_depth_profiling_frame())
141
142#define PROFILE_CAMERA_SCOPE(name) \
143 PROFILE_SCOPE(name, get_camera_profiling_frame())
144
145#define PROFILE_OBJECT_SCOPE(name) \
146 PROFILE_SCOPE(name, get_object_profiling_frame())
147
148#define PROFILE_AUDIO_SCOPE(name) \
149 PROFILE_SCOPE(name, get_audio_profiling_frame())
150
151#define PROFILE_FUNCTION(profiling_frame) \
152 const ProfileScope COMBINE2(__profile_scope_, __LINE__)( \
153 FUNCTION_NAME(), profiling_frame \
154 );
155
156#define PROFILE_DEPTH_FUNCTION() PROFILE_FUNCTION(get_depth_profiling_frame())
157
158#define PROFILE_CAMERA_FUNCTION() PROFILE_FUNCTION(get_camera_profiling_frame())
159
160#define PROFILE_OBJECT_FUNCTION() PROFILE_FUNCTION(get_object_profiling_frame())
161
162#define PROFILE_AUDIO_FUNCTION() PROFILE_FUNCTION(get_audio_profiling_frame())
163
164#endif
void set_last_depth_profiling_frame_formatted(std::string &&formatted)
Definition Profiling.cpp:116
ProfilingFrame & get_object_profiling_frame()
Definition Profiling.cpp:132
void set_last_object_profiling_frame_formatted(std::string &&formatted)
Definition Profiling.cpp:133
std::string get_last_object_profiling_frame_formatted()
Definition Profiling.cpp:136
std::string get_last_audio_profiling_frame_formatted()
Definition Profiling.cpp:144
ProfilingFrame & get_camera_profiling_frame()
Definition Profiling.cpp:124
void set_last_audio_profiling_frame_formatted(std::string &&formatted)
Definition Profiling.cpp:141
ProfilingFrame & get_depth_profiling_frame()
Definition Profiling.cpp:122
std::string get_last_depth_profiling_frame_formatted()
Definition Profiling.cpp:119
std::chrono::high_resolution_clock profile_clock
Definition Profiling.hpp:9
void set_last_camera_profiling_frame_formatted(std::string &&formatted)
Definition Profiling.cpp:125
ProfilingFrame & get_audio_profiling_frame()
Definition Profiling.cpp:140
std::string get_last_camera_profiling_frame_formatted()
Definition Profiling.cpp:128
collection of profile records from different threads (lock-free thread-safe)
Definition Profiling.hpp:52
void end_scope(const ProfileScopeRecord &scope) noexcept
Definition Profiling.cpp:48
std::string finish()
Definition Profiling.cpp:53
ProfilingFrame(std::string_view name) noexcept
Definition Profiling.hpp:54
int start_scope() noexcept
returns the scopes depth, should always include calling end_scope after
Definition Profiling.cpp:44
Definition Profiling.hpp:32
ProfileScopeRecord()=default
ProfileScopeRecord(std::string_view name, int scope_depth, profile_clock::time_point start, profile_clock::duration duration)
Definition Profiling.hpp:39
std::string formatted() const
Definition Profiling.cpp:37
int scope_depth
Definition Profiling.hpp:34
std::string_view name
Definition Profiling.hpp:33
profile_clock::time_point start
Definition Profiling.hpp:35
profile_clock::duration duration
Definition Profiling.hpp:36
void operator=(ProfileScope &&)=delete
void operator=(const ProfileScope &)=delete
ProfileScope(ProfileScope &&)=delete
~ProfileScope() noexcept
Definition Profiling.cpp:32
ProfileScope(const ProfileScope &)=delete
ProfileScope(std::string_view name, ProfilingFrame &frame)
Definition Profiling.cpp:28