mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 20:40:18 +00:00
add _ceval_on_step_profile
This commit is contained in:
parent
209b7f6831
commit
6cff9ff9ec
@ -19,11 +19,6 @@
|
|||||||
#define PK_ENABLE_THREAD 0
|
#define PK_ENABLE_THREAD 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Enable this for `vm->_ceval_on_step`
|
|
||||||
#ifndef PK_ENABLE_CEVAL_CALLBACK // can be overrided by cmake
|
|
||||||
#define PK_ENABLE_CEVAL_CALLBACK 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// GC min threshold
|
// GC min threshold
|
||||||
#ifndef PK_GC_MIN_THRESHOLD // can be overrided by cmake
|
#ifndef PK_GC_MIN_THRESHOLD // can be overrided by cmake
|
||||||
#define PK_GC_MIN_THRESHOLD 32768
|
#define PK_GC_MIN_THRESHOLD 32768
|
||||||
|
@ -150,9 +150,10 @@ public:
|
|||||||
// cached code objects for FSTRING_EVAL
|
// cached code objects for FSTRING_EVAL
|
||||||
std::map<std::string_view, CodeObject_> _cached_codes;
|
std::map<std::string_view, CodeObject_> _cached_codes;
|
||||||
|
|
||||||
#if PK_ENABLE_CEVAL_CALLBACK
|
// for user defined logic
|
||||||
void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr;
|
void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr;
|
||||||
#endif
|
// for line_profiler (users not to use this)
|
||||||
|
void (*_ceval_on_step_profile)(VM*, Frame*, Bytecode bc) = nullptr;
|
||||||
|
|
||||||
PrintFunc _stdout;
|
PrintFunc _stdout;
|
||||||
PrintFunc _stderr;
|
PrintFunc _stderr;
|
||||||
|
@ -5,6 +5,6 @@ class LineProfiler:
|
|||||||
|
|
||||||
def add_function(self, func: Callable) -> None: ...
|
def add_function(self, func: Callable) -> None: ...
|
||||||
|
|
||||||
def runcall(self, func: Callable, *args, **kw) -> None: ...
|
def runcall(self, func: Callable, *args) -> None: ...
|
||||||
|
|
||||||
def print_stats(self) -> None: ...
|
def print_stats(self) -> None: ...
|
||||||
|
@ -66,15 +66,15 @@ PyObject* VM::_run_top_frame(){
|
|||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
|
|
||||||
#if PK_ENABLE_CEVAL_CALLBACK
|
#define CEVAL_STEP_CALLBACK() \
|
||||||
#define CEVAL_STEP() byte = frame->next_bytecode(); if(_ceval_on_step) _ceval_on_step(this, frame.get(), byte)
|
if(_ceval_on_step) _ceval_on_step(this, frame.get(), byte); \
|
||||||
#else
|
if(_ceval_on_step_profile) _ceval_on_step_profile(this, frame.get(), byte);
|
||||||
#define CEVAL_STEP() byte = frame->next_bytecode()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; }
|
#define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; }
|
||||||
__NEXT_FRAME:
|
__NEXT_FRAME:
|
||||||
Bytecode CEVAL_STEP();
|
Bytecode byte = frame->next_bytecode();
|
||||||
|
CEVAL_STEP_CALLBACK();
|
||||||
|
|
||||||
// cache
|
// cache
|
||||||
const CodeObject* co = frame->co;
|
const CodeObject* co = frame->co;
|
||||||
const auto& co_consts = co->consts;
|
const auto& co_consts = co->consts;
|
||||||
@ -86,13 +86,13 @@ static void* OP_LABELS[] = {
|
|||||||
#undef OPCODE
|
#undef OPCODE
|
||||||
};
|
};
|
||||||
|
|
||||||
#define DISPATCH() { CEVAL_STEP(); goto *OP_LABELS[byte.op];}
|
#define DISPATCH() { byte = frame->next_bytecode(); CEVAL_STEP_CALLBACK(); goto *OP_LABELS[byte.op];}
|
||||||
#define TARGET(op) CASE_OP_##op:
|
#define TARGET(op) CASE_OP_##op:
|
||||||
goto *OP_LABELS[byte.op];
|
goto *OP_LABELS[byte.op];
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define TARGET(op) case OP_##op:
|
#define TARGET(op) case OP_##op:
|
||||||
#define DISPATCH() { CEVAL_STEP(); goto __NEXT_STEP;}
|
#define DISPATCH() { byte = frame->next_bytecode(); CEVAL_STEP_CALLBACK(); goto __NEXT_STEP;}
|
||||||
|
|
||||||
__NEXT_STEP:;
|
__NEXT_STEP:;
|
||||||
#if PK_DEBUG_CEVAL_STEP
|
#if PK_DEBUG_CEVAL_STEP
|
||||||
@ -831,7 +831,7 @@ __NEXT_STEP:;
|
|||||||
#undef DISPATCH
|
#undef DISPATCH
|
||||||
#undef TARGET
|
#undef TARGET
|
||||||
#undef DISPATCH_OP_CALL
|
#undef DISPATCH_OP_CALL
|
||||||
#undef CEVAL_STEP
|
#undef CEVAL_STEP_CALLBACK
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
PK_UNREACHABLE()
|
PK_UNREACHABLE()
|
||||||
}catch(HandledException){
|
}catch(HandledException){
|
||||||
|
@ -15,9 +15,16 @@ struct LineProfiler{
|
|||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind(type, "runcall(self, func, *args, **kw)", [](VM* vm, ArgsView args){
|
vm->bind(type, "runcall(self, func, *args)", [](VM* vm, ArgsView view){
|
||||||
// ...
|
LineProfiler& self = PK_OBJ_GET(LineProfiler, view[0]);
|
||||||
return vm->None;
|
// enable_by_count
|
||||||
|
PyObject* func = view[1];
|
||||||
|
const Tuple& args = CAST(Tuple&, view[2]);
|
||||||
|
for(PyObject* arg : args) vm->s_data.push(arg);
|
||||||
|
vm->s_data.push(func);
|
||||||
|
PyObject* ret = vm->vectorcall(args.size());
|
||||||
|
// disable_by_count
|
||||||
|
return ret;
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind(type, "print_stats(self)", [](VM* vm, ArgsView args){
|
vm->bind(type, "print_stats(self)", [](VM* vm, ArgsView args){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user