Debian Codegraph @

Lib/test/test_perf_profiler.py

raw
1 import unittest
2 import string
3 import subprocess
4 import sys
5 import sysconfig
6 import os
7 import pathlib
8 from test import support
9 from test.support.script_helper import (
10 make_script,
11 assert_python_failure,
12 assert_python_ok,
13 )
14 from test.support.os_helper import temp_dir
15
16
17 if not support.has_subprocess_support:
18 raise unittest.SkipTest("test module requires subprocess")
19
20 if support.check_sanitizer(address=True, memory=True, ub=True, function=True):
21 # gh-109580: Skip the test because it does crash randomly if Python is
22 # built with ASAN.
23 raise unittest.SkipTest("test crash randomly on ASAN/MSAN/UBSAN build")
24
25
26 def supports_trampoline_profiling():
27 perf_trampoline = sysconfig.get_config_var("PY_HAVE_PERF_TRAMPOLINE")
28 if not perf_trampoline:
29 return False
30 return int(perf_trampoline) == 1
31
32
33 if not supports_trampoline_profiling():
34 raise unittest.SkipTest("perf trampoline profiling not supported")
35
36
37 class TestPerfTrampoline(unittest.TestCase):
38 def setUp(self):
39 super().setUp()
40 self.perf_files = set(pathlib.Path("/tmp/").glob("perf-*.map"))
41
42 def tearDown(self) -> None:
43 super().tearDown()
44 files_to_delete = (
45 set(pathlib.Path("/tmp/").glob("perf-*.map")) - self.perf_files
46 )
47 for file in files_to_delete:
48 file.unlink()
49
50 @unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
51 def test_trampoline_works(self):
52 code = """if 1:
53 def foo():
54 pass
55
56 def bar():
57 foo()
58
59 def baz():
60 bar()
61
62 baz()
63 """
64 with temp_dir() as script_dir:
65 script = make_script(script_dir, "perftest", code)
66 env = {**os.environ, "PYTHON_JIT": "0"}
67 with subprocess.Popen(
68 [sys.executable, "-Xperf", script],
69 text=True,
70 stderr=subprocess.PIPE,
71 stdout=subprocess.PIPE,
72 env=env,
73 ) as process:
74 stdout, stderr = process.communicate()
75
76 self.assertEqual(stderr, "")
77 self.assertEqual(stdout, "")
78
79 perf_file = pathlib.Path(f"/tmp/perf-{process.pid}.map")
80 self.assertTrue(perf_file.exists())
81 perf_file_contents = perf_file.read_text()
82 perf_lines = perf_file_contents.splitlines()
83 expected_symbols = [
84 f"py::foo:{script}",
85 f"py::bar:{script}",
86 f"py::baz:{script}",
87 ]
88 for expected_symbol in expected_symbols:
89 perf_line = next(
90 (line for line in perf_lines if expected_symbol in line), None
91 )
92 self.assertIsNotNone(
93 perf_line, f"Could not find {expected_symbol} in perf file"
94 )
95 perf_addr = perf_line.split(" ")[0]
96 self.assertNotStartsWith(perf_addr, "0x")
97 self.assertTrue(
98 set(perf_addr).issubset(string.hexdigits),
99 "Address should contain only hex characters",
100 )
101
102 @unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
103 def test_trampoline_works_with_forks(self):
104 code = """if 1:
105 import os, sys
106
107 def foo_fork():
108 pass
109
110 def bar_fork():
111 foo_fork()
112
113 def baz_fork():
114 bar_fork()
115
116 def foo():
117 pid = os.fork()
118 if pid == 0:
119 print(os.getpid())
120 baz_fork()
121 else:
122 _, status = os.waitpid(-1, 0)
123 sys.exit(status)
124
125 def bar():
126 foo()
127
128 def baz():
129 bar()
130
131 baz()
132 """
133 with temp_dir() as script_dir:
134 script = make_script(script_dir, "perftest", code)
135 env = {**os.environ, "PYTHON_JIT": "0"}
136 with subprocess.Popen(
137 [sys.executable, "-Xperf", script],
138 text=True,
139 stderr=subprocess.PIPE,
140 stdout=subprocess.PIPE,
141 env=env,
142 ) as process:
143 stdout, stderr = process.communicate()
144
145 self.assertEqual(process.returncode, 0)
146 self.assertEqual(stderr, "")
147 child_pid = int(stdout.strip())
148 perf_file = pathlib.Path(f"/tmp/perf-{process.pid}.map")
149 perf_child_file = pathlib.Path(f"/tmp/perf-{child_pid}.map")
150 self.assertTrue(perf_file.exists())
151 self.assertTrue(perf_child_file.exists())
152
153 perf_file_contents = perf_file.read_text()
154 self.assertIn(f"py::foo:{script}", perf_file_contents)
155 self.assertIn(f"py::bar:{script}", perf_file_contents)
156 self.assertIn(f"py::baz:{script}", perf_file_contents)
157
158 child_perf_file_contents = perf_child_file.read_text()
159 self.assertIn(f"py::foo_fork:{script}", child_perf_file_contents)
160 self.assertIn(f"py::bar_fork:{script}", child_perf_file_contents)
161 self.assertIn(f"py::baz_fork:{script}", child_perf_file_contents)
162
163 # The parent's map should not contain the child's symbols.
164 self.assertNotIn(f"py::foo_fork:{script}", perf_file_contents)
165 self.assertNotIn(f"py::bar_fork:{script}", perf_file_contents)
166 self.assertNotIn(f"py::baz_fork:{script}", perf_file_contents)
167
168 # The child's map should not contain the parent's symbols.
169 self.assertNotIn(f"py::foo:{script}", child_perf_file_contents)
170 self.assertNotIn(f"py::bar:{script}", child_perf_file_contents)
171 self.assertNotIn(f"py::baz:{script}", child_perf_file_contents)
172
173 @unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
174 def test_trampoline_works_after_fork_with_many_code_objects(self):
175 code = """if 1:
176 import gc, os, sys, signal
177
178 # Create many code objects so trampoline_refcount > 1
179 for i in range(50):
180 exec(compile(f"def _dummy_{i}(): pass", f"<test{i}>", "exec"))
181
182 pid = os.fork()
183 if pid == 0:
184 # Child: create and destroy new code objects,
185 # then collect garbage. If the old code watcher
186 # survived the fork, the double-decrement of
187 # trampoline_refcount will cause a SIGSEGV.
188 for i in range(50):
189 exec(compile(f"def _child_{i}(): pass", f"<child{i}>", "exec"))
190 gc.collect()
191 os._exit(0)
192 else:
193 _, status = os.waitpid(pid, 0)
194 if os.WIFSIGNALED(status):
195 print(f"FAIL: child killed by signal {os.WTERMSIG(status)}", file=sys.stderr)
196 sys.exit(1)
197 sys.exit(os.WEXITSTATUS(status))
198 """
199 with temp_dir() as script_dir:
200 script = make_script(script_dir, "perftest", code)
201 env = {**os.environ, "PYTHON_JIT": "0"}
202 with subprocess.Popen(
203 [sys.executable, "-Xperf", script],
204 text=True,
205 stderr=subprocess.PIPE,
206 stdout=subprocess.PIPE,
207 env=env,
208 ) as process:
209 stdout, stderr = process.communicate()
210
211 self.assertEqual(process.returncode, 0, stderr)
212 self.assertEqual(stderr, "")
213
214 @unittest.skipIf(support.check_bolt_optimized(), "fails on BOLT instrumented binaries")
215 def test_sys_api(self):
216 code = """if 1:
217 import sys
218 def foo():
219 pass
220
221 def spam():
222 pass
223
224 def bar():
225 sys.deactivate_stack_trampoline()
226 foo()
227 sys.activate_stack_trampoline("perf")
228 spam()
229
230 def baz():
231 bar()
232
233 sys.activate_stack_trampoline("perf")
234 baz()
235 """
236 with temp_dir() as script_dir:
237 script = make_script(script_dir, "perftest", code)
238 env = {**os.environ, "PYTHON_JIT": "0"}
239 with subprocess.Popen(
240 [sys.executable, script],
241 text=True,
242 stderr=subprocess.PIPE,
243 stdout=subprocess.PIPE,
244 env=env,
245 ) as process:
246 stdout, stderr = process.communicate()
247
248 self.assertEqual(stderr, "")
249 self.assertEqual(stdout, "")
250
251 perf_file = pathlib.Path(f"/tmp/perf-{process.pid}.map")
252 self.assertTrue(perf_file.exists())
253 perf_file_contents = perf_file.read_text()
254 self.assertNotIn(f"py::foo:{script}", perf_file_contents)
255 self.assertIn(f"py::spam:{script}", perf_file_contents)
256 self.assertIn(f"py::bar:{script}", perf_file_contents)
257 self.assertIn(f"py::baz:{script}", perf_file_contents)
258
259 def test_sys_api_with_existing_trampoline(self):
260 code = """if 1:
261 import sys
262 sys.activate_stack_trampoline("perf")
263 sys.activate_stack_trampoline("perf")
264 """
265 assert_python_ok("-c", code, PYTHON_JIT="0")
266
267 def test_sys_api_with_invalid_trampoline(self):
268 code = """if 1:
269 import sys
270 sys.activate_stack_trampoline("invalid")
271 """
272 rc, out, err = assert_python_failure("-c", code, PYTHON_JIT="0")
273 self.assertIn("invalid backend: invalid", err.decode())
274
275 def test_sys_api_get_status(self):
276 code = """if 1:
277 import sys
278 sys.activate_stack_trampoline("perf")
279 assert sys.is_stack_trampoline_active() is True
280 sys.deactivate_stack_trampoline()
281 assert sys.is_stack_trampoline_active() is False
282 """
283 assert_python_ok("-c", code, PYTHON_JIT="0")
284
285 def test_sys_api_perf_jit_backend(self):
286 code = """if 1:
287 import sys
288 sys.activate_stack_trampoline("perf_jit")
289 assert sys.is_stack_trampoline_active() is True
290 sys.deactivate_stack_trampoline()
291 assert sys.is_stack_trampoline_active() is False
292 """
293 assert_python_ok("-c", code, PYTHON_JIT="0")
294
295 def test_sys_api_with_existing_perf_jit_trampoline(self):
296 code = """if 1:
297 import sys
298 sys.activate_stack_trampoline("perf_jit")
299 sys.activate_stack_trampoline("perf_jit")
300 """
301 assert_python_ok("-c", code, PYTHON_JIT="0")
302
303
304 def is_unwinding_reliable_with_frame_pointers():
305 cflags = sysconfig.get_config_var("PY_CORE_CFLAGS")
306 if not cflags:
307 return False
308 return "no-omit-frame-pointer" in cflags
309
310
311 def perf_command_works():
312 try:
313 cmd = ["perf", "--help"]
314 stdout = subprocess.check_output(cmd, text=True)
315 except (subprocess.SubprocessError, OSError):
316 return False
317
318 # perf version does not return a version number on Fedora. Use presence
319 # of "perf.data" in help as indicator that it's perf from Linux tools.
320 if "perf.data" not in stdout:
321 return False
322
323 # Check that we can run a simple perf run
324 with temp_dir() as script_dir:
325 try:
326 output_file = script_dir + "/perf_output.perf"
327 cmd = (
328 "perf",
329 "record",
330 "--no-buildid",
331 "--no-buildid-cache",
332 "-g",
333 "--call-graph=fp",
334 "-o",
335 output_file,
336 "--",
337 sys.executable,
338 "-c",
339 'print("hello")',
340 )
341 env = {**os.environ, "PYTHON_JIT": "0"}
342 stdout = subprocess.check_output(
343 cmd, cwd=script_dir, text=True, stderr=subprocess.STDOUT, env=env
344 )
345 except (subprocess.SubprocessError, OSError):
346 return False
347
348 if "hello" not in stdout:
349 return False
350
351 return True
352
353
354 def run_perf(cwd, *args, use_jit=False, **env_vars):
355 env = os.environ.copy()
356 if env_vars:
357 env.update(env_vars)
358 env["PYTHON_JIT"] = "0"
359 output_file = cwd + "/perf_output.perf"
360 if not use_jit:
361 base_cmd = (
362 "perf",
363 "record",
364 "--no-buildid",
365 "--no-buildid-cache",
366 "-g",
367 "--call-graph=fp",
368 "-o", output_file,
369 "--"
370 )
371 else:
372 base_cmd = (
373 "perf",
374 "record",
375 "--no-buildid",
376 "--no-buildid-cache",
377 "-g",
378 "--call-graph=dwarf,65528",
379 "-F99",
380 "-k1",
381 "-o",
382 output_file,
383 "--",
384 )
385 proc = subprocess.run(
386 base_cmd + args,
387 stdout=subprocess.PIPE,
388 stderr=subprocess.PIPE,
389 env=env,
390 text=True,
391 )
392 if proc.returncode:
393 print(proc.stderr, file=sys.stderr)
394 raise ValueError(f"Perf failed with return code {proc.returncode}")
395
396 if use_jit:
397 jit_output_file = cwd + "/jit_output.dump"
398 command = ("perf", "inject", "-j", "-i", output_file, "-o", jit_output_file)
399 proc = subprocess.run(
400 command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, env=env, text=True
401 )
402 if proc.returncode:
403 print(proc.stderr, file=sys.stderr)
404 raise ValueError(f"Perf failed with return code {proc.returncode}")
405 # Copy the jit_output_file to the output_file
406 os.rename(jit_output_file, output_file)
407
408 base_cmd = ("perf", "script")
409 proc = subprocess.run(
410 ("perf", "script", "-i", output_file),
411 stdout=subprocess.PIPE,
412 stderr=subprocess.PIPE,
413 env=env,
414 check=True,
415 text=True,
416 )
417 return proc.stdout, proc.stderr
418
419
420 class TestPerfProfilerMixin:
421 def run_perf(self, script_dir, perf_mode, script):
422 raise NotImplementedError()
423
424 def test_python_calls_appear_in_the_stack_if_perf_activated(self):
425 with temp_dir() as script_dir:
426 code = """if 1:
427 def foo(n):
428 x = 0
429 for i in range(n):
430 x += i
431
432 def bar(n):
433 foo(n)
434
435 def baz(n):
436 bar(n)
437
438 baz(10000000)
439 """
440 script = make_script(script_dir, "perftest", code)
441 stdout, stderr = self.run_perf(script_dir, script)
442 self.assertEqual(stderr, "")
443
444 self.assertIn(f"py::foo:{script}", stdout)
445 self.assertIn(f"py::bar:{script}", stdout)
446 self.assertIn(f"py::baz:{script}", stdout)
447
448 def test_python_calls_do_not_appear_in_the_stack_if_perf_deactivated(self):
449 with temp_dir() as script_dir:
450 code = """if 1:
451 def foo(n):
452 x = 0
453 for i in range(n):
454 x += i
455
456 def bar(n):
457 foo(n)
458
459 def baz(n):
460 bar(n)
461
462 baz(10000000)
463 """
464 script = make_script(script_dir, "perftest", code)
465 stdout, stderr = self.run_perf(
466 script_dir, script, activate_trampoline=False
467 )
468 self.assertEqual(stderr, "")
469
470 self.assertNotIn(f"py::foo:{script}", stdout)
471 self.assertNotIn(f"py::bar:{script}", stdout)
472 self.assertNotIn(f"py::baz:{script}", stdout)
473
474
475 @unittest.skipUnless(perf_command_works(), "perf command doesn't work")
476 @unittest.skipUnless(
477 is_unwinding_reliable_with_frame_pointers(),
478 "Unwinding is unreliable with frame pointers",
479 )
480 class TestPerfProfiler(unittest.TestCase, TestPerfProfilerMixin):
481 def run_perf(self, script_dir, script, activate_trampoline=True):
482 if activate_trampoline:
483 return run_perf(script_dir, sys.executable, "-Xperf", script)
484 return run_perf(script_dir, sys.executable, script)
485
486 def setUp(self):
487 super().setUp()
488 self.perf_files = set(pathlib.Path("/tmp/").glob("perf-*.map"))
489
490 def tearDown(self) -> None:
491 super().tearDown()
492 files_to_delete = (
493 set(pathlib.Path("/tmp/").glob("perf-*.map")) - self.perf_files
494 )
495 for file in files_to_delete:
496 file.unlink()
497
498 def test_pre_fork_compile(self):
499 code = """if 1:
500 import sys
501 import os
502 import sysconfig
503 from _testinternalcapi import (
504 compile_perf_trampoline_entry,
505 perf_trampoline_set_persist_after_fork,
506 )
507
508 def foo_fork():
509 pass
510
511 def bar_fork():
512 foo_fork()
513
514 def foo():
515 import time; time.sleep(1)
516
517 def bar():
518 foo()
519
520 def compile_trampolines_for_all_functions():
521 perf_trampoline_set_persist_after_fork(1)
522 for _, obj in globals().items():
523 if callable(obj) and hasattr(obj, '__code__'):
524 compile_perf_trampoline_entry(obj.__code__)
525
526 if __name__ == "__main__":
527 compile_trampolines_for_all_functions()
528 pid = os.fork()
529 if pid == 0:
530 print(os.getpid())
531 bar_fork()
532 else:
533 bar()
534 """
535
536 with temp_dir() as script_dir:
537 script = make_script(script_dir, "perftest", code)
538 env = {**os.environ, "PYTHON_JIT": "0"}
539 with subprocess.Popen(
540 [sys.executable, "-Xperf", script],
541 universal_newlines=True,
542 stderr=subprocess.PIPE,
543 stdout=subprocess.PIPE,
544 env=env,
545 ) as process:
546 stdout, stderr = process.communicate()
547
548 self.assertEqual(process.returncode, 0)
549 self.assertNotIn("Error:", stderr)
550 child_pid = int(stdout.strip())
551 perf_file = pathlib.Path(f"/tmp/perf-{process.pid}.map")
552 perf_child_file = pathlib.Path(f"/tmp/perf-{child_pid}.map")
553 self.assertTrue(perf_file.exists())
554 self.assertTrue(perf_child_file.exists())
555
556 perf_file_contents = perf_file.read_text()
557 self.assertIn(f"py::foo:{script}", perf_file_contents)
558 self.assertIn(f"py::bar:{script}", perf_file_contents)
559 self.assertIn(f"py::foo_fork:{script}", perf_file_contents)
560 self.assertIn(f"py::bar_fork:{script}", perf_file_contents)
561
562 child_perf_file_contents = perf_child_file.read_text()
563 self.assertIn(f"py::foo_fork:{script}", child_perf_file_contents)
564 self.assertIn(f"py::bar_fork:{script}", child_perf_file_contents)
565
566 # Pre-compiled perf-map entries of a forked process must be
567 # identical in both the parent and child perf-map files.
568 perf_file_lines = perf_file_contents.split("\n")
569 for line in perf_file_lines:
570 if f"py::foo_fork:{script}" in line or f"py::bar_fork:{script}" in line:
571 self.assertIn(line, child_perf_file_contents)
572
573
574 def _is_perf_version_at_least(major, minor):
575 # The output of perf --version looks like "perf version 6.7-3" but
576 # it can also be perf version "perf version 5.15.143", or even include
577 # a commit hash in the version string, like "6.12.9.g242e6068fd5c"
578 #
579 # PermissionError is raised if perf does not exist on the Windows Subsystem
580 # for Linux, see #134987
581 try:
582 output = subprocess.check_output(["perf", "--version"], text=True)
583 except (subprocess.CalledProcessError, FileNotFoundError, PermissionError):
584 return False
585 version = output.split()[2]
586 version = version.split("-")[0]
587 version = version.split(".")
588 version = tuple(map(int, version[:2]))
589 return version >= (major, minor)
590
591
592 @unittest.skipUnless(perf_command_works(), "perf command doesn't work")
593 @unittest.skipUnless(
594 _is_perf_version_at_least(6, 6), "perf command may not work due to a perf bug"
595 )
596 class TestPerfProfilerWithDwarf(unittest.TestCase, TestPerfProfilerMixin):
597 def run_perf(self, script_dir, script, activate_trampoline=True):
598 if activate_trampoline:
599 return run_perf(
600 script_dir, sys.executable, "-Xperf_jit", script, use_jit=True
601 )
602 return run_perf(script_dir, sys.executable, script, use_jit=True)
603
604 def setUp(self):
605 super().setUp()
606 self.perf_files = set(pathlib.Path("/tmp/").glob("jit*.dump"))
607 self.perf_files |= set(pathlib.Path("/tmp/").glob("jitted-*.so"))
608
609 def tearDown(self) -> None:
610 super().tearDown()
611 files_to_delete = set(pathlib.Path("/tmp/").glob("jit*.dump"))
612 files_to_delete |= set(pathlib.Path("/tmp/").glob("jitted-*.so"))
613 files_to_delete = files_to_delete - self.perf_files
614 for file in files_to_delete:
615 file.unlink()
616
617
618 if __name__ == "__main__":
619 unittest.main()