Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2a918e4
Allow Linux perf profiler to see Python calls
pablogsal Aug 7, 2022
cea1420
Add test
pablogsal Aug 20, 2022
4107c53
Update PCbuild/_freeze_module.vcxproj.filters
pablogsal Aug 20, 2022
5e34e66
munmap pages on shutdown, keep FILE open
tiran Aug 21, 2022
a26a850
Fix tests
pablogsal Aug 21, 2022
8170b24
Skip tests if sanitizer is active
pablogsal Aug 21, 2022
9df1c93
Add ARM64 code generated by aarch64-linux-gnu-gcc
tiran Aug 21, 2022
d8f396d
Address review comments
pablogsal Aug 21, 2022
d35c5d7
Secure fopen, use unraisable, continue on error
tiran Aug 22, 2022
2664b12
cleanup resources, set to uninit
tiran Aug 22, 2022
e6c365a
Allow to set custom callbacks
pablogsal Aug 22, 2022
5513fb1
Add comment to asm file
pablogsal Aug 22, 2022
76c7dc0
fixup! Merge pull request #36 from tiran/perf-file
pablogsal Aug 22, 2022
a545b3c
Add comments to the perf_trampoline file and format file
pablogsal Aug 22, 2022
5130c8d
Correct News entry
pablogsal Aug 22, 2022
991366b
Update Lib/test/test_perf_profiler.py
pablogsal Aug 22, 2022
0a0e53d
Rename perf macro
pablogsal Aug 22, 2022
7ea3371
Fix some typos
pablogsal Aug 22, 2022
680db66
Improve perf profiler tests
tiran Aug 22, 2022
1263a29
Add guard for initialization
pablogsal Aug 22, 2022
a42bde5
Add acks
pablogsal Aug 22, 2022
b780d2a
Initialize perf file lazily
pablogsal Aug 22, 2022
04bf416
Address review comments
pablogsal Aug 22, 2022
7558df2
Complain if there is already a evaluator frame when deactivating/acti…
pablogsal Aug 22, 2022
d1ebc88
Fix some errors on CI
pablogsal Aug 22, 2022
a83a31b
Reorder arguments to speed up trampoline
tiran Aug 22, 2022
0febd84
Preserve frame pointer
pablogsal Aug 22, 2022
dc5a6a5
Support perf backend and better handle forks
pablogsal Aug 22, 2022
be72b92
Fix more fork problems
pablogsal Aug 22, 2022
b5739f4
Update Lib/test/test_perf_profiler.py
pablogsal Aug 22, 2022
04c0c14
Handle missing backends
pablogsal Aug 22, 2022
e810ce6
Update Lib/test/test_perf_profiler.py
pablogsal Aug 22, 2022
bc8bf4e
clean up perf files
pablogsal Aug 22, 2022
0252845
Update Misc/NEWS.d/next/Core and Builtins/2022-08-20-18-36-40.gh-issu…
pablogsal Aug 22, 2022
264bed7
Test fork support, fix some fork problems and improve test file
pablogsal Aug 23, 2022
a31a498
Add more tests
pablogsal Aug 23, 2022
f591e8d
Update Objects/perf_trampoline.c
pablogsal Aug 23, 2022
0af2a08
make argument mandatory
pablogsal Aug 23, 2022
861ae09
Use struct for perf callbacks
tiran Aug 23, 2022
3058cf0
Rename macro to PY_HAVE_PERF_TRAMPOLINE
tiran Aug 23, 2022
07ee991
Merge pull request #39 from tiran/perf_callback_struct
pablogsal Aug 23, 2022
be612a9
Allow gdb to unwind
pablogsal Aug 23, 2022
f4e3fff
Merge remote-tracking branch 'upstream/main' into perf
pablogsal Aug 25, 2022
c27f8b1
Add docs
pablogsal Aug 25, 2022
e27a2c4
fixup! Add docs
pablogsal Aug 25, 2022
81c7f4b
fixup! fixup! Add docs
pablogsal Aug 25, 2022
ef0650b
Update sys API names in the NEWS entry.
gpshead Aug 29, 2022
d8932d2
Add environment variable
pablogsal Aug 29, 2022
0f303ff
Merge branch 'main' into perf
pablogsal Aug 29, 2022
e3f846e
Document the env var and the -X option
pablogsal Aug 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use struct for perf callbacks
  • Loading branch information
tiran committed Aug 23, 2022
commit 861ae09e9c8e8d76f9434869dad0ad6c8e4b393a
33 changes: 15 additions & 18 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,25 @@ extern PyObject* _PyEval_BuiltinsFromGlobals(

// Trampoline API

typedef void* (*trampoline_state_init)(void);
typedef void (*trampoline_state_write)(void* state, const void *code_addr,
unsigned int code_size, PyCodeObject* code);
typedef int (*trampoline_state_free)(void* state);
extern int _PyPerfTrampoline_SetCallbacks(
trampoline_state_init init_state,
trampoline_state_write write_state,
trampoline_state_free free_state
);
extern void _PyPerfTrampoline_GetCallbacks(
trampoline_state_init *init_state,
trampoline_state_write *write_state,
trampoline_state_free *free_state
);
typedef struct {
// Callback to initialize the trampoline state
void* (*init_state)(void);
// Callback to register every trampoline being created
void (*write_state)(void* state, const void *code_addr,
unsigned int code_size, PyCodeObject* code);
// Callback to free the trampoline state
int (*free_state)(void* state);
} _PyPerf_Callbacks;

extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
extern int _PyPerfTrampoline_Init(int activate);
extern int _PyPerfTrampoline_Fini(void);
extern int _PyIsPerfTrampolineActive(void);
extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);

extern void* _Py_perf_map_get_file(void);
extern void _Py_perf_map_write_entry(void*, const void*, unsigned int, PyCodeObject*);
extern int _Py_perf_map_close(void*);
#ifdef _PY_HAVE_PERF_TRAMPOLINE
extern _PyPerf_Callbacks _Py_perfmap_callbacks;
#endif

static inline PyObject*
_PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag)
Expand Down
60 changes: 31 additions & 29 deletions Objects/perf_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,10 @@ struct code_arena_st {
typedef struct code_arena_st code_arena_t;

struct trampoline_api_st {
trampoline_state_init
init_state; // Callback to initialize the trampoline state
trampoline_state_write
write_state; // Callback to register every trampoline being created
trampoline_state_free free_state; // Callback to free the trampoline state
void* (*init_state)(void);
void (*write_state)(void* state, const void *code_addr,
unsigned int code_size, PyCodeObject* code);
int (*free_state)(void* state);
void *state;
};

Expand All @@ -192,8 +191,9 @@ static code_arena_t *code_arena;
static trampoline_api_t trampoline_api;

static FILE *perf_map_file;
void *
_Py_perf_map_get_file(void)

static void *
perf_map_get_file(void)
{
if (perf_map_file) {
return perf_map_file;
Expand Down Expand Up @@ -221,8 +221,8 @@ _Py_perf_map_get_file(void)
return perf_map_file;
}

int
_Py_perf_map_close(void *state)
static int
perf_map_close(void *state)
{
FILE *fp = (FILE *)state;
int ret = 0;
Expand All @@ -234,8 +234,8 @@ _Py_perf_map_close(void *state)
return ret;
}

void
_Py_perf_map_write_entry(void *state, const void *code_addr,
static void
perf_map_write_entry(void *state, const void *code_addr,
unsigned int code_size, PyCodeObject *co)
{
assert(state != NULL);
Expand All @@ -257,6 +257,12 @@ _Py_perf_map_write_entry(void *state, const void *code_addr,
fflush(method_file);
}

_PyPerf_Callbacks _Py_perfmap_callbacks = {
&perf_map_get_file,
&perf_map_write_entry,
&perf_map_close
};

static int
new_code_arena(void)
{
Expand Down Expand Up @@ -393,36 +399,32 @@ _PyIsPerfTrampolineActive(void)
}

void
_PyPerfTrampoline_GetCallbacks(trampoline_state_init *init_state,
trampoline_state_write *write_state,
trampoline_state_free *free_state)
_PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *callbacks)
{
#ifdef _PY_HAVE_PERF_TRAMPOLINE
if (init_state) {
*init_state = trampoline_api.init_state;
}
if (write_state) {
*write_state = trampoline_api.write_state;
}
if (free_state) {
*free_state = trampoline_api.free_state;
if (callbacks == NULL) {
return;
}
#ifdef _PY_HAVE_PERF_TRAMPOLINE
callbacks->init_state = trampoline_api.init_state;
callbacks->write_state = trampoline_api.write_state;
callbacks->free_state = trampoline_api.free_state;
#endif
return;
}

int
_PyPerfTrampoline_SetCallbacks(trampoline_state_init init_state,
trampoline_state_write write_state,
trampoline_state_free free_state)
_PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *callbacks)
{
if (callbacks == NULL) {
return -1;
}
#ifdef _PY_HAVE_PERF_TRAMPOLINE
if (trampoline_api.state) {
_PyPerfTrampoline_Fini();
}
trampoline_api.init_state = init_state;
trampoline_api.write_state = write_state;
trampoline_api.free_state = free_state;
trampoline_api.init_state = callbacks->init_state;
trampoline_api.write_state = callbacks->write_state;
trampoline_api.free_state = callbacks->free_state;
trampoline_api.state = NULL;
perf_status = PERF_STATUS_OK;
#endif
Expand Down
5 changes: 2 additions & 3 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1153,9 +1153,8 @@ init_interp_main(PyThreadState *tstate)

#ifdef _PY_HAVE_PERF_TRAMPOLINE
if (config->perf_profiling) {
if (_PyPerfTrampoline_SetCallbacks(
_Py_perf_map_get_file, _Py_perf_map_write_entry, _Py_perf_map_close
) < 0 || _PyPerfTrampoline_Init(config->perf_profiling) < 0) {
if (_PyPerfTrampoline_SetCallbacks(&_Py_perfmap_callbacks) < 0 ||
_PyPerfTrampoline_Init(config->perf_profiling) < 0) {
return _PyStatus_ERR("can't initialize the perf trampoline");
}
}
Expand Down
12 changes: 4 additions & 8 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2009,14 +2009,10 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend)
{
if (strcmp(backend, "perf") == 0) {
#ifdef _PY_HAVE_PERF_TRAMPOLINE
trampoline_state_init init_callback = NULL;
_PyPerfTrampoline_GetCallbacks(&init_callback, NULL, NULL);
if (init_callback != _Py_perf_map_get_file) {
if ( _PyPerfTrampoline_SetCallbacks(
_Py_perf_map_get_file,
_Py_perf_map_write_entry,
_Py_perf_map_close
) < 0 ) {
_PyPerf_Callbacks cur_cb;
_PyPerfTrampoline_GetCallbacks(&cur_cb);
if (cur_cb.init_state != _Py_perfmap_callbacks.init_state) {
if (_PyPerfTrampoline_SetCallbacks(&_Py_perfmap_callbacks) < 0 ) {
PyErr_SetString(PyExc_ValueError, "can't activate perf trampoline");
return NULL;
}
Expand Down