From 35aa00fb138604c2bccdb122550dfee7ea1ba221 Mon Sep 17 00:00:00 2001 From: wudeng Date: Tue, 30 Apr 2019 15:35:43 +0800 Subject: [PATCH] Add jemalloc heap profilling in debug console --- lualib-src/lua-memory.c | 22 ++++++++++++++++++++++ service/debug_console.lua | 19 ++++++++++++++++++- skynet-src/malloc_hook.c | 35 ++++++++++++++++++++++++++--------- skynet-src/malloc_hook.h | 3 +++ 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/lualib-src/lua-memory.c b/lualib-src/lua-memory.c index 5fc75752..426acf18 100644 --- a/lualib-src/lua-memory.c +++ b/lualib-src/lua-memory.c @@ -49,6 +49,26 @@ lcurrent(lua_State *L) { return 1; } +static int +ldumpheap(lua_State *L) { + mallctl_cmd("prof.dump"); + return 0; +} + +static int +lprofactive(lua_State *L) { + bool *pval, active; + if (lua_isnone(L, 1)) { + pval = NULL; + } else { + active = lua_toboolean(L, 1) ? true : false; + pval = &active; + } + bool ret = mallctl_bool("prof.active", pval); + lua_pushboolean(L, ret); + return 1; +} + LUAMOD_API int luaopen_skynet_memory(lua_State *L) { luaL_checkversion(L); @@ -62,6 +82,8 @@ luaopen_skynet_memory(lua_State *L) { { "ssinfo", luaS_shrinfo }, { "ssexpand", lexpandshrtbl }, { "current", lcurrent }, + { "dumpheap", ldumpheap }, + { "profactive", lprofactive }, { NULL, NULL }, }; diff --git a/service/debug_console.lua b/service/debug_console.lua index daa2e688..0323c215 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -160,6 +160,8 @@ function COMMAND.help() call = "call address ...", trace = "trace address [proto] [on|off]", netstat = "netstat : show netstat", + profactive = "profactive [on|off] : active/deactive jemalloc heap profilling", + dumpheap = "dumpheap : dump heap profilling", } end @@ -421,4 +423,19 @@ function COMMAND.netstat() convert_stat(info) end return stat -end \ No newline at end of file +end + +function COMMAND.dumpheap() + memory.dumpheap() +end + +function COMMAND.profactive(flag) + if flag ~= nil then + if flag == "on" or flag == "off" then + flag = toboolean(flag) + end + memory.profactive(flag) + end + local active = memory.profactive() + return "heap profilling is ".. (active and "active" or "deactive") +end diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index 02b6c091..a3ce6693 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -65,10 +65,10 @@ get_allocated_field(uint32_t handle) { return &data->allocated; } -inline static void +inline static void update_xmalloc_stat_alloc(uint32_t handle, size_t __n) { ATOM_ADD(&_used_memory, __n); - ATOM_INC(&_memory_block); + ATOM_INC(&_memory_block); ssize_t* allocated = get_allocated_field(handle); if(allocated) { ATOM_ADD(allocated, __n); @@ -126,12 +126,29 @@ static void malloc_oom(size_t size) { abort(); } -void +void memory_info_dump(void) { je_malloc_stats_print(0,0,0); } -size_t +bool +mallctl_bool(const char* name, bool* newval) { + bool v = 0; + size_t len = sizeof(v); + if(newval) { + je_mallctl(name, &v, &len, newval, sizeof(bool)); + } else { + je_mallctl(name, &v, &len, NULL, 0); + } + return v; +} + +int +mallctl_cmd(const char* name) { + return je_mallctl(name, NULL, NULL, NULL, 0); +} + +size_t mallctl_int64(const char* name, size_t* newval) { size_t v = 0; size_t len = sizeof(v); @@ -144,7 +161,7 @@ mallctl_int64(const char* name, size_t* newval) { return v; } -int +int mallctl_opt(const char* name, int* newval) { int v = 0; size_t len = sizeof(v); @@ -223,18 +240,18 @@ skynet_posix_memalign(void **memptr, size_t alignment, size_t size) { #define raw_realloc realloc #define raw_free free -void +void memory_info_dump(void) { skynet_error(NULL, "No jemalloc"); } -size_t +size_t mallctl_int64(const char* name, size_t* newval) { skynet_error(NULL, "No jemalloc : mallctl_int64 %s.", name); return 0; } -int +int mallctl_opt(const char* name, int* newval) { skynet_error(NULL, "No jemalloc : mallctl_opt %s.", name); return 0; @@ -275,7 +292,7 @@ skynet_strdup(const char *str) { return ret; } -void * +void * skynet_lalloc(void *ptr, size_t osize, size_t nsize) { if (nsize == 0) { raw_free(ptr); diff --git a/skynet-src/malloc_hook.h b/skynet-src/malloc_hook.h index 4da4123a..04f522ac 100644 --- a/skynet-src/malloc_hook.h +++ b/skynet-src/malloc_hook.h @@ -2,6 +2,7 @@ #define SKYNET_MALLOC_HOOK_H #include +#include #include extern size_t malloc_used_memory(void); @@ -9,6 +10,8 @@ extern size_t malloc_memory_block(void); extern void memory_info_dump(void); extern size_t mallctl_int64(const char* name, size_t* newval); extern int mallctl_opt(const char* name, int* newval); +extern bool mallctl_bool(const char* name, bool* newval); +extern int mallctl_cmd(const char* name); extern void dump_c_mem(void); extern int dump_mem_lua(lua_State *L); extern size_t malloc_current_memory(void);