Skip to content

refactor: simplify allocation tracker lifecycle#543

Merged
nsavoire merged 4 commits intomainfrom
nsavoire/simplify_allocation_tracker
Apr 17, 2026
Merged

refactor: simplify allocation tracker lifecycle#543
nsavoire merged 4 commits intomainfrom
nsavoire/simplify_allocation_tracker

Conversation

@nsavoire
Copy link
Copy Markdown
Collaborator

@nsavoire nsavoire commented Apr 16, 2026

What does this PR do?

Use _instance != nullptr as the single source of truth for "tracking active", dropping the separate track_allocations atomic and the lazy remaining_bytes_initialized flag (TLS shrinks 48 -> 40 bytes). The per-thread sampling offset is now applied once in init_tl_state instead of on every first allocation.

  • Initialize the starter thread's TLS before publishing _instance to avoid a window where other threads observe a non-null instance with half-initialized state.
  • Defer pevent_munmap_event to the next allocation_tracking_init (guarded by mapfd != -1) so in-flight hooks cannot race with teardown; add default initializers to PEvent to make the guard sound.
  • Call restore_overrides() in allocation_profiling_stop to pair with setup_overrides() on start.

Possibly fixes #541

@nsavoire nsavoire requested a review from r1viollet as a code owner April 16, 2026 22:34
Copy link
Copy Markdown

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

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: 4d0f3b704e

ℹ️ 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/allocation_tracker.cc Outdated
Use `_instance != nullptr` as the single source of truth for "tracking
active", dropping the separate `track_allocations` atomic and the lazy
`remaining_bytes_initialized` flag (TLS shrinks 48 -> 40 bytes). The
per-thread sampling offset is now applied once in `init_tl_state`
instead of on every first allocation.

- Make `_instance` a `std::atomic<AllocationTracker *>` with
acquire/release ordering and route all accesses through a
`get_instance()` helper so publishing and tearing down the instance
synchronize correctly with observing threads.
- Initialize the starter thread's TLS before publishing `_instance`
to avoid a window where other threads observe a non-null instance
with half-initialized state.
- Defer `pevent_munmap_event` to the next `allocation_tracking_init`
(guarded by `mapfd != -1`) so in-flight hooks cannot race with
teardown; add default initializers to `PEvent` to make the guard
sound.
- Call `restore_overrides()` in `allocation_profiling_stop` to pair
with `setup_overrides()` on start.
@nsavoire nsavoire force-pushed the nsavoire/simplify_allocation_tracker branch from 4d0f3b7 to 51371be Compare April 17, 2026 12:58
Comment thread src/lib/dd_profiling.cc

void allocation_profiling_stop() {
if (g_state.allocation_profiling_started) {
restore_overrides();
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.

Good idea, restoring should improve performance for the user.

nsavoire and others added 2 commits April 17, 2026 14:53
Document that ddprof_stop_profiling() does not drain in-flight
allocation hooks, and that a stop/start sequence requires the caller
to quiesce allocation activity to avoid racing with ring buffer
re-init.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@r1viollet r1viollet left a comment

Choose a reason for hiding this comment

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

LGTM

@nsavoire nsavoire merged commit 3c60440 into main Apr 17, 2026
3 checks passed
@nsavoire nsavoire deleted the nsavoire/simplify_allocation_tracker branch April 17, 2026 15:55
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.

Rare crash (segmentation fault) caused by memory profiling

2 participants