diff --git a/lualib-src/lua-skynet.c b/lualib-src/lua-skynet.c index 6c42c98f..b029edaa 100644 --- a/lualib-src/lua-skynet.c +++ b/lualib-src/lua-skynet.c @@ -9,9 +9,31 @@ #include #include +struct stat { + lua_State *L; + int count; +}; + +static int +_stat(lua_State *L) { + lua_rawgetp(L, LUA_REGISTRYINDEX, _stat); + struct stat *S = lua_touserdata(L,-1); + if (S==NULL) { + luaL_error(L, "set callback first"); + } + const char * what = luaL_checkstring(L,1); + if (strcmp(what,"count")==0) { + lua_pushinteger(L, S->count); + return 1; + } + return 0; +} + static int _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { - lua_State *L = ud; + struct stat *S = ud; + lua_State *L = S->L; + ++S->count; int trace = 1; int top = lua_gettop(L); if (top == 1) { @@ -61,10 +83,16 @@ _callback(lua_State *L) { luaL_checktype(L,1,LUA_TFUNCTION); lua_settop(L,1); lua_rawsetp(L, LUA_REGISTRYINDEX, _cb); + lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *gL = lua_tothread(L,-1); - skynet_callback(context, gL, _cb); + struct stat * S = lua_newuserdata(L, sizeof(*S)); + S->L = gL; + S->count = 0; + lua_rawsetp(L, LUA_REGISTRYINDEX, _stat); + + skynet_callback(context, S, _cb); return 0; } @@ -309,8 +337,14 @@ luaopen_skynet_c(lua_State *L) { luaL_setfuncs(L,l,1); - lua_pushcfunction(L, remoteobj_init); - lua_setfield(L, -2, "remote_init"); + luaL_Reg l2[] = { + { "stat", _stat }, + { "remote_init", remoteobj_init }, + { NULL, NULL }, + }; + + + luaL_setfuncs(L,l2,0); return 1; } diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 435efb67..93df6cfc 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -383,6 +383,16 @@ function dbgcmd.GC() collectgarbage "collect" end +local function query_state(stat, what) + stat[what] = c.stat(what) +end + +function dbgcmd.STAT() + local stat = {} + query_state(stat, "count") + skynet.ret(skynet.pack(stat)) +end + local function _debug_dispatch(session, address, cmd, ...) local f = dbgcmd[cmd] assert(f, cmd) diff --git a/service/launcher.lua b/service/launcher.lua index 6fc82bd7..0d721016 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -13,6 +13,19 @@ function command.LIST() skynet.ret(skynet.pack(list)) end +function command.STAT() + local list = {} + for k,v in pairs(services) do + local stat = skynet.call(k,"debug","STAT") + local result = {} + for k,v in pairs(stat) do + table.insert(result, string.format("%s = %s", k, tostring(v))) + end + list[skynet.address(k)] = table.concat(result,",") .. " (" .. v .. ")" + end + skynet.ret(skynet.pack(list)) +end + function command.KILL(handle) skynet.kill(handle) skynet.ret( skynet.pack({ [handle] = tostring(services[handle]) })) diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 5d51d405..1cd53a67 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -95,9 +95,9 @@ main(int argc, char *argv[]) { } _init_env(L); - const char *path = optstring("lua_path","./build/lua/?.lua;./lualib/?.lua;./lualib/?/init.lua"); + const char *path = optstring("lua_path","./lualib/?.lua;./lualib/?/init.lua"); setenv("LUA_PATH",path,1); - const char *cpath = optstring("lua_cpath","./build/lua/?.so"); + const char *cpath = optstring("lua_cpath","./luaclib/?.so"); setenv("LUA_CPATH",cpath,1); optstring("luaservice","./service/?.lua");