Skip to content

Commit f2b1bd7

Browse files
fanlixfx
andauthored
stat: add mem stat for jemalloc (#1218)
1, add opts arg for mallctl,dumpinfo 2, collect jemalloc.mem info 3, add debug_console cmd: jmem Co-authored-by: fx <fx@f.x>
1 parent 693176e commit f2b1bd7

4 files changed

Lines changed: 48 additions & 5 deletions

File tree

lualib-src/lua-memory.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,42 @@ lblock(lua_State *L) {
2323

2424
static int
2525
ldumpinfo(lua_State *L) {
26-
memory_info_dump();
26+
const char *opts = NULL;
27+
if (lua_isstring(L, 1)) {
28+
opts = luaL_checkstring(L,1);
29+
}
30+
memory_info_dump(opts);
2731

2832
return 0;
2933
}
3034

35+
static int
36+
ljestat(lua_State *L) {
37+
static const char* names[] = {
38+
"stats.allocated",
39+
"stats.resident",
40+
"stats.retained",
41+
"stats.mapped",
42+
"stats.active" };
43+
static size_t flush = 1;
44+
mallctl_int64("epoch", &flush); // refresh je.stats.cache
45+
lua_newtable(L);
46+
int i;
47+
for (i = 0; i < (sizeof(names)/sizeof(names[0])); i++) {
48+
lua_pushstring(L, names[i]);
49+
lua_pushinteger(L, (lua_Integer) mallctl_int64(names[i], NULL));
50+
lua_settable(L, -3);
51+
}
52+
return 1;
53+
}
54+
55+
static int
56+
lmallctl(lua_State *L) {
57+
const char *name = luaL_checkstring(L,1);
58+
lua_pushinteger(L, (lua_Integer) mallctl_int64(name, NULL));
59+
return 1;
60+
}
61+
3162
static int
3263
ldump(lua_State *L) {
3364
dump_c_mem();
@@ -69,6 +100,8 @@ luaopen_skynet_memory(lua_State *L) {
69100
{ "total", ltotal },
70101
{ "block", lblock },
71102
{ "dumpinfo", ldumpinfo },
103+
{ "jestat", ljestat },
104+
{ "mallctl", lmallctl },
72105
{ "dump", ldump },
73106
{ "info", dump_mem_lua },
74107
{ "current", lcurrent },

service/debug_console.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ function COMMAND.help()
156156
debug = "debug address : debug a lua service",
157157
signal = "signal address sig",
158158
cmem = "Show C memory info",
159+
jmem = "Show jemalloc mem stats",
159160
ping = "ping address",
160161
call = "call address ...",
161162
trace = "trace address [proto] [on|off]",
@@ -342,6 +343,15 @@ function COMMAND.cmem()
342343
return tmp
343344
end
344345

346+
function COMMAND.jmem()
347+
local info = memory.jestat()
348+
local tmp = {}
349+
for k,v in pairs(info) do
350+
tmp[k] = string.format("%11d %8.2f Mb", v, v/1048576)
351+
end
352+
return tmp
353+
end
354+
345355
function COMMAND.ping(address)
346356
address = adjust_address(address)
347357
local ti = skynet.now()

skynet-src/malloc_hook.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ static void malloc_oom(size_t size) {
127127
}
128128

129129
void
130-
memory_info_dump(void) {
131-
je_malloc_stats_print(0,0,0);
130+
memory_info_dump(const char* opts) {
131+
je_malloc_stats_print(0,0, opts);
132132
}
133133

134134
bool
@@ -241,7 +241,7 @@ skynet_posix_memalign(void **memptr, size_t alignment, size_t size) {
241241
#define raw_free free
242242

243243
void
244-
memory_info_dump(void) {
244+
memory_info_dump(const char* opts) {
245245
skynet_error(NULL, "No jemalloc");
246246
}
247247

skynet-src/malloc_hook.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
extern size_t malloc_used_memory(void);
99
extern size_t malloc_memory_block(void);
10-
extern void memory_info_dump(void);
10+
extern void memory_info_dump(const char *opts);
1111
extern size_t mallctl_int64(const char* name, size_t* newval);
1212
extern int mallctl_opt(const char* name, int* newval);
1313
extern bool mallctl_bool(const char* name, bool* newval);

0 commit comments

Comments
 (0)