From 4438b548461d27f7df12e2b71d053a8c6eb19aa7 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 22 May 2025 17:17:06 +0800 Subject: [PATCH] refactor watchdog --- include/pocketpy/interpreter/vm.h | 6 ++---- include/pocketpy/pocketpy.h | 8 +++----- include/typings/pkpy.pyi | 10 ++++------ src/interpreter/ceval.c | 9 ++++----- src/interpreter/vm.c | 1 + src/modules/pkpy.c | 19 +++---------------- src/public/py_dict.c | 2 +- src/public/py_list.c | 1 + 8 files changed, 19 insertions(+), 37 deletions(-) diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index 2dabd1d4..fbfe8964 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -5,10 +5,9 @@ #include "pocketpy/pocketpy.h" #include "pocketpy/interpreter/heap.h" #include "pocketpy/interpreter/frame.h" -#include "pocketpy/interpreter/modules.h" #include "pocketpy/interpreter/typeinfo.h" #include "pocketpy/interpreter/name.h" -#include "pocketpy/interpreter/types.h" +#include // TODO: // 1. __eq__ and __ne__ fallbacks @@ -24,8 +23,7 @@ typedef struct TraceInfo { } TraceInfo; typedef struct WatchdogInfo { - py_i64 timeout; - py_i64 last_reset_time; + clock_t max_reset_time; } WatchdogInfo; typedef struct VM { diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index afe38808..14652041 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -111,14 +111,12 @@ PK_API void py_sys_settrace(py_TraceFunc func); /// Setup the callbacks for the current VM. PK_API py_Callbacks* py_callbacks(); -/// Begin the watchdog with a timeout in milliseconds. +/// Begin the watchdog with `timeout` in milliseconds. /// `PK_ENABLE_WATCHDOG` must be defined to `1` to use this feature. -/// You need to call `py_watchdog_reset()` periodically to keep the watchdog alive. -/// If the timeout is reached, `TimeoutError` will be raised. +/// You need to call `py_watchdog_end()` later. +/// If `timeout` is reached, `TimeoutError` will be raised. PK_API void py_watchdog_begin(py_i64 timeout); /// Reset the watchdog. -PK_API void py_watchdog_reset(); -/// End the watchdog. PK_API void py_watchdog_end(); /// Get the current source location of the frame. diff --git a/include/typings/pkpy.pyi b/include/typings/pkpy.pyi index 69b9c691..7c5e9285 100644 --- a/include/typings/pkpy.pyi +++ b/include/typings/pkpy.pyi @@ -27,15 +27,13 @@ def currentvm() -> int: def watchdog_begin(timeout: int): """ - Begin the watchdog with a timeout in milliseconds. + Begin the watchdog with `timeout` in milliseconds. `PK_ENABLE_WATCHDOG` must be defined to `1` to use this feature. - You need to call `watchdog_reset()` periodically to keep the watchdog alive. - If the timeout is reached, `TimeoutError` will be raised. + You need to call `watchdog_end()` later. + If `timeout` is reached, `TimeoutError` will be raised. """ -def watchdog_reset(): - """Reset the watchdog.""" def watchdog_end(): - """End the watchdog.""" + """Reset the watchdog.""" class ComputeThread: diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 3f889bae..328a8377 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -109,11 +109,10 @@ FrameResult VM__run_top_frame(VM* self) { } #if PK_ENABLE_WATCHDOG - if(self->watchdog_info.timeout > 0){ - py_i64 now = clock() / (CLOCKS_PER_SEC / 1000); - py_i64 delta = now - self->watchdog_info.last_reset_time; - if(delta > self->watchdog_info.timeout) { - self->watchdog_info.last_reset_time = now; + if(self->watchdog_info.max_reset_time > 0){ + clock_t now = clock(); + if(now > self->watchdog_info.max_reset_time) { + self->watchdog_info.max_reset_time = 0; TimeoutError("watchdog timeout"); goto __ERROR; } diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index e9b5b6e3..721f0bb8 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -6,6 +6,7 @@ #include "pocketpy/interpreter/modules.h" #include "pocketpy/interpreter/typeinfo.h" #include "pocketpy/objects/base.h" +#include "pocketpy/interpreter/types.h" #include "pocketpy/common/_generated.h" #include "pocketpy/pocketpy.h" #include diff --git a/src/modules/pkpy.c b/src/modules/pkpy.c index 62c34533..919bb8f3 100644 --- a/src/modules/pkpy.c +++ b/src/modules/pkpy.c @@ -7,6 +7,7 @@ #include "pocketpy/interpreter/vm.h" #include "pocketpy/common/threads.h" +#include #define DEF_TVALUE_METHODS(T, Field) \ static bool TValue_##T##__new__(int argc, py_Ref argv) { \ @@ -81,18 +82,12 @@ static bool pkpy_currentvm(int argc, py_Ref argv) { #if PK_ENABLE_WATCHDOG void py_watchdog_begin(py_i64 timeout) { WatchdogInfo* info = &pk_current_vm->watchdog_info; - info->timeout = timeout; - py_watchdog_reset(); -} - -void py_watchdog_reset() { - WatchdogInfo* info = &pk_current_vm->watchdog_info; - info->last_reset_time = clock() / (CLOCKS_PER_SEC / 1000); + info->max_reset_time = clock() + (timeout * (CLOCKS_PER_SEC / 1000)); } void py_watchdog_end() { WatchdogInfo* info = &pk_current_vm->watchdog_info; - info->timeout = 0; + info->max_reset_time = 0; } static bool pkpy_watchdog_begin(int argc, py_Ref argv) { @@ -103,13 +98,6 @@ static bool pkpy_watchdog_begin(int argc, py_Ref argv) { return true; } -static bool pkpy_watchdog_reset(int argc, py_Ref argv) { - PY_CHECK_ARGC(0); - py_watchdog_reset(); - py_newnone(py_retval()); - return true; -} - static bool pkpy_watchdog_end(int argc, py_Ref argv) { PY_CHECK_ARGC(0); py_watchdog_end(); @@ -508,7 +496,6 @@ void pk__add_module_pkpy() { #if PK_ENABLE_WATCHDOG py_bindfunc(mod, "watchdog_begin", pkpy_watchdog_begin); - py_bindfunc(mod, "watchdog_reset", pkpy_watchdog_reset); py_bindfunc(mod, "watchdog_end", pkpy_watchdog_end); #endif diff --git a/src/public/py_dict.c b/src/public/py_dict.c index 6ed723bf..347b1f26 100644 --- a/src/public/py_dict.c +++ b/src/public/py_dict.c @@ -2,7 +2,7 @@ #include "pocketpy/common/utils.h" #include "pocketpy/common/sstream.h" -#include "pocketpy/objects/object.h" +#include "pocketpy/interpreter/types.h" #include "pocketpy/interpreter/vm.h" static uint32_t Dict__next_cap(uint32_t cap) { diff --git a/src/public/py_list.c b/src/public/py_list.c index cb0ac7f8..35585a6e 100644 --- a/src/public/py_list.c +++ b/src/public/py_list.c @@ -2,6 +2,7 @@ #include "pocketpy/common/utils.h" #include "pocketpy/interpreter/vm.h" +#include "pocketpy/interpreter/types.h" #include "pocketpy/common/sstream.h" void py_newlist(py_OutRef out) {