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 end
function skynet.exit() function skynet.exit()
skynet.send(".launcher","lua","REMOVE",skynet.self())
c.command("EXIT") c.command("EXIT")
end end
function skynet.kill(name) function skynet.kill(name)
if type(name) == "number" then
name = skynet.address(name)
end
c.command("KILL",name) c.command("KILL",name)
end end
@@ -245,7 +249,8 @@ local function dispatch_message(prototype, msg, sz, session, source, ...)
end end
function skynet.newservice(name, ...) 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 return handle
end end
@@ -360,6 +365,25 @@ do
} }
end 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 ----- register protocol
do do
local REG = skynet.register_protocol local REG = skynet.register_protocol
@@ -391,6 +415,14 @@ do
name = "response", name = "response",
id = 1, id = 1,
} }
REG {
name = "debug",
id = 9,
pack = skynet.pack,
unpack = skynet.unpack,
dispatch = _debug_dispatch,
}
end end
local init_func = {} local init_func = {}
@@ -428,7 +460,7 @@ function skynet.start(start_func)
c.callback(dispatch_message) c.callback(dispatch_message)
skynet.timeout(0, function() skynet.timeout(0, function()
init_template(start_func) init_template(start_func)
skynet.send(".launcher","lua", nil) skynet.send(".launcher","text", "")
end) end)
end end
@@ -438,7 +470,7 @@ function skynet.filter(f ,start_func)
end) end)
skynet.timeout(0, function() skynet.timeout(0, function()
init_template(start_func) init_template(start_func)
skynet.send(".launcher","lua", nil) skynet.send(".launcher","text", "")
end) end)
end end

View File

@@ -15,10 +15,7 @@ local function split_package()
while true do while true do
local cmd = readline "\n" local cmd = readline "\n"
if cmd ~= "" then if cmd ~= "" then
local handle = skynet.launch("snlua", cmd) skynet.send(skynet.self(), "text", cmd)
if handle == nil then
print("Launch error:",cmd)
end
end end
end end
end end
@@ -39,5 +36,11 @@ skynet.register_protocol {
skynet.start(function() 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() socket.stdin()
end) 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 skynet = require "skynet"
local string = string 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 = {} local instance = {}
skynet.register(".launcher") skynet.register(".launcher")
skynet.start(function() skynet.start(function()
skynet.dispatch("lua" , function(session, address , service, ...) skynet.dispatch("text" , function(session, address , cmd)
if service == nil then if cmd == "" then
-- init notice -- init notice
local reply = instance[address] local reply = instance[address]
if reply then if reply then
@@ -16,13 +54,18 @@ skynet.start(function()
end end
else else
-- launch request -- launch request
local param = table.concat({...}, " ") local service, param = string.match(cmd,"([^ ]+) (.*)")
local inst = skynet.launch(service, param) local inst = skynet.launch(service, param)
if inst then if inst then
services[inst] = cmd
instance[inst] = { session = session, address = address } instance[inst] = { session = session, address = address }
else else
skynet.ret(skynet.pack(nil)) skynet.ret(skynet.pack(nil))
end end
end end
end) end)
skynet.dispatch("lua", function(session, address, cmd , ...)
cmd = string.upper(cmd)
command[cmd](...)
end)
end) end)

View File

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

View File

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