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
Address review comments
  • Loading branch information
pablogsal committed Aug 23, 2022
commit d8f396d1b0fd7dc25630a75b9faefc4efe847372
1 change: 1 addition & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extern PyObject* _PyEval_BuiltinsFromGlobals(

extern int _PyPerfTrampoline_Init(int activate);
extern int _PyPerfTrampoline_Fini(void);
extern int _PyIsPerfTrampolineActive(void);
extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);

static inline PyObject*
Expand Down
32 changes: 8 additions & 24 deletions Lib/test/test_perf_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import sys
import sysconfig
import os
from test.support.script_helper import make_script
from test.support.os_helper import temp_dir
from test.support import check_sanitizer
Expand All @@ -11,20 +12,13 @@
def get_perf_version():
try:
cmd = ["perf", "--version"]
proc = subprocess.Popen(
proc = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
)
with proc:
version, stderr = proc.communicate()

if proc.returncode:
raise Exception(
f"Command {' '.join(cmd)!r} failed "
f"with exit code {proc.returncode}: "
f"stdout={version!r} stderr={stderr!r}"
)
except OSError:
except subprocess.SubprocessError:
raise unittest.SkipTest("Couldn't find perf on the path")

version = proc.stdout

match = re.search(r"^perf version\s+(.*)", version)
if match is None:
Expand All @@ -50,32 +44,22 @@ def run_perf(cwd, *args, **env_vars):
env.update(env_vars)
else:
env = None
# -nx: Do not execute commands from any .gdbinit initialization files
# (issue #22188)
output_file = cwd + "/perf_output.perf"
base_cmd = ("perf", "record", "-g", "--call-graph=fp", "-o", output_file, "--")
proc = subprocess.Popen(
proc = subprocess.run(
base_cmd + args,
# Redirect stdin to prevent GDB from messing with
# the terminal settings
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
)
with proc:
out, err = proc.communicate()
base_cmd = ("perf", "script")
proc = subprocess.Popen(
proc = subprocess.run(
("perf", "script", "-i", output_file),
# Redirect stdin to prevent GDB from messing with
# the terminal settings
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
)
with proc:
out, err = proc.communicate()
return out.decode("utf-8", "replace"), err.decode("utf-8", "replace")
return proc.stdout.decode("utf-8", "replace"), proc.stderr.decode("utf-8", "replace")


class TestPerfProfiler(unittest.TestCase):
Expand Down
30 changes: 22 additions & 8 deletions Objects/perf_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ new_code_arena(void)
assert(mem_size % sysconf(_SC_PAGESIZE) == 0);
char *memory = mmap(NULL, // address
mem_size,
PROT_READ | PROT_WRITE | PROT_EXEC,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1, // fd (not used here)
0); // offset (not used here)
Expand All @@ -53,8 +53,8 @@ new_code_arena(void)
void *end = &_Py_trampoline_func_end;
size_t code_size = end - start;

long n_copies = mem_size / code_size;
for (int i = 0; i < n_copies; i++) {
size_t n_copies = mem_size / code_size;
for (size_t i = 0; i < n_copies; i++) {
memcpy(memory + i * code_size, start, code_size * sizeof(char));
}

Expand Down Expand Up @@ -144,7 +144,8 @@ perf_map_write_entry(FILE *method_file, const void *code_addr,
unsigned int code_size, const char *entry,
const char *file)
{
fprintf(method_file, "%lx %x py::%s:%s\n", (unsigned long)code_addr,
assert(entry != NULL && file != NULL);
fprintf(method_file, "%p %x py::%s:%s\n", code_addr,
code_size, entry, file);
fflush(method_file);
}
Expand All @@ -157,17 +158,17 @@ py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame,
py_trampoline f = NULL;
_PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f);
if (f == NULL) {
FILE *pfile = perf_map_get_file();
if (pfile == NULL) {
return NULL;
}
if (extra_code_index == -1) {
extra_code_index = _PyEval_RequestCodeExtraIndex(NULL);
}
py_trampoline new_trampoline = compile_trampoline();
if (new_trampoline == NULL) {
return NULL;
}
FILE *pfile = perf_map_get_file();
if (pfile == NULL) {
return NULL;
}
perf_map_write_entry(pfile, new_trampoline, code_arena->code_size,
PyUnicode_AsUTF8(co->co_qualname),
PyUnicode_AsUTF8(co->co_filename));
Expand All @@ -178,7 +179,19 @@ py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame,
assert(f != NULL);
return f(_PyEval_EvalFrameDefault, ts, frame, throw);
}
#endif // HAVE_PERF_TRAMPOLINE

int
_PyIsPerfTrampolineActive(void)
{
#ifdef HAVE_PERF_TRAMPOLINE
PyThreadState *tstate = _PyThreadState_GET();
return tstate->interp->eval_frame == py_trampoline_evaluator;
#endif
return 0;
}



int
_PyPerfTrampoline_Init(int activate)
Expand Down Expand Up @@ -214,6 +227,7 @@ _PyPerfTrampoline_AfterFork_Child(void)
#ifdef HAVE_PERF_TRAMPOLINE
// close file in child.
perf_map_close(perf_map_file);
perf_map_file = NULL;
#endif
return PyStatus_Ok();
}
50 changes: 34 additions & 16 deletions Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,7 @@ config_read_complex_options(PyConfig *config)
return status;
}
}
if (config->tracemalloc < 0) {
if (config->perf_profiling < 0) {
status = config_init_perf_profiling(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down
37 changes: 28 additions & 9 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1995,14 +1995,14 @@ sys_getandroidapilevel_impl(PyObject *module)
#endif /* ANDROID_API_LEVEL */

/*[clinic input]
sys._activate_perf_trampoline
sys.activate_perf_trampoline

Activate the perf profiler trampoline.
[clinic start generated code]*/

static PyObject *
sys__activate_perf_trampoline_impl(PyObject *module)
/*[clinic end generated code: output=248f6dc862887fd0 input=67667f43ffabb1e4]*/
sys_activate_perf_trampoline_impl(PyObject *module)
/*[clinic end generated code: output=7f97c60d4f580b85 input=666a2d744a97a220]*/
{
if (_PyPerfTrampoline_Init(1) < 0) {
return NULL;
Expand All @@ -2012,21 +2012,39 @@ sys__activate_perf_trampoline_impl(PyObject *module)


/*[clinic input]
sys._deactivate_perf_trampoline
sys.deactivate_perf_trampoline

Activate the perf profiler trampoline.
Dectivate the perf profiler trampoline.
[clinic start generated code]*/

static PyObject *
sys__deactivate_perf_trampoline_impl(PyObject *module)
/*[clinic end generated code: output=7dde745eb7ba5e54 input=3d4fbb4aef9ad3d8]*/
sys_deactivate_perf_trampoline_impl(PyObject *module)
/*[clinic end generated code: output=5ba2f93711f85b6e input=d85cf6e3cd37d81e]*/
{
if (_PyPerfTrampoline_Init(0) < 0) {
return NULL;
}
Py_RETURN_NONE;
}

/*[clinic input]
sys.is_perf_trampoline_active

Returns *True* if the perf profiler trampoline is active.
[clinic start generated code]*/

static PyObject *
sys_is_perf_trampoline_active_impl(PyObject *module)
/*[clinic end generated code: output=7bbf80001165b590 input=59f045e52c228654]*/
{
#ifdef HAVE_PERF_TRAMPOLINE
if (_PyIsPerfTrampolineActive()) {
Py_RETURN_TRUE;
}
#endif
Py_RETURN_FALSE;
}




Expand Down Expand Up @@ -2083,8 +2101,9 @@ static PyMethodDef sys_methods[] = {
METH_VARARGS | METH_KEYWORDS, set_asyncgen_hooks_doc},
SYS_GET_ASYNCGEN_HOOKS_METHODDEF
SYS_GETANDROIDAPILEVEL_METHODDEF
SYS__ACTIVATE_PERF_TRAMPOLINE_METHODDEF
SYS__DEACTIVATE_PERF_TRAMPOLINE_METHODDEF
SYS_ACTIVATE_PERF_TRAMPOLINE_METHODDEF
SYS_IS_PERF_TRAMPOLINE_ACTIVE_METHODDEF
SYS_DEACTIVATE_PERF_TRAMPOLINE_METHODDEF
SYS_UNRAISABLEHOOK_METHODDEF
#ifdef Py_STATS
SYS__STATS_ON_METHODDEF
Expand Down