From 01a4ee63d41c0eb4f7c57cc1be5428306c58025a Mon Sep 17 00:00:00 2001 From: bol-van Date: Tue, 28 Apr 2026 10:21:46 +0300 Subject: [PATCH] nfqws2: timer_info, timer_enum --- docs/changes.txt | 4 ++++ lua/zapret-tests.lua | 24 ++++++++++++++++++++- nfq2/lua.c | 51 +++++++++++++++++++++++++++++++++++++++++++- nfq2/timer.c | 4 +++- nfq2/timer.h | 2 +- 5 files changed, 81 insertions(+), 4 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 6422fd2..51879b1 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -284,3 +284,7 @@ v0.9.4.3 0.9.5 * nfqws2: timers + +0.9.5.1 + +* nfqws2: timer_info, timer_enum diff --git a/lua/zapret-tests.lua b/lua/zapret-tests.lua index cbe84c8..fd098f4 100644 --- a/lua/zapret-tests.lua +++ b/lua/zapret-tests.lua @@ -907,13 +907,27 @@ function test_ifaddrs(opts) end end +function timer_info_print(tinfo) + print(" timer_info.name="..tinfo.name) + print(" timer_info.func="..tinfo.func) + print(" timer_info.period="..tinfo.period) + print(" timer_info.oneshot="..tostring(tinfo.oneshot)) + print(" timer_info.fires="..tinfo.fires) +end +function timer_info_print_by_name(name) + local tinfo = timer_info(name) + timer_info_print(tinfo) +end + function timer1(name, data) print("timer "..name.." fired. data="..tostring(data)) + timer_info_print_by_name(name) end function timer2(name, data) data.n = data.n+1 print("timer "..name.." fired. data.n="..tostring(data.n)) - if data.n>=3 then + timer_info_print_by_name(name) + if data.n>=4 then timer_del(name) end end @@ -921,6 +935,14 @@ function test_timer(opts) timer_set("t1","timer1",500,true,"sample_data"); local tbl = {n=0} timer_set("t2","timer2",700,false,tbl); + + print("* timers\n") + local timers=timer_enum() + for i,timer in ipairs(timers) do + print("TIMER "..i.." :") + timer_info_print(timer) + end + print() end diff --git a/nfq2/lua.c b/nfq2/lua.c index 6aad541..12c97dd 100644 --- a/nfq2/lua.c +++ b/nfq2/lua.c @@ -3847,6 +3847,53 @@ static int luacall_timer_del(lua_State *L) lua_pushboolean(L, !!timer); LUA_STACK_GUARD_RETURN(L,1) } +static int lua_push_timer_info(lua_State *L, const timer_pool *timer) +{ + lua_newtable(L); + if (timer->str) lua_pushf_str(L, "name", timer->str); + if (timer->func) lua_pushf_str(L, "func", timer->func); + lua_pushf_lint(L, "period", timer->period); + lua_pushf_bool(L, "oneshot", timer->oneshot); + lua_pushf_lint(L, "fires", timer->fires); +} +static void lua_pushi_timer_info(lua_State *L, lua_Integer idx, const timer_pool *timer) +{ + lua_pushinteger(L, idx); + lua_push_timer_info(L, timer); + lua_rawset(L,-3); +} +static int luacall_timer_info(lua_State *L) +{ + // timer_del(name) + lua_check_argc(L,"timer_info",1); + + LUA_STACK_GUARD_ENTER(L) + + const char *name = luaL_checkstring(L,1); + const timer_pool *timer = TimerPoolSearch(params.timers, name); + if (timer) + lua_push_timer_info(L, timer); + else + lua_pushnil(L); + + LUA_STACK_GUARD_RETURN(L,1) +} +static int luacall_timer_enum(lua_State *L) +{ + // timer_enum() + lua_check_argc(L,"timer_enum",0); + + LUA_STACK_GUARD_ENTER(L) + + lua_Integer n=1; + timer_pool *timer, *tmp, *p; + + lua_newtable(L); + HASH_ITER(hh, params.timers, timer, tmp) lua_pushi_timer_info(L, n++, timer); + + LUA_STACK_GUARD_RETURN(L,1) +} + // ---------------------------------------- @@ -4505,7 +4552,9 @@ static void lua_init_functions(void) // timers {"timer_set",luacall_timer_set}, - {"timer_del",luacall_timer_del} + {"timer_del",luacall_timer_del}, + {"timer_info",luacall_timer_info}, + {"timer_enum",luacall_timer_enum} }; for(int i=0;i<(sizeof(lfunc)/sizeof(*lfunc));i++) lua_register(params.L,lfunc[i].name,lfunc[i].f); diff --git a/nfq2/timer.c b/nfq2/timer.c index 36a42e8..bb8cedb 100644 --- a/nfq2/timer.c +++ b/nfq2/timer.c @@ -52,6 +52,7 @@ struct timer_pool *TimerPoolAdd(timer_pool **pp, const char *str, const char *fu elem->lua_ref = LUA_NOREF; elem->bt_prev = boottime_ms(); elem->n = ++timer_n; + elem->fires = 0; return elem; } @@ -66,7 +67,8 @@ static bool TimerPoolRunTimer(timer_pool *p) } lua_pushstring(params.L, p->str); lua_rawgeti(params.L, LUA_REGISTRYINDEX, p->lua_ref); - DLOG("\ntimer: '%s' function '%s' period %llu oneshot %u\n",p->str,p->func,p->period,p->oneshot); + p->fires++; + DLOG("\ntimer: '%s' function '%s' period %llu oneshot %u fires=%u\n",p->str,p->func,p->period,p->oneshot,p->fires); int status = lua_pcall(params.L, 2, 0, 0); if (status) { diff --git a/nfq2/timer.h b/nfq2/timer.h index 4df727d..a1eba0a 100644 --- a/nfq2/timer.h +++ b/nfq2/timer.h @@ -12,7 +12,7 @@ typedef struct timer_pool { bool oneshot; int lua_ref; uint64_t bt_prev; - unsigned int n; + unsigned int n, fires; UT_hash_handle hh; /* makes this structure hashable */ } timer_pool;