https://pyflame.readthedocs.io/
Pyflame is a unique profiling tool that generates flame graphs for Python. Pyflame is the only Python profiler based on the Linux ptrace(2) system call. This allows it to take snapshots of the Python call stack without explicit instrumentation, meaning you can profile a program without modifying its source code! Pyflame is capable of profiling embedded Python interpreters like uWSGI. It fully supports profiling multi-threaded Python programs.
Pyflame is written in C++, with attention to speed and performance. Pyflame
usually introduces less overhead than the builtin profile
(or cProfile
)
modules, and also emits richer profiling data. The profiling overhead is low
enough that you can use it to profile live processes in production.
For Debian/Ubuntu, install the following:
# Install build dependencies on Debian or Ubuntu.
sudo apt-get install autoconf automake autotools-dev g++ pkg-config python-dev python3-dev libtool make
Once you've got the build dependencies installed:
./autogen.sh
./configure
make
The make
command will produce an executable at src/pyflame
that you can run
and use.
The full documentation for using Pyflame is here. But here's a quick guide:
# Attach to PID 12345 and profile it for 1 second
pyflame 12345
# Attach to PID 768 and profile it for 5 seconds, sampling every 0.01 seconds
pyflame -s 5 -r 0.01 768
# Run py.test against tests/, emitting sample data to prof.txt
pyflame -o prof.txt -t py.test tests/
In all of these cases you will get flame graph data on stdout (or to a file if
you used -o
). This data is in the format expected by flamegraph.pl
, which
you can find here.
The full FAQ is here.
Full
answer
here.
tl;dr: use the -x
flag to suppress (idle) output.
See here.
Use the --threads
option.