Skip to content

Commit

Permalink
refine endless loop warning
Browse files Browse the repository at this point in the history
introduce tracebacks for improved problem diagnosis
  • Loading branch information
findstr committed Jun 7, 2024
1 parent a839ab9 commit a4b415a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
18 changes: 10 additions & 8 deletions lualib-src/lualib-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lualib/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 6 additions & 25 deletions silly-src/silly_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


1 change: 0 additions & 1 deletion silly-src/silly_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

void silly_monitor_init();
void silly_monitor_check();
void silly_monitor_trigger(int msgtype);


#endif
Expand Down
45 changes: 42 additions & 3 deletions silly-src/silly_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
{
Expand All @@ -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);
Expand All @@ -55,15 +61,14 @@ 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;
silly_message_free(tmp);
} while (msg);
msg = silly_queue_pop(W->queue);
} while (msg);
silly_monitor_trigger(0);
W->maxmsg = WARNING_THRESHOLD;
return ;
}
Expand Down Expand Up @@ -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()
{
Expand Down
5 changes: 5 additions & 0 deletions silly-src/silly_worker.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef _SILLY_WORKER_H
#define _SILLY_WORKER_H
#include <lua.h>

struct silly_message;
struct lua_State;
Expand All @@ -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));
Expand Down

0 comments on commit a4b415a

Please sign in to comment.