diff --git a/lualib-src/lualib-silly.c b/lualib-src/lualib-silly.c index 991279da..1a7a6ef5 100644 --- a/lualib-src/lualib-silly.c +++ b/lualib-src/lualib-silly.c @@ -566,6 +566,16 @@ lmemallocator(lua_State *L) return 1; } +static int +lmemallocatorinfo(lua_State *L) +{ + size_t allocated, active, resident; + silly_allocator_info(&allocated, &active, &resident); + lua_pushinteger(L, allocated); + lua_pushinteger(L, active); + lua_pushinteger(L, resident); + return 3; +} static int lmemused(lua_State *L) { @@ -646,6 +656,16 @@ lnetinfo(lua_State *L) return 1; } +static int +ltimerinfo(lua_State *L) +{ + uint32_t active, expired; + active = silly_timer_info(&expired); + lua_pushinteger(L, active); + lua_pushinteger(L, expired); + return 2; +} + static int lsocketinfo(lua_State *L) { @@ -709,10 +729,12 @@ luaopen_sys_silly(lua_State *L) {"memused", lmemused}, {"memrss", lmemrss}, {"memallocator", lmemallocator}, + {"memallocatorinfo", lmemallocatorinfo}, {"msgsize", lmsgsize}, {"cpuinfo", lcpuinfo}, {"pollapi", lpollapi}, {"netinfo", lnetinfo}, + {"timerinfo", ltimerinfo}, {"socketinfo", lsocketinfo}, {"timerresolution", ltimerresolution}, //end diff --git a/lualib/sys/console.lua b/lualib/sys/console.lua index fb5c6cd0..8e1ed4d7 100644 --- a/lualib/sys/console.lua +++ b/lualib/sys/console.lua @@ -21,10 +21,11 @@ local desc = { "HELP: List command description. [HELP]", "PING: Test connection alive. [PING ]", "GC: Performs a full garbage-collection cycle. [GC]", -"INFO: Show all information of server, include CPUINFO,MINFO,QINFO,NETINFO,TASK. [INFO]", +"INFO: Show all information of server, include CPUINFO,MINFO,QINFO. [INFO]", "MINFO: Show memory infomation. [MINFO ]", "QINFO: Show framework message queue size. [QINFO]", "NETINFO: Show network info. [NETINFO]", +"TIMERINFO: Show timer pending/process event count. [TIMERINFO]", "CPUINFO: Show system time and user time statistics. [CPUINFO]", "SOCKET: Show socket detail information. [SOCKET]", "TASK: Show all task status and traceback. [TASK]", @@ -59,39 +60,55 @@ function console.gc() return format("Lua Mem Used:%.2f KiB", collectgarbage("count")) end +function console.timerinfo() + return format("#Timer\r\n\z + resolution:%s\r\n\z + active:%s\r\n\z + expired:%s\r\n", + core.timerrs, core.timerinfo()) +end + function console.cpuinfo() local sys, usr = core.cpuinfo() return format("#CPU\r\ncpu_sys:%.2fs\r\ncpu_user:%.2fs", sys, usr) end +local function format_size(fmt, size) + if fmt == "kb" then + return format("%.2f KiB", size / 1024) + elseif fmt == "mb" then + return format("%.2f MB", size / (1024 * 1024)) + else + return format("%d B", size) + end +end + function console.minfo(_, fmt) - local tbl = {} - local sz = core.memused() if fmt then fmt = lower(fmt) else fmt = NULL end - tbl[1] = "#Memory\r\n" - tbl[2] = "memory_used:" - if fmt == "kb" then - tbl[3] = format("%.2f", sz / 1024) - tbl[4] = " KiB\r\n" - elseif fmt == "mb" then - tbl[3] = format("%.2f", sz / (1024 * 1024)) - tbl[4] = " MB\r\n" - else - tbl[3] = sz - tbl[4] = " B\r\n" - end + local sz = core.memused() local rss = core.memrss() - tbl[5] = "memory_rss:" - tbl[6] = rss - tbl[7] = " B\r\n" - tbl[8] = "memory_fragmentation_ratio:" - tbl[9] = format("%.2f\r\n", rss / sz) - tbl[10] = "memory_allocator:" - tbl[11] = core.allocator + local allocated, active, resident = core.allocatorinfo() + local tbl = { + "#Memory", + "\r\nused_memory:", + format_size(fmt, sz), + "\r\nused_memory_rss:", + format_size(fmt, rss), + "\r\nallocator_allocated:", + format_size(fmt, allocated), + "\r\nallocator_active:", + format_size(fmt, active), + "\r\nallocator_resident:", + format_size(fmt, resident), + "\r\nmemory_fragmentation_ratio:", + format("%.2f", rss / sz), + "\r\nmemory_allocator:", + core.allocator, + } return concat(tbl) end @@ -147,18 +164,15 @@ function console.info() insert(tbl, format("version:%s", core.version)) insert(tbl, format("process_id:%s", core.getpid())) insert(tbl, format("multiplexing_api:%s", core.pollapi)) - insert(tbl, format("timer_resolution:%s", core.timerrs)) insert(tbl, format("uptime_in_seconds:%s", uptime)) insert(tbl, format("uptime_in_days:%.2f\r\n", uptime / (24 * 3600))) insert(tbl, console.cpuinfo()) insert(tbl, NULL) insert(tbl, console.qinfo()) insert(tbl, NULL) - insert(tbl, console.minfo("MB")) - insert(tbl, NULL) - insert(tbl, console.netinfo()) + insert(tbl, console.minfo(nil, "MB")) insert(tbl, NULL) - insert(tbl, console.task()) + insert(tbl, console.timerinfo()) return concat(tbl, "\r\n") end diff --git a/lualib/sys/core.lua b/lualib/sys/core.lua index 20f6193e..29b187da 100644 --- a/lualib/sys/core.lua +++ b/lualib/sys/core.lua @@ -83,6 +83,8 @@ core.cpuinfo = silly.cpuinfo core.getpid = silly.getpid core.netinfo = silly.netinfo core.socketinfo = silly.socketinfo +core.timerinfo = silly.timerinfo +core.allocatorinfo = silly.memallocatorinfo --const core.allocator = silly.memallocator() core.version = silly.version() diff --git a/silly-src/silly_malloc.c b/silly-src/silly_malloc.c index 531e889b..51f3ee17 100644 --- a/silly-src/silly_malloc.c +++ b/silly-src/silly_malloc.c @@ -131,6 +131,22 @@ silly_memrss() #else return allocsize; #endif +} +void +silly_allocator_info(size_t *allocated, size_t *active, size_t *resident) +{ + size_t sz; + uint64_t epoch = 1; + *allocated = *resident = *active = 0; +#ifndef DISABLE_JEMALLOC + sz = sizeof(epoch); + je_mallctl("epoch", &epoch, &sz, &epoch, sz); + sz = sizeof(size_t); + je_mallctl("stats.resident", resident, &sz, NULL, 0); + je_mallctl("stats.active", active, &sz, NULL, 0); + je_mallctl("stats.allocated", allocated, &sz, NULL, 0); +#endif + return ; } diff --git a/silly-src/silly_malloc.h b/silly-src/silly_malloc.h index 5f688906..b4e3661d 100644 --- a/silly-src/silly_malloc.h +++ b/silly-src/silly_malloc.h @@ -10,6 +10,7 @@ void silly_free(void *ptr); const char *silly_allocator(); size_t silly_memused(); size_t silly_memrss(); +void silly_allocator_info(size_t *allocated, size_t *active, size_t *resident); #endif diff --git a/silly-src/silly_timer.c b/silly-src/silly_timer.c index 0d5b131c..36b7341e 100644 --- a/silly-src/silly_timer.c +++ b/silly-src/silly_timer.c @@ -49,6 +49,8 @@ struct silly_timer { uint64_t monotonic; struct slot_root root; struct slot_level level[4]; + uint32_t expired_count; + uint32_t active_count; }; static struct silly_timer *T; @@ -56,6 +58,8 @@ static struct silly_timer *T; static inline struct node * newnode() { + atomic_add(&T->active_count, 1); + atomic_add(&T->expired_count, 1); struct node *n = silly_malloc(sizeof(*n)); uint32_t session = silly_worker_genid(); n->session = session; @@ -65,6 +69,7 @@ newnode() static inline void freenode(struct node *n) { + atomic_sub(&T->active_count, 1); silly_free(n); return ; } @@ -107,6 +112,14 @@ silly_timer_monotonicsec() return T->monotonic / scale; } +uint32_t +silly_timer_info(uint32_t *expired) +{ + if (expired!= NULL) + *expired = T->expired_count; + return T->active_count; +} + static inline void linklist(struct node **list, struct node *n) { diff --git a/silly-src/silly_timer.h b/silly-src/silly_timer.h index d6194184..5c30f30f 100644 --- a/silly-src/silly_timer.h +++ b/silly-src/silly_timer.h @@ -11,6 +11,7 @@ uint64_t silly_timer_now(); time_t silly_timer_nowsec(); uint64_t silly_timer_monotonic(); time_t silly_timer_monotonicsec(); +uint32_t silly_timer_info(uint32_t *expired); #endif