Skip to main content

A Flamegraph generator for Python

Project description

Generate flamegraphs for Python code, using Brendan Gregg’s excellent FlameGraph project to perform the heavy lifting.

pyflame can be used to invoke a Python script from the command line and generate a flamegraph of its execution. It also provides a panel for Django Debug Toolbar that generates a flamegraph for each request, as well as an IPython extension that provides a cell magic to generate flamegraphs for display within a Jupyter Notebook.

Basic Installation

  • Run python -m pip install pyflame.

  • Download flamegraph.pl and then either:

    • make it available via the PATH environment variable, or

    • set up the appropriate configuration as described below to indicate where the script is located on the filesystem.

Command Line Usage

Invoke using python -m pyflame [<options>] <script> [<args>].

The following options are supported:

  • -p path, --flamegraph-script-path path: the path to the flamegraph.pl script (if not specified, search using the PATH environment variable).

  • -a arg_string, --flamegraph-extra-args arg_string: a string specifying extra command line arguments to pass when invoking flamegraph.pl.

  • -s interval, --sample-interval interval: the amount of time the sampler thread will wait between capturing stack traces, in seconds (default: 0.001).

  • -o path, --output-file path: the location to save the flamegraph. If not specified, save in the current directory using the name of the Python script with an .svg extension appended.

Django Debug Toolbar Configuration

To enable, add pyflame to INSTALLED_APPS and pyflame.djdt.panel.FlamegraphPanel to DEBUG_TOOLBAR_PANELS in the project’s Django settings module.

pyflame uses a similar configuration mechanism to that of Django Debug Toolbar. To modify the default configuration, add a PYFLAME_CONFIG setting to the project’s Django settings module. This must be a dictionary which may contain any of the following options:

  • FLAMEGRAPH_SCRIPT_PATH

    Default: None

    The path to the flamegraph.pl script. The default of None tells pyflame to search for flamegraph.pl using the PATH environment variable.

  • FLAMEGRAPH_SCRIPT_EXTRA_ARGS

    Default: []

    A list of extra command line arguments to pass when invoking flamegraph.pl.

  • SAMPLE_INTERVAL

    Default: 0.001,

    The amount of time the sampler thread will wait between capturing stack traces, in seconds.

Jupyter Notebook Configuration

To enable the %%pyflame magic within an IPython kernel running under Jupyter Notebook, first load the IPython extension using the %load_ext magic:

In [1]: %load_ext pyflame

To load the extension automatically each time the IPython kernel starts, list it in the ipython_config.py file:

c.InteractiveShellApp.extensions = [
    'pyflame'
]

There are three other configuration attributes that can be set to configure the extension:

  • PyFlameMagic.flamegraph_script_path

    Default: None

    The path to the flamegraph.pl script. The default of None tells pyflame to search for flamegraph.pl using the PATH environment variable.

  • PyFlameMagic.flamegraph_script_extra_args

    Default: []

    A list of extra command line arguments to pass when invoking flamegraph.pl.

  • PyFlameMagic.default_sample_interval

    Default: 0.001

    The amount of time the sampler thread will wait between capturing stack traces, in seconds.

Licensing

pyflame is distributed under the terms of the MIT license. A copy of the license text is avaiable in the LICENSE file.

Credits

pyflame could not exist without the work of Brendan Gregg and other contributors to create FlameGraph. pyflame also draws inspiration from two related projects. The original idea was inspired by Bo Lopker’s djdt-flamegraph project (pyflame actually started out as a fork of djdt-flamegraph, but over time I ended up completely rewriting it). The approach of spawning a separate thread to sample stack traces using sys._current_frames() was drawn from Evan Hempel’s python-flamegraph project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyflame-0.3.2.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

pyflame-0.3.2-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file pyflame-0.3.2.tar.gz.

File metadata

  • Download URL: pyflame-0.3.2.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for pyflame-0.3.2.tar.gz
Algorithm Hash digest
SHA256 8f5e5146781bddef387b33170b27cfc5be907fae0179516db564a723f30e5121
MD5 e65d9a79583dccc531ca4d39966ae295
BLAKE2b-256 749e0e750a25b3166a0e4e4f6915c1cec971c774d3ea4f84827a64323700d5ae

See more details on using hashes here.

File details

Details for the file pyflame-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: pyflame-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for pyflame-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b67abf9a9f9c0051b5468a3ff5f0e26376609f447c99cb0674e3eba8d5ce0d22
MD5 02a9d411eb198f4ea321d9ed9ece297a
BLAKE2b-256 c352f0ac92524d90cda68d66d1ba98b781c5080c8dbeb85b99562caa33f971d0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page