Skip to main content

A diagnostic toolkit for on-the-fly analysis of remote python processes.

Project description

PyFlightProfiler

OS Linux PyPI - Python Version PyPI - Implementation PyPI PyPI - Downloads

A diagnostic toolbox for Python applications that provides non-intrusive, low-overhead capabilities for online analysis.

Background

The growing demand for AI inference and agent-based applications has led to an increased deployment of Python systems in production environments. Inference services frequently encounter performance bottlenecks, while agent-based services face challenges in troubleshooting complex business logic. Production issues are often difficult to reproduce and debug, and traditional logging-based diagnostics typically result in prolonged investigation cycles with limited efficiency. PyFlightProfiler is designed to help address some of these common pain points.

What is PyFlightProfiler

PyFlightProfiler is a toolbox that lets you inspect the status of a running Python process. It allows developers to observe method execution, trace call paths, monitor GIL status, and more, without restarting the application.

What PyFlightProfiler can do

PyFlightProfiler has the following amazing features:

  • 🛫 Non-intrusive Online Diagnostics: Analyze running Python processes without restarting or modifying the application code.
  • 🎯 Method Execution Observation: Watch method inputs, outputs, and execution time with customizable expressions.
  • 🔍 Call Path Tracing: Trace internal method calls and their execution times to identify performance bottlenecks.
  • 🧵 Thread Stack Analysis: Display Python stack frames for all threads, including native stack information.
  • 🌐 Cross-time Method Observation: Record method calls over time and replay them for detailed analysis.
  • 🌍 Global Variable Inspection: View module global variables and class static variables.
  • 🧮 Class Instance Management: Get and manipulate live class instances in the process.
  • 🔥 Performance Profiling: Generate flame graphs for performance hotspot analysis.
  • 🐚 Interactive Console: Execute custom scripts within the target process.
  • 📦 Memory Analysis: Analyze memory usage, including memory diffs and object summaries.
  • 🧠 PyTorch Profiling: Profile PyTorch function execution times and GPU memory allocation behavior.
  • 🔒 GIL Status Monitoring: Observe the Global Interpreter Lock (GIL) status, including lock acquisition and release times.

What platforms are supported

PyFlightProfiler currently supports Linux x86_64 (glibc ≥ 2.17) and macOS (arm64), and requires Python 3.8 or later.

Installation

To install PyFlightProfiler, simply use pip:

pip3 install flight_profiler

Documentation

You can find the full documentation here.

Usage

For a Python process to be profiled, PyFlightProfiler must reside in the same Python environment. Only then can the process correctly reference the flight_profiler library once attached. PyFlightProfiler follows a two-stage attach-then-profile workflow. After attaching to a target process, you can use various subcommands to inspect and debug your Python program in real time.

Attaching to a Running Process

The first step for attachment is to use the flight_profiler command followed by the PID of the process you want to analyze.

usage: flight_profiler <pid>

description: A realtime analysis tool used for profiling python program!

positional arguments:
  pid         python process id to analyze.

optional arguments:
  -h, --help  show this help message and exit
  --cmd CMD   One-time profile, primarily used for unit testing.
  --debug     enable debug logging for attachment.

For CPython 3.14 and above, we utilize sys.remote_exec for remote code execution, a feature introduced by PEP-0768. This approach is therefore largely safe.

In CPython 3.13 and earlier versions, the implementation of remote code execution differs between macOS and Linux. On macOS, we utilize LLDB to inject agent logic into the target process. On Linux, however, code is dispatched to the remote process via ptrace. This approach requires that the Linux distribution supports ptrace and that the SYS_PTRACE capability is enabled in Docker environments.

[!Note]

It is important to note that this procedure involves certain risks, and its safety cannot be fully guaranteed, as it is influenced by CPython's internal mechanisms. For example, if the Global Interpreter Lock (GIL) is held indefinitely by another thread, the injected code may never execute. We have thoroughly tested PyFlightProfiler with a variety of applications on CentOS systems using libc versions 2.17 to 2.32. We welcome further user feedback to help us reduce attachment failures and improve reliability.

As a best practice, PyFlightProfiler should be comprehensively validated in a non-production environment to ensure stability and performance before being trusted with live services.

Watching Method Input and Output

For method location, PyFlightProfiler uses the format module [class] method, where module corresponds to the import path as it appears in the program. For instance, __main__ for top-level scripts executed directly, pkg_a.pkg_b when the code uses from pkg_a import pkg_b, and pkg_b when using import pkg_b. This simple module resolution scheme suffices for most use cases. If you know the file path of the target code but are unsure of its runtime module name, you can use the module subcommand (described below) to verify whether the module as well as the target method is loaded in the process. The [class] component is optional and should only be included when the method belongs to a class. It is omitted for standalone (non-class) functions.

In addition, PyFlightProfiler provides mechanisms for method filtering (-f) and custom extraction of key properties (--expr). You can specify a Python expression that operates within a context where the effective function signature is (target, return_obj, cost, *args, **kwargs), and the expression you provide serves as the body of a return statement. Here, target refers to the class instance if the method is bound (or None for standalone functions), return_obj is the value returned by the method, cost represents the execution time in milliseconds, and args and kwargs correspond to the positional and keyword arguments passed to the method, respectively. For concrete usage patterns, refer to the examples provided in the watch command below.

USAGE:                                                                                                                                                                                               12:01:21 [3/52]
  watch module [class] method [--expr <value>] [-nm <value] [-e] [-r] [-v] [-n <value>] [-x <value>] [-f <value>]

SUMMARY:
  Display the input/output args, return object and cost time of method invocation.

EXAMPLES:
  watch __main__ func -x 2
  watch __main__ func -f args[0]["query"]=='hello'
  watch __main__ func -f return_obj['success']==True
  watch __main__ func --expr return_obj,args -f cost>10
  watch __main__ classA func

OPTIONS:
<module>                           the module that method locates.
<class>                            the class name if method belongs to class.
<method>                           target method name.
--expr <value>                     contents you want to watch,  write python bool statement like input func args is (target, return_obj, cost, *args, **kwargs). default is args,kwargs .
-x, --expand                       object represent tree expand level(default 1), -1 means infinity.
-e, --exception                    short for --exception, only record when method throws exception.
-nm, --nested-method               watch nested method with depth restrict to 1.
-r, --raw                          display raw output without json format.
-v, --verbose                      display all the nested items in target list or dict.
-n, --limits <value>               limit the the upperbound of display watched result, default is 10.
-f, --filter <value>               filter method params&args&return_obj&cost&target, expressions according to --expreg: args[0]=='hello'.

Tracing Method Call Paths

The trace subcommand is used to inspect method execution time and its inner method call chain. It uses the same method location mechanism as described in the watch subcommand. The -i, --interval option sets the threshold (in milliseconds) for displaying inner method calls—only invocations with a cost greater than ${interval} will be shown, with a default value of 0.1 ms. A higher interval reduces the profiler’s impact on the original program. In practice, we observe an average runtime overhead of 5%–20%, depending on the chosen interval.

USAGE:
  trace module [class] method [-i <value>] [-nm <value>] [-et <value>] [-d <value>] [-n <value>] [-f <value>]

SUMMARY:
  Trace the execution time of specified method invocation.

EXAMPLES:
  trace __main__ func
  trace __main__ func --interval 1
  trace __main__ func -et 30 -i 1
  trace __main__ classA func

OPTIONS:
<module>                           the module that method locates.
<class>                            the class name if method belongs to class.
<method>                           target method name.
-i, --interval <value>             display function invocation cost more than ${value} milliseconds, default is 0.1ms.
-et, --entrance_time <value>       filter function execute cost more than ${value} milliseconds, but on entrance filter.
-d, --depth <value>                display the method call stack, limited to the specified depth ${value}. When a depth is specified, the ${interval} parameter is ignored and its value is constrained to 0.
-nm, --nested-method               trace nested method with depth restrict to 1.
-f, --filter_expr <value>          filter method params expressions, only support filter target&args, write python bool statement like input func args is (target, *args, **kwargs), eg: args[0]=='hello'.
-n, --limits <value>               threshold of trace method times, default is 10.

Locating module with filepath

Use the module subcommand to determine the correct module name for a given source file path. This is helpful when you know the file location but are unsure how it was imported.

USAGE:
  module filepath

EXAMPLES:
  module /home/admin/application/compute.py

OPTIONS:
<filepath>          Absolute or relative path to the Python source file.

Other Subcommands

PyFlightProfiler includes several additional tools for advanced diagnostics:

  • help - Display help for any command.
  • console - Launch an interactive Python console inside the target process.
  • stack - Analyze Python thread stacks (based on pystack).
  • tt, timetunnel - Observe method behavior across time (historical execution context).
  • reload - Reload method implementation from the latest source file.
  • getglobal - Inspect global variables in the target process.
  • vmtool - Inspect live class instances and their attributes.
  • perf - Sample CPU hotspots and generate flame graphs (based on py-spy).
  • torch - Profile PyTorch operations using the pre-installed PyTorch profiler (based on pytorch).
  • mem - Report memory usage statistics (based on pympler).
  • gilstat - Monitor Python’s Global Interpreter Lock (GIL) contention and performance impact.

Acknowledgements

PyFlightProfiler is inspired by the design of Arthas. The project is developed by Alibaba Group. The code is distributed under the Apache License (Version 2.0). This product contains various third-party components under other open-source licenses. See the NOTICE file for more information.

The implementation of the trace subcommand is conceptually inspired by pyinstrument. The process attachment mechanism on Linux is inspired by linux-inject. Python bytecode transform mechanism is inspired by Python on-the-fly function update. The following open-source libraries and tools are used in PyFlightProfiler, either in their close-to-original form or as an inspiration:

  • pystack - Used for Python stack analysis on Linux
  • frida - Used for GIL lock analysis
  • pympler - Used for memory usage analysis
  • py-spy - Used for CPU hotspot function analysis
  • pytorch - Used for sampling torch timeline via torch.profiler

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

flight_profiler-1.0.4-cp314-cp314-manylinux_2_17_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

flight_profiler-1.0.4-cp314-cp314-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

flight_profiler-1.0.4-cp313-cp313-manylinux_2_17_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

flight_profiler-1.0.4-cp313-cp313-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

flight_profiler-1.0.4-cp312-cp312-manylinux_2_17_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

flight_profiler-1.0.4-cp312-cp312-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

flight_profiler-1.0.4-cp311-cp311-manylinux_2_17_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

flight_profiler-1.0.4-cp311-cp311-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

flight_profiler-1.0.4-cp310-cp310-manylinux_2_17_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

flight_profiler-1.0.4-cp310-cp310-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

flight_profiler-1.0.4-cp39-cp39-manylinux_2_17_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

flight_profiler-1.0.4-cp39-cp39-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.9macOS 15.0+ ARM64

flight_profiler-1.0.4-cp38-cp38-manylinux_2_17_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

flight_profiler-1.0.4-cp38-cp38-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.8macOS 15.0+ ARM64

File details

Details for the file flight_profiler-1.0.4-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 050c0fa93cc30b0bfd081af26ce3fed2264b14ecbc2b66bebb509135e8a8b30b
MD5 dc52860c806534829ac157930aa0ccbe
BLAKE2b-256 1f65395fa7cfc95770f8d6cb0e9a2864af6fd8d226eb533ef8f5e8da8fc8b065

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 6fb93d51c5072feb77d1799067ff0ef410ab4e2d329e25834a9ea85e66e7f581
MD5 f338ec6ed4af4e91cf794ba310eda836
BLAKE2b-256 86c55935f4d23af8deb712ee9b2d607ef5292da13a063d686ebf66ac7d8ede08

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp313-cp313-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 60a2c8122cdd0ad4f475985dfe1ebaeea24771746c5b1103860e0c623eee1dfb
MD5 6c86503b932ee5cba67469498477a84a
BLAKE2b-256 493ec4d29d90a2e0025d7ed8c7c5a823e945aac99d0eeaf6c605ff1141cc7467

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 433f8540092fda8f0ea4f821c348297a3af7b90c39179745631d629680cfca7c
MD5 f6e954ef2730db7f7625d6c13604e07e
BLAKE2b-256 33f098db4df215aa79ddd10e219f620e7cc93de48333b0c0b96494bb4f393cad

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 88abfbe3503391d2ee296912db75e0b331c4f59f474e337db2ff3b49516a5a4b
MD5 bd60a16e5686bf763f54d2085215ee25
BLAKE2b-256 a7e905d385b24443f70493564e1885c1118076d7073ad40601b9fe85363d8a54

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 2679e64ce82220f83600012962ccf4f0b6979e5c40c7dc37751c3ebb8746ab35
MD5 8e7f19616cf1ca083548dbccdcf49ec7
BLAKE2b-256 fdc7044cb3ebee3c7e9d6bbfcc8bdec5889f27e316a4f956b1ed0b8d26a72186

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 2d48ab1c01044bed4ac20acf4726427633e622cb5110e6b746c19f35f83d5186
MD5 b2deaed946a20e2e196f4c243e17c1fa
BLAKE2b-256 4fa643efd9514f220cc0f73d0b572c17b9fd9ddd4f7e8082fbdf06f71e8bbf1e

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 ce5f79881c95c7ce7a368900271ecbf72bb66052108d46f7173a3d4f9b07f4f9
MD5 d1e98365406257e05035528b2323a399
BLAKE2b-256 391e81ac89cc37a876497e32d69c59ab3647d5e673e98216eae4a122b53e7dd2

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c6eb6297c0b632e927a59659d129e4b105da1a94a219aeb831ad52f8af2eb20c
MD5 186520f253f19106987a38753fc62fce
BLAKE2b-256 c46446012afc3de3a07b254edcd8cb73ac028769dd5d60ebc4e8e75d396b9d8f

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 0755005ecaae1ab3949698feb956eb7796fa7f5cb21d5c79ea6e381e5847c2a0
MD5 deefc168cc0452b703629c0aec5c08a5
BLAKE2b-256 3fd874fea756ff4f2569c4fa38e13bf1f68e40d6ebd990b7147edcffbdb0a31e

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 02aef0f7f8cb3cf1482d28faa57cb4119df443b39b93d16fdf087506a2faaad9
MD5 5add629b978163aa23b701a800d9267f
BLAKE2b-256 290ccc17776e58d9c68e5422b3d30aea1c6ce0b7f67dc81ad1b66885b2c76ceb

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp39-cp39-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp39-cp39-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 e7c08494b99fc9815abed396f41ab06975258976f332151253468518da25aee0
MD5 d28bc7359f6cf00b27782143d8dbb5f9
BLAKE2b-256 1e375f75123c054d9226fe477354c17223ef328c71e9021700fc782fb2e4ccc9

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 1bd0538537ad64679095adb844aefe33fcaed706f706fdffaeaa82a8a9d40dda
MD5 8e27752690e2e6ab44822ee1494fca73
BLAKE2b-256 96b540b12625578a9fc4f3466b7066262a974d68013ac665696f57746f6381f5

See more details on using hashes here.

File details

Details for the file flight_profiler-1.0.4-cp38-cp38-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.4-cp38-cp38-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 eb57a0fb66656c86eea4d6c2a257ea22ef830af78fb22bd2da854e74261463c7
MD5 da0f2b995f67127efc1c62e9f73c77fb
BLAKE2b-256 f0bd8f5ff9956c311e5073f6adae1455df10a4f95197adb64cbd37b190c925d3

See more details on using hashes here.

Supported by

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