Skip to content

Commit

Permalink
pythongh-112367: Only free perf trampoline arenas at shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal committed Nov 24, 2023
1 parent e9d1360 commit fdc69f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions Include/internal/pycore_ceval.h
Expand Up @@ -101,6 +101,7 @@ extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
extern int _PyPerfTrampoline_Init(int activate);
extern int _PyPerfTrampoline_Fini(void);
extern void _PyPerfTrampoline_FreeArenas(void);
extern int _PyIsPerfTrampolineActive(void);
extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);
#ifdef PY_HAVE_PERF_TRAMPOLINE
Expand Down
31 changes: 27 additions & 4 deletions Python/perf_trampoline.c
Expand Up @@ -216,10 +216,24 @@ perf_map_write_entry(void *state, const void *code_addr,
PyMem_RawFree(perf_map_entry);
}

static void*
perf_map_init_state(void)
{
PyUnstable_PerfMapState_Init();
return NULL;
}

static int
perf_map_free_state(void *state)
{
PyUnstable_PerfMapState_Fini();
return 0;
}

_PyPerf_Callbacks _Py_perfmap_callbacks = {
NULL,
&perf_map_init_state,
&perf_map_write_entry,
NULL,
&perf_map_free_state,
};

static int
Expand Down Expand Up @@ -444,6 +458,9 @@ _PyPerfTrampoline_Init(int activate)
if (extra_code_index == -1) {
return -1;
}
if (trampoline_api.state == NULL && trampoline_api.init_state != NULL) {
trampoline_api.state = trampoline_api.init_state();
}
perf_status = PERF_STATUS_OK;
}
#endif
Expand All @@ -458,12 +475,18 @@ _PyPerfTrampoline_Fini(void)
if (tstate->interp->eval_frame == py_trampoline_evaluator) {
tstate->interp->eval_frame = NULL;
}
free_code_arenas();
if (perf_status == PERF_STATUS_OK && trampoline_api.state) {
trampoline_api.free_state(trampoline_api.state);
}
extra_code_index = -1;
#endif
return 0;
}

void _PyPerfTrampoline_FreeArenas(void) {
free_code_arenas();
}

int
PyUnstable_PerfTrampoline_SetPersistAfterFork(int enable){
#ifdef PY_HAVE_PERF_TRAMPOLINE
Expand All @@ -477,7 +500,7 @@ PyStatus
_PyPerfTrampoline_AfterFork_Child(void)
{
#ifdef PY_HAVE_PERF_TRAMPOLINE
PyUnstable_PerfMapState_Fini();
trampoline_api.free_state(trampoline_api.state);
if (persist_after_fork) {
char filename[256];
pid_t parent_pid = getppid();
Expand Down
2 changes: 1 addition & 1 deletion Python/pylifecycle.c
Expand Up @@ -1797,6 +1797,7 @@ finalize_interp_clear(PyThreadState *tstate)
_PyArg_Fini();
_Py_ClearFileSystemEncoding();
_PyPerfTrampoline_Fini();
_PyPerfTrampoline_FreeArenas();
}

finalize_interp_types(tstate->interp);
Expand Down Expand Up @@ -1854,7 +1855,6 @@ Py_FinalizeEx(void)
*/

_PyAtExit_Call(tstate->interp);
PyUnstable_PerfMapState_Fini();

/* Copy the core config, PyInterpreterState_Delete() free
the core config memory */
Expand Down

0 comments on commit fdc69f9

Please sign in to comment.