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.6-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.6-cp314-cp314-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

flight_profiler-1.0.6-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.6-cp313-cp313-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

flight_profiler-1.0.6-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.6-cp312-cp312-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

flight_profiler-1.0.6-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.6-cp311-cp311-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

flight_profiler-1.0.6-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.6-cp310-cp310-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

flight_profiler-1.0.6-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.6-cp39-cp39-macosx_15_0_arm64.whl (2.6 MB view details)

Uploaded CPython 3.9macOS 15.0+ ARM64

flight_profiler-1.0.6-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.6-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.6-cp314-cp314-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp314-cp314-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 fdc04d5896e8615959b714f4b3a69830952d6bd888d1bb81166ddd7ce935c372
MD5 631eb1c33dc73422e6dbaae5142fd8a9
BLAKE2b-256 79b503651fbd8d7189f3f11f82ae0e069798d211151762abb117ca73867dc650

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 a25b8b740fc8c37e9a658fefc4097c424d45cfa432968bad3f21d611c4c07f80
MD5 466e3d13f4da4fe9fbc18175b2544f29
BLAKE2b-256 04fbf47a36152f1ac8ea5ff071b0aa48ee063d228a97a1577c49d32b3a7f5276

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp313-cp313-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 49d3a6c44037da8d9eece75d4fce51ba03aeef3435da911246f6f0793c528365
MD5 926d6d54af788a89adc1a708dc8b2cb5
BLAKE2b-256 2d36d420da27eef9e476d8faaf4f811ff135904118c90512f71bfcbe4143dce6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 4ba8ac85adb405381cb1357aa3cfbf005a520a1edb29069a7c1e29d1cd0a4419
MD5 5475bba17ef6625f9e5a5e18b4362547
BLAKE2b-256 8c51efeeff50e80f00188c10aa9baed580acf294b9f7d0d7a759e58baddf89e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c047f223e7bd89c954895337ce7e060f277683010a0196c7d812a00e4ab8e583
MD5 4d6ac9ba2ec42477b54311d692c061f4
BLAKE2b-256 dbfbaca175bd54ed2f8beb9c8612cded1be339c23b65e3393e0f43247201d68b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 7b6e558eb94d86314a38770ae785ca723a8937976d801f8f9ad5014ce44d48b8
MD5 d823c405c475a7ca13ff43327f9acb2b
BLAKE2b-256 69787abbe329156e721013d413d0b3caefb334997d48b0f4e28fb47a213df8c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 6341cd92fb4b74260c90094df20b583c417b510a08a35f8f34a27e94eb04d926
MD5 7c22589eeb8c3e41add8bfce5e742ac2
BLAKE2b-256 e1385ba9a00fc27cc8d65e7945b7706639ca0de4c9ead62d767b805d0d28d98e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 0cbfc8415b70b6f58882bda8dadaa4ea533ebbfda889b96151c8c86b2cb9145a
MD5 72be0358514cfa59948115581fd62ae4
BLAKE2b-256 9b8f86e0eb991b2ab578ac330d05f12609ecddf64fb5b3625b757242c3bbaed2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 8aec78d10830ff7534a7387d9a8babead00c72709a41c0fea4fa7ed39387f8e6
MD5 2fd0484adb48a6e956246894e96f1a4f
BLAKE2b-256 33f7b4f15a508fc4d70c9855498a58489252661899c1d9cbd61322c0f89cb807

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 d599b83ffaa9d8e2272a97543d5024d1c91ca23fc7781e1ddd67f5c0ae951aa8
MD5 df749db41268a9024ef2b48d73b06cfe
BLAKE2b-256 9166084766a6b98d6225f32115df80353a616f614a2de93a1d0cf81c2945b8ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 8170aa410cd25c6c6ec489ae54201c608f54822c21b943f33e91e0594c74fbd2
MD5 b673001cc44ef3b5dfa7e8c6dd13698b
BLAKE2b-256 11c75a7ace0ee4a72109c21cf28dc62c39a71e099edfb8c2d55fe5c494e2a34d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp39-cp39-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 b5e8c54a8145c5cfa8c70f70a3cfdd88eb458256d22156d2e02e5681eea018fc
MD5 a107ec7a7807ba7f1a3756ff73bde658
BLAKE2b-256 534eaf0ce0593683590803a3394e491236d37a9924ad11490868a4c866998bcb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 58edbdde781382b88f51953806ffbc0d8279393f2b453ccd7a4f33c904e944eb
MD5 fa0fda4794a2b838962929d69ad00827
BLAKE2b-256 150cbae184b0e64e1de9b12818fa88d4dd5408d9f8983b087e17db4b585564fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flight_profiler-1.0.6-cp38-cp38-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 5987bd0e2c2b4446c1c7ff374d1ea3ce513dd8a85219559c7f99648c6ad0ab8d
MD5 944cb2be08d048638fa045f8377cfd4f
BLAKE2b-256 26ef64f935c4f0b5310bc0d8439bd066ed9f37ac30fa9c7d63d370b2a53cd2f8

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