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
6ec6c5290b
commit
d2052557f7
@ -14,7 +14,8 @@ struct _LineRecord{
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct _FrameRecord{
|
struct _FrameRecord{
|
||||||
LinkedFrame* frame;
|
int callstack_size;
|
||||||
|
Frame* frame;
|
||||||
clock_t prev_time;
|
clock_t prev_time;
|
||||||
_LineRecord* prev_record;
|
_LineRecord* prev_record;
|
||||||
};
|
};
|
||||||
@ -26,8 +27,8 @@ struct LineProfiler{
|
|||||||
std::set<FuncDecl*> functions;
|
std::set<FuncDecl*> functions;
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
void _step(LinkedFrame*);
|
void _step(int, Frame*);
|
||||||
void _step_end(LinkedFrame*, int);
|
void _step_end(int, Frame*, int);
|
||||||
void end();
|
void end();
|
||||||
Str stats();
|
Str stats();
|
||||||
};
|
};
|
||||||
|
@ -74,7 +74,7 @@ PyObject* VM::_run_top_frame(){
|
|||||||
|
|
||||||
#define CEVAL_STEP_CALLBACK() \
|
#define CEVAL_STEP_CALLBACK() \
|
||||||
if(_ceval_on_step) _ceval_on_step(this, frame, byte); \
|
if(_ceval_on_step) _ceval_on_step(this, frame, byte); \
|
||||||
if(_profiler) _profiler->_step(callstack._tail); \
|
if(_profiler) _profiler->_step(callstack.size(), frame); \
|
||||||
if(!_next_breakpoint.empty()) { _next_breakpoint._step(this); }
|
if(!_next_breakpoint.empty()) { _next_breakpoint._step(this); }
|
||||||
|
|
||||||
#define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; }
|
#define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; }
|
||||||
|
@ -18,17 +18,16 @@ void LineProfiler::begin(){
|
|||||||
frames.clear();
|
frames.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineProfiler::_step(LinkedFrame* linked_frame){
|
void LineProfiler::_step(int callstack_size, Frame* frame){
|
||||||
Frame* frame = &linked_frame->frame;
|
|
||||||
auto line_info = frame->co->lines[frame->_ip];
|
auto line_info = frame->co->lines[frame->_ip];
|
||||||
if(line_info.is_virtual) return;
|
if(line_info.is_virtual) return;
|
||||||
std::string_view filename = frame->co->src->filename.sv();
|
std::string_view filename = frame->co->src->filename.sv();
|
||||||
int line = line_info.lineno;
|
int line = line_info.lineno;
|
||||||
|
|
||||||
if(frames.empty()){
|
if(frames.empty()){
|
||||||
frames.push({linked_frame, clock(), nullptr});
|
frames.push({callstack_size, frame, clock(), nullptr});
|
||||||
}else{
|
}else{
|
||||||
_step_end(linked_frame, line);
|
_step_end(callstack_size, frame, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& file_records = records[filename];
|
auto& file_records = records[filename];
|
||||||
@ -44,19 +43,13 @@ void LineProfiler::_step(LinkedFrame* linked_frame){
|
|||||||
frames.top().prev_record = &file_records.at(line);
|
frames.top().prev_record = &file_records.at(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineProfiler::_step_end(LinkedFrame* linked_frame, int line){
|
void LineProfiler::_step_end(int callstack_size, Frame* frame, int line){
|
||||||
clock_t now = clock();
|
clock_t now = clock();
|
||||||
_FrameRecord& top_frame_record = frames.top();
|
_FrameRecord& top_frame_record = frames.top();
|
||||||
_LineRecord* prev_record = top_frame_record.prev_record;
|
_LineRecord* prev_record = top_frame_record.prev_record;
|
||||||
|
|
||||||
int id_delta;
|
int id_delta = callstack_size - top_frame_record.callstack_size;
|
||||||
if(linked_frame == top_frame_record.frame){
|
PK_ASSERT(abs(id_delta) <= 1)
|
||||||
id_delta = 0;
|
|
||||||
}else if(linked_frame->f_back == top_frame_record.frame){
|
|
||||||
id_delta = 1;
|
|
||||||
}else{
|
|
||||||
id_delta = -1; // unsafe
|
|
||||||
}
|
|
||||||
|
|
||||||
// current line is about to change
|
// current line is about to change
|
||||||
if(prev_record->line != line){
|
if(prev_record->line != line){
|
||||||
@ -67,7 +60,7 @@ void LineProfiler::_step_end(LinkedFrame* linked_frame, int line){
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(id_delta == 1){
|
if(id_delta == 1){
|
||||||
frames.push({linked_frame, now, nullptr});
|
frames.push({callstack_size, frame, now, nullptr});
|
||||||
}else{
|
}else{
|
||||||
if(id_delta == -1) frames.pop();
|
if(id_delta == -1) frames.pop();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user