Skip to content

Commit fa5c17d

Browse files
enetheruIvorforce
andcommitted
Add memory profile macros to profiling with tracy implementation
Co-authored-by: Lukas Tenbrink <[email protected]>
1 parent ef34c3d commit fa5c17d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

core/os/memory.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "memory.h"
3232

33+
#include "core/profiling/profiling.h"
3334
#include "core/templates/safe_refcount.h"
3435

3536
#include <cstdlib>
@@ -68,6 +69,7 @@ void *Memory::alloc_aligned_static(size_t p_bytes, size_t p_alignment) {
6869
if ((p1 = (void *)malloc(p_bytes + p_alignment - 1 + sizeof(uint32_t))) == nullptr) {
6970
return nullptr;
7071
}
72+
GodotProfileAlloc(p1, p_bytes + p_alignment - 1 + sizeof(uint32_t));
7173

7274
p2 = (void *)(((uintptr_t)p1 + sizeof(uint32_t) + p_alignment - 1) & ~((p_alignment)-1));
7375
*((uint32_t *)p2 - 1) = (uint32_t)((uintptr_t)p2 - (uintptr_t)p1);
@@ -90,6 +92,7 @@ void *Memory::realloc_aligned_static(void *p_memory, size_t p_bytes, size_t p_pr
9092
void Memory::free_aligned_static(void *p_memory) {
9193
uint32_t offset = *((uint32_t *)p_memory - 1);
9294
void *p = (void *)((uint8_t *)p_memory - offset);
95+
GodotProfileFree(p);
9396
free(p);
9497
}
9598

@@ -107,6 +110,7 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
107110
} else {
108111
mem = malloc(p_bytes + (prepad ? DATA_OFFSET : 0));
109112
}
113+
GodotProfileAlloc(mem, p_bytes + (prepad ? DATA_OFFSET : 0));
110114

111115
ERR_FAIL_NULL_V(mem, nullptr);
112116

@@ -156,13 +160,16 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
156160
#endif
157161

158162
if (p_bytes == 0) {
163+
GodotProfileFree(mem);
159164
free(mem);
160165
return nullptr;
161166
} else {
162167
*s = p_bytes;
163168

169+
GodotProfileFree(mem);
164170
mem = (uint8_t *)realloc(mem, p_bytes + DATA_OFFSET);
165171
ERR_FAIL_NULL_V(mem, nullptr);
172+
GodotProfileAlloc(mem, p_bytes + DATA_OFFSET);
166173

167174
s = (uint64_t *)(mem + SIZE_OFFSET);
168175

@@ -171,7 +178,9 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
171178
return mem + DATA_OFFSET;
172179
}
173180
} else {
181+
GodotProfileFree(mem);
174182
mem = (uint8_t *)realloc(mem, p_bytes);
183+
GodotProfileAlloc(mem, p_bytes);
175184

176185
ERR_FAIL_COND_V(mem == nullptr && p_bytes > 0, nullptr);
177186

@@ -198,8 +207,10 @@ void Memory::free_static(void *p_ptr, bool p_pad_align) {
198207
_current_mem_usage.sub(*s);
199208
#endif
200209

210+
GodotProfileFree(mem);
201211
free(mem);
202212
} else {
213+
GodotProfileFree(mem);
203214
free(mem);
204215
}
205216
}

core/profiling/profiling.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
new (&__godot_tracy_zone_##m_group_name) tracy::ScopedZone(&TracyConcat(__tracy_source_location, TracyLine), TRACY_CALLSTACK, true)
6767
#endif
6868

69+
// Memory allocation
70+
#define GodotProfileAlloc(m_ptr, m_size) TracyAlloc(m_ptr, m_size)
71+
#define GodotProfileFree(m_ptr) TracyFree(m_ptr)
72+
6973
void godot_init_profiler();
7074

7175
#elif defined(GODOT_USE_PERFETTO)
@@ -98,6 +102,8 @@ struct PerfettoGroupedEventEnder {
98102
__godot_perfetto_zone_##m_group_name._end_now(); \
99103
TRACE_EVENT_BEGIN("godot", m_zone_name);
100104

105+
#define GodotProfileAlloc(m_ptr, m_size)
106+
#define GodotProfileFree(m_ptr)
101107
void godot_init_profiler();
102108

103109
#else
@@ -117,5 +123,9 @@ void godot_init_profiler();
117123
// Replace the profile zone group's current profile zone.
118124
// The new zone ends either when the next zone starts, or when the scope ends.
119125
#define GodotProfileZoneGrouped(m_group_name, m_zone_name)
120-
126+
// Tell the profiling backend that an allocation happened, with its location and size.
127+
#define GodotProfileAlloc(m_ptr, m_size)
128+
// Tell the profiling backend that an allocation was freed.
129+
// There must be a one to one correspondence of GodotProfileAlloc and GodotProfileFree calls.
130+
#define GodotProfileFree(m_ptr)
121131
#endif

0 commit comments

Comments
 (0)