From 7b53c95832a84ab4dca15f74592d8de1f46c9552 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 4 Jun 2022 15:59:58 +0000 Subject: [PATCH] Track OpenSSL memory usage. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3890 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/main.c | 1 + src/task.c | 3 ++- src/util.js.c | 71 +++++++++++++++++++++++++++++++++++++++++---------- src/util.js.h | 5 +++- 4 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index 92727e5b..42fa2f6a 100644 --- a/src/main.c +++ b/src/main.c @@ -629,6 +629,7 @@ int main(int argc, char* argv[]) prctl(PR_SET_PDEATHSIG, SIGKILL); #endif tf_util_replace_uv_allocator(); + tf_util_replace_tls_allocator(); uv_setup_args(argc, argv); tf_taskstub_startup(); diff --git a/src/task.c b/src/task.c index 638a4e5e..e74703b8 100644 --- a/src/task.c +++ b/src/task.c @@ -739,7 +739,8 @@ static JSValue _tf_task_getStats(JSContext* context, JSValueConst this_val, int JS_ComputeMemoryUsage(runtime, &js); JS_SetPropertyStr(context, result, "js_malloc_percent", JS_NewFloat64(context, 100.0f * js.malloc_size / total_memory)); - JS_SetPropertyStr(context, result, "uv_malloc_percent", JS_NewFloat64(context, 100.0f * tf_util_uv_get_malloc_size() / total_memory)); + JS_SetPropertyStr(context, result, "uv_malloc_percent", JS_NewFloat64(context, 100.0f * tf_util_get_uv_malloc_size() / total_memory)); + JS_SetPropertyStr(context, result, "tls_malloc_percent", JS_NewFloat64(context, 100.0f * tf_util_get_tls_malloc_size() / total_memory)); JS_SetPropertyStr(context, result, "socket_count", JS_NewInt32(context, tf_socket_get_count())); JS_SetPropertyStr(context, result, "socket_open_count", JS_NewInt32(context, tf_socket_get_open_count())); diff --git a/src/util.js.c b/src/util.js.c index 7c12025c..113373a4 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -7,16 +7,19 @@ #include +#include + #include static int64_t s_uv_malloc_size; +static int64_t s_tls_malloc_size; -static void* _tf_uv_alloc(size_t size) +static void* _tf_alloc(int64_t* total, size_t size) { void* ptr = malloc(size + sizeof(size_t)); if (ptr) { - __atomic_add_fetch(&s_uv_malloc_size, size, __ATOMIC_RELAXED); + __atomic_add_fetch(total, size, __ATOMIC_RELAXED); memcpy(ptr, &size, sizeof(size_t)); return (void*)((intptr_t)ptr + sizeof(size_t)); } @@ -26,7 +29,7 @@ static void* _tf_uv_alloc(size_t size) } } -static void* _tf_uv_realloc(void* ptr, size_t size) +static void* _tf_realloc(int64_t* total, void* ptr, size_t size) { void* old_ptr = ptr ? (void*)((intptr_t)ptr - sizeof(size_t)) : NULL; size_t old_size = 0; @@ -37,17 +40,39 @@ static void* _tf_uv_realloc(void* ptr, size_t size) void* new_ptr = realloc(old_ptr, size + sizeof(size_t)); if (new_ptr) { - __atomic_add_fetch(&s_uv_malloc_size, (int64_t)size - (int64_t)old_size, __ATOMIC_RELAXED); + __atomic_add_fetch(total, (int64_t)size - (int64_t)old_size, __ATOMIC_RELAXED); memcpy(new_ptr, &size, sizeof(size_t)); return (void*)((intptr_t)new_ptr + sizeof(size_t)); } else { - __atomic_sub_fetch(&s_uv_malloc_size, old_size, __ATOMIC_RELAXED); + __atomic_sub_fetch(total, old_size, __ATOMIC_RELAXED); return NULL; } } +static void _tf_free(int64_t* total, void* ptr) +{ + if (ptr) + { + void* old_ptr = (void*)((intptr_t)ptr - sizeof(size_t)); + size_t size = 0; + memcpy(&size, old_ptr, sizeof(size_t)); + __atomic_sub_fetch(total, size, __ATOMIC_RELAXED); + free(old_ptr); + } +} + +static void* _tf_uv_alloc(size_t size) +{ + return _tf_alloc(&s_uv_malloc_size, size); +} + +static void* _tf_uv_realloc(void* ptr, size_t size) +{ + return _tf_realloc(&s_uv_malloc_size, ptr, size); +} + static void* _tf_uv_calloc(size_t nmemb, size_t size) { void* ptr = calloc(1, nmemb * size + sizeof(size_t)); @@ -66,14 +91,7 @@ static void* _tf_uv_calloc(size_t nmemb, size_t size) static void _tf_uv_free(void* ptr) { - if (ptr) - { - void* old_ptr = (void*)((intptr_t)ptr - sizeof(size_t)); - size_t size = 0; - memcpy(&size, old_ptr, sizeof(size_t)); - __atomic_sub_fetch(&s_uv_malloc_size, size, __ATOMIC_RELAXED); - free(old_ptr); - } + _tf_free(&s_uv_malloc_size, ptr); } void tf_util_replace_uv_allocator() @@ -81,11 +99,36 @@ void tf_util_replace_uv_allocator() uv_replace_allocator(_tf_uv_alloc, _tf_uv_realloc, _tf_uv_calloc, _tf_uv_free); } -size_t tf_util_uv_get_malloc_size() +size_t tf_util_get_uv_malloc_size() { return s_uv_malloc_size; } +void* _tf_tls_alloc(size_t size, const char* file, int line) +{ + return _tf_alloc(&s_tls_malloc_size, size); +} + +void* _tf_tls_realloc(void* ptr, size_t size, const char* file, int line) +{ + return _tf_realloc(&s_tls_malloc_size, ptr, size); +} + +void _tf_tls_free(void* ptr, const char* file, int line) +{ + _tf_free(&s_tls_malloc_size, ptr); +} + +void tf_util_replace_tls_allocator() +{ + CRYPTO_set_mem_functions(_tf_tls_alloc, _tf_tls_realloc, _tf_tls_free); +} + +size_t tf_util_get_tls_malloc_size() +{ + return s_tls_malloc_size; +} + static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { size_t length = 0; diff --git a/src/util.js.h b/src/util.js.h index 9bd00e03..cfb01f86 100644 --- a/src/util.js.h +++ b/src/util.js.h @@ -5,7 +5,10 @@ #include void tf_util_replace_uv_allocator(); -size_t tf_util_uv_get_malloc_size(); +size_t tf_util_get_uv_malloc_size(); + +void tf_util_replace_tls_allocator(); +size_t tf_util_get_tls_malloc_size(); void tf_util_register(JSContext* context); JSValue tf_util_utf8_decode(JSContext* context, JSValue value);