dbg for debug

This commit is contained in:
云风
2012-09-14 12:04:14 +08:00
parent 374b65d199
commit 32dee2bbaa
6 changed files with 101 additions and 11 deletions

View File

@@ -138,10 +138,14 @@ function skynet.starttime()
end
function skynet.exit()
skynet.send(".launcher","lua","REMOVE",skynet.self())
c.command("EXIT")
end
function skynet.kill(name)
if type(name) == "number" then
name = skynet.address(name)
end
c.command("KILL",name)
end
@@ -245,7 +249,8 @@ local function dispatch_message(prototype, msg, sz, session, source, ...)
end
function skynet.newservice(name, ...)
local handle = skynet.call(".launcher", "lua" , "snlua", name, ...)
local param = table.concat({"snlua", name, ...}, " ")
local handle = skynet.call(".launcher", "text" , param)
return handle
end
@@ -360,6 +365,25 @@ do
}
end
----- debug
local dbgcmd = {}
function dbgcmd.MEM()
local kb, bytes = collectgarbage "count"
skynet.ret(skynet.pack(kb,bytes))
end
function dbgcmd.GC()
collectgarbage "collect"
end
local function _debug_dispatch(session, address, cmd, ...)
local f = dbgcmd[cmd]
assert(f, cmd)
f(...)
end
----- register protocol
do
local REG = skynet.register_protocol
@@ -391,6 +415,14 @@ do
name = "response",
id = 1,
}
REG {
name = "debug",
id = 9,
pack = skynet.pack,
unpack = skynet.unpack,
dispatch = _debug_dispatch,
}
end
local init_func = {}
@@ -428,7 +460,7 @@ function skynet.start(start_func)
c.callback(dispatch_message)
skynet.timeout(0, function()
init_template(start_func)
skynet.send(".launcher","lua", nil)
skynet.send(".launcher","text", "")
end)
end
@@ -438,7 +470,7 @@ function skynet.filter(f ,start_func)
end)
skynet.timeout(0, function()
init_template(start_func)
skynet.send(".launcher","lua", nil)
skynet.send(".launcher","text", "")
end)
end

View File

@@ -15,10 +15,7 @@ local function split_package()
while true do
local cmd = readline "\n"
if cmd ~= "" then
local handle = skynet.launch("snlua", cmd)
if handle == nil then
print("Launch error:",cmd)
end
skynet.send(skynet.self(), "text", cmd)
end
end
end
@@ -39,5 +36,11 @@ skynet.register_protocol {
skynet.start(function()
skynet.dispatch("text", function (session, address, cmd)
local handle = skynet.newservice(cmd)
if handle == nil then
print("Launch error:",cmd)
end
end)
socket.stdin()
end)

11
service/dbg.lua Normal file
View File

@@ -0,0 +1,11 @@
local skynet = require "skynet"
local cmd = { ... }
skynet.start(function()
local list = skynet.call(".launcher","lua", unpack(cmd))
for k,v in pairs(list) do
print(k,v)
end
skynet.exit()
end)

View File

@@ -1,13 +1,51 @@
local skynet = require "skynet"
local string = string
local services = {}
local command = {}
function command.LIST()
local list = {}
for k,v in pairs(services) do
list[skynet.address(k)] = v
end
skynet.ret(skynet.pack(list))
end
function command.KILL(handle)
skynet.kill(handle)
skynet.ret( skynet.pack({ [handle] = tostring(services[handle]) }))
services[handle] = nil
end
function command.MEM()
local list = {}
for k,v in pairs(services) do
local kb, bytes = skynet.call(k,"debug","MEM")
list[skynet.address(k)] = string.format("%d Kb (%s)",kb,v)
end
skynet.ret(skynet.pack(list))
end
function command.GC()
for k,v in pairs(services) do
skynet.send(k,"debug","GC")
end
command.MEM()
end
function command.REMOVE(handle)
services[handle] = nil
end
local instance = {}
skynet.register(".launcher")
skynet.start(function()
skynet.dispatch("lua" , function(session, address , service, ...)
if service == nil then
skynet.dispatch("text" , function(session, address , cmd)
if cmd == "" then
-- init notice
local reply = instance[address]
if reply then
@@ -16,13 +54,18 @@ skynet.start(function()
end
else
-- launch request
local param = table.concat({...}, " ")
local service, param = string.match(cmd,"([^ ]+) (.*)")
local inst = skynet.launch(service, param)
if inst then
services[inst] = cmd
instance[inst] = { session = session, address = address }
else
skynet.ret(skynet.pack(nil))
end
end
end)
skynet.dispatch("lua", function(session, address, cmd , ...)
cmd = string.upper(cmd)
command[cmd](...)
end)
end)

View File

@@ -394,6 +394,7 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
} else if (param[0] == '.') {
handle = skynet_handle_findname(param+1);
} else {
skynet_error(context, "Can't kill %s",param);
// todo : kill global service
}
if (handle) {

View File

@@ -84,7 +84,7 @@ skynet_start(struct skynet_config * config) {
}
ctx = skynet_context_new("snlua", "launcher");
if (ctx == NULL) {
fprintf(stderr,"launch launcher error");
fprintf(stderr,"launch launcher error\n");
exit(1);
}
ctx = skynet_context_new("snlua", config->start);