Skip to content

GH-91048: Add utils for capturing async call stack for asyncio programs and enable profiling #124640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 88 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
1b01a91
Add `f_generator` property to Python frame objects
1st1 Sep 24, 2024
0fc5511
Working implementation of `asyncio.capture_call_stack()`
1st1 Sep 25, 2024
1d20a51
Address Guido's comments
1st1 Sep 26, 2024
c8be18e
Add a comment for capture_call_stack()
1st1 Sep 26, 2024
abf2cb9
Add a couple more tests
1st1 Sep 26, 2024
20ceab7
Remove setter for C impl of Task._awaited_by
1st1 Sep 26, 2024
72d9321
Intoduce cr_task
1st1 Sep 26, 2024
c9475f6
Unbreak shield() and gather()
1st1 Sep 26, 2024
e1099e9
Add convinience fields to C Task/Future for profilers
1st1 Sep 26, 2024
817f88b
Fix ups
1st1 Sep 27, 2024
54386ac
Add basic docs
1st1 Sep 27, 2024
98434f0
Implement, test, and document asyncio.print_call_stack()
1st1 Sep 28, 2024
485c166
Reorder a few things
1st1 Sep 28, 2024
8802be7
Add a test to exercise asyncio stack traces in out-of-process profilers
pablogsal Sep 29, 2024
1ddc9cf
Refactor the section-generated code into an evil macro
pablogsal Sep 30, 2024
bc9beb8
Rename "capture_call_stack()" et al to "capture_call_graph()"
1st1 Oct 2, 2024
fd141d4
Add NEWS
1st1 Oct 2, 2024
2d72f24
Per mpage's suggestion: use traceback style formatring for frames
1st1 Oct 2, 2024
391defa
mpage feedback: fix typo
1st1 Oct 2, 2024
d6357fd
Per mpage suggestion: get rid of CoroutineCallGraphEntry
1st1 Oct 2, 2024
bb3b6df
Strip sloppy white space!
1st1 Oct 2, 2024
54c99ec
Fix sloppy set iteration!
1st1 Oct 2, 2024
c1a4f09
Fix a sloppy test!
1st1 Oct 2, 2024
027d522
Run 'make regen-all'
1st1 Oct 2, 2024
c2d5ec6
Add a what's new entry
1st1 Oct 2, 2024
08d09eb
Fix (hopefully) a compiler warning
1st1 Oct 2, 2024
fe3113b
Fix sloppy what's new!
1st1 Oct 2, 2024
18ec26d
Fix CI complaining about suspicious global in C
1st1 Oct 2, 2024
e4cc462
Add a test for depth=2
1st1 Oct 2, 2024
d5cdc36
Add critical sections for free threaded builds
pablogsal Oct 2, 2024
83606f2
Add more slopy tests
pablogsal Oct 2, 2024
5edac41
Apply suggestions from code review
1st1 Oct 16, 2024
8dc6d34
Apply suggestions from code review
1st1 Oct 16, 2024
30884ea
Update Modules/_asynciomodule.c
1st1 Oct 16, 2024
1317658
Update Modules/_asynciomodule.c
1st1 Oct 16, 2024
81b0a31
Use dataclasses instead of tuples for asyncio.stack
1st1 Oct 16, 2024
258ce3d
Fix sloppy whitespace!
1st1 Oct 16, 2024
b9ecefb
Remove critical sections for now
pablogsal Oct 16, 2024
b77dcb0
Eplain the weird macro
1st1 Oct 16, 2024
8867946
Fix test
pablogsal Oct 16, 2024
87d2524
Merge remote-tracking branch 'upstream/main' into stack
pablogsal Oct 16, 2024
b47bef1
Add a docs clarification
1st1 Oct 16, 2024
230b7ec
Fix typing in asyncio.stack
ambv Oct 16, 2024
b1d6158
Fix memory leak
pablogsal Oct 17, 2024
ac51364
Address comments from Kumar's review
ambv Oct 17, 2024
c7e59eb
Fix rare Mach-O linker bug when .bss is uninitialized data
ambv Oct 22, 2024
9eba5e1
Merge branch 'main' into pr-124640
ambv Oct 22, 2024
59121f6
you saw nothing, okay
ambv Oct 22, 2024
f8f48f0
Update Lib/asyncio/futures.py
1st1 Oct 23, 2024
74c5ad1
Remove unnecessary imports from tests
ambv Oct 30, 2024
067c043
Remove cr_task/gi_task
ambv Oct 31, 2024
9f04911
Fix refleaks
ambv Nov 13, 2024
0774805
Rename "asyncio.stack" to "asyncio.graph"
ambv Nov 13, 2024
3048493
Allow returning a string with `format_call_graph`
ambv Nov 13, 2024
1f42873
Add test for eager task factory support
ambv Nov 13, 2024
7799391
Merge branch 'main' into pr-124640
ambv Nov 13, 2024
03ed5c1
Make _asyncio_awaited_by a frozenset in the Python version as well pe…
ambv Nov 13, 2024
21f9ea9
Address picnixz' feedback
1st1 Nov 23, 2024
8a43dfa
Blacken the C/Py test code by hand, by request from picnixz
1st1 Nov 23, 2024
b3fae68
More style fixes per picnixz' suggestions
1st1 Nov 23, 2024
d0aedf0
Address Kumar's latest comment
1st1 Nov 23, 2024
df0032a
diff cleanup
kumaraditya303 Nov 23, 2024
0ce241b
change dataclasses to use tuples
kumaraditya303 Nov 23, 2024
8f126f6
doc fixes
kumaraditya303 Nov 23, 2024
966d84e
remove reduntant headers include and add my name to whatsnew
kumaraditya303 Nov 23, 2024
f56468a
improve comment
kumaraditya303 Nov 23, 2024
404b88a
fix leave task
kumaraditya303 Nov 23, 2024
911fed8
fix external inspection on linux
kumaraditya303 Nov 23, 2024
ab511a4
minor format
kumaraditya303 Nov 23, 2024
c3c685a
try to fix docs build
kumaraditya303 Nov 23, 2024
785adeb
Update Doc/whatsnew/3.14.rst
kumaraditya303 Nov 23, 2024
a577328
Match indentation with _asynciomodule.c after ab511a4f67ffd88f7709c1b…
ambv Nov 23, 2024
064129a
Improve comments after f56468af8b3789d0e20be30715c20ac1bb367641
ambv Nov 23, 2024
ce332d9
Restore the Oxford comma
ambv Nov 23, 2024
d6d943f
Address remaining suggestions of Andrew Svetlov
ambv Nov 23, 2024
703ff46
Fix gather()
1st1 Nov 23, 2024
e867863
Replace lambda by closure
pablogsal Nov 26, 2024
9cb5b29
Let’s not emphasize *asyncio*
1st1 Nov 26, 2024
61b2b7b
Apply suggestions from Irit's review
ambv Nov 26, 2024
9533ab9
Address remaining review by Irit
ambv Nov 26, 2024
596191d
Test pure-Python and C-accelerated versions of future_add_to/future_d…
ambv Nov 26, 2024
ad9152e
fix blooper
ambv Nov 26, 2024
066bf21
Fix another blooper
ambv Nov 26, 2024
4caeec4
Fix crash
pablogsal Jan 21, 2025
38f061d
Merge remote-tracking branch 'upstream/main' into stack
pablogsal Jan 21, 2025
a8dd667
use private method for the policy
pablogsal Jan 22, 2025
cf8f5e5
Avoid importing typing
ambv Jan 22, 2025
eda9c7c
Remove debug printing from test_asyncio.test_graph
ambv Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions Doc/library/asyncio-graph.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
.. currentmodule:: asyncio


.. _asyncio-graph:

========================
Call Graph Introspection
========================

**Source code:** :source:`Lib/asyncio/graph.py`

-------------------------------------

asyncio has powerful runtime call graph introspection utilities
to trace the entire call graph of a running *coroutine* or *task*, or
a suspended *future*. These utilities and the underlying machinery
can be used from within a Python program or by external profilers
and debuggers.

.. versionadded:: next


.. function:: print_call_graph(future=None, /, *, file=None, depth=1, limit=None)

Print the async call graph for the current task or the provided
:class:`Task` or :class:`Future`.

This function prints entries starting from the top frame and going
down towards the invocation point.

The function receives an optional *future* argument.
If not passed, the current running task will be used.

If the function is called on *the current task*, the optional
keyword-only *depth* argument can be used to skip the specified
number of frames from top of the stack.

If the optional keyword-only *limit* argument is provided, each call stack
in the resulting graph is truncated to include at most ``abs(limit)``
entries. If *limit* is positive, the entries left are the closest to
the invocation point. If *limit* is negative, the topmost entries are
left. If *limit* is omitted or ``None``, all entries are present.
If *limit* is ``0``, the call stack is not printed at all, only
"awaited by" information is printed.

If *file* is omitted or ``None``, the function will print
to :data:`sys.stdout`.

**Example:**

The following Python code:

.. code-block:: python

import asyncio

async def test():
asyncio.print_call_graph()

async def main():
async with asyncio.TaskGroup() as g:
g.create_task(test())

asyncio.run(main())

will print::

* Task(name='Task-2', id=0x1039f0fe0)
+ Call stack:
| File 't2.py', line 4, in async test()
+ Awaited by:
* Task(name='Task-1', id=0x103a5e060)
+ Call stack:
| File 'taskgroups.py', line 107, in async TaskGroup.__aexit__()
| File 't2.py', line 7, in async main()

.. function:: format_call_graph(future=None, /, *, depth=1, limit=None)

Like :func:`print_call_graph`, but returns a string.
If *future* is ``None`` and there's no current task,
the function returns an empty string.


.. function:: capture_call_graph(future=None, /, *, depth=1, limit=None)

Capture the async call graph for the current task or the provided
:class:`Task` or :class:`Future`.

The function receives an optional *future* argument.
If not passed, the current running task will be used. If there's no
current task, the function returns ``None``.

If the function is called on *the current task*, the optional
keyword-only *depth* argument can be used to skip the specified
number of frames from top of the stack.

Returns a ``FutureCallGraph`` data class object:

* ``FutureCallGraph(future, call_stack, awaited_by)``

Where *future* is a reference to a :class:`Future` or
a :class:`Task` (or their subclasses.)

``call_stack`` is a tuple of ``FrameCallGraphEntry`` objects.

``awaited_by`` is a tuple of ``FutureCallGraph`` objects.

* ``FrameCallGraphEntry(frame)``

Where *frame* is a frame object of a regular Python function
in the call stack.


Low level utility functions
===========================

To introspect an async call graph asyncio requires cooperation from
control flow structures, such as :func:`shield` or :class:`TaskGroup`.
Any time an intermediate :class:`Future` object with low-level APIs like
:meth:`Future.add_done_callback() <asyncio.Future.add_done_callback>` is
involved, the following two functions should be used to inform asyncio
about how exactly such intermediate future objects are connected with
the tasks they wrap or control.


.. function:: future_add_to_awaited_by(future, waiter, /)

Record that *future* is awaited on by *waiter*.

Both *future* and *waiter* must be instances of
:class:`Future` or :class:`Task` or their subclasses,
otherwise the call would have no effect.

A call to ``future_add_to_awaited_by()`` must be followed by an
eventual call to the :func:`future_discard_from_awaited_by` function
with the same arguments.


.. function:: future_discard_from_awaited_by(future, waiter, /)

Record that *future* is no longer awaited on by *waiter*.

Both *future* and *waiter* must be instances of
:class:`Future` or :class:`Task` or their subclasses, otherwise
the call would have no effect.
1 change: 1 addition & 0 deletions Doc/library/asyncio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ You can experiment with an ``asyncio`` concurrent context in the :term:`REPL`:
asyncio-subprocess.rst
asyncio-queue.rst
asyncio-exceptions.rst
asyncio-graph.rst

.. toctree::
:caption: Low-level APIs
Expand Down
10 changes: 10 additions & 0 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
| | f_locals | local namespace seen by |
| | | this frame |
+-----------------+-------------------+---------------------------+
| | f_generator | returns the generator or |
| | | coroutine object that |
| | | owns this frame, or |
| | | ``None`` if the frame is |
| | | of a regular function |
+-----------------+-------------------+---------------------------+
| | f_trace | tracing function for this |
| | | frame, or ``None`` |
+-----------------+-------------------+---------------------------+
Expand Down Expand Up @@ -310,6 +316,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):

Add ``__builtins__`` attribute to functions.

.. versionchanged:: next

Add ``f_generator`` attribute to frames.

.. function:: getmembers(object[, predicate])

Return all the members of an object in a list of ``(name, value)``
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,11 @@ asyncio
reduces memory usage.
(Contributed by Kumar Aditya in :gh:`107803`.)

* :mod:`asyncio` has new utility functions for introspecting and printing
the program's call graph: :func:`asyncio.capture_call_graph` and
:func:`asyncio.print_call_graph`.
(Contributed by Yury Selivanov, Pablo Galindo Salgado, and Łukasz Langa
in :gh:`91048`.)

base64
------
Expand Down
65 changes: 65 additions & 0 deletions Include/internal/pycore_debug_offsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,41 @@ extern "C" {

#define _Py_Debug_Cookie "xdebugpy"

#if defined(__APPLE__)
# include <mach-o/loader.h>
#endif

// Macros to burn global values in custom sections so out-of-process
// profilers can locate them easily.

#define GENERATE_DEBUG_SECTION(name, declaration) \
_GENERATE_DEBUG_SECTION_WINDOWS(name) \
_GENERATE_DEBUG_SECTION_APPLE(name) \
declaration \
_GENERATE_DEBUG_SECTION_LINUX(name)

#if defined(MS_WINDOWS)
#define _GENERATE_DEBUG_SECTION_WINDOWS(name) \
_Pragma(Py_STRINGIFY(section(Py_STRINGIFY(name), read, write))) \
__declspec(allocate(Py_STRINGIFY(name)))
#else
#define _GENERATE_DEBUG_SECTION_WINDOWS(name)
#endif

#if defined(__APPLE__)
#define _GENERATE_DEBUG_SECTION_APPLE(name) \
__attribute__((section(SEG_DATA "," Py_STRINGIFY(name))))
#else
#define _GENERATE_DEBUG_SECTION_APPLE(name)
#endif

#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
#define _GENERATE_DEBUG_SECTION_LINUX(name) \
__attribute__((section("." Py_STRINGIFY(name))))
#else
#define _GENERATE_DEBUG_SECTION_LINUX(name)
#endif

#ifdef Py_GIL_DISABLED
# define _Py_Debug_gilruntimestate_enabled offsetof(struct _gil_runtime_state, enabled)
# define _Py_Debug_Free_Threaded 1
Expand Down Expand Up @@ -69,6 +104,7 @@ typedef struct _Py_DebugOffsets {
uint64_t instr_ptr;
uint64_t localsplus;
uint64_t owner;
uint64_t stackpointer;
} interpreter_frame;

// Code object offset;
Expand Down Expand Up @@ -113,6 +149,14 @@ typedef struct _Py_DebugOffsets {
uint64_t ob_size;
} list_object;

// PySet object offset;
struct _set_object {
uint64_t size;
uint64_t used;
uint64_t table;
uint64_t mask;
} set_object;

// PyDict object offset;
struct _dict_object {
uint64_t size;
Expand Down Expand Up @@ -153,6 +197,14 @@ typedef struct _Py_DebugOffsets {
uint64_t size;
uint64_t collecting;
} gc;

// Generator object offset;
struct _gen_object {
uint64_t size;
uint64_t gi_name;
uint64_t gi_iframe;
uint64_t gi_frame_state;
} gen_object;
} _Py_DebugOffsets;


Expand Down Expand Up @@ -198,6 +250,7 @@ typedef struct _Py_DebugOffsets {
.instr_ptr = offsetof(_PyInterpreterFrame, instr_ptr), \
.localsplus = offsetof(_PyInterpreterFrame, localsplus), \
.owner = offsetof(_PyInterpreterFrame, owner), \
.stackpointer = offsetof(_PyInterpreterFrame, stackpointer), \
}, \
.code_object = { \
.size = sizeof(PyCodeObject), \
Expand Down Expand Up @@ -231,6 +284,12 @@ typedef struct _Py_DebugOffsets {
.ob_item = offsetof(PyListObject, ob_item), \
.ob_size = offsetof(PyListObject, ob_base.ob_size), \
}, \
.set_object = { \
.size = sizeof(PySetObject), \
.used = offsetof(PySetObject, used), \
.table = offsetof(PySetObject, table), \
.mask = offsetof(PySetObject, mask), \
}, \
.dict_object = { \
.size = sizeof(PyDictObject), \
.ma_keys = offsetof(PyDictObject, ma_keys), \
Expand Down Expand Up @@ -260,6 +319,12 @@ typedef struct _Py_DebugOffsets {
.size = sizeof(struct _gc_runtime_state), \
.collecting = offsetof(struct _gc_runtime_state, collecting), \
}, \
.gen_object = { \
.size = sizeof(PyGenObject), \
.gi_name = offsetof(PyGenObject, gi_name), \
.gi_iframe = offsetof(PyGenObject, gi_iframe), \
.gi_frame_state = offsetof(PyGenObject, gi_frame_state), \
}, \
}


Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_tstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef struct _PyThreadStateImpl {
PyThreadState base;

PyObject *asyncio_running_loop; // Strong reference
PyObject *asyncio_running_task; // Strong reference

struct _qsbr_thread_state *qsbr; // only used by free-threaded build
struct llist_node mem_free_queue; // delayed free queue
Expand Down
2 changes: 2 additions & 0 deletions Lib/asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .events import *
from .exceptions import *
from .futures import *
from .graph import *
from .locks import *
from .protocols import *
from .runners import *
Expand All @@ -27,6 +28,7 @@
events.__all__ +
exceptions.__all__ +
futures.__all__ +
graph.__all__ +
locks.__all__ +
protocols.__all__ +
runners.__all__ +
Expand Down
Loading
Loading