Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
profiler.h
1#pragma once
2#include "gaia/config/config.h"
3
4#ifndef GAIA_PROFILER_CPU
5 #define GAIA_PROFILER_CPU 0
6#endif
7#ifndef GAIA_PROFILER_MEM
8 #define GAIA_PROFILER_MEM 0
9#endif
10
11#if GAIA_PROFILER_CPU || GAIA_PROFILER_MEM
12// Keep it small on Windows
13// TODO: What if user doesn't want this?
14// #if GAIA_PLATFORM_WINDOWS && !defined(WIN32_LEAN_AND_MEAN)
15// #define WIN32_LEAN_AND_MEAN
16// #endif
17GAIA_MSVC_WARNING_PUSH()
18GAIA_MSVC_WARNING_DISABLE(4668)
19 #include <tracy/Tracy.hpp>
20 #include <tracy/TracyC.h>
21GAIA_MSVC_WARNING_POP()
22#endif
23
24#if GAIA_PROFILER_CPU
25
26namespace tracy {
27
28 template <class T>
29 class gaia_LockableExt: public Lockable<T> {
30 public:
31 tracy_force_inline gaia_LockableExt(const SourceLocationData* srcloc): Lockable<T>(srcloc) {}
32
37 T& get_lock() {
38 // Assuming TLockable is non-virtual and the memory layout is as follows:
39 // T m_lockable;
40 // LockableCtx m_ctx;
41 // ...
42 //
43 // m_lockable is the first member. We simply cast to it.
44 T* ptr_lockable = reinterpret_cast<T*>(this);
45 return (T&)*(ptr_lockable + 0);
46 // return (T&)m_lockable;
47 }
48 };
49 #define LockableBaseExt(type) tracy::gaia_LockableExt<type>
50 #define TracyLockableExt(type, varname) \
51 tracy::gaia_LockableExt<type> varname { \
52 []() -> const tracy::SourceLocationData* { \
53 static constexpr tracy::SourceLocationData srcloc{nullptr, #type " " #varname, TracyFile, TracyLine, 0}; \
54 return &srcloc; \
55 }() \
56 }
57
59 struct ZoneRT {
60 TracyCZoneCtx m_ctx;
61
62 ZoneRT(const char* name, const char* file, uint32_t line, const char* function) {
63 const auto srcloc =
64 ___tracy_alloc_srcloc_name(line, file, strlen(file), function, strlen(function), name, strlen(name), 0);
65 m_ctx = ___tracy_emit_zone_begin_alloc(srcloc, 1);
66 }
67 ~ZoneRT() {
68 TracyCZoneEnd(m_ctx);
69 }
70 };
71
72 struct ScopeStack {
73 static constexpr uint32_t StackSize = 64;
74
75 uint32_t count;
76 TracyCZoneCtx buffer[StackSize];
77 };
78
79 inline thread_local ScopeStack t_ScopeStack;
80
81 inline void ZoneBegin(const ___tracy_source_location_data* srcloc) {
82 auto& stack = t_ScopeStack;
83 const auto pos = stack.count++;
84 if (pos < ScopeStack::StackSize) {
85 stack.buffer[pos] = ___tracy_emit_zone_begin(srcloc, 1);
86 }
87 }
88
89 inline void ZoneRTBegin(uint64_t srcloc) {
90 auto& stack = t_ScopeStack;
91 const auto pos = stack.count++;
92 if (pos < ScopeStack::StackSize)
93 stack.buffer[pos] = ___tracy_emit_zone_begin_alloc(srcloc, 1);
94 }
95
96 inline void ZoneEnd() {
97 auto& stack = t_ScopeStack;
98 GAIA_ASSERT(stack.count > 0);
99 const auto pos = --stack.count;
100 if (pos < ScopeStack::StackSize)
101 ___tracy_emit_zone_end(stack.buffer[pos]);
102 }
103} // namespace tracy
104
105 #define TRACY_ZoneNamedRT(name, function) \
106 tracy::ZoneRT TracyConcat(__tracy_zone_dynamic, __LINE__)(name, __FILE__, __LINE__, function);
107
108 #define TRACY_ZoneNamedRTBegin(name, function) \
109 tracy::ZoneRTBegin(___tracy_alloc_srcloc_name( \
110 __LINE__, __FILE__, strlen(__FILE__), function, strlen(function), name, strlen(name), 0));
111
112 #define TRACY_ZoneBegin(name, function) \
113 static constexpr ___tracy_source_location_data TracyConcat(__tracy_source_location, __LINE__) { \
114 name "", function, __FILE__, uint32_t(__LINE__), 0, \
115 }
116 #define TRACY_ZoneEnd() tracy::ZoneEnd
117
118 #define GAIA_PROF_START_IMPL(name, function) \
119 TRACY_ZoneBegin(name, function); \
120 tracy::ZoneBegin(&TracyConcat(__tracy_source_location, __LINE__));
121
122 #define GAIA_PROF_STOP_IMPL() TRACY_ZoneEnd()
123
124 #define GAIA_PROF_SCOPE_IMPL(name) ZoneNamedN(GAIA_CONCAT(___tracy_scoped_zone_, __LINE__), name "", 1)
125 #define GAIA_PROF_SCOPE_DYN_IMPL(name) TRACY_ZoneNamedRT(name, GAIA_PRETTY_FUNCTION)
126
127//------------------------------------------------------------------------
128// Tracy profiler GAIA implementation
129//------------------------------------------------------------------------
130
132 #if !defined(GAIA_PROF_FRAME)
133 #define GAIA_PROF_FRAME() FrameMark
134 #endif
136 #if !defined(GAIA_PROF_SCOPE)
137 #define GAIA_PROF_SCOPE(zoneName) GAIA_PROF_SCOPE_IMPL(#zoneName)
138 #endif
140 #if !defined(GAIA_PROF_SCOPE2)
141 #define GAIA_PROF_SCOPE2(zoneName) GAIA_PROF_SCOPE_DYN_IMPL(zoneName)
142 #endif
144 #if !defined(GAIA_PROF_START)
145 #define GAIA_PROF_START(zoneName) GAIA_PROF_START_IMPL(#zoneName, GAIA_PRETTY_FUNCTION)
146 #endif
148 #if !defined(GAIA_PROF_STOP)
149 #define GAIA_PROF_STOP() GAIA_PROF_STOP_IMPL()
150 #endif
152 #if !defined(GAIA_PROF_MUTEX_BASE)
153 #define GAIA_PROF_EXTRACT_MUTEX(name) (name).get_lock()
154 #define GAIA_PROF_MUTEX_BASE(type) LockableBaseExt<type>
155 #define GAIA_PROF_LOCK_MARK(name) LockMark(name)
156 #endif
157 #if !defined(GAIA_PROF_MUTEX)
158 #define GAIA_PROF_MUTEX(type, name) TracyLockableExt(type, name)
159 #endif
161 #if !defined(GAIA_PROF_USE_PROFILER_THREAD_NAME)
162 #define GAIA_PROF_USE_PROFILER_THREAD_NAME 1
163 #endif
165 #if !defined(GAIA_PROF_THREAD_NAME)
166 #define GAIA_PROF_THREAD_NAME(name) tracy::SetThreadName(name)
167 #endif
168#else
170 #if !defined(GAIA_PROF_FRAME)
171 #define GAIA_PROF_FRAME()
172 #endif
174 #if !defined(GAIA_PROF_SCOPE)
175 #define GAIA_PROF_SCOPE(zoneName)
176 #endif
178 #if !defined(GAIA_PROF_SCOPE2)
179 #define GAIA_PROF_SCOPE2(zoneName)
180 #endif
182 #if !defined(GAIA_PROF_START)
183 #define GAIA_PROF_START(zoneName)
184 #endif
186 #if !defined(GAIA_PROF_STOP)
187 #define GAIA_PROF_STOP()
188 #endif
190 #if !defined(GAIA_PROF_MUTEX_BASE)
191 #define GAIA_PROF_EXTRACT_MUTEX(name) name
192 #define GAIA_PROF_MUTEX_BASE(type) type
193 #define GAIA_PROF_LOCK_MARK(name)
194 #endif
195 #if !defined(GAIA_PROF_MUTEX)
196 #define GAIA_PROF_MUTEX(type, name) GAIA_PROF_MUTEX_BASE(type) name
197 #endif
199 #if !defined(GAIA_PROF_USE_PROFILER_THREAD_NAME)
200 #define GAIA_PROF_USE_PROFILER_THREAD_NAME 0
201 #endif
203 #if !defined(GAIA_PROF_THREAD_NAME)
204 #define GAIA_PROF_THREAD_NAME(name)
205 #endif
206#endif
207
208#if GAIA_PROFILER_MEM
210 #if !defined(GAIA_PROF_ALLOC)
211 #define GAIA_PROF_ALLOC(ptr, size) TracyAlloc(ptr, size)
212 #endif
214 #if !defined(GAIA_PROF_ALLOC2)
215 #define GAIA_PROF_ALLOC2(ptr, size, name) TracyAllocN(ptr, size, name)
216 #endif
218 #if !defined(GAIA_PROF_FREE)
219 #define GAIA_PROF_FREE(ptr) TracyFree(ptr)
220 #endif
222 #if !defined(GAIA_PROF_FREE2)
223 #define GAIA_PROF_FREE2(ptr, name) TracyFreeN(ptr, name)
224 #endif
225#else
227 #if !defined(GAIA_PROF_ALLOC)
228 #define GAIA_PROF_ALLOC(ptr, size)
229 #endif
231 #if !defined(GAIA_PROF_ALLOC2)
232 #define GAIA_PROF_ALLOC2(ptr, size, name)
233 #endif
235 #if !defined(GAIA_PROF_FREE)
236 #define GAIA_PROF_FREE(ptr)
237 #endif
239 #if !defined(GAIA_PROF_FREE2)
240 #define GAIA_PROF_FREE2(ptr, name)
241 #endif
242#endif