A pure Python implementation of flamegraph generation tools. This project provides Python replacements for Brendan Gregg's original Perl-based flamegraph utilities, enabling performance profiling visualization without Perl dependencies.
PyFlamegraph converts Linux perf profiling data into interactive flamegraph visualizations in SVG or PNG format. Flamegraphs are a visualization technique that helps identify hot code paths in software performance profiles.
An all-in-one script that combines stack collapsing and flamegraph generation. It reads perf script output from stdin and produces either PNG or SVG flamegraph visualizations.
A Python replacement for stackcollapse-perf.pl. This script parses the output of perf script and collapses identical call stacks, outputting them in the folded stack format (one line per unique stack with its sample count).
A Python replacement for flamegraph.pl. This script reads folded stack data and generates PNG flamegraph visualizations.
- Python 3.6 or higher
- Optional: Pillow (PIL) for enhanced PNG generation with text labels
If Pillow is not installed, PNG generation will use a basic fallback method.
Clone the repository:
git clone <repository-url>
cd PyFlamegraphOptional: Install Pillow for better PNG output:
pip install PillowGenerate an SVG flamegraph:
perf script | python3 perf-flame.py --format svg > flamegraph.svgGenerate a PNG flamegraph:
perf script | python3 perf-flame.py --format png > flamegraph.pngSpecify a custom title:
perf script | python3 perf-flame.py --format svg --title "My Application CPU Profile" > flamegraph.svgFirst, collapse the stacks:
perf script | python3 stackcollapse_perf.py > collapsed.txtThen, generate the flamegraph:
cat collapsed.txt | python3 flamegraph.py > flamegraph.pngRecord performance data:
perf record -g -F 99 ./your_applicationGenerate the flamegraph:
perf script | python3 perf-flame.py --format svg > flamegraph.svg| Option | Description | Default |
|---|---|---|
--format |
Output format: png or svg |
png |
--title |
Title displayed on the flamegraph | CPU Flame Graph |
- Interactive and scalable
- Smaller file sizes for large profiles
- Viewable in any web browser
- Recommended for most use cases
- Static image format
- Requires Pillow for full-featured output
- Useful for embedding in documents or presentations
-
Stack Parsing: The tool reads
perf scriptoutput, which contains sampled call stacks from the profiled application. -
Stack Collapsing: Identical call stacks are grouped and counted. Each unique stack trace becomes a single line with its occurrence count.
-
Visualization: The collapsed stacks are rendered as a flamegraph where:
- The x-axis represents the proportion of samples
- The y-axis shows the call stack depth
- Each rectangle represents a function in the call stack
- Wider rectangles indicate more time spent in that function
- Colors are assigned based on function name hashing for consistency
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.
This project is inspired by Brendan Gregg's original flamegraph tools:
Contributions are welcome. Please ensure that any modifications maintain compatibility with the existing interface and follow Python best practices.