Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(profiling): remove slow getpid call from memalloc path [backport 2.19] #11964

Merged
merged 1 commit into from
Jan 15, 2025

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Jan 15, 2025

Backport 6bfe77e from #11848 to 2.19.

memalloc uses getpid to detect whether the process has forked, so that
we can unlock the memalloc lock in the child process (if it isn't
already locked). Unfortunately the getpid call is quite slow. From the
man page: "calls to getpid() always invoke the actual system call,
rather than returning a cached value." Furthermore, we always attempt
to take the lock for allocations, even if we aren't going to sample
them. So this is basically adding a syscall to every allocation.

Move this logic out of the allocation path. Switch to using
pthread_atfork handlers to ensure that the lock is held prior to
forking, and unlock it in the parent and child after forking. This
(maybe) has the added benefit of making sure the data structures are in
a consistent state in the child process after forking. Unclear if that's
an issue prior to this change, though. I may be missing some code that
resets the profiler on fork anyway?

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

memalloc uses getpid to detect whether the process has forked, so that
we can unlock the memalloc lock in the child process (if it isn't
already locked). Unfortunately the getpid call is quite slow. From the
man page: "calls to getpid() always invoke the actual system call,
rather than returning a cached value." Furthermore, we _always_ attempt
to take the lock for allocations, even if we aren't going to sample
them. So this is basically adding a syscall to every allocation.

Move this logic out of the allocation path. Switch to using
pthread_atfork handlers to ensure that the lock is held prior to
forking, and unlock it in the parent and child after forking. This
(maybe) has the added benefit of making sure the data structures are in
a consistent state in the child process after forking. Unclear if that's
an issue prior to this change, though. I may be missing some code that
resets the profiler on fork anyway?

(cherry picked from commit 6bfe77e)
@github-actions github-actions bot requested review from a team as code owners January 15, 2025 18:17
@github-actions github-actions bot requested review from wantsui and sabrenner January 15, 2025 18:17
@taegyunkim taegyunkim closed this Jan 15, 2025
@taegyunkim taegyunkim reopened this Jan 15, 2025
@taegyunkim taegyunkim enabled auto-merge (squash) January 15, 2025 18:18
Copy link
Contributor Author

CODEOWNERS have been resolved as:

releasenotes/notes/profiling-remove-getpid-from-memalloc-74f54043accdfc9e.yaml  @DataDog/apm-python
ddtrace/profiling/collector/_memalloc.c                                 @DataDog/profiling-python
ddtrace/profiling/collector/_memalloc_heap.c                            @DataDog/profiling-python
ddtrace/profiling/collector/_memalloc_reentrant.h                       @DataDog/profiling-python

Copy link
Contributor

@nsrip-dd nsrip-dd left a comment

Choose a reason for hiding this comment

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

Thanks!

@pr-commenter
Copy link

pr-commenter bot commented Jan 15, 2025

Benchmarks

Benchmark execution time: 2025-01-15 18:59:28

Comparing candidate commit fd6f4b4 in PR branch backport-11848-to-2.19 with baseline commit e24bae1 in branch 2.19.

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

@taegyunkim taegyunkim merged commit 35b6889 into 2.19 Jan 15, 2025
255 checks passed
@taegyunkim taegyunkim deleted the backport-11848-to-2.19 branch January 15, 2025 19:00
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