Skip to content

[k2] implement async stack#1320

Merged
astrophysik merged 38 commits into
masterfrom
vsadokhov/async-stack
May 28, 2025
Merged

[k2] implement async stack#1320
astrophysik merged 38 commits into
masterfrom
vsadokhov/async-stack

Conversation

@astrophysik
Copy link
Copy Markdown
Contributor

General

This PR adds support for collecting async stack traces in runtime-light. Additionally, the implementation of debug_backtrace that uses coroutine traces is also added

Problem

Basic usage of backtrace with c++20 coroutines doesn't give enough information about execution. Common traces look like:

* nested_coroutine
* std::coroutine_handle<>::resume
* coroutine_handler_callee
* ...

Such traces don't show from which coroutine the coroutine requesting the stack trace was called. Moreover, at runtime, there should be the ability to reconstruct the sequence of coroutine calls not only for debugging purposes but also for language features, such as debug_backtrace, Exception::getTrace.

Solution

The solution is based on a similar approach from folly. InstanceState contains async_stack_root, which acts as the equivalent of the rbp register for the coroutine stack. To maintain a pointer to the top coroutine frame in async_stack_root, awaitables are used async_stack_watcher_t. High-level view can be found in the file async-stack.h.

Result

With the help of the asynchronous stack, it is possible to obtain a correct trace of coroutines, for example:

* nested_coroutine
* std::coroutine_handle<>::resume
* coroutine_func2
* coroutine_func1
* ...

Important aspects

  • User code in KPHP is executed within forks. The top of the trace obtained from a fork will be the first function, which was the root of the fork

@astrophysik astrophysik self-assigned this May 20, 2025
@astrophysik astrophysik added the k2 Affects compiler or runtime in K2 mode label May 20, 2025
Copy link
Copy Markdown
Contributor

@apolyakov apolyakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

Comment thread runtime-light/coroutine/async-stack.h Outdated
Comment thread runtime-light/coroutine/async-stack.h Outdated
Comment thread runtime-light/coroutine/async-stack.h Outdated
Comment thread runtime-light/coroutine/awaitable.h Outdated
Comment thread runtime-light/coroutine/awaitable.h
Comment thread runtime-light/coroutine/shared-task.h Outdated
Comment thread runtime-light/k2-platform/k2-api.h Outdated
Comment thread runtime-light/runtime-light.cmake Outdated
Comment thread runtime-light/stdlib/diagnostics/error-handling.h Outdated
Comment thread runtime-light/stdlib/diagnostics/error-handling.h Outdated
@astrophysik astrophysik changed the title implement async stack [k2] implement async stack May 26, 2025
@astrophysik astrophysik force-pushed the vsadokhov/async-stack branch from 66211a0 to 58e3191 Compare May 26, 2025 14:55
Comment thread runtime-light/stdlib/diagnostics/backtrace.h Outdated
Comment thread runtime-light/scheduler/scheduler.cpp Outdated
Comment thread runtime-light/coroutine/coroutine-state.h Outdated
@astrophysik astrophysik force-pushed the vsadokhov/async-stack branch from 35847e8 to f4477cd Compare May 28, 2025 09:16
Copy link
Copy Markdown
Contributor

@apolyakov apolyakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@astrophysik astrophysik merged commit b85e4bc into master May 28, 2025
7 checks passed
@astrophysik astrophysik deleted the vsadokhov/async-stack branch May 28, 2025 12:03
@astrophysik astrophysik added this to the next milestone May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

k2 Affects compiler or runtime in K2 mode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants