fix line_profiler

This commit is contained in:
blueloveTH 2025-05-28 17:33:03 +08:00
parent f4b2cd4a5f
commit 45828643b7
4 changed files with 13 additions and 56 deletions

View File

@ -24,3 +24,5 @@ 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);
void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event);

View File

@ -105,9 +105,6 @@ 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.enabled) {
LineProfiler__tracefunc_line(&self->line_profiler, frame);
}
} }
} }

View File

@ -32,6 +32,11 @@ static void pk_default_print(const char* data) { printf("%s", data); }
static void pk_default_flush() { fflush(stdout); } static void pk_default_flush() { fflush(stdout); }
void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event) {
LineProfiler* self = &pk_current_vm->line_profiler;
if(self->enabled && event == TRACE_EVENT_LINE) { LineProfiler__tracefunc_line(self, frame); }
}
static void py_TypeInfo__ctor(py_TypeInfo* self, static void py_TypeInfo__ctor(py_TypeInfo* self,
py_Name name, py_Name name,
py_Type index, py_Type index,

View File

@ -6,30 +6,6 @@
#include "pocketpy.h" #include "pocketpy.h"
#define py_interrupt()
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static BOOL WINAPI sigint_handler(DWORD dwCtrlType) {
if(dwCtrlType == CTRL_C_EVENT) {
py_interrupt();
return TRUE;
}
return FALSE;
}
#else
// set ctrl+c handler
#include <signal.h>
#include <unistd.h>
static void sigint_handler(int sig) { py_interrupt(); }
#endif
static char* read_file(const char* path) { static char* read_file(const char* path) {
FILE* file = fopen(path, "rb"); FILE* file = fopen(path, "rb");
if(file == NULL) { if(file == NULL) {
@ -45,57 +21,34 @@ static char* read_file(const char* path) {
return buffer; return buffer;
} }
static void tracefunc(py_Frame* frame, enum py_TraceEvent event) { void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event);
int line;
const char* filename = py_Frame_sourceloc(frame, &line);
const char* event_str;
switch(event) {
case TRACE_EVENT_LINE:
event_str = "line";
break;
case TRACE_EVENT_EXCEPTION:
event_str = "exception";
break;
case TRACE_EVENT_PUSH:
event_str = "push";
break;
case TRACE_EVENT_POP:
event_str = "pop";
break;
}
printf("\x1b[30m%s:%d, event=%s\x1b[0m\n", filename, line, event_str);
}
static char buf[2048]; static char buf[2048];
int main(int argc, char** argv) { int main(int argc, char** argv) {
#if _WIN32 #if _WIN32
SetConsoleCP(CP_UTF8); SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8);
// SetConsoleCtrlHandler((PHANDLER_ROUTINE)sigint_handler, TRUE);
#else
// signal(SIGINT, sigint_handler);
#endif #endif
bool trace = false; bool profile = false;
const char* filename = NULL; const char* filename = NULL;
for(int i = 1; i < argc; i++) { for(int i = 1; i < argc; i++) {
if(strcmp(argv[i], "--trace") == 0) { if(strcmp(argv[i], "--profile") == 0) {
trace = true; profile = true;
continue; continue;
} }
if(filename == NULL) { if(filename == NULL) {
filename = argv[i]; filename = argv[i];
continue; continue;
} }
printf("Usage: pocketpy [--trace] filename\n"); printf("Usage: pocketpy [--profile] filename\n");
} }
py_initialize(); py_initialize();
py_sys_setargv(argc, argv); py_sys_setargv(argc, argv);
if(trace) py_sys_settrace(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__ ") ");