Brute force memory tracking.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4186 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-02-18 21:00:39 +00:00
parent 53cb80ebf7
commit 88c7d91858
8 changed files with 347 additions and 66 deletions

View File

@ -29,9 +29,6 @@
#include <backtrace.h>
#ifndef _WIN32
#ifndef __ANDROID__
#include <execinfo.h>
#endif
#include <unistd.h>
#endif
@ -827,6 +824,29 @@ static JSValue _tf_task_getDebug(JSContext* context, JSValueConst this_val, int
return result;
}
static JSValue _tf_task_getAllocations(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_NewObject(context);
int count = 0;
tf_mem_allocation_t* allocation_info = tf_mem_summarize_allocations(&count);
JSValue allocations = JS_NewArray(context);
JS_SetPropertyStr(context, result, "allocations", allocations);
for (int i = 0; i < count; i++)
{
JSValue allocation = JS_NewObject(context);
JS_SetPropertyStr(context, allocation, "size", JS_NewInt64(context, allocation_info[i].size));
JS_SetPropertyStr(context, allocation, "count", JS_NewInt32(context, allocation_info[i].count));
const char* stack = tf_util_backtrace_to_string(allocation_info[i].frames, allocation_info[i].frames_count);
JS_SetPropertyStr(context, allocation, "stack", JS_NewString(context, stack));
tf_free((void*)stack);
JS_SetPropertyUint32(context, allocations, i, allocation);
}
return result;
}
static JSValue _tf_task_disconnectionsDebug(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_task_t* task = JS_GetContextOpaque(context);
@ -1152,15 +1172,9 @@ JSValue tf_task_allocate_promise(tf_task_t* task, promiseid_t* out_promise)
size_t length = 0;
const char* stack = JS_ToCStringLen(task->_context, &length, stack_value);
uint32_t stack_hash = fnv32a((const void*)stack, (int)length, 0);
int count = 0;
void* buffer[32];
#ifdef _WIN32
count = CaptureStackBackTrace(0, sizeof(buffer) / sizeof(*buffer), buffer, NULL);
int count = tf_util_backtrace(buffer, sizeof(buffer) / sizeof(*buffer));
stack_hash = fnv32a((const void*)buffer, sizeof(void*) * count, stack_hash);
#elif !defined(__ANDROID__)
count = backtrace(buffer, sizeof(buffer) / sizeof(*buffer));
stack_hash = fnv32a((const void*)buffer, sizeof(void*) * count, stack_hash);
#endif
_add_promise_stack(task, stack_hash, stack, buffer, count);
JS_FreeCString(task->_context, stack);
JS_FreeValue(task->_context, stack_value);
@ -1588,6 +1602,7 @@ void tf_task_activate(tf_task_t* task)
JS_SetPropertyStr(context, global, "trace", JS_NewCFunction(context, _tf_task_trace, "trace", 1));
JS_SetPropertyStr(context, global, "getStats", JS_NewCFunction(context, _tf_task_getStats, "getStats", 0));
JS_SetPropertyStr(context, global, "getDebug", JS_NewCFunction(context, _tf_task_getDebug, "getDebug", 0));
JS_SetPropertyStr(context, global, "getAllocations", JS_NewCFunction(context, _tf_task_getAllocations, "getAllocations", 0));
JS_SetPropertyStr(context, global, "disconnectionsDebug", JS_NewCFunction(context, _tf_task_disconnectionsDebug, "disconnectionsDebug", 0));
}
else