Skip to content

Commit 9344867

Browse files
authored
refactor(profiling): split stack sampler helpers (#18975)
## Description Pure refactor of existing stack sampler internals, with no intended behavior or internal contract changes, in preparation for timer_create based cpu time profiling in #18724 Specifically: - Extracts the existing task/greenlet/thread-stack rendering branch in `ThreadInfo::sample()` into a local `render_unwound_stacks()` helper. - Extracts duplicate one-time thread registration failure logging in `Sampler::register_thread()` into a local helper. This does not introduce CPU timer profiling, new configuration, new native bindings, or changes to thread registration semantics. ## Testing - `scripts/lint cformat` Attempted a targeted profiling test run from the worktree, but the test runner failed during riot venv setup before executing tests due the worktree using `/home/bits/project/.riot`, which was not writable/available in that context. ## Risks Low. This is intended to be behavior-preserving refactoring only. ## Additional Notes No changelog needed, internal refactor only. Co-authored-by: taegyun.kim <taegyun.kim@datadoghq.com>
1 parent 44a33e5 commit 9344867

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

ddtrace/internal/datadog/profiling/stack/echion/echion/threads.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class ThreadInfo
111111
};
112112

113113
private:
114+
void render_unwound_stacks(EchionSampler&);
114115
[[nodiscard]] Result<void> unwind_tasks(EchionSampler&, PyThreadState*);
115116
void unwind_greenlets(EchionSampler&, PyThreadState*, unsigned long);
116117
[[nodiscard]] Result<std::vector<TaskInfo::Ptr>> get_all_tasks(EchionSampler&, PyThreadState* tstate);

ddtrace/internal/datadog/profiling/stack/src/echion/threads.cc

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -708,21 +708,10 @@ ThreadInfo::unwind_greenlets(EchionSampler& echion, PyThreadState* tstate, unsig
708708
}
709709

710710
// ----------------------------------------------------------------------------
711-
Result<void>
712-
ThreadInfo::sample(EchionSampler& echion, PyThreadState* tstate, microsecond_t delta)
711+
void
712+
ThreadInfo::render_unwound_stacks(EchionSampler& echion)
713713
{
714714
auto& renderer = echion.renderer();
715-
renderer.render_thread_begin(tstate, name, delta, thread_id, native_id);
716-
717-
microsecond_t previous_cpu_time = cpu_time;
718-
auto update_cpu_time_success = update_cpu_time();
719-
if (!update_cpu_time_success) {
720-
return ErrorKind::CpuTimeError;
721-
}
722-
723-
renderer.render_cpu_time(cpu_time - previous_cpu_time);
724-
725-
this->unwind(echion, tstate);
726715

727716
// Render in this order of priority
728717
// 1. asyncio Tasks stacks (if any)
@@ -755,6 +744,25 @@ ThreadInfo::sample(EchionSampler& echion, PyThreadState* tstate, microsecond_t d
755744
python_stack.render(echion);
756745
renderer.render_stack_end();
757746
}
747+
}
748+
749+
// ----------------------------------------------------------------------------
750+
Result<void>
751+
ThreadInfo::sample(EchionSampler& echion, PyThreadState* tstate, microsecond_t delta)
752+
{
753+
auto& renderer = echion.renderer();
754+
renderer.render_thread_begin(tstate, name, delta, thread_id, native_id);
755+
756+
microsecond_t previous_cpu_time = cpu_time;
757+
auto update_cpu_time_success = update_cpu_time();
758+
if (!update_cpu_time_success) {
759+
return ErrorKind::CpuTimeError;
760+
}
761+
762+
renderer.render_cpu_time(cpu_time - previous_cpu_time);
763+
764+
this->unwind(echion, tstate);
765+
this->render_unwound_stacks(echion);
758766

759767
return Result<void>::ok();
760768
}

0 commit comments

Comments
 (0)