mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
fix line_profiler
This commit is contained in:
parent
c4ab63ca15
commit
f4b2cd4a5f
@ -14,12 +14,13 @@ typedef struct LineProfiler {
|
|||||||
c11_smallmap_p2i records; // SourceData* -> LineRecord[]
|
c11_smallmap_p2i records; // SourceData* -> LineRecord[]
|
||||||
SourceLocation prev_loc;
|
SourceLocation prev_loc;
|
||||||
clock_t prev_time;
|
clock_t prev_time;
|
||||||
|
bool enabled;
|
||||||
} LineProfiler;
|
} LineProfiler;
|
||||||
|
|
||||||
void LineProfiler__ctor(LineProfiler* self);
|
void LineProfiler__ctor(LineProfiler* self);
|
||||||
void LineProfiler__dtor(LineProfiler* self);
|
void LineProfiler__dtor(LineProfiler* self);
|
||||||
LineRecord* LineProfiler__get_record(LineProfiler* self, SourceLocation loc);
|
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__tracefunc_line(LineProfiler* self, py_Frame* frame);
|
||||||
void LineProfiler__end(LineProfiler* self);
|
void LineProfiler__end(LineProfiler* self);
|
||||||
void VM__set_line_profiler(VM* self, LineProfiler* profiler);
|
void LineProfiler__reset(LineProfiler* self);
|
||||||
|
@ -53,7 +53,7 @@ typedef struct VM {
|
|||||||
py_StackRef curr_decl_based_function;
|
py_StackRef curr_decl_based_function;
|
||||||
TraceInfo trace_info;
|
TraceInfo trace_info;
|
||||||
WatchdogInfo watchdog_info;
|
WatchdogInfo watchdog_info;
|
||||||
LineProfiler* line_profiler;
|
LineProfiler line_profiler;
|
||||||
py_TValue vectorcall_buffer[PK_MAX_CO_VARNAMES];
|
py_TValue vectorcall_buffer[PK_MAX_CO_VARNAMES];
|
||||||
|
|
||||||
InternedNames names;
|
InternedNames names;
|
||||||
|
@ -105,8 +105,8 @@ FrameResult VM__run_top_frame(VM* self) {
|
|||||||
PK_INCREF(loc.src);
|
PK_INCREF(loc.src);
|
||||||
self->trace_info.prev_loc = loc;
|
self->trace_info.prev_loc = loc;
|
||||||
self->trace_info.func(frame, TRACE_EVENT_LINE);
|
self->trace_info.func(frame, TRACE_EVENT_LINE);
|
||||||
if(self->line_profiler) {
|
if(self->line_profiler.enabled) {
|
||||||
LineProfiler__tracefunc_line(self->line_profiler, frame);
|
LineProfiler__tracefunc_line(&self->line_profiler, frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ void LineProfiler__ctor(LineProfiler* self) {
|
|||||||
c11_smallmap_p2i__ctor(&self->records);
|
c11_smallmap_p2i__ctor(&self->records);
|
||||||
self->prev_loc.src = NULL;
|
self->prev_loc.src = NULL;
|
||||||
self->prev_time = 0;
|
self->prev_time = 0;
|
||||||
|
self->enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineProfiler__dtor(LineProfiler* self) {
|
void LineProfiler__dtor(LineProfiler* self) {
|
||||||
@ -25,17 +26,15 @@ LineRecord* LineProfiler__get_record(LineProfiler* self, SourceLocation loc) {
|
|||||||
return &lines[loc.lineno];
|
return &lines[loc.lineno];
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineProfiler__begin(LineProfiler* self, bool reset) {
|
void LineProfiler__begin(LineProfiler* self) {
|
||||||
if(self->records.length > 0 && reset) {
|
assert(!self->enabled);
|
||||||
LineProfiler__dtor(self);
|
|
||||||
LineProfiler__ctor(self);
|
|
||||||
}
|
|
||||||
self->prev_loc.src = NULL;
|
self->prev_loc.src = NULL;
|
||||||
self->prev_time = 0;
|
self->prev_time = 0;
|
||||||
VM__set_line_profiler(pk_current_vm, self);
|
self->enabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineProfiler__tracefunc_line(LineProfiler* self, py_Frame* frame) {
|
void LineProfiler__tracefunc_line(LineProfiler* self, py_Frame* frame) {
|
||||||
|
assert(self->enabled);
|
||||||
clock_t now = clock();
|
clock_t now = clock();
|
||||||
if(self->prev_loc.src != NULL) {
|
if(self->prev_loc.src != NULL) {
|
||||||
LineRecord* line = LineProfiler__get_record(self, self->prev_loc);
|
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) {
|
void LineProfiler__end(LineProfiler* self) {
|
||||||
|
assert(self->enabled);
|
||||||
if(self->prev_loc.src != NULL) {
|
if(self->prev_loc.src != NULL) {
|
||||||
LineRecord* line = LineProfiler__get_record(self, self->prev_loc);
|
LineRecord* line = LineProfiler__get_record(self, self->prev_loc);
|
||||||
line->hits++;
|
line->hits++;
|
||||||
line->time += clock() - self->prev_time;
|
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);
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ void VM__ctor(VM* self) {
|
|||||||
self->curr_decl_based_function = NULL;
|
self->curr_decl_based_function = NULL;
|
||||||
memset(&self->trace_info, 0, sizeof(TraceInfo));
|
memset(&self->trace_info, 0, sizeof(TraceInfo));
|
||||||
memset(&self->watchdog_info, 0, sizeof(WatchdogInfo));
|
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);
|
FixedMemoryPool__ctor(&self->pool_frame, sizeof(py_Frame), 32);
|
||||||
|
|
||||||
@ -259,6 +259,7 @@ void VM__ctor(VM* self) {
|
|||||||
void VM__dtor(VM* self) {
|
void VM__dtor(VM* self) {
|
||||||
// reset traceinfo
|
// reset traceinfo
|
||||||
py_sys_settrace(NULL, true);
|
py_sys_settrace(NULL, true);
|
||||||
|
LineProfiler__dtor(&self->line_profiler);
|
||||||
// destroy all objects
|
// destroy all objects
|
||||||
ManagedHeap__dtor(&self->heap);
|
ManagedHeap__dtor(&self->heap);
|
||||||
// clear frames
|
// clear frames
|
||||||
@ -271,8 +272,6 @@ void VM__dtor(VM* self) {
|
|||||||
InternedNames__dtor(&self->names);
|
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) {
|
void VM__push_frame(VM* self, py_Frame* frame) {
|
||||||
frame->f_back = self->top_frame;
|
frame->f_back = self->top_frame;
|
||||||
self->top_frame = frame;
|
self->top_frame = frame;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user