fix: cache ELF file descriptors per file, not per process#528
Open
fix: cache ELF file descriptors per file, not per process#528
Conversation
Benchmark results for collatzParameters
SummaryFound 0 performance improvements and 0 performance regressions! Performance is the same for 1 metrics, 0 unstable metrics. See unchanged results
|
Benchmark results for BadBoggleSolver_runParameters
SummaryFound 0 performance improvements and 0 performance regressions! Performance is the same for 1 metrics, 0 unstable metrics. See unchanged results
|
When profiling many forked processes sharing the same ELF files (e.g. libc), report_module() was opening a fresh fd for each process's Dwfl session. With N forks and M shared libraries, this resulted in N*M open file descriptors. Fix: add _fd_cache (unordered_map<FileInfoId_t, UniqueFd>) to DsoHdr, which is shared across all processes in UnwindState. The first time an ELF file is registered, it is opened and cached. Subsequent processes dup() the cached fd for dwfl ownership transfer. elfutils uses MAP_PRIVATE mmaps so the OS page cache already deduplicates physical pages; the dup() avoids redundant open() syscalls and fd exhaustion. Add STATS_ELF_FDS metric (elf.fds) to track the fd cache size each profiling cycle, observable in the notice-level stats output. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
4519337 to
95e24c7
Compare
r1viollet
commented
Mar 27, 2026
| // dup with O_CLOEXEC preserved: plain dup() drops close-on-exec, which | ||
| // would leak ELF fds into exec'd children. F_DUPFD_CLOEXEC atomically | ||
| // duplicates and sets FD_CLOEXEC, matching the original open(O_CLOEXEC). | ||
| UniqueFd fd_holder{::fcntl(cached_fd.get(), F_DUPFD_CLOEXEC, 0)}; |
Collaborator
Author
There was a problem hiding this comment.
not very useful to add this (cloexec)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
When profiling many forked processes sharing the same ELF files (e.g. libc), report_module() was opening a fresh fd for each process's Dwfl session. With N forks and M shared libraries, this resulted in N*M open file descriptors.
Fix: add _fd_cache (unordered_map<FileInfoId_t, UniqueFd>) to DsoHdr, which is shared across all processes in UnwindState. The first time an ELF file is registered, it is opened and cached. Subsequent processes dup() the cached fd for dwfl ownership transfer. elfutils uses MAP_PRIVATE mmaps so the OS page cache already deduplicates physical pages; the dup() avoids redundant open() syscalls and fd exhaustion.
Add STATS_ELF_FDS metric (elf.fds) to track the fd cache size each profiling cycle, observable in the notice-level stats output.
Motivation
@bwoebi reported an issue with forking processes
Additional Notes
NA
How to test the change?
I straced a process that was forking before and after the fix: