Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lualib/skynet/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 41 additions & 6 deletions service/debug_console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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("<CMD: %s>\n---------------------------------", command))
if cmd then
ok, list = pcall(cmd, table.unpack(split,2))
else
Expand All @@ -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
Expand All @@ -81,10 +88,10 @@ local function docmd(cmdline, print, fd)
dump_list(print, list)
end
end
print("<CMD OK>")
print(string.format("<CMD OK> cost=%.4f sec", t_cost))
else
print(list)
print("<CMD Error>")
print(string.format("<CMD Error> cost=%.4f sec", t_cost))
end
end

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
11 changes: 9 additions & 2 deletions service/launcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down