@@ -8,10 +8,11 @@ Python support for the Linux ``perf`` profiler
88
99:author: Pablo Galindo
1010
11- The Linux ``perf `` profiler is a very powerful tool that allows you to profile and
12- obtain information about the performance of your application. ``perf `` also has
13- a very vibrant ecosystem of tools that aid with the analysis of the data that it
14- produces.
11+ `The Linux perf profiler <https://perf.wiki.kernel.org >`_
12+ is a very powerful tool that allows you to profile and obtain
13+ information about the performance of your application.
14+ ``perf `` also has a very vibrant ecosystem of tools
15+ that aid with the analysis of the data that it produces.
1516
1617The main problem with using the ``perf `` profiler with Python applications is that
1718``perf `` only allows to get information about native symbols, this is, the names of
@@ -25,7 +26,7 @@ fly before the execution of every Python function and it will teach ``perf`` the
2526relationship between this piece of code and the associated Python function using
2627`perf map files `_.
2728
28- .. warning ::
29+ .. note ::
2930
3031 Support for the ``perf `` profiler is only currently available for Linux on
3132 selected architectures. Check the output of the configure build step or
@@ -51,11 +52,11 @@ For example, consider the following script:
5152 if __name__ == " __main__" :
5253 baz(1000000 )
5354
54- We can run perf to sample CPU stack traces at 9999 Hertz:
55+ We can run `` perf `` to sample CPU stack traces at 9999 Hertz: :
5556
5657 $ perf record -F 9999 -g -o perf.data python my_script.py
5758
58- Then we can use perf report to analyze the data:
59+ Then we can use `` perf `` report to analyze the data:
5960
6061.. code-block :: shell-session
6162
@@ -101,7 +102,7 @@ As you can see here, the Python functions are not shown in the output, only ``_P
101102functions use the same C function to evaluate bytecode so we cannot know which Python function corresponds to which
102103bytecode-evaluating function.
103104
104- Instead, if we run the same experiment with perf support activated we get:
105+ Instead, if we run the same experiment with `` perf `` support enabled we get:
105106
106107.. code-block :: shell-session
107108
@@ -147,52 +148,58 @@ Instead, if we run the same experiment with perf support activated we get:
147148
148149
149150
150- Enabling perf profiling mode
151- ----------------------------
151+ How to enable `` perf `` profiling support
152+ ----------------------------------------
152153
153- There are two main ways to activate the perf profiling mode. If you want it to be
154- active since the start of the Python interpreter, you can use the ``-Xperf `` option:
154+ ``perf `` profiling support can either be enabled from the start using
155+ the environment variable :envvar: `PYTHONPERFSUPPORT ` or the
156+ :option: `-X perf <-X> ` option,
157+ or dynamically using :func: `sys.activate_stack_trampoline ` and
158+ :func: `sys.deactivate_stack_trampoline `.
155159
156- $ python -Xperf my_script.py
160+ The :mod: `!sys ` functions take precedence over the :option: `!-X ` option,
161+ the :option: `!-X ` option takes precedence over the environment variable.
157162
158- You can also set the :envvar: `PYTHONPERFSUPPORT ` to a nonzero value to actiavate perf
159- profiling mode globally.
163+ Example, using the environment variable::
160164
161- There is also support for dynamically activating and deactivating the perf
162- profiling mode by using the APIs in the :mod: `sys ` module:
165+ $ PYTHONPERFSUPPORT=1
166+ $ python script.py
167+ $ perf report -g -i perf.data
163168
164- .. code-block :: python
165-
166- import sys
167- sys.activate_stack_trampoline(" perf" )
169+ Example, using the :option: `!-X ` option::
168170
169- # Run some code with Perf profiling active
171+ $ python -X perf script.py
172+ $ perf report -g -i perf.data
170173
171- sys.deactivate_stack_trampoline()
174+ Example, using the :mod: ` sys ` APIs in file :file: ` example.py `:
172175
173- # Perf profiling is not active anymore
176+ .. code-block :: python
174177
175- These APIs can be handy if you want to activate/deactivate profiling mode in
176- response to a signal or other communication mechanism with your process.
178+ import sys
177179
180+ sys.activate_stack_trampoline(" perf" )
181+ do_profiled_stuff()
182+ sys.deactivate_stack_trampoline()
178183
184+ non_profiled_stuff()
179185
180- Now we can analyze the data with `` perf report `` :
186+ ...then: :
181187
182- $ perf report -g -i perf.data
188+ $ python ./example.py
189+ $ perf report -g -i perf.data
183190
184191
185192How to obtain the best results
186- -------------------------------
193+ ------------------------------
187194
188195For the best results, Python should be compiled with
189196``CFLAGS="-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer" `` as this allows
190197profilers to unwind using only the frame pointer and not on DWARF debug
191- information. This is because as the code that is interposed to allow perf
198+ information. This is because as the code that is interposed to allow `` perf ``
192199support is dynamically generated it doesn't have any DWARF debugging information
193200available.
194201
195- You can check if you system has been compiled with this flag by running:
202+ You can check if your system has been compiled with this flag by running: :
196203
197204 $ python -m sysconfig | grep 'no-omit-frame-pointer'
198205
0 commit comments