From cecff1c269026f7feb628d14ce2d46f4e5d7fe54 Mon Sep 17 00:00:00 2001 From: Taegyun Kim Date: Mon, 9 Nov 2026 21:17:33 +0000 Subject: [PATCH] refactor(profiling): key native span links by execution unit SpanLinks maps a thread id to its active span. Change the key to a (domain, identifier) pair with Thread, AsyncioTask and Greenlet domains, so that later changes can give each asyncio task and greenlet its own span next to the thread's. Only thread keys are written for now: existing methods and callers change only to build a thread key, and behavior is unchanged. --- .../profiling/stack/include/span_links.hpp | 50 +++++++++--- .../profiling/stack/src/span_links.cpp | 62 +++++++-------- .../datadog/profiling/stack/src/stack.cpp | 8 +- .../profiling/stack/src/stack_renderer.cpp | 4 +- .../profiling/stack/test/test_span_links.cpp | 77 ++++++++++++------- 5 files changed, 127 insertions(+), 74 deletions(-) diff --git a/ddtrace/internal/datadog/profiling/stack/include/span_links.hpp b/ddtrace/internal/datadog/profiling/stack/include/span_links.hpp index 3a23abe3637..4cf3c86cee6 100644 --- a/ddtrace/internal/datadog/profiling/stack/include/span_links.hpp +++ b/ddtrace/internal/datadog/profiling/stack/include/span_links.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -32,6 +33,35 @@ struct Span } }; +// The kind of execution unit a link belongs to. Many asyncio tasks or gevent greenlets can run on one thread, so each +// needs its own key. The domain keeps a task's or greenlet's address from colliding with an equal thread id. +enum class SpanLinkDomain : uint8_t +{ + Thread = 0, + AsyncioTask = 1, + Greenlet = 2, +}; + +// Names one execution unit. For a thread, identifier is its PyThreadState thread id. For an asyncio task or a greenlet, +// it is id() of the object. +struct SpanLinkKey +{ + SpanLinkDomain domain; + uint64_t identifier; + + static SpanLinkKey thread(uint64_t thread_id) { return SpanLinkKey{ SpanLinkDomain::Thread, thread_id }; } + + bool operator==(const SpanLinkKey& other) const { return domain == other.domain && identifier == other.identifier; } +}; + +struct SpanLinkKeyHash +{ + std::size_t operator()(const SpanLinkKey& key) const + { + return std::hash{}(key.identifier) ^ (static_cast(key.domain) << 1U); + } +}; + class SpanLinks { public: @@ -47,10 +77,10 @@ class SpanLinks SpanLinks(SpanLinks const&) = delete; SpanLinks& operator=(SpanLinks const&) = delete; - void link_span(uint64_t thread_id, uint64_t span_id, uint64_t local_root_span_id, std::string span_type); - const std::optional get_active_span_from_thread_id(uint64_t thread_id); - void unlink_span(uint64_t thread_id); - void unlink_span(uint64_t thread_id, uint64_t expected_span_id); + void link_span(SpanLinkKey key, uint64_t span_id, uint64_t local_root_span_id, std::string span_type); + const std::optional get_active_span(SpanLinkKey key); + void unlink_span(SpanLinkKey key); + void unlink_span(SpanLinkKey key, uint64_t expected_span_id); void unlink_finished_span(uint64_t span_id); void reset(); @@ -62,9 +92,9 @@ class SpanLinks static void postfork_child(); private: - using ThreadIdSet = std::unordered_set; - using ThreadIdToSpanMap = std::unordered_map; - using SpanToThreadMap = std::unordered_map; + using KeySet = std::unordered_set; + using KeyToSpanMap = std::unordered_map; + using SpanToKeysMap = std::unordered_map; struct PendingSpanLink { @@ -72,11 +102,11 @@ class SpanLinks bool finished = false; }; - void remove_thread_locked(uint64_t thread_id); + void remove_key_locked(SpanLinkKey key); std::mutex mtx; - ThreadIdToSpanMap thread_id_to_span; - SpanToThreadMap span_to_threads; + KeyToSpanMap key_to_span; + SpanToKeysMap span_to_keys; // Protected by the GIL. This bridges lifecycle callback order to mutations that release it above. std::unordered_map pending_span_links; diff --git a/ddtrace/internal/datadog/profiling/stack/src/span_links.cpp b/ddtrace/internal/datadog/profiling/stack/src/span_links.cpp index cf1686fa260..42636713b90 100644 --- a/ddtrace/internal/datadog/profiling/stack/src/span_links.cpp +++ b/ddtrace/internal/datadog/profiling/stack/src/span_links.cpp @@ -9,64 +9,64 @@ namespace Datadog { void -SpanLinks::remove_thread_locked(uint64_t thread_id) +SpanLinks::remove_key_locked(SpanLinkKey key) { - auto thread_it = thread_id_to_span.find(thread_id); - if (thread_it == thread_id_to_span.end()) { + auto key_it = key_to_span.find(key); + if (key_it == key_to_span.end()) { return; } - const auto& span = thread_it->second; - auto span_it = span_to_threads.find(span.span_id); - if (span_it != span_to_threads.end()) { - span_it->second.erase(thread_id); + const auto& span = key_it->second; + auto span_it = span_to_keys.find(span.span_id); + if (span_it != span_to_keys.end()) { + span_it->second.erase(key); if (span_it->second.empty()) { - span_to_threads.erase(span_it); + span_to_keys.erase(span_it); } } - thread_id_to_span.erase(thread_it); + key_to_span.erase(key_it); } void -SpanLinks::link_span(uint64_t thread_id, uint64_t span_id, uint64_t local_root_span_id, std::string span_type) +SpanLinks::link_span(SpanLinkKey key, uint64_t span_id, uint64_t local_root_span_id, std::string span_type) { std::lock_guard lock(mtx); - remove_thread_locked(thread_id); + remove_key_locked(key); Span span(span_id, local_root_span_id, std::move(span_type)); - thread_id_to_span.try_emplace(thread_id, std::move(span)); + key_to_span.try_emplace(key, std::move(span)); // Index only the current span. A local root can finish before an active child, and finishing it must not remove the // child's attribution before that child finishes. - span_to_threads[span_id].insert(thread_id); + span_to_keys[span_id].insert(key); } const std::optional -SpanLinks::get_active_span_from_thread_id(uint64_t thread_id) +SpanLinks::get_active_span(SpanLinkKey key) { std::lock_guard lock(mtx); - auto it = thread_id_to_span.find(thread_id); - if (it == thread_id_to_span.end()) { + auto it = key_to_span.find(key); + if (it == key_to_span.end()) { return std::nullopt; } return it->second; } void -SpanLinks::unlink_span(uint64_t thread_id) +SpanLinks::unlink_span(SpanLinkKey key) { std::lock_guard lock(mtx); - remove_thread_locked(thread_id); + remove_key_locked(key); } void -SpanLinks::unlink_span(uint64_t thread_id, uint64_t expected_span_id) +SpanLinks::unlink_span(SpanLinkKey key, uint64_t expected_span_id) { std::lock_guard lock(mtx); - auto it = thread_id_to_span.find(thread_id); - if (it != thread_id_to_span.end() && it->second.span_id == expected_span_id) { - remove_thread_locked(thread_id); + auto it = key_to_span.find(key); + if (it != key_to_span.end() && it->second.span_id == expected_span_id) { + remove_key_locked(key); } } @@ -75,23 +75,23 @@ SpanLinks::unlink_finished_span(uint64_t span_id) { std::lock_guard lock(mtx); - auto span_it = span_to_threads.find(span_id); - if (span_it == span_to_threads.end()) { + auto span_it = span_to_keys.find(span_id); + if (span_it == span_to_keys.end()) { return; } - for (const auto thread_id : span_it->second) { - thread_id_to_span.erase(thread_id); + for (const auto& key : span_it->second) { + key_to_span.erase(key); } - span_to_threads.erase(span_it); + span_to_keys.erase(span_it); } void SpanLinks::reset() { std::lock_guard lock(mtx); - thread_id_to_span.clear(); - span_to_threads.clear(); + key_to_span.clear(); + span_to_keys.clear(); } void @@ -136,8 +136,8 @@ SpanLinks::postfork_child() // which is UB. Reconstruct the maps in place without inspecting their contents. This intentionally leaks the old // maps' heap allocations, because their possibly corrupted pointers cannot be safely traversed or freed in the // child. - new (&instance.thread_id_to_span) std::unordered_map(); - new (&instance.span_to_threads) SpanToThreadMap(); + new (&instance.key_to_span) KeyToSpanMap(); + new (&instance.span_to_keys) SpanToKeysMap(); new (&instance.pending_span_links) std::unordered_map(); } diff --git a/ddtrace/internal/datadog/profiling/stack/src/stack.cpp b/ddtrace/internal/datadog/profiling/stack/src/stack.cpp index 07bea11721d..726607f6783 100644 --- a/ddtrace/internal/datadog/profiling/stack/src/stack.cpp +++ b/ddtrace/internal/datadog/profiling/stack/src/stack.cpp @@ -162,7 +162,7 @@ stack_thread_unregister(PyObject* self, PyObject* args) Py_BEGIN_ALLOW_THREADS; Sampler::get().unregister_thread(id); - SpanLinks::get_instance().unlink_span(id); + SpanLinks::get_instance().unlink_span(SpanLinkKey::thread(id)); OriginTaskLinks::get_instance().unlink_origin_task(id); Py_END_ALLOW_THREADS; @@ -203,7 +203,7 @@ stack_link_span_impl(PyObject* self, PyObject* args, PyObject* kwargs) links.on_link_start(span_id); Py_BEGIN_ALLOW_THREADS; - links.link_span(thread_id, span_id, local_root_span_id, std::string(span_type)); + links.link_span(SpanLinkKey::thread(thread_id), span_id, local_root_span_id, std::string(span_type)); Py_END_ALLOW_THREADS; if (links.on_link_end(span_id)) { @@ -236,7 +236,7 @@ stack_unlink_span(PyObject* self, PyObject* args) uint64_t thread_id = state->thread_id; Py_BEGIN_ALLOW_THREADS; - SpanLinks::get_instance().unlink_span(thread_id, expected_span_id); + SpanLinks::get_instance().unlink_span(SpanLinkKey::thread(thread_id), expected_span_id); Py_END_ALLOW_THREADS; Py_RETURN_NONE; @@ -254,7 +254,7 @@ stack_clear_span(PyObject* self, PyObject* args) } Py_BEGIN_ALLOW_THREADS; - SpanLinks::get_instance().unlink_span(state->thread_id); + SpanLinks::get_instance().unlink_span(SpanLinkKey::thread(state->thread_id)); Py_END_ALLOW_THREADS; Py_RETURN_NONE; diff --git a/ddtrace/internal/datadog/profiling/stack/src/stack_renderer.cpp b/ddtrace/internal/datadog/profiling/stack/src/stack_renderer.cpp index 631e6249881..781a3306fc6 100644 --- a/ddtrace/internal/datadog/profiling/stack/src/stack_renderer.cpp +++ b/ddtrace/internal/datadog/profiling/stack/src/stack_renderer.cpp @@ -59,7 +59,7 @@ StackRenderer::render_thread_begin(PyThreadState* tstate, sample->push_threadinfo(static_cast(thread_id), static_cast(native_id), name); sample->push_walltime(thread_state.wall_time_ns, 1); - const std::optional active_span = SpanLinks::get_instance().get_active_span_from_thread_id(thread_id); + const std::optional active_span = SpanLinks::get_instance().get_active_span(SpanLinkKey::thread(thread_id)); if (active_span) { sample->push_span_id(active_span->span_id); sample->push_local_root_span_id(active_span->local_root_span_id); @@ -113,7 +113,7 @@ StackRenderer::render_task_begin(std::string_view task_name, // We also want to make sure the tid -> span_id mapping is present in the sample for the task const std::optional active_span = - SpanLinks::get_instance().get_active_span_from_thread_id(thread_state.id); + SpanLinks::get_instance().get_active_span(SpanLinkKey::thread(thread_state.id)); if (active_span) { sample->push_span_id(active_span->span_id); sample->push_local_root_span_id(active_span->local_root_span_id); diff --git a/ddtrace/internal/datadog/profiling/stack/test/test_span_links.cpp b/ddtrace/internal/datadog/profiling/stack/test/test_span_links.cpp index c6d863b5e0e..60fbc4c4031 100644 --- a/ddtrace/internal/datadog/profiling/stack/test/test_span_links.cpp +++ b/ddtrace/internal/datadog/profiling/stack/test/test_span_links.cpp @@ -9,6 +9,8 @@ #include #include +using Datadog::SpanLinkKey; + static void get() { @@ -17,7 +19,7 @@ get() for (int j = 0; j < i; j++) { span_type.append("a"); } - Datadog::SpanLinks::get_instance().link_span(42, 1, 2, span_type); + Datadog::SpanLinks::get_instance().link_span(SpanLinkKey::thread(42), 1, 2, span_type); } } @@ -26,7 +28,7 @@ set() { std::string s; for (int i = 0; i < 100; i++) { - auto thing = Datadog::SpanLinks::get_instance().get_active_span_from_thread_id(42); + auto thing = Datadog::SpanLinks::get_instance().get_active_span(SpanLinkKey::thread(42)); if (!thing) { continue; } @@ -49,13 +51,13 @@ TEST(SpanLinks, UnlinkOnlyMatchingSpan) constexpr uint64_t thread_id = 43; constexpr uint64_t span_id = 100; - links.link_span(thread_id, span_id, 200, "web"); - links.unlink_span(thread_id, span_id + 1); + links.link_span(SpanLinkKey::thread(thread_id), span_id, 200, "web"); + links.unlink_span(SpanLinkKey::thread(thread_id), span_id + 1); - EXPECT_EQ(links.get_active_span_from_thread_id(thread_id), Datadog::Span(span_id, 200, "web")); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(thread_id)), Datadog::Span(span_id, 200, "web")); - links.unlink_span(thread_id, span_id); - EXPECT_EQ(links.get_active_span_from_thread_id(thread_id), std::nullopt); + links.unlink_span(SpanLinkKey::thread(thread_id), span_id); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(thread_id)), std::nullopt); } TEST(SpanLinks, ClearFinished) @@ -74,7 +76,7 @@ TEST(SpanLinks, ClearFinished) // Call link_span with the thread ids for (auto thread_id : thread_ids) { - Datadog::SpanLinks::get_instance().link_span(thread_id, thread_id, thread_id, "test"); + Datadog::SpanLinks::get_instance().link_span(SpanLinkKey::thread(thread_id), thread_id, thread_id, "test"); } std::unordered_set finished_threads; @@ -83,14 +85,14 @@ TEST(SpanLinks, ClearFinished) for (auto thread_id : thread_ids) { if (real_dis(gen) < 0.5) { finished_threads.insert(thread_id); - Datadog::SpanLinks::get_instance().unlink_span(thread_id); + Datadog::SpanLinks::get_instance().unlink_span(SpanLinkKey::thread(thread_id)); } } // Check that the unseen ids are removed for (auto thread_id : thread_ids) { std::optional span_opt = - Datadog::SpanLinks::get_instance().get_active_span_from_thread_id(thread_id); + Datadog::SpanLinks::get_instance().get_active_span(SpanLinkKey::thread(thread_id)); if (finished_threads.find(thread_id) == finished_threads.end()) { EXPECT_EQ(span_opt, Datadog::Span(thread_id, thread_id, "test")); @@ -105,21 +107,21 @@ TEST(SpanLinks, UnlinkFinishedSpanAcrossThreads) auto& links = Datadog::SpanLinks::get_instance(); links.reset(); - links.link_span(100, 1000, 1000, "web"); - links.link_span(101, 1001, 1000, "web"); - links.link_span(102, 1001, 1000, "web"); - links.link_span(103, 2001, 2000, "worker"); + links.link_span(SpanLinkKey::thread(100), 1000, 1000, "web"); + links.link_span(SpanLinkKey::thread(101), 1001, 1000, "web"); + links.link_span(SpanLinkKey::thread(102), 1001, 1000, "web"); + links.link_span(SpanLinkKey::thread(103), 2001, 2000, "worker"); links.unlink_finished_span(1000); - EXPECT_EQ(links.get_active_span_from_thread_id(100), std::nullopt); - EXPECT_EQ(links.get_active_span_from_thread_id(101), Datadog::Span(1001, 1000, "web")); - EXPECT_EQ(links.get_active_span_from_thread_id(102), Datadog::Span(1001, 1000, "web")); - EXPECT_EQ(links.get_active_span_from_thread_id(103), Datadog::Span(2001, 2000, "worker")); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(100)), std::nullopt); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(101)), Datadog::Span(1001, 1000, "web")); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(102)), Datadog::Span(1001, 1000, "web")); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(103)), Datadog::Span(2001, 2000, "worker")); links.unlink_finished_span(1001); - EXPECT_EQ(links.get_active_span_from_thread_id(101), std::nullopt); - EXPECT_EQ(links.get_active_span_from_thread_id(102), std::nullopt); - EXPECT_EQ(links.get_active_span_from_thread_id(103), Datadog::Span(2001, 2000, "worker")); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(101)), std::nullopt); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(102)), std::nullopt); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(103)), Datadog::Span(2001, 2000, "worker")); } TEST(SpanLinks, RelinkUpdatesFinishedSpanIndex) @@ -127,13 +129,34 @@ TEST(SpanLinks, RelinkUpdatesFinishedSpanIndex) auto& links = Datadog::SpanLinks::get_instance(); links.reset(); - links.link_span(201, 3001, 3000, "old"); - links.link_span(201, 4001, 4000, "new"); + links.link_span(SpanLinkKey::thread(201), 3001, 3000, "old"); + links.link_span(SpanLinkKey::thread(201), 4001, 4000, "new"); links.unlink_finished_span(3000); - EXPECT_EQ(links.get_active_span_from_thread_id(201), Datadog::Span(4001, 4000, "new")); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(201)), Datadog::Span(4001, 4000, "new")); links.unlink_finished_span(4001); - EXPECT_EQ(links.get_active_span_from_thread_id(201), std::nullopt); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(201)), std::nullopt); +} + +TEST(SpanLinks, KeysWithEqualIdentifiersInDifferentDomainsAreSeparate) +{ + auto& links = Datadog::SpanLinks::get_instance(); + constexpr uint64_t identifier = 401; + const SpanLinkKey thread = SpanLinkKey::thread(identifier); + const SpanLinkKey task{ Datadog::SpanLinkDomain::AsyncioTask, identifier }; + links.reset(); + + links.link_span(thread, 4001, 4000, "web"); + links.link_span(task, 4002, 4000, "web"); + EXPECT_EQ(links.get_active_span(thread), Datadog::Span(4001, 4000, "web")); + EXPECT_EQ(links.get_active_span(task), Datadog::Span(4002, 4000, "web")); + + links.unlink_span(thread); + EXPECT_EQ(links.get_active_span(thread), std::nullopt); + EXPECT_EQ(links.get_active_span(task), Datadog::Span(4002, 4000, "web")); + + links.unlink_finished_span(4002); + EXPECT_EQ(links.get_active_span(task), std::nullopt); } TEST(SpanLinks, FinishingWhileLinkingDefersCleanup) @@ -146,11 +169,11 @@ TEST(SpanLinks, FinishingWhileLinkingDefersCleanup) links.on_link_start(span_id); EXPECT_FALSE(links.on_span_finish(span_id)); - links.link_span(thread_id, span_id, span_id, "web"); + links.link_span(SpanLinkKey::thread(thread_id), span_id, span_id, "web"); EXPECT_TRUE(links.on_link_end(span_id)); links.unlink_finished_span(span_id); - EXPECT_EQ(links.get_active_span_from_thread_id(thread_id), std::nullopt); + EXPECT_EQ(links.get_active_span(SpanLinkKey::thread(thread_id)), std::nullopt); } int