@@ -8,10 +8,11 @@ Python support for the Linux ``perf`` profiler
8
8
9
9
:author: Pablo Galindo
10
10
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.
15
16
16
17
The main problem with using the ``perf `` profiler with Python applications is that
17
18
``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
25
26
relationship between this piece of code and the associated Python function using
26
27
`perf map files `_.
27
28
28
- .. warning ::
29
+ .. note ::
29
30
30
31
Support for the ``perf `` profiler is only currently available for Linux on
31
32
selected architectures. Check the output of the configure build step or
@@ -51,11 +52,11 @@ For example, consider the following script:
51
52
if __name__ == " __main__" :
52
53
baz(1000000 )
53
54
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: :
55
56
56
57
$ perf record -F 9999 -g -o perf.data python my_script.py
57
58
58
- Then we can use perf report to analyze the data:
59
+ Then we can use `` perf `` report to analyze the data:
59
60
60
61
.. code-block :: shell-session
61
62
@@ -101,7 +102,7 @@ As you can see here, the Python functions are not shown in the output, only ``_P
101
102
functions use the same C function to evaluate bytecode so we cannot know which Python function corresponds to which
102
103
bytecode-evaluating function.
103
104
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:
105
106
106
107
.. code-block :: shell-session
107
108
@@ -147,52 +148,58 @@ Instead, if we run the same experiment with perf support activated we get:
147
148
148
149
149
150
150
- Enabling perf profiling mode
151
- ----------------------------
151
+ How to enable `` perf `` profiling support
152
+ ----------------------------------------
152
153
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 `.
155
159
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.
157
162
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::
160
164
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
163
168
164
- .. code-block :: python
165
-
166
- import sys
167
- sys.activate_stack_trampoline(" perf" )
169
+ Example, using the :option: `!-X ` option::
168
170
169
- # Run some code with Perf profiling active
171
+ $ python -X perf script.py
172
+ $ perf report -g -i perf.data
170
173
171
- sys.deactivate_stack_trampoline()
174
+ Example, using the :mod: ` sys ` APIs in file :file: ` example.py `:
172
175
173
- # Perf profiling is not active anymore
176
+ .. code-block :: python
174
177
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
177
179
180
+ sys.activate_stack_trampoline(" perf" )
181
+ do_profiled_stuff()
182
+ sys.deactivate_stack_trampoline()
178
183
184
+ non_profiled_stuff()
179
185
180
- Now we can analyze the data with `` perf report `` :
186
+ ...then: :
181
187
182
- $ perf report -g -i perf.data
188
+ $ python ./example.py
189
+ $ perf report -g -i perf.data
183
190
184
191
185
192
How to obtain the best results
186
- -------------------------------
193
+ ------------------------------
187
194
188
195
For the best results, Python should be compiled with
189
196
``CFLAGS="-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer" `` as this allows
190
197
profilers 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 ``
192
199
support is dynamically generated it doesn't have any DWARF debugging information
193
200
available.
194
201
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: :
196
203
197
204
$ python -m sysconfig | grep 'no-omit-frame-pointer'
198
205
0 commit comments