Add jemalloc heap profilling in debug console

This commit is contained in:
wudeng
2019-04-30 15:35:43 +08:00
committed by 云风
parent 4159600fbc
commit 35aa00fb13
4 changed files with 69 additions and 10 deletions

View File

@@ -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 },
};

View File

@@ -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
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

View File

@@ -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);

View File

@@ -2,6 +2,7 @@
#define SKYNET_MALLOC_HOOK_H
#include <stdlib.h>
#include <stdbool.h>
#include <lua.h>
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);