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
Test fork support, fix some fork problems and improve test file
  • Loading branch information
pablogsal committed Aug 23, 2022
commit 264bed72d5582880731fb7c69ca37c00f0b49985
190 changes: 149 additions & 41 deletions Lib/test/test_perf_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,134 @@
raise unittest.SkipTest("test module requires subprocess")


def supports_trampoline_profiling():
perf_trampoline = sysconfig.get_config_var("PERF_TRAMPOLINE_SUPPORT")
if not perf_trampoline:
return False
return int(perf_trampoline) == 1


if not supports_trampoline_profiling():
raise unittest.SkipTest("perf trampoline profiling not supported")


class TestPerfTrampoline(unittest.TestCase):
def setUp(self):
super().setUp()
self.perf_files = set(pathlib.Path("/tmp/").glob("perf-*.map"))

def tearDown(self) -> None:
super().tearDown()
files_to_delete = (
set(pathlib.Path("/tmp/").glob("perf-*.map")) - self.perf_files
)
for file in files_to_delete:
file.unlink()

def test_trampoline_works(self):
code = """if 1:
def foo():
pass

def bar():
foo()

def baz():
bar()

baz()
"""
with subprocess.Popen(
[sys.executable, "-Xperf", "-c", code],
universal_newlines=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
) as process:
stdout, stderr = process.communicate()

self.assertEqual(process.returncode, 0)
self.assertEqual(stderr, "")
self.assertEqual(stdout, "")
perf_file = pathlib.Path(f"/tmp/perf-{process.pid}.map")
self.assertTrue(perf_file.exists())

def test_trampoline_works_with_forks(self):
code = """if 1:
import os, sys

def foo_fork():
pass

def bar_fork():
foo_fork()

def baz_fork():
bar_fork()

def foo():
pid = os.fork()
if pid == 0:
print(os.getpid())
baz_fork()
else:
_, status = os.waitpid(-1, 0)
sys.exit(status)

def bar():
foo()

def baz():
bar()

baz()
"""
with temp_dir() as script_dir:
script = make_script(script_dir, "perftest", code)
with subprocess.Popen(
[sys.executable, "-Xperf", script],
universal_newlines=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
) as process:
stdout, stderr = process.communicate()

self.assertEqual(process.returncode, 0)
self.assertEqual(stderr, "")
child_pid = int(stdout.strip())
perf_file = pathlib.Path(f"/tmp/perf-{process.pid}.map")
perf_child_file = pathlib.Path(f"/tmp/perf-{child_pid}.map")
self.assertTrue(perf_file.exists())
self.assertTrue(perf_child_file.exists())

perf_file_contents = perf_file.read_text()
self.assertIn(f"py::foo:{script}", perf_file_contents)
self.assertIn(f"py::bar:{script}", perf_file_contents)
self.assertIn(f"py::baz:{script}", perf_file_contents)

child_perf_file_contents = perf_child_file.read_text()
self.assertIn(f"py::foo_fork:{script}", child_perf_file_contents)
self.assertIn(f"py::bar_fork:{script}", child_perf_file_contents)
self.assertIn(f"py::baz_fork:{script}", child_perf_file_contents)


def is_unwinding_reliable():
cflags = sysconfig.get_config_var("PY_CORE_CFLAGS")
if not cflags:
return False
return "no-omit-frame-pointer" in cflags


if not is_unwinding_reliable():
raise unittest.SkipTest("Unwinding without frame pointer is unreliable")

if support.check_sanitizer(address=True, memory=True, ub=True):
raise unittest.SkipTest("Perf unwinding doesn't work with sanitizers")


def check_perf_command():
def perf_command_works():
try:
cmd = ["perf", "--help"]
stdout = subprocess.check_output(cmd, universal_newlines=True)
except (subprocess.SubprocessError, OSError):
raise unittest.SkipTest("Couldn't find perf on the path")
return False

# perf version does not return a version number on Fedora. Use presence
# of "perf.data" in help as indicator that it's perf from Linux tools.
if "perf.data" not in stdout:
raise unittest.SkipTest("perf command does not look like Linux tool perf")
return False

# Check that we can run a simple perf run
with temp_dir() as script_dir:
Expand All @@ -59,13 +162,12 @@ def check_perf_command():
cmd, cwd=script_dir, universal_newlines=True, stderr=subprocess.STDOUT
)
except (subprocess.SubprocessError, OSError):
raise unittest.SkipTest("Couldn't run perf on simple script")
return False

if "hello" not in stdout:
raise unittest.SkipTest("perf run did not work correctly")

return False

check_perf_command()
return True


def run_perf(cwd, *args, **env_vars):
Expand All @@ -76,39 +178,45 @@ def run_perf(cwd, *args, **env_vars):
env = None
output_file = cwd + "/perf_output.perf"
base_cmd = ("perf", "record", "-g", "--call-graph=fp", "-o", output_file, "--")
prev_perf_files = set(pathlib.Path("/tmp/").glob("perf-*.map"))
try:
proc = subprocess.run(
base_cmd + args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
)
if proc.returncode:
print(proc.stderr)
raise ValueError(f"Perf failed with return code {proc.returncode}")
proc = subprocess.run(
base_cmd + args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
)
if proc.returncode:
print(proc.stderr)
raise ValueError(f"Perf failed with return code {proc.returncode}")

base_cmd = ("perf", "script")
proc = subprocess.run(
("perf", "script", "-i", output_file),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
check=True,
)
return proc.stdout.decode("utf-8", "replace"), proc.stderr.decode(
"utf-8", "replace"
)
finally:
# Clean up the perf map file at the end
base_cmd = ("perf", "script")
proc = subprocess.run(
("perf", "script", "-i", output_file),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
check=True,
)
return proc.stdout.decode("utf-8", "replace"), proc.stderr.decode(
"utf-8", "replace"
)


@unittest.skipUnless(perf_command_works(), "perf command doesn't work")
@unittest.skipUnless(is_unwinding_reliable(), "Unwinding is unreliable")
@support.skip_if_sanitizer(address=True, memory=True, ub=True)
class TestPerfProfiler(unittest.TestCase):
def setUp(self):
super().setUp()
self.perf_files = set(pathlib.Path("/tmp/").glob("perf-*.map"))

def tearDown(self) -> None:
super().tearDown()
files_to_delete = (
set(pathlib.Path("/tmp/").glob("perf-*.map")) - prev_perf_files
set(pathlib.Path("/tmp/").glob("perf-*.map")) - self.perf_files
)
for file in files_to_delete:
file.unlink()


class TestPerfProfiler(unittest.TestCase):
def test_python_calls_appear_in_the_stack_if_perf_activated(self):
with temp_dir() as script_dir:
code = """if 1:
Expand Down
1 change: 1 addition & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ DTRACE= @DTRACE@
DFLAGS= @DFLAGS@
DTRACE_HEADERS= @DTRACE_HEADERS@
DTRACE_OBJS= @DTRACE_OBJS@
PERF_TRAMPOLINE_SUPPORT= @PERF_TRAMPOLINE_SUPPORT@

GNULD= @GNULD@

Expand Down
17 changes: 10 additions & 7 deletions Objects/perf_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,11 @@ py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame,
}
PyCodeObject *co = frame->f_code;
py_trampoline f = NULL;
int ret = -1;
if (extra_code_index != -1) {
ret = _PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f);
}
assert(extra_code_index != -1);
int ret = _PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f);
if (ret != 0 || f == NULL) {
// This is the first time we see this code object so we need
// to compile a trampoline for it.
if (extra_code_index == -1) {
extra_code_index = _PyEval_RequestCodeExtraIndex(NULL);
}
py_trampoline new_trampoline = compile_trampoline();
if (new_trampoline == NULL) {
goto default_eval;
Expand Down Expand Up @@ -445,6 +440,10 @@ _PyPerfTrampoline_Init(int activate)
}
trampoline_api.state = state;
}
extra_code_index = _PyEval_RequestCodeExtraIndex(NULL);
if (extra_code_index == -1) {
return -1;
}
perf_status = PERF_STATUS_OK;
}
#endif
Expand All @@ -455,6 +454,10 @@ int
_PyPerfTrampoline_Fini(void)
{
#ifdef _PY_HAVE_PERF_TRAMPOLINE
PyThreadState *tstate = _PyThreadState_GET();
if (tstate->interp->eval_frame == py_trampoline_evaluator) {
tstate->interp->eval_frame = NULL;
}
free_code_arenas();
if (trampoline_api.state != NULL) {
trampoline_api.free_state(trampoline_api.state);
Expand Down
4 changes: 4 additions & 0 deletions configure

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

3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3435,9 +3435,12 @@ AS_CASE([$PLATFORM_TRIPLET],
)
AC_MSG_RESULT([$perf_trampoline])

AC_SUBST(PERF_TRAMPOLINE_SUPPORT)
PERF_TRAMPOLINE_SUPPORT=
AS_VAR_IF([perf_trampoline], [yes], [
AC_DEFINE([_PY_HAVE_PERF_TRAMPOLINE], [1], [Define to 1 if you have the perf trampoline.])
PERF_TRAMPOLINE_OBJ=Objects/asm_trampoline.o
PERF_TRAMPOLINE_SUPPORT=1

dnl perf needs frame pointers for unwinding, include compiler option in debug builds
AS_VAR_IF([Py_DEBUG], [true], [
Expand Down