From 20610723a10eda9913b563b5fb65a8bac35c473c Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Dec 2015 22:13:51 +0800 Subject: [PATCH] add debug api to show current service c memory --- lualib-src/lua-memory.c | 7 +++++++ skynet-src/malloc_hook.c | 22 ++++++++++++++++++++++ skynet-src/malloc_hook.h | 1 + skynet-src/skynet.h | 1 + 4 files changed, 31 insertions(+) diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index a3afecfc..89e2996f 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -41,6 +41,12 @@ lexpandshrtbl(lua_State *L) { return 0; } +static int +lcurrent(lua_State *L) { + lua_pushinteger(L, malloc_current_memory()); + return 1; +} + int luaopen_memory(lua_State *L) { luaL_checkversion(L); @@ -53,6 +59,7 @@ luaopen_memory(lua_State *L) { { "info", dump_mem_lua }, { "ssinfo", luaS_shrinfo }, { "ssexpand", lexpandshrtbl }, + { "current", lcurrent }, { NULL, NULL }, }; diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 06e3bbf7..6cad2a9a 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "malloc_hook.h" #include "skynet.h" @@ -248,3 +249,24 @@ dump_mem_lua(lua_State *L) { } return 1; } + +size_t +malloc_current_memory(void) { + uint32_t handle = skynet_current_handle(); + int i; + for(i=0; ihandle == (uint32_t)handle && data->allocated != 0) { + return (size_t) data->allocated; + } + } + return 0; +} + +void +skynet_debug_memory(const char *info) { + // for debug use + uint32_t handle = skynet_current_handle(); + size_t mem = malloc_current_memory(); + fprintf(stderr, "[:%08x] %s %p\n", handle, info, (void *)mem); +} diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index 03339556..4da4123a 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -11,6 +11,7 @@ extern size_t mallctl_int64(const char* name, size_t* newval); extern int mallctl_opt(const char* name, int* newval); extern void dump_c_mem(void); extern int dump_mem_lua(lua_State *L); +extern size_t malloc_current_memory(void); #endif /* SKYNET_MALLOC_HOOK_H */ diff --git a/skynet-src/skynet.h b/skynet-src/skynet.h index 26df5ac6..7d3e478a 100644 --- a/skynet-src/skynet.h +++ b/skynet-src/skynet.h @@ -39,5 +39,6 @@ void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb); uint32_t skynet_current_handle(void); uint64_t skynet_now(void); +void skynet_debug_memory(const char *info); // for debug use, output current service memory to stderr #endif