Skip to content

Commit

Permalink
Update perf_trampoline.c
Browse files Browse the repository at this point in the history
  • Loading branch information
gsallam authored Sep 21, 2023
1 parent ccc1ec5 commit 3098e9d
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions Python/perf_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ typedef struct trampoline_api_st trampoline_api_t;
#define perf_code_arena _PyRuntime.ceval.perf.code_arena
#define trampoline_api _PyRuntime.ceval.perf.trampoline_api
#define perf_map_file _PyRuntime.ceval.perf.map_file

#define persist_after_fork _PyRuntime.ceval.perf.persist_after_fork

static void
perf_map_write_entry(void *state, const void *code_addr,
Expand Down Expand Up @@ -328,6 +328,24 @@ compile_trampoline(void)
return code_arena_new_code(perf_code_arena);
}

int PyUnstable_PerfTrampoline_CompileCode(PyCodeObject *co)
{
py_trampoline f = NULL;
assert(extra_code_index != -1);
int ret = _PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f);
if (ret != 0 || f == NULL) {
py_trampoline new_trampoline = compile_trampoline();
if (new_trampoline == NULL) {
return 0;
}
trampoline_api.write_state(trampoline_api.state, new_trampoline,
code_arena->code_size, co);

Check failure on line 342 in Python/perf_trampoline.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘code_arena’ undeclared (first use in this function); did you mean ‘code_arena_t’?

Check failure on line 342 in Python/perf_trampoline.c

View workflow job for this annotation

GitHub Actions / Ubuntu

‘code_arena’ undeclared (first use in this function); did you mean ‘code_arena_t’?

Check failure on line 342 in Python/perf_trampoline.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1v)

‘code_arena’ undeclared (first use in this function); did you mean ‘code_arena_t’?

Check failure on line 342 in Python/perf_trampoline.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.10)

‘code_arena’ undeclared (first use in this function); did you mean ‘code_arena_t’?

Check failure on line 342 in Python/perf_trampoline.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.2)

‘code_arena’ undeclared (first use in this function); did you mean ‘code_arena_t’?

Check failure on line 342 in Python/perf_trampoline.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘code_arena’ undeclared (first use in this function); did you mean ‘code_arena_t’?
return _PyCode_SetExtra((PyObject *)co, extra_code_index,
(void *)new_trampoline);
}
return 0;
}

static PyObject *
py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame,
int throw)
Expand Down Expand Up @@ -447,17 +465,35 @@ _PyPerfTrampoline_Fini(void)
#endif
return 0;
}

int
PyUnstable_PerfTrampoline_SetPersistAfterFork(int enable){
#ifdef PY_HAVE_PERF_TRAMPOLINE
persist_after_fork = enable;
return persist_after_fork;
#endif
return 0;
}

PyStatus
_PyPerfTrampoline_AfterFork_Child(void)
{
#ifdef PY_HAVE_PERF_TRAMPOLINE
// Restart trampoline in file in child.
int was_active = _PyIsPerfTrampolineActive();
_PyPerfTrampoline_Fini();
PyUnstable_PerfMapState_Fini();
if (was_active) {
_PyPerfTrampoline_Init(1);
if (persist_after_fork){
char filename[256];
pid_t parent_pid = getppid();
snprintf(filename, sizeof(filename), "/tmp/perf-%d.map", parent_pid);
if(PyUnstable_CopyPerfMapFile(filename) != 0){
return PyStatus_Error("Failed to copy perf map file.");
}
} else {
// Restart trampoline in file in child.
int was_active = _PyIsPerfTrampolineActive();
_PyPerfTrampoline_Fini();
if (was_active) {
_PyPerfTrampoline_Init(1);
}
}
#endif
return PyStatus_Ok();
Expand Down

0 comments on commit 3098e9d

Please sign in to comment.