snlua stat

This commit is contained in:
云风
2012-09-14 15:20:18 +08:00
parent 6fef0a871d
commit 564efd1b8b
4 changed files with 63 additions and 6 deletions

View File

@@ -9,9 +9,31 @@
#include <string.h>
#include <assert.h>
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;
}

View File

@@ -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)

View File

@@ -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]) }))

View File

@@ -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");