diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 78fd1a166..e206a760f 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -29,6 +29,7 @@ local function init(skynet, export) stat.mqlen = skynet.stat "mqlen" stat.cpu = skynet.stat "cpu" stat.message = skynet.stat "message" + stat.mem = collectgarbage "count" skynet.ret(skynet.pack(stat)) end diff --git a/service/debug_console.lua b/service/debug_console.lua index 35c4f1d08..169f91edf 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -55,11 +55,17 @@ local function split_cmdline(cmdline) return split end +local function htime() + return skynet.hpc() / 1000000000 +end + local function docmd(cmdline, print, fd) local split = split_cmdline(cmdline) local command = split[1] local cmd = COMMAND[command] local ok, list + local t_start = htime() + print(string.format("\n---------------------------------", command)) if cmd then ok, list = pcall(cmd, table.unpack(split,2)) else @@ -73,6 +79,7 @@ local function docmd(cmdline, print, fd) end end + local t_cost = htime() - t_start if ok then if list then if type(list) == "string" then @@ -81,10 +88,10 @@ local function docmd(cmdline, print, fd) dump_list(print, list) end end - print("") + print(string.format(" cost=%.4f sec", t_cost)) else print(list) - print("") + print(string.format(" cost=%.4f sec", t_cost)) end end @@ -94,7 +101,7 @@ local function console_main_loop(stdin, print) local ok, err = pcall(function() while true do local cmdline = socket.readline(stdin, "\n") - if not cmdline then + if not cmdline or cmdline == "q" or cmdline == "q\r" then break end if cmdline:sub(1,4) == "GET " then @@ -156,13 +163,16 @@ function COMMAND.help() debug = "debug address : debug a lua service", signal = "signal address sig", cmem = "Show C memory info", - jmem = "Show jemalloc mem stats", + jmem = "Show jemalloc mem stats", + osmem = "Show OS memory stats like ps: vsz/rss", ping = "ping address", 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", + i = "info of this server", + q = "close console connection, bye", } end @@ -335,9 +345,10 @@ function COMMAND.cmem() local info = memory.info() local tmp = {} for k,v in pairs(info) do - tmp[skynet.address(k)] = v + tmp[skynet.address(k)] = string.format("%11d %8.2f Mb", v, v/1048576) end - tmp.total = memory.total() + local total = memory.total() + tmp.total = string.format("%d %.2f Mb", total, total/1048576) tmp.block = memory.block() return tmp @@ -352,6 +363,30 @@ function COMMAND.jmem() return tmp end +function COMMAND.osmem() + local info = {} + local fproc = io.open("/proc/self/statm") + if fproc then + info['vsz'],info['rss'],info['shr'],info['txt'],info['dat'] = string.match( + fproc:read("a"), + '(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+%d+%s+(%d+)%s+%d+') + if info['vsz'] then + for k,v in pairs(info) do + info[k] = string.format("%8d Kb %5d Mb", v*4, math.ceil(v*4/1024)) + end + end + end + return info +end + +function COMMAND.i() + local info = {} + info.harbor = skynet.getenv("harbor") + info.address = skynet.getenv("address") + info.path = os.getenv("PWD") + return info +end + function COMMAND.ping(address) address = adjust_address(address) local ti = skynet.now() diff --git a/service/launcher.lua b/service/launcher.lua index 77300a23f..023a1c1f7 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -25,10 +25,14 @@ end function command.STAT() local list = {} for k,v in pairs(services) do + local t0 = skynet.time() local ok, stat = pcall(skynet.call,k,"debug","STAT") if not ok then - stat = string.format("ERROR (%s)",v) + stat = {err=stat or "ERROR"} end + stat.name = v + stat.lag = skynet.time() - t0 + stat.mem = string.format("%.3f", stat.mem) list[skynet.address(k)] = stat end return list @@ -44,14 +48,17 @@ end function command.MEM() local list = {} + local sum = 0 for k,v in pairs(services) do local ok, kb = pcall(skynet.call,k,"debug","MEM") if not ok then list[skynet.address(k)] = string.format("ERROR (%s)",v) else - list[skynet.address(k)] = string.format("%.2f Kb (%s)",kb,v) + list[skynet.address(k)] = string.format("%11.2f %8.2f Mb (%s)", kb, kb/1024, v) + sum = sum + kb end end + list["sum"] = string.format("%11.2f Kb %.2f Mb (sum)", sum, sum/1024) return list end