Repository navigation
chore(profiling): read span labels from a contextvars.Context without the GIL - #22109
taegyunkim wants to merge 1 commit into
Conversation
… the GIL Add an Echion reader that finds the tracer's context variable in a contextvars.Context by walking CPython's HAMT from raw memory, resolves the active span the way DefaultContextProvider.active() does, and computes the labels StackCollector._link_span attaches. It uses Echion's fault-safe copy, does no allocation, locking or Python calls, and bounds every walk. The HAMT node types are found at startup from live objects, and the reader stays disabled if the layout does not check out. The reader is not used by the sampler yet. Test-only bindings expose it so Python tests can compare it with the tracer.
Dependency direction analysis
|
Codeowners resolved asResolved from the full PR diff against |
BenchmarksBenchmark execution time: 2026-11-26 00:22:44 Comparing candidate commit 74fa5f1 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 593 metrics, 10 unstable metrics, 1 flaky benchmarks without significant changes.
|
Description
A step toward attributing asyncio task samples to the task's own span (PROF-16348). Stacked on #22069.
This adds an Echion reader that finds
DD_CONTEXTVARin acontextvars.Contextby walking CPython's HAMT from raw memory. It resolves the active span the wayDefaultContextProvider.active()does, and computes the labelsStackCollector._link_spanattaches. It uses Echion's fault-safe copy and does no allocation, locking or Python calls, so the CPU timer's signal handler can later reuse it with a different copy function. Every walk is bounded.CPython doesn't export its HAMT node types, so
stack.start()finds them from live Contexts, which also checks the struct mirrors. On any mismatch the reader stays disabled.The sampler doesn't use the reader yet. Test-only bindings expose it so Python tests can compare it with the tracer.
Customer impact
None. The reader isn't used for samples yet.
Testing
test_stack_context_reader.pycompares the reader withactive()and_link_spanacross fixed scenarios and 300 random span trees, on 3.9 to 3.15.Risks
None for samples. At
stack.start(), the startup check creates a few dozen ContextVars in private Contexts.Additional Notes
A span id of 0 gives no labels, unlike
_link_span.