-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-96143: Allow Linux perf profiler to see Python calls #96123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 46 commits
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 cea1420
Add test
pablogsal 4107c53
Update PCbuild/_freeze_module.vcxproj.filters
pablogsal 5e34e66
munmap pages on shutdown, keep FILE open
tiran a26a850
Fix tests
pablogsal 8170b24
Skip tests if sanitizer is active
pablogsal 9df1c93
Add ARM64 code generated by aarch64-linux-gnu-gcc
tiran d8f396d
Address review comments
pablogsal d35c5d7
Secure fopen, use unraisable, continue on error
tiran 2664b12
cleanup resources, set to uninit
tiran e6c365a
Allow to set custom callbacks
pablogsal 5513fb1
Add comment to asm file
pablogsal 76c7dc0
fixup! Merge pull request #36 from tiran/perf-file
pablogsal a545b3c
Add comments to the perf_trampoline file and format file
pablogsal 5130c8d
Correct News entry
pablogsal 991366b
Update Lib/test/test_perf_profiler.py
pablogsal 0a0e53d
Rename perf macro
pablogsal 7ea3371
Fix some typos
pablogsal 680db66
Improve perf profiler tests
tiran 1263a29
Add guard for initialization
pablogsal a42bde5
Add acks
pablogsal b780d2a
Initialize perf file lazily
pablogsal 04bf416
Address review comments
pablogsal 7558df2
Complain if there is already a evaluator frame when deactivating/acti…
pablogsal d1ebc88
Fix some errors on CI
pablogsal a83a31b
Reorder arguments to speed up trampoline
tiran 0febd84
Preserve frame pointer
pablogsal dc5a6a5
Support perf backend and better handle forks
pablogsal be72b92
Fix more fork problems
pablogsal b5739f4
Update Lib/test/test_perf_profiler.py
pablogsal 04c0c14
Handle missing backends
pablogsal e810ce6
Update Lib/test/test_perf_profiler.py
pablogsal bc8bf4e
clean up perf files
pablogsal 0252845
Update Misc/NEWS.d/next/Core and Builtins/2022-08-20-18-36-40.gh-issu…
pablogsal 264bed7
Test fork support, fix some fork problems and improve test file
pablogsal a31a498
Add more tests
pablogsal f591e8d
Update Objects/perf_trampoline.c
pablogsal 0af2a08
make argument mandatory
pablogsal 861ae09
Use struct for perf callbacks
tiran 3058cf0
Rename macro to PY_HAVE_PERF_TRAMPOLINE
tiran 07ee991
Merge pull request #39 from tiran/perf_callback_struct
pablogsal be612a9
Allow gdb to unwind
pablogsal f4e3fff
Merge remote-tracking branch 'upstream/main' into perf
pablogsal c27f8b1
Add docs
pablogsal e27a2c4
fixup! Add docs
pablogsal 81c7f4b
fixup! fixup! Add docs
pablogsal ef0650b
Update sys API names in the NEWS entry.
gpshead d8932d2
Add environment variable
pablogsal 0f303ff
Merge branch 'main' into perf
pablogsal e3f846e
Document the env var and the -X option
pablogsal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
.. highlight:: shell-session | ||
|
||
.. _perf_profiling: | ||
|
||
============================================== | ||
Python support for the Linux ``perf`` profiler | ||
============================================== | ||
|
||
:author: Pablo Galindo | ||
|
||
The Linux ``perf`` profiler is a very powerful tool that allows you to profile and | ||
obtain information about the performance of your application. ``perf`` also has | ||
a very vibrant ecosystem of tools that aid with the analysis of the data that it | ||
produces. | ||
|
||
The main problem with using the ``perf`` profiler with Python applications is that | ||
``perf`` only allows to get information about native symbols, this is, the names of | ||
the functions and procedures written in C. This means that the names and file names | ||
of the Python functions in your code will not appear in the output of the ``perf``. | ||
|
||
Since Python 3.12, the interpreter can run in a special mode that allows Python | ||
functions to appear in the output of the ``perf`` profiler. When this mode is | ||
enabled, the interpreter will interpose a small piece of code compiled on the | ||
fly before the execution of every Python function and it will teach ``perf`` the | ||
relationship between this piece of code and the associated Python function using | ||
`perf map files`_. | ||
|
||
.. warning:: | ||
|
||
Support for the ``perf`` profiler is only currently available for Linux on | ||
selected architectures. Check the output of the configure build step or | ||
check the output of ``python -m sysconfig | grep HAVE_PERF_TRAMPOLINE`` | ||
to see if your system is supported. | ||
|
||
For example, consider the following script: | ||
|
||
.. code-block:: python | ||
|
||
def foo(n): | ||
result = 0 | ||
for _ in range(n): | ||
result += 1 | ||
return result | ||
|
||
def bar(n): | ||
foo(n) | ||
|
||
def baz(n): | ||
bar(n) | ||
|
||
if __name__ == "__main__": | ||
baz(1000000) | ||
|
||
We can run perf to sample CPU stack traces at 9999 Hertz: | ||
|
||
$ perf record -F 9999 -g -o perf.data python my_script.py | ||
|
||
Then we can use perf report to analyze the data: | ||
|
||
.. code-block:: shell-session | ||
|
||
$ perf report --stdio -n -g | ||
|
||
# Children Self Samples Command Shared Object Symbol | ||
# ........ ........ ............ .......... .................. .......................................... | ||
# | ||
91.08% 0.00% 0 python.exe python.exe [.] _start | ||
| | ||
---_start | ||
| | ||
--90.71%--__libc_start_main | ||
Py_BytesMain | ||
| | ||
|--56.88%--pymain_run_python.constprop.0 | ||
| | | ||
| |--56.13%--_PyRun_AnyFileObject | ||
| | _PyRun_SimpleFileObject | ||
| | | | ||
| | |--55.02%--run_mod | ||
| | | | | ||
| | | --54.65%--PyEval_EvalCode | ||
| | | _PyEval_EvalFrameDefault | ||
| | | PyObject_Vectorcall | ||
| | | _PyEval_Vector | ||
| | | _PyEval_EvalFrameDefault | ||
| | | PyObject_Vectorcall | ||
| | | _PyEval_Vector | ||
| | | _PyEval_EvalFrameDefault | ||
| | | PyObject_Vectorcall | ||
| | | _PyEval_Vector | ||
| | | | | ||
| | | |--51.67%--_PyEval_EvalFrameDefault | ||
| | | | | | ||
| | | | |--11.52%--_PyLong_Add | ||
| | | | | | | ||
| | | | | |--2.97%--_PyObject_Malloc | ||
... | ||
|
||
As you can see here, the Python functions are not shown in the output, only ``_Py_Eval_EvalFrameDefault`` appears | ||
(the function that evaluates the Python bytecode) shows up. Unfortunately that's not very useful because all Python | ||
functions use the same C function to evaluate bytecode so we cannot know which Python function corresponds to which | ||
bytecode-evaluating function. | ||
|
||
Instead, if we run the same experiment with perf support activated we get: | ||
|
||
.. code-block:: shell-session | ||
|
||
$ perf report --stdio -n -g | ||
|
||
# Children Self Samples Command Shared Object Symbol | ||
# ........ ........ ............ .......... .................. ..................................................................... | ||
# | ||
90.58% 0.36% 1 python.exe python.exe [.] _start | ||
| | ||
---_start | ||
| | ||
--89.86%--__libc_start_main | ||
Py_BytesMain | ||
| | ||
|--55.43%--pymain_run_python.constprop.0 | ||
| | | ||
| |--54.71%--_PyRun_AnyFileObject | ||
| | _PyRun_SimpleFileObject | ||
| | | | ||
| | |--53.62%--run_mod | ||
| | | | | ||
| | | --53.26%--PyEval_EvalCode | ||
| | | py::<module>:/src/script.py | ||
| | | _PyEval_EvalFrameDefault | ||
| | | PyObject_Vectorcall | ||
| | | _PyEval_Vector | ||
| | | py::baz:/src/script.py | ||
| | | _PyEval_EvalFrameDefault | ||
| | | PyObject_Vectorcall | ||
| | | _PyEval_Vector | ||
| | | py::bar:/src/script.py | ||
| | | _PyEval_EvalFrameDefault | ||
| | | PyObject_Vectorcall | ||
| | | _PyEval_Vector | ||
| | | py::foo:/src/script.py | ||
| | | | | ||
| | | |--51.81%--_PyEval_EvalFrameDefault | ||
| | | | | | ||
| | | | |--13.77%--_PyLong_Add | ||
| | | | | | | ||
| | | | | |--3.26%--_PyObject_Malloc | ||
|
||
|
||
|
||
Enabling perf profiling mode | ||
---------------------------- | ||
|
||
There are two main ways to activate the perf profiling mode. If you want it to be | ||
active since the start of the Python interpreter, you can use the `-Xperf` option: | ||
|
||
$ python -Xperf my_script.py | ||
|
||
There is also support for dynamically activating and deactivating the perf | ||
profiling mode by using the APIs in the :mod:`sys` module: | ||
|
||
.. code-block:: python | ||
|
||
import sys | ||
sys.activate_stack_trampoline("perf") | ||
|
||
# Run some code with Perf profiling active | ||
|
||
sys.deactivate_stack_trampoline() | ||
|
||
# Perf profiling is not active anymore | ||
|
||
These APIs can be handy if you want to activate/deactivate profiling mode in | ||
response to a signal or other communication mechanism with your process. | ||
|
||
|
||
|
||
pablogsal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Now we can analyze the data with ``perf report``: | ||
|
||
$ perf report -g -i perf.data | ||
|
||
|
||
How to obtain the best results | ||
------------------------------- | ||
|
||
For the best results, Python should be compiled with | ||
``CFLAGS="-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer"`` as this allows | ||
profilers to unwind using only the frame pointer and not on DWARF debug | ||
information. This is because as the code that is interposed to allow perf | ||
support is dynamically generated it doesn't have any DWARF debugging information | ||
available. | ||
|
||
You can check if you system has been compiled with this flag by running: | ||
|
||
$ python -m sysconfig | grep 'no-omit-frame-pointer' | ||
|
||
If you don't see any output it means that your interpreter has not been compiled with | ||
frame pointers and therefore it may not be able to show Python functions in the output | ||
of ``perf``. | ||
|
||
.. _perf map files: https://github.com/torvalds/linux/blob/0513e464f9007b70b96740271a948ca5ab6e7dd7/tools/perf/Documentation/jit-interface.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.