forked from cory/tildefriends
		
	Track OpenSSL memory usage.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3890 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
		| @@ -629,6 +629,7 @@ int main(int argc, char* argv[]) | |||||||
| 	prctl(PR_SET_PDEATHSIG, SIGKILL); | 	prctl(PR_SET_PDEATHSIG, SIGKILL); | ||||||
| #endif | #endif | ||||||
| 	tf_util_replace_uv_allocator(); | 	tf_util_replace_uv_allocator(); | ||||||
|  | 	tf_util_replace_tls_allocator(); | ||||||
| 	uv_setup_args(argc, argv); | 	uv_setup_args(argc, argv); | ||||||
| 	tf_taskstub_startup(); | 	tf_taskstub_startup(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -739,7 +739,8 @@ static JSValue _tf_task_getStats(JSContext* context, JSValueConst this_val, int | |||||||
| 	JS_ComputeMemoryUsage(runtime, &js); | 	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, "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_count", JS_NewInt32(context, tf_socket_get_count())); | ||||||
| 	JS_SetPropertyStr(context, result, "socket_open_count", JS_NewInt32(context, tf_socket_get_open_count())); | 	JS_SetPropertyStr(context, result, "socket_open_count", JS_NewInt32(context, tf_socket_get_open_count())); | ||||||
|   | |||||||
| @@ -7,16 +7,19 @@ | |||||||
|  |  | ||||||
| #include <uv.h> | #include <uv.h> | ||||||
|  |  | ||||||
|  | #include <openssl/crypto.h> | ||||||
|  |  | ||||||
| #include <string.h> | #include <string.h> | ||||||
|  |  | ||||||
| static int64_t s_uv_malloc_size; | 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)); | 	void* ptr = malloc(size + sizeof(size_t)); | ||||||
| 	if (ptr) | 	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)); | 		memcpy(ptr, &size, sizeof(size_t)); | ||||||
| 		return (void*)((intptr_t)ptr + 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; | 	void* old_ptr = ptr ? (void*)((intptr_t)ptr - sizeof(size_t)) : NULL; | ||||||
| 	size_t old_size = 0; | 	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)); | 	void* new_ptr = realloc(old_ptr, size + sizeof(size_t)); | ||||||
| 	if (new_ptr) | 	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)); | 		memcpy(new_ptr, &size, sizeof(size_t)); | ||||||
| 		return (void*)((intptr_t)new_ptr + sizeof(size_t)); | 		return (void*)((intptr_t)new_ptr + sizeof(size_t)); | ||||||
| 	} | 	} | ||||||
| 	else | 	else | ||||||
| 	{ | 	{ | ||||||
| 		__atomic_sub_fetch(&s_uv_malloc_size, old_size, __ATOMIC_RELAXED); | 		__atomic_sub_fetch(total, old_size, __ATOMIC_RELAXED); | ||||||
| 		return NULL; | 		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) | static void* _tf_uv_calloc(size_t nmemb, size_t size) | ||||||
| { | { | ||||||
| 	void* ptr = calloc(1, nmemb * size + sizeof(size_t)); | 	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) | static void _tf_uv_free(void* ptr) | ||||||
| { | { | ||||||
| 	if (ptr) | 	_tf_free(&s_uv_malloc_size, 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); |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void tf_util_replace_uv_allocator() | 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); | 	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; | 	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) | static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) | ||||||
| { | { | ||||||
| 	size_t length = 0; | 	size_t length = 0; | ||||||
|   | |||||||
| @@ -5,7 +5,10 @@ | |||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
|  |  | ||||||
| void tf_util_replace_uv_allocator(); | 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); | void tf_util_register(JSContext* context); | ||||||
| JSValue tf_util_utf8_decode(JSContext* context, JSValue value); | JSValue tf_util_utf8_decode(JSContext* context, JSValue value); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user