1
0
mirror of https://github.com/godotengine/godot.git synced 2025-12-05 17:15:09 +00:00

Add support for profiling GDScript with tracy.

This adds macro `GodotProfileZoneGroupedFirstScript`, and uses interning for speedy lookups.

Co-authored-by: Samuel Nicholas <nicholas.samuel@gmail.com>
This commit is contained in:
Lukas Tenbrink
2025-11-28 15:10:58 +01:00
parent 9dd6c4dbac
commit acefbbbbcd
11 changed files with 201 additions and 1 deletions

View File

@@ -31,11 +31,169 @@
#include "profiling.h"
#if defined(GODOT_USE_TRACY)
// Use the tracy profiler.
#include "core/os/mutex.h"
#include "core/templates/paged_allocator.h"
namespace TracyInternal {
static bool configured = false;
// Implementation similar to StringName.
struct StringInternData {
StringName name;
CharString name_utf8;
uint32_t hash = 0;
StringInternData *prev = nullptr;
StringInternData *next = nullptr;
StringInternData() {}
};
struct SourceLocationInternData {
const StringInternData *file;
const StringInternData *function;
tracy::SourceLocationData source_location_data;
uint32_t function_ptr_hash = 0;
SourceLocationInternData *prev = nullptr;
SourceLocationInternData *next = nullptr;
SourceLocationInternData() {}
};
struct TracyInternTable {
constexpr static uint32_t TABLE_BITS = 16;
constexpr static uint32_t TABLE_LEN = 1 << TABLE_BITS;
constexpr static uint32_t TABLE_MASK = TABLE_LEN - 1;
static inline BinaryMutex mutex;
static inline SourceLocationInternData *source_location_table[TABLE_LEN];
static inline PagedAllocator<SourceLocationInternData> source_location_allocator;
static inline StringInternData *string_table[TABLE_LEN];
static inline PagedAllocator<StringInternData> string_allocator;
};
const StringInternData *_intern_name(const StringName &p_name) {
CRASH_COND(!configured);
const uint32_t hash = p_name.hash();
const uint32_t idx = hash & TracyInternTable::TABLE_MASK;
StringInternData *_data = TracyInternTable::string_table[idx];
while (_data) {
if (_data->hash == hash) {
return _data;
}
_data = _data->next;
}
_data = TracyInternTable::string_allocator.alloc();
_data->name = p_name;
_data->name_utf8 = p_name.operator String().utf8();
_data->next = TracyInternTable::string_table[idx];
_data->prev = nullptr;
if (TracyInternTable::string_table[idx]) {
TracyInternTable::string_table[idx]->prev = _data;
}
TracyInternTable::string_table[idx] = _data;
return _data;
}
const char *intern_name(const StringName &p_name) {
MutexLock lock(TracyInternTable::mutex);
return _intern_name(p_name)->name_utf8.get_data();
}
const tracy::SourceLocationData *intern_source_location(const void *p_function_ptr, const StringName &p_file, const StringName &p_function, uint32_t p_line) {
CRASH_COND(!configured);
const uint32_t hash = HashMapHasherDefault::hash(p_function_ptr);
const uint32_t idx = hash & TracyInternTable::TABLE_MASK;
MutexLock lock(TracyInternTable::mutex);
SourceLocationInternData *_data = TracyInternTable::source_location_table[idx];
while (_data) {
if (_data->function_ptr_hash == hash && _data->source_location_data.line == p_line && _data->file->name == p_file && _data->function->name == p_function) {
return &_data->source_location_data;
}
_data = _data->next;
}
_data = TracyInternTable::source_location_allocator.alloc();
_data->function_ptr_hash = hash;
_data->file = _intern_name(p_file);
_data->function = _intern_name(p_function);
_data->source_location_data.file = _data->file->name_utf8.get_data();
_data->source_location_data.function = _data->function->name_utf8.get_data();
_data->source_location_data.name = _data->source_location_data.function;
_data->source_location_data.line = p_line;
_data->source_location_data.color = 0x478cbf; // godot_logo_blue
_data->next = TracyInternTable::source_location_table[idx];
_data->prev = nullptr;
if (TracyInternTable::source_location_table[idx]) {
TracyInternTable::source_location_table[idx]->prev = _data;
}
TracyInternTable::source_location_table[idx] = _data;
return &_data->source_location_data;
}
} // namespace TracyInternal
void godot_init_profiler() {
MutexLock lock(TracyInternal::TracyInternTable::mutex);
ERR_FAIL_COND(TracyInternal::configured);
for (uint32_t i = 0; i < TracyInternal::TracyInternTable::TABLE_LEN; i++) {
TracyInternal::TracyInternTable::source_location_table[i] = nullptr;
}
for (uint32_t i = 0; i < TracyInternal::TracyInternTable::TABLE_LEN; i++) {
TracyInternal::TracyInternTable::string_table[i] = nullptr;
}
TracyInternal::configured = true;
// Send our first event to tracy; otherwise it doesn't start collecting data.
// FrameMark is kind of fitting because it communicates "this is where we started tracing".
FrameMark;
}
void godot_cleanup_profiler() {
MutexLock lock(TracyInternal::TracyInternTable::mutex);
ERR_FAIL_COND(!TracyInternal::configured);
for (uint32_t i = 0; i < TracyInternal::TracyInternTable::TABLE_LEN; i++) {
while (TracyInternal::TracyInternTable::source_location_table[i]) {
TracyInternal::SourceLocationInternData *d = TracyInternal::TracyInternTable::source_location_table[i];
TracyInternal::TracyInternTable::source_location_table[i] = TracyInternal::TracyInternTable::source_location_table[i]->next;
TracyInternal::TracyInternTable::source_location_allocator.free(d);
}
}
for (uint32_t i = 0; i < TracyInternal::TracyInternTable::TABLE_LEN; i++) {
while (TracyInternal::TracyInternTable::string_table[i]) {
TracyInternal::StringInternData *d = TracyInternal::TracyInternTable::string_table[i];
TracyInternal::TracyInternTable::string_table[i] = TracyInternal::TracyInternTable::string_table[i]->next;
TracyInternal::TracyInternTable::string_allocator.free(d);
}
}
TracyInternal::configured = false;
}
#elif defined(GODOT_USE_PERFETTO)
PERFETTO_TRACK_EVENT_STATIC_STORAGE();
@@ -47,8 +205,17 @@ void godot_init_profiler() {
perfetto::Tracing::Initialize(args);
perfetto::TrackEvent::Register();
}
void godot_cleanup_profiler() {
// Stub
}
#else
void godot_init_profiler() {
// Stub
}
void godot_cleanup_profiler() {
// Stub
}
#endif