Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 40 additions & 10 deletions ddtrace/internal/datadog/profiling/stack/include/span_links.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstddef>
#include <functional>
#include <mutex>
#include <optional>
#include <stdint.h>
Expand Down Expand Up @@ -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<uint64_t>{}(key.identifier) ^ (static_cast<std::size_t>(key.domain) << 1U);
}
};

class SpanLinks
{
public:
Expand All @@ -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<Span> 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<Span> 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();

Expand All @@ -62,21 +92,21 @@ class SpanLinks
static void postfork_child();

private:
using ThreadIdSet = std::unordered_set<uint64_t>;
using ThreadIdToSpanMap = std::unordered_map<uint64_t, Span>;
using SpanToThreadMap = std::unordered_map<uint64_t, ThreadIdSet>;
using KeySet = std::unordered_set<SpanLinkKey, SpanLinkKeyHash>;
using KeyToSpanMap = std::unordered_map<SpanLinkKey, Span, SpanLinkKeyHash>;
using SpanToKeysMap = std::unordered_map<uint64_t, KeySet>;
Comment thread
taegyunkim marked this conversation as resolved.
Resolved

struct PendingSpanLink
{
size_t count = 0;
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<uint64_t, PendingSpanLink> pending_span_links;
Expand Down
62 changes: 31 additions & 31 deletions ddtrace/internal/datadog/profiling/stack/src/span_links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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<Span>
SpanLinks::get_active_span_from_thread_id(uint64_t thread_id)
SpanLinks::get_active_span(SpanLinkKey key)
{
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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);
}
}

Expand All @@ -75,23 +75,23 @@ SpanLinks::unlink_finished_span(uint64_t span_id)
{
std::lock_guard<std::mutex> 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<std::mutex> lock(mtx);
thread_id_to_span.clear();
span_to_threads.clear();
key_to_span.clear();
span_to_keys.clear();
}

void
Expand Down Expand Up @@ -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<uint64_t, Span>();
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<uint64_t, PendingSpanLink>();
}

Expand Down
8 changes: 4 additions & 4 deletions ddtrace/internal/datadog/profiling/stack/src/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ StackRenderer::render_thread_begin(PyThreadState* tstate,
sample->push_threadinfo(static_cast<int64_t>(thread_id), static_cast<int64_t>(native_id), name);
sample->push_walltime(thread_state.wall_time_ns, 1);

const std::optional<Span> active_span = SpanLinks::get_instance().get_active_span_from_thread_id(thread_id);
const std::optional<Span> 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);
Expand Down Expand Up @@ -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<Span> 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);
Expand Down
Loading
Loading