起因
最近上线服务器遇到了一些问题,上个月CPU暴涨的问题,那个经查验是死循环导致endless loop了。
这周又遇到了mem占用达到96%的问题,在debug console里调用了gc之后,跌落至85%左右,稳定了一天。
但是在debug console里面调用list的时候,发现很多service数量有点异常,所以打算扩展一下debug console来统计各个service的数量。目前已经初步完成,特记录于此。
Debug Console
debug_console源代码的位置是skynet/service/debug_console.lua,我们一般在项目的config.lua中配置他的地址和端口即可。
这里面的代码比较简单,我们可以看到大部分的功能都实现在skynet/service/launcher.lua中。
增加statistic统计接口
我们的需求是增加一个统计接口,叫做statis,也就是statistic的简写,太长了别人容易写错。
第一步 COMMAND.help
COMMAND.help是debug console中help指令的输出内容,我们需要在其中添加新增的接口的简单描述信息。在其返回的table中添加:
statis = "Show service counts"
此时重启skynet服务器可以看到在控制台输入 help 可以看到我们新增的内容:
Welcome to skynet console
help
call call address ...
clearcache clear lua code cache
cmem Show C memory info
debug debug address : debug a lua service
exit exit address : kill a lua service
gc gc : force every lua service do garbage collect
help This help message
info info address : get service infomation
inject inject address luascript.lua
kill kill address : kill service
list List all the service
log launch a new lua service with log
logoff logoff address
logon logon address
mem mem : show memory status
ping ping address
service List unique service
shrtbl Show shared short string table info
signal signal address sig
snax lanuch a new snax service
start lanuch a new lua service
stat Dump all stats
statis Show service counts
task task address : show service task detail
<CMD OK>
第二步 在launcher.lua添加接口实现
仔细观察launcher.lua中的方法,发现了一个可用的services。里面存放了所有的service,存放的格式是{address = 启动参数}。启动参数里面包含了service的名字,所以,我们只需要对这个table进行统计就可以了。
function command.STATIS()
local list = {}
for k,v in pairs(services) do
if list[v] == nil then
list[v] = 0
end
list[v] = list[v] + 1
end
return list
end
好,完成了。
重启服务器,在控制台statics,我们就可以看到我们想要的结果了。
![](https://img.haomeiwen.com/i12084159/d8702d8a07740750.png)
网友评论