From f4b2cd4a5f93cc83be5a0953172450b2309baf0e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 28 May 2025 11:24:36 +0800 Subject: [PATCH] fix line_profiler --- include/pocketpy/interpreter/line_profiler.h | 5 +++-- include/pocketpy/interpreter/vm.h | 2 +- src/interpreter/ceval.c | 4 ++-- src/interpreter/line_profier.c | 19 ++++++++++++------- src/interpreter/vm.c | 5 ++--- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/include/pocketpy/interpreter/line_profiler.h b/include/pocketpy/interpreter/line_profiler.h index 4b5b345d..036dd99a 100644 --- a/include/pocketpy/interpreter/line_profiler.h +++ b/include/pocketpy/interpreter/line_profiler.h @@ -14,12 +14,13 @@ typedef struct LineProfiler { c11_smallmap_p2i records; // SourceData* -> LineRecord[] SourceLocation prev_loc; clock_t prev_time; + bool enabled; } LineProfiler; void LineProfiler__ctor(LineProfiler* self); void LineProfiler__dtor(LineProfiler* self); LineRecord* LineProfiler__get_record(LineProfiler* self, SourceLocation loc); -void LineProfiler__begin(LineProfiler* self, bool reset); +void LineProfiler__begin(LineProfiler* self); void LineProfiler__tracefunc_line(LineProfiler* self, py_Frame* frame); void LineProfiler__end(LineProfiler* self); -void VM__set_line_profiler(VM* self, LineProfiler* profiler); +void LineProfiler__reset(LineProfiler* self); diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index 2605e17b..00381d7a 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -53,7 +53,7 @@ typedef struct VM { py_StackRef curr_decl_based_function; TraceInfo trace_info; WatchdogInfo watchdog_info; - LineProfiler* line_profiler; + LineProfiler line_profiler; py_TValue vectorcall_buffer[PK_MAX_CO_VARNAMES]; InternedNames names; diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 66ebdb79..8c53e1db 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -105,8 +105,8 @@ FrameResult VM__run_top_frame(VM* self) { PK_INCREF(loc.src); self->trace_info.prev_loc = loc; self->trace_info.func(frame, TRACE_EVENT_LINE); - if(self->line_profiler) { - LineProfiler__tracefunc_line(self->line_profiler, frame); + if(self->line_profiler.enabled) { + LineProfiler__tracefunc_line(&self->line_profiler, frame); } } } diff --git a/src/interpreter/line_profier.c b/src/interpreter/line_profier.c index ac5a8916..b9251356 100644 --- a/src/interpreter/line_profier.c +++ b/src/interpreter/line_profier.c @@ -4,6 +4,7 @@ void LineProfiler__ctor(LineProfiler* self) { c11_smallmap_p2i__ctor(&self->records); self->prev_loc.src = NULL; self->prev_time = 0; + self->enabled = false; } void LineProfiler__dtor(LineProfiler* self) { @@ -25,17 +26,15 @@ LineRecord* LineProfiler__get_record(LineProfiler* self, SourceLocation loc) { return &lines[loc.lineno]; } -void LineProfiler__begin(LineProfiler* self, bool reset) { - if(self->records.length > 0 && reset) { - LineProfiler__dtor(self); - LineProfiler__ctor(self); - } +void LineProfiler__begin(LineProfiler* self) { + assert(!self->enabled); self->prev_loc.src = NULL; self->prev_time = 0; - VM__set_line_profiler(pk_current_vm, self); + self->enabled = true; } void LineProfiler__tracefunc_line(LineProfiler* self, py_Frame* frame) { + assert(self->enabled); clock_t now = clock(); if(self->prev_loc.src != NULL) { LineRecord* line = LineProfiler__get_record(self, self->prev_loc); @@ -47,10 +46,16 @@ void LineProfiler__tracefunc_line(LineProfiler* self, py_Frame* frame) { } void LineProfiler__end(LineProfiler* self) { + assert(self->enabled); if(self->prev_loc.src != NULL) { LineRecord* line = LineProfiler__get_record(self, self->prev_loc); line->hits++; line->time += clock() - self->prev_time; } - VM__set_line_profiler(pk_current_vm, NULL); + self->enabled = false; +} + +void LineProfiler__reset(LineProfiler* self) { + LineProfiler__dtor(self); + LineProfiler__ctor(self); } diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 43a6fe59..2c36a1c7 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -86,7 +86,7 @@ void VM__ctor(VM* self) { self->curr_decl_based_function = NULL; memset(&self->trace_info, 0, sizeof(TraceInfo)); memset(&self->watchdog_info, 0, sizeof(WatchdogInfo)); - self->line_profiler = NULL; + LineProfiler__ctor(&self->line_profiler); FixedMemoryPool__ctor(&self->pool_frame, sizeof(py_Frame), 32); @@ -259,6 +259,7 @@ void VM__ctor(VM* self) { void VM__dtor(VM* self) { // reset traceinfo py_sys_settrace(NULL, true); + LineProfiler__dtor(&self->line_profiler); // destroy all objects ManagedHeap__dtor(&self->heap); // clear frames @@ -271,8 +272,6 @@ void VM__dtor(VM* self) { InternedNames__dtor(&self->names); } -void VM__set_line_profiler(VM* self, LineProfiler* profiler) { self->line_profiler = profiler; } - void VM__push_frame(VM* self, py_Frame* frame) { frame->f_back = self->top_frame; self->top_frame = frame;