Skip to content

fix: allow lazy TLS init for threads started before profiling#531

Merged
nsavoire merged 3 commits into
mainfrom
nsavoire/allow_lazy_tls_init
Apr 2, 2026
Merged

fix: allow lazy TLS init for threads started before profiling#531
nsavoire merged 3 commits into
mainfrom
nsavoire/allow_lazy_tls_init

Conversation

@nsavoire

Copy link
Copy Markdown
Collaborator

What does this PR do?

When profiling starts on a process with already-running threads calling pthread_getattr_np, the allocation tracker's TLS state may not be initialized yet. This adds a pthread_getattr_np hook that guards against deadlock withthe following sequence:
(pthread_getattr_np -> malloc -> tracker -> init_tl_state -> save_context -> pthread_getattr_np deadlock) and enables lazy TLS init in get_tl_state() when safe.

Motivation

Allow allocation profiling of existing threads when profiler is not started at process startup.

@nsavoire
nsavoire force-pushed the nsavoire/allow_lazy_tls_init branch 2 times, most recently from 5ae34ef to 780ea42 Compare March 31, 2026 09:20
@pr-commenter

pr-commenter Bot commented Mar 31, 2026

Copy link
Copy Markdown

Benchmark results for collatz

Parameters

Baseline Candidate
config baseline candidate
profiler-version ddprof 0.24.0+978f4be5.105195233 ddprof 0.24.0+780ea42a.105213418

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 1 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean execution_time
scenario:ddprof -S bench-collatz --preset cpu_only collatz_runner.sh same

@pr-commenter

pr-commenter Bot commented Mar 31, 2026

Copy link
Copy Markdown

Benchmark results for BadBoggleSolver_run

Parameters

Baseline Candidate
config baseline candidate
profiler-version ddprof 0.24.0+978f4be5.105195233 ddprof 0.24.0+780ea42a.105213418

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 1 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean execution_time
scenario:ddprof -S bench-bad-boggle-solver BadBoggleSolver_run work 1000 same

Comment thread src/lib/allocation_tracker.cc
@r1viollet

Copy link
Copy Markdown
Collaborator

Nicolas had a smart idea, the scenario we are worried about is doing an init while we already have a lock (allocation) held. This can happens when you are doing an mmap within a malloc (especially as we can not hook all mallocs).
His proposal was to only perform the init on APIs where it is impossible for us to hold a lock (like inside a malloc hook).
This is a great idea!

When profiling starts on a process with already-running threads calling
pthread_getattr_np, the allocation tracker's TLS state may not be
initialized yet. This adds a pthread_getattr_np hook that guards against
deadlock withthe following sequence:
(pthread_getattr_np -> malloc -> tracker -> init_tl_state -> save_context
-> pthread_getattr_np deadlock) and enables lazy TLS init in get_tl_state()
when safe.
@nsavoire
nsavoire force-pushed the nsavoire/allow_lazy_tls_init branch from 780ea42 to a9d5823 Compare March 31, 2026 16:57
@nsavoire
nsavoire marked this pull request as ready for review April 1, 2026 12:47

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a9d5823d65

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/lib/symbol_overrides.cc Outdated
Comment thread src/lib/allocation_tracker.cc
Comment thread src/lib/symbol_overrides.cc Outdated
// malloc implementation and cause a deadlock when pthread_getattr_np (which
// can call malloc) is called by tls state initialization.
AllocTrackerHelperImpl()
: _tl_state{ddprof::AllocationTracker::get_tl_state(mmap_alloc)},

@r1viollet r1viollet Apr 1, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

is the point not to have get_tl_state(!mmap_alloc)
I guess this is what codex is saying ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

oups,indeed I inverted the condition

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

the fact that tests are still OK with this inverted is strange ? Or just lucky ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

that's because there is no test for "mmap hook called inside malloc without malloc hook being called"
I added one

@nsavoire
nsavoire force-pushed the nsavoire/allow_lazy_tls_init branch from 9e6c139 to cfb933a Compare April 1, 2026 14:58
@nsavoire
nsavoire requested a review from r1viollet April 1, 2026 15:04
Comment thread test/allocation_tracker_fork_test.cc Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit, this requires false ? to avoid running init in child

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I changed all get_tl_state() to get_tl_state(false) to ensure that the behaviour remains the same in the test.

r1viollet
r1viollet previously approved these changes Apr 1, 2026

@r1viollet r1viollet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@r1viollet r1viollet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for adjusting tests.
LGTM

@nsavoire
nsavoire merged commit c5fb258 into main Apr 2, 2026
3 checks passed
@nsavoire
nsavoire deleted the nsavoire/allow_lazy_tls_init branch April 2, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants