diff --git a/core/client.js b/core/client.js index 22f35edc..cf14d468 100644 --- a/core/client.js +++ b/core/client.js @@ -432,8 +432,9 @@ function receive(message) { rpc_in: {group: 'rpc', name: 'in'}, rpc_out: {group: 'rpc', name: 'out'}, + arena_percent: {group: 'memory', name: 'm'}, js_malloc_percent: {group: 'memory', name: 'js'}, - memory_percent: {group: 'memory', name: 'total'}, + memory_percent: {group: 'memory', name: 'tot'}, sqlite3_memory_percent: {group: 'memory', name: 'sql'}, tf_malloc_percent: {group: 'memory', name: 'tf'}, tls_malloc_percent: {group: 'memory', name: 'tls'}, @@ -445,7 +446,7 @@ function receive(message) { import_count: {group: 'functions', name: 'imports'}, export_count: {group: 'functions', name: 'exports'}, }; - const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff', '#f00']; + const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff', '#f00', '#888']; let graph_key = k_groups[key]?.group || key; let graph = gGraphs[graph_key]; if (!graph) { diff --git a/src/mem.c b/src/mem.c index 6f429112..41b76a73 100644 --- a/src/mem.c +++ b/src/mem.c @@ -154,6 +154,32 @@ char* tf_strdup(const char* string) return buffer; } +void* tf_resize_vec(void* ptr, size_t size) +{ + void* alloc_ptr = ptr ? (void*)((intptr_t)ptr - sizeof(size_t)) : NULL; + size_t alloc_size = 0; + if (alloc_ptr) + { + memcpy(&alloc_size, alloc_ptr, sizeof(size_t)); + } + + if (alloc_size >= 16 * size + sizeof(size_t)) + { + /* If we've dropped significantly in size, resize down. */ + return tf_realloc(ptr, size); + } + else if (alloc_size >= size + sizeof(size_t)) + { + /* Otherwise, if we're big enough, stay the same size. */ + return ptr; + } + else + { + /* If we need to grow, overallocate 2x to give room to continue growing. */ + return tf_realloc(ptr, size * 2); + } +} + size_t tf_mem_get_tf_malloc_size() { return s_tf_malloc_size; diff --git a/src/mem.h b/src/mem.h index bfb828a5..d2769b89 100644 --- a/src/mem.h +++ b/src/mem.h @@ -14,3 +14,5 @@ void* tf_malloc(size_t size); void* tf_realloc(void* ptr, size_t size); void tf_free(void* ptr); char* tf_strdup(const char* string); + +void* tf_resize_vec(void* ptr, size_t size); diff --git a/src/task.c b/src/task.c index fb1e55f0..c744fd1d 100644 --- a/src/task.c +++ b/src/task.c @@ -15,6 +15,7 @@ #include "util.js.h" #include +#include #include #include #include @@ -672,7 +673,7 @@ exportid_t tf_task_export_function(tf_task_t* task, tf_taskstub_t* to, JSValue f }; int index = _insert_index(&id, task->_exports, task->_export_count, sizeof(export_record_t*), _export_compare); - task->_exports = tf_realloc(task->_exports, sizeof(export_record_t*) * (task->_export_count + 1)); + task->_exports = tf_resize_vec(task->_exports, sizeof(export_record_t*) * (task->_export_count + 1)); if (task->_export_count - index) { memmove(task->_exports + index + 1, task->_exports + index, sizeof(export_record_t*) * (task->_export_count - index)); @@ -744,6 +745,9 @@ static JSValue _tf_task_getStats(JSContext* context, JSValueConst this_val, int JS_SetPropertyStr(context, result, "tls_malloc_percent", JS_NewFloat64(context, 100.0f * tf_mem_get_tls_malloc_size() / total_memory)); JS_SetPropertyStr(context, result, "tf_malloc_percent", JS_NewFloat64(context, 100.0f * tf_mem_get_tf_malloc_size() / total_memory)); + struct mallinfo mi = mallinfo(); + JS_SetPropertyStr(context, result, "arena_percent", JS_NewFloat64(context, 100.0f * mi.arena / 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())); @@ -1200,7 +1204,7 @@ JSValue tf_task_allocate_promise(tf_task_t* task, promiseid_t* out_promise) }; JSValue result = JS_NewPromiseCapability(task->_context, promise.values); int index = _insert_index((void*)(intptr_t)promiseId, task->_promises, task->_promise_count, sizeof(promise_t), _promise_compare); - task->_promises = tf_realloc(task->_promises, sizeof(promise_t) * (task->_promise_count + 1)); + task->_promises = tf_resize_vec(task->_promises, sizeof(promise_t) * (task->_promise_count + 1)); if (task->_promise_count - index) { memmove(task->_promises + index + 1, task->_promises + index, sizeof(promise_t) * (task->_promise_count - index)); @@ -1717,7 +1721,7 @@ JSValue tf_task_add_import(tf_task_t* task, taskid_t stub_id, exportid_t export_ }; int index = _insert_index(import, task->_imports, task->_import_count, sizeof(import_record_t*), _import_compare); - task->_imports = tf_realloc(task->_imports, sizeof(import_record_t*) * (task->_import_count + 1)); + task->_imports = tf_resize_vec(task->_imports, sizeof(import_record_t*) * (task->_import_count + 1)); if (task->_import_count - index) { memmove(task->_imports + index + 1, task->_imports + index, sizeof(import_record_t*) * (task->_import_count - index));