Adds ability to debug stringnames

* References (which include hash tables) can be profiled with --debug-stringnames
This commit is contained in:
reduz 2021-07-20 17:01:18 -03:00
parent 693f9b4e20
commit cee905f04f
3 changed files with 90 additions and 4 deletions

View File

@ -48,6 +48,10 @@ StringName _scs_create(const char *p_chr, bool p_static) {
bool StringName::configured = false;
Mutex StringName::mutex;
#ifdef DEBUG_ENABLED
bool StringName::debug_stringname = false;
#endif
void StringName::setup() {
ERR_FAIL_COND(configured);
for (int i = 0; i < STRING_TABLE_LEN; i++) {
@ -59,6 +63,23 @@ void StringName::setup() {
void StringName::cleanup() {
MutexLock lock(mutex);
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
Vector<_Data *> data;
for (int i = 0; i < STRING_TABLE_LEN; i++) {
_Data *d = _table[i];
while (d) {
data.push_back(d);
d = d->next;
}
}
print_line("\nStringName Reference Ranking:\n");
data.sort_custom<DebugSortReferences>();
for (int i = 0; i < MIN(100, data.size()); i++) {
print_line(itos(i + 1) + ": " + data[i]->get_name() + " - " + itos(data[i]->debug_references));
}
}
#endif
int lost_strings = 0;
for (int i = 0; i < STRING_TABLE_LEN; i++) {
while (_table[i]) {
@ -192,8 +213,14 @@ StringName::StringName(const char *p_name, bool p_static) {
if (p_static) {
_data->static_count.increment();
}
return;
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
_data->debug_references++;
}
#endif
}
return;
}
_data = memnew(_Data);
@ -205,6 +232,13 @@ StringName::StringName(const char *p_name, bool p_static) {
_data->cname = nullptr;
_data->next = _table[idx];
_data->prev = nullptr;
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
// Keep in memory, force static.
_data->refcount.ref();
_data->static_count.increment();
}
#endif
if (_table[idx]) {
_table[idx]->prev = _data;
}
@ -240,6 +274,11 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) {
if (p_static) {
_data->static_count.increment();
}
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
_data->debug_references++;
}
#endif
return;
}
}
@ -253,6 +292,13 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) {
_data->cname = p_static_string.ptr;
_data->next = _table[idx];
_data->prev = nullptr;
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
// Keep in memory, force static.
_data->refcount.ref();
_data->static_count.increment();
}
#endif
if (_table[idx]) {
_table[idx]->prev = _data;
}
@ -288,6 +334,11 @@ StringName::StringName(const String &p_name, bool p_static) {
if (p_static) {
_data->static_count.increment();
}
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
_data->debug_references++;
}
#endif
return;
}
}
@ -301,6 +352,14 @@ StringName::StringName(const String &p_name, bool p_static) {
_data->cname = nullptr;
_data->next = _table[idx];
_data->prev = nullptr;
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
// Keep in memory, force static.
_data->refcount.ref();
_data->static_count.increment();
}
#endif
if (_table[idx]) {
_table[idx]->prev = _data;
}
@ -331,6 +390,12 @@ StringName StringName::search(const char *p_name) {
}
if (_data && _data->refcount.ref()) {
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
_data->debug_references++;
}
#endif
return StringName(_data);
}
@ -388,6 +453,11 @@ StringName StringName::search(const String &p_name) {
}
if (_data && _data->refcount.ref()) {
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
_data->debug_references++;
}
#endif
return StringName(_data);
}

View File

@ -52,10 +52,11 @@ class StringName {
struct _Data {
SafeRefCount refcount;
SafeNumeric<uint32_t> static_count;
SafeRefCount static_refcount;
const char *cname = nullptr;
String name;
#ifdef DEBUG_ENABLED
uint32_t debug_references = 0;
#endif
String get_name() const { return cname ? String(cname) : name; }
int idx = 0;
uint32_t hash = 0;
@ -81,6 +82,15 @@ class StringName {
static void setup();
static void cleanup();
static bool configured;
#ifdef DEBUG_ENABLED
struct DebugSortReferences {
bool operator()(const _Data *p_left, const _Data *p_right) const {
return p_left->debug_references > p_right->debug_references;
}
};
static bool debug_stringname;
#endif
StringName(_Data *p_data) { _data = p_data; }
@ -158,6 +168,10 @@ public:
unref();
}
}
#ifdef DEBUG_ENABLED
static void set_debug_stringnames(bool p_enable) { debug_stringname = p_enable; }
#endif
};
bool operator==(const String &p_name, const StringName &p_string_name);

View File

@ -986,11 +986,13 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
} else if (I->get() == "-d" || I->get() == "--debug") {
debug_uri = "local://";
OS::get_singleton()->_debug_stdout = true;
#if defined(DEBUG_ENABLED) && !defined(SERVER_ENABLED)
#if defined(DEBUG_ENABLED)
} else if (I->get() == "--debug-collisions") {
debug_collisions = true;
} else if (I->get() == "--debug-navigation") {
debug_navigation = true;
} else if (I->get() == "--debug-stringnames") {
StringName::set_debug_stringnames(true);
#endif
} else if (I->get() == "--remote-debug") {
if (I->next()) {