diff --git a/lualib-src/lualib-core.c b/lualib-src/lualib-core.c index 663e585..341ff99 100644 --- a/lualib-src/lualib-core.c +++ b/lualib-src/lualib-core.c @@ -477,14 +477,16 @@ ltracenew(lua_State *L) static int ltraceset(lua_State *L) { - silly_trace_id_t traceid; - if lua_isnoneornil(L, 1) { - traceid = TRACE_WORKER_ID; - } else { - traceid = (silly_trace_id_t)luaL_checkinteger(L, 1); - } - traceid = silly_trace_set(traceid); - lua_pushinteger(L, (lua_Integer)traceid); + silly_trace_id_t traceid; + lua_State *co = lua_tothread(L, 1); + silly_worker_resume(co); + if lua_isnoneornil(L, 2) { + traceid = TRACE_WORKER_ID; + } else { + traceid = (silly_trace_id_t)luaL_checkinteger(L, 2); + } + traceid = silly_trace_set(traceid); + lua_pushinteger(L, (lua_Integer)traceid); return 1; } diff --git a/lualib/core.lua b/lualib/core.lua index 19d7cde..4bce402 100644 --- a/lualib/core.lua +++ b/lualib/core.lua @@ -52,7 +52,7 @@ local function task_resume(t, ...) local save = task_running task_status[t] = "RUN" task_running = t - local traceid = trace_set(task_traceid[t]) + local traceid = trace_set(t, task_traceid[t]) local ok, err = coresume(t, ...) trace_set(traceid) task_running = save @@ -89,7 +89,7 @@ end function core.trace(id) task_traceid[task_running] = id - return (trace_set(id)) + return (trace_set(task_running, id)) end function core.error(errmsg) diff --git a/silly-src/silly_monitor.c b/silly-src/silly_monitor.c index 0138c56..93b1d76 100644 --- a/silly-src/silly_monitor.c +++ b/silly-src/silly_monitor.c @@ -2,47 +2,28 @@ #include "atomic.h" #include "compiler.h" #include "silly_log.h" +#include "silly_worker.h" #include "silly_monitor.h" struct monitor { - unsigned int process_id; - unsigned int check_id; - int msgtype; + uint32_t check_id; } M; -static const char *msgname[] = { - "NIL", - "EXPIRE", - "ACCEPT", - "CLOSE", - "CONNECTED", - "TCPDATA", - "UDPDATA", -}; - void silly_monitor_init() { - M.process_id = 0; M.check_id = 0; - M.msgtype = 0; } void silly_monitor_check() { - if (M.msgtype != 0 && unlikely(M.check_id == M.process_id)) { - silly_log_warn("[monitor] message of %s processed slowly\n", - msgname[M.msgtype]); + uint32_t check_id = silly_worker_processid(); + if (unlikely(M.check_id == check_id)) { + silly_worker_warnendless(); } - M.check_id = M.process_id; + M.check_id = check_id; } -void -silly_monitor_trigger(int msgtype) -{ - M.msgtype = msgtype; - atomic_add(&M.process_id, 1); -} diff --git a/silly-src/silly_monitor.h b/silly-src/silly_monitor.h index 849c5ff..a65976d 100644 --- a/silly-src/silly_monitor.h +++ b/silly-src/silly_monitor.h @@ -3,7 +3,6 @@ void silly_monitor_init(); void silly_monitor_check(); -void silly_monitor_trigger(int msgtype); #endif diff --git a/silly-src/silly_worker.c b/silly-src/silly_worker.c index 4da2121..43659da 100644 --- a/silly-src/silly_worker.c +++ b/silly-src/silly_worker.c @@ -7,6 +7,7 @@ #include "silly.h" #include "compiler.h" +#include "atomic.h" #include "silly_log.h" #include "silly_malloc.h" #include "silly_queue.h" @@ -20,15 +21,19 @@ struct silly_worker { int argc; char **argv; lua_State *L; + lua_State *running; uint32_t id; + uint32_t process_id; size_t maxmsg; + lua_Hook oldhook; + int oldmask; + int oldcount; struct silly_queue *queue; void (*callback)(lua_State *L, struct silly_message *msg); }; struct silly_worker *W; - void silly_worker_push(struct silly_message *msg) { @@ -47,6 +52,7 @@ silly_worker_dispatch() struct silly_message *msg; struct silly_message *tmp; msg = silly_queue_pop(W->queue); + atomic_add(&W->process_id, 1); if (msg == NULL) { #ifdef LUA_GC_STEP lua_gc(W->L, LUA_GCSTEP, LUA_GC_STEP); @@ -55,7 +61,7 @@ silly_worker_dispatch() } do { do { - silly_monitor_trigger(msg->type); + atomic_add(&W->process_id, 1); W->callback(W->L, msg); tmp = msg; msg = msg->next; @@ -63,7 +69,6 @@ silly_worker_dispatch() } while (msg); msg = silly_queue_pop(W->queue); } while (msg); - silly_monitor_trigger(0); W->maxmsg = WARNING_THRESHOLD; return ; } @@ -213,6 +218,40 @@ silly_worker_args(int *argc) return W->argv; } +void +silly_worker_resume(lua_State *L) +{ + W->running = L; +} + + +uint32_t +silly_worker_processid() +{ + return W->process_id; +} + +static void warn_hook(lua_State *L, lua_Debug *ar) +{ + (void)ar; + int top = lua_gettop(L);; + luaL_traceback(L, L, "maybe in an endless loop.", 1); + silly_log_warn("[worker] %s\n", lua_tostring(L, -1)); + lua_settop(L, top); + lua_sethook(L,W->oldhook,W->oldmask,W->oldcount); +} + +void +silly_worker_warnendless() +{ + if (W->running == NULL) + return; + W->oldhook=lua_gethook(W->running); + W->oldmask=lua_gethookmask(W->running); + W->oldcount=lua_gethookcount(W->running); + lua_sethook(W->running,warn_hook,LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT,1); +} + void silly_worker_exit() { diff --git a/silly-src/silly_worker.h b/silly-src/silly_worker.h index 235117f..a736c01 100644 --- a/silly-src/silly_worker.h +++ b/silly-src/silly_worker.h @@ -1,5 +1,6 @@ #ifndef _SILLY_WORKER_H #define _SILLY_WORKER_H +#include struct silly_message; struct lua_State; @@ -15,6 +16,10 @@ void silly_worker_dispatch(); uint32_t silly_worker_genid(); size_t silly_worker_msgsize(); +uint32_t silly_worker_processid(); +void silly_worker_resume(lua_State *L); +void silly_worker_warnendless(); + char **silly_worker_args(int *argc); void silly_worker_callback(void (*callback)(struct lua_State *L, struct silly_message *msg));