mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
add profiler
This commit is contained in:
parent
3c36b30f04
commit
169e825197
@ -24,5 +24,4 @@ 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 LineProfiler__reset(LineProfiler* self);
|
void LineProfiler__reset(LineProfiler* self);
|
||||||
|
c11_string* LineProfiler__get_report(LineProfiler* self);
|
||||||
void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event);
|
|
@ -163,6 +163,9 @@ PK_API void py_Frame_newlocals(py_Frame* frame, py_OutRef out);
|
|||||||
/// Returns `NULL` if not available.
|
/// Returns `NULL` if not available.
|
||||||
PK_API py_StackRef py_Frame_function(py_Frame* frame);
|
PK_API py_StackRef py_Frame_function(py_Frame* frame);
|
||||||
|
|
||||||
|
/// Trace function for the line profiler.
|
||||||
|
PK_API void py_LineProfiler_tracefunc(py_Frame* frame, enum py_TraceEvent event);
|
||||||
|
|
||||||
/// Run a source string.
|
/// Run a source string.
|
||||||
/// @param source source string.
|
/// @param source source string.
|
||||||
/// @param filename filename (for error messages).
|
/// @param filename filename (for error messages).
|
||||||
|
@ -28,15 +28,19 @@ def currentvm() -> int:
|
|||||||
|
|
||||||
|
|
||||||
def watchdog_begin(timeout: int):
|
def watchdog_begin(timeout: int):
|
||||||
"""
|
"""Begin the watchdog with `timeout` in milliseconds.
|
||||||
Begin the watchdog with `timeout` in milliseconds.
|
|
||||||
`PK_ENABLE_WATCHDOG` must be defined to `1` to use this feature.
|
`PK_ENABLE_WATCHDOG` must be defined to `1` to use this feature.
|
||||||
You need to call `watchdog_end()` later.
|
You need to call `watchdog_end()` later.
|
||||||
If `timeout` is reached, `TimeoutError` will be raised.
|
If `timeout` is reached, `TimeoutError` will be raised.
|
||||||
"""
|
"""
|
||||||
def watchdog_end():
|
def watchdog_end() -> None:
|
||||||
"""Reset the watchdog."""
|
"""End the watchdog after a call to `watchdog_begin()`."""
|
||||||
|
|
||||||
|
def profiler_begin() -> None: ...
|
||||||
|
def profiler_end() -> None: ...
|
||||||
|
def profiler_reset() -> None: ...
|
||||||
|
def profiler_report() -> dict[str, list[list]]: ...
|
||||||
|
|
||||||
class ComputeThread:
|
class ComputeThread:
|
||||||
def __init__(self, vm_index: Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]): ...
|
def __init__(self, vm_index: Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]): ...
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
#include "pocketpy/common/sstream.h"
|
||||||
#include "pocketpy/interpreter/line_profiler.h"
|
#include "pocketpy/interpreter/line_profiler.h"
|
||||||
|
#include "pocketpy/objects/sourcedata.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
void LineProfiler__ctor(LineProfiler* self) {
|
void LineProfiler__ctor(LineProfiler* self) {
|
||||||
@ -60,3 +62,33 @@ void LineProfiler__reset(LineProfiler* self) {
|
|||||||
LineProfiler__dtor(self);
|
LineProfiler__dtor(self);
|
||||||
LineProfiler__ctor(self);
|
LineProfiler__ctor(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c11_string* LineProfiler__get_report(LineProfiler* self) {
|
||||||
|
c11_sbuf sbuf;
|
||||||
|
c11_sbuf__ctor(&sbuf);
|
||||||
|
c11_sbuf__write_char(&sbuf, '{');
|
||||||
|
for(int i = 0; i < self->records.length; i++) {
|
||||||
|
c11_smallmap_p2i_KV kv = c11__getitem(c11_smallmap_p2i_KV, &self->records, i);
|
||||||
|
SourceData_ src = (SourceData_)kv.key;
|
||||||
|
int line_record_length = src->line_starts.length + 1;
|
||||||
|
c11_sv src_name = c11_string__sv(src->filename);
|
||||||
|
c11_sbuf__write_quoted(&sbuf, src_name, '"');
|
||||||
|
c11_sbuf__write_cstr(&sbuf, ": [");
|
||||||
|
LineRecord* lines = (LineRecord*)kv.value;
|
||||||
|
for(int j = 1; j < line_record_length; j++) {
|
||||||
|
// [<j>, <hits>, <time>]
|
||||||
|
c11_sbuf__write_cstr(&sbuf, "[");
|
||||||
|
c11_sbuf__write_int(&sbuf, j);
|
||||||
|
c11_sbuf__write_cstr(&sbuf, ", ");
|
||||||
|
c11_sbuf__write_i64(&sbuf, lines[j].hits);
|
||||||
|
c11_sbuf__write_cstr(&sbuf, ", ");
|
||||||
|
c11_sbuf__write_i64(&sbuf, lines[j].time);
|
||||||
|
c11_sbuf__write_cstr(&sbuf, "]");
|
||||||
|
if(j < line_record_length - 1) c11_sbuf__write_cstr(&sbuf, ", ");
|
||||||
|
}
|
||||||
|
c11_sbuf__write_cstr(&sbuf, "]");
|
||||||
|
if(i < self->records.length - 1) c11_sbuf__write_cstr(&sbuf, ", ");
|
||||||
|
}
|
||||||
|
c11_sbuf__write_char(&sbuf, '}');
|
||||||
|
return c11_sbuf__submit(&sbuf);
|
||||||
|
}
|
||||||
|
@ -34,9 +34,9 @@ static void pk_default_flush() { fflush(stdout); }
|
|||||||
|
|
||||||
static int pk_default_getchr() { return getchar(); }
|
static int pk_default_getchr() { return getchar(); }
|
||||||
|
|
||||||
void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event) {
|
void py_LineProfiler_tracefunc(py_Frame* frame, enum py_TraceEvent event) {
|
||||||
LineProfiler* self = &pk_current_vm->line_profiler;
|
LineProfiler* self = &pk_current_vm->line_profiler;
|
||||||
if(self->enabled && event == TRACE_EVENT_LINE) { LineProfiler__tracefunc_line(self, frame); }
|
if(self->enabled && event == TRACE_EVENT_LINE) LineProfiler__tracefunc_line(self, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int BinTree__cmp_cstr(void* lhs, void* rhs) {
|
static int BinTree__cmp_cstr(void* lhs, void* rhs) {
|
||||||
|
@ -467,6 +467,35 @@ static void pkpy_configmacros_add(py_Ref dict, const char* key, int val) {
|
|||||||
py_dict_setitem_by_str(dict, key, &tmp);
|
py_dict_setitem_by_str(dict, key, &tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool pkpy_profiler_begin(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(0);
|
||||||
|
LineProfiler__begin(&pk_current_vm->line_profiler);
|
||||||
|
py_newnone(py_retval());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool pkpy_profiler_end(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(0);
|
||||||
|
LineProfiler__end(&pk_current_vm->line_profiler);
|
||||||
|
py_newnone(py_retval());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool pkpy_profiler_reset(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(0);
|
||||||
|
LineProfiler__reset(&pk_current_vm->line_profiler);
|
||||||
|
py_newnone(py_retval());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool pkpy_profiler_report(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(0);
|
||||||
|
c11_string* report = LineProfiler__get_report(&pk_current_vm->line_profiler);
|
||||||
|
bool ok = py_json_loads(report->data);
|
||||||
|
c11_string__delete(report);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
void pk__add_module_pkpy() {
|
void pk__add_module_pkpy() {
|
||||||
py_Ref mod = py_newmodule("pkpy");
|
py_Ref mod = py_newmodule("pkpy");
|
||||||
|
|
||||||
@ -516,6 +545,11 @@ void pk__add_module_pkpy() {
|
|||||||
pk_ComputeThread__register(mod);
|
pk_ComputeThread__register(mod);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
py_bindfunc(mod, "profiler_begin", pkpy_profiler_begin);
|
||||||
|
py_bindfunc(mod, "profiler_end", pkpy_profiler_end);
|
||||||
|
py_bindfunc(mod, "profiler_reset", pkpy_profiler_reset);
|
||||||
|
py_bindfunc(mod, "profiler_report", pkpy_profiler_report);
|
||||||
|
|
||||||
py_Ref configmacros = py_emplacedict(mod, py_name("configmacros"));
|
py_Ref configmacros = py_emplacedict(mod, py_name("configmacros"));
|
||||||
py_newdict(configmacros);
|
py_newdict(configmacros);
|
||||||
pkpy_configmacros_add(configmacros, "PK_ENABLE_OS", PK_ENABLE_OS);
|
pkpy_configmacros_add(configmacros, "PK_ENABLE_OS", PK_ENABLE_OS);
|
||||||
|
@ -25,7 +25,6 @@ static char* read_file(const char* path) {
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event);
|
|
||||||
static char buf[2048];
|
static char buf[2048];
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
@ -52,8 +51,7 @@ int main(int argc, char** argv) {
|
|||||||
py_initialize();
|
py_initialize();
|
||||||
py_sys_setargv(argc, argv);
|
py_sys_setargv(argc, argv);
|
||||||
|
|
||||||
assert(!profile); // not implemented yet
|
if(profile) py_sys_settrace(py_LineProfiler_tracefunc, true);
|
||||||
// if(profile) py_sys_settrace(LineProfiler__tracefunc, true);
|
|
||||||
|
|
||||||
if(filename == NULL) {
|
if(filename == NULL) {
|
||||||
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user