|  | 
| 2 | 2 | 
 | 
| 3 | 3 | .. _perf_profiling: | 
| 4 | 4 | 
 | 
| 5 |  | -============================================== | 
| 6 |  | -Python support for the Linux ``perf`` profiler | 
| 7 |  | -============================================== | 
|  | 5 | +======================================================== | 
|  | 6 | +Python support for the ``perf map`` compatible profilers | 
|  | 7 | +======================================================== | 
| 8 | 8 | 
 | 
| 9 | 9 | :author: Pablo Galindo | 
| 10 | 10 | 
 | 
| 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. | 
|  | 11 | +`The Linux perf profiler <https://perf.wiki.kernel.org>`_ and | 
|  | 12 | +`samply <https://github.com/mstange/samply>`_ are powerful tools that allow you to | 
|  | 13 | +profile and obtain information about the performance of your application. | 
|  | 14 | +Both tools have vibrant ecosystems that aid with the analysis of the data they produce. | 
| 16 | 15 | 
 | 
| 17 |  | -The main problem with using the ``perf`` profiler with Python applications is that | 
| 18 |  | -``perf`` only gets information about native symbols, that is, the names of | 
|  | 16 | +The main problem with using these profilers with Python applications is that | 
|  | 17 | +they only get information about native symbols, that is, the names of | 
| 19 | 18 | functions and procedures written in C. This means that the names and file names | 
| 20 |  | -of Python functions in your code will not appear in the output of ``perf``. | 
|  | 19 | +of Python functions in your code will not appear in the profiler output. | 
| 21 | 20 | 
 | 
| 22 | 21 | Since Python 3.12, the interpreter can run in a special mode that allows Python | 
| 23 |  | -functions to appear in the output of the ``perf`` profiler. When this mode is | 
|  | 22 | +functions to appear in the output of compatible profilers. When this mode is | 
| 24 | 23 | enabled, the interpreter will interpose a small piece of code compiled on the | 
| 25 |  | -fly before the execution of every Python function and it will teach ``perf`` the | 
|  | 24 | +fly before the execution of every Python function and it will teach the profiler the | 
| 26 | 25 | relationship between this piece of code and the associated Python function using | 
| 27 | 26 | :doc:`perf map files <../c-api/perfmaps>`. | 
| 28 | 27 | 
 | 
| 29 | 28 | .. note:: | 
| 30 | 29 | 
 | 
| 31 |  | -    Support for the ``perf`` profiler is currently only available for Linux on | 
| 32 |  | -    select architectures. Check the output of the ``configure`` build step or | 
|  | 30 | +    Support for profiling is available on Linux and macOS on select architectures. | 
|  | 31 | +    ``perf`` is available on Linux, while ``samply`` can be used on both Linux and macOS. | 
|  | 32 | +    ``samply`` support on macOS is available starting from Python 3.14. | 
|  | 33 | +    Check the output of the ``configure`` build step or | 
| 33 | 34 |     check the output of ``python -m sysconfig | grep HAVE_PERF_TRAMPOLINE`` | 
| 34 | 35 |     to see if your system is supported. | 
| 35 | 36 | 
 | 
| @@ -148,6 +149,26 @@ Instead, if we run the same experiment with ``perf`` support enabled we get: | 
| 148 | 149 | 
 | 
| 149 | 150 | 
 | 
| 150 | 151 | 
 | 
|  | 152 | +Using ``samply`` profiler | 
|  | 153 | +------------------------- | 
|  | 154 | + | 
|  | 155 | +``samply`` is a modern profiler that can be used as an alternative to ``perf``. | 
|  | 156 | +It uses the same perf map files that Python generates, making it compatible | 
|  | 157 | +with Python's profiling support. ``samply`` is particularly useful on macOS | 
|  | 158 | +where ``perf`` is not available. | 
|  | 159 | + | 
|  | 160 | +To use ``samply`` with Python, first install it following the instructions at | 
|  | 161 | +https://github.com/mstange/samply, then run:: | 
|  | 162 | + | 
|  | 163 | +    $ samply record PYTHONPERFSUPPORT=1 python my_script.py | 
|  | 164 | + | 
|  | 165 | +This will open a web interface where you can analyze the profiling data | 
|  | 166 | +interactively. The advantage of ``samply`` is that it provides a modern | 
|  | 167 | +web-based interface for analyzing profiling data and works on both Linux | 
|  | 168 | +and macOS. | 
|  | 169 | + | 
|  | 170 | +On macOS, ``samply`` support requires Python 3.14 or later. | 
|  | 171 | + | 
| 151 | 172 | How to enable ``perf`` profiling support | 
| 152 | 173 | ---------------------------------------- | 
| 153 | 174 | 
 | 
|  | 
0 commit comments