perkolatte opened a new pull request, #71114:
URL: https://github.com/apache/doris/pull/71114
### Background
`be/src/common/phdr_cache.cpp` interposes the process-wide
`dl_iterate_phdr`. Inside a `ScopedPHDRCacheRead` (the stack-trace signal
handler), it reads a lock-free snapshot. Everywhere else it forwards to glibc's
original function, which it looks up with `dlsym(RTLD_NEXT,
"dl_iterate_phdr")`. Ordinary C++ exception unwinding takes the forwarding
path, because `_Unwind_Find_FDE` calls `dl_iterate_phdr`.
### What problem does this PR solve?
Issue Number: close #71064
Related PR: #64093 (enabled the interposer), #65428 (startup deadlock
through the same `dlsym`)
PR Type: Performance
Problem Summary:
The forwarding path calls `dlsym` every time and never keeps the result.
Each C++ exception therefore pays for a symbol lookup that takes glibc's loader
lock and allocates, plus the original function's own lock. Threads that throw
concurrently serialize on that lock. The issue reports a dashboard query with a
constant `REGEXP` predicate that became about 4x slower after the interposer
was enabled. Hyperscan compiles the pattern once per segment and uses
exceptions as control flow, so the query throws thousands of them. This PR
resolves the original function once and forwards through the cached pointer.
### Details
- **Why it was slow:** `getOriginalDLIteratePHDR()` ran `dlsym(RTLD_NEXT,
...)` on every call. `dlsym` takes `dl_load_lock` and allocates, so every
exception unwind paid for a lookup and an extra global lock.
- **Optimization:** The resolved pointer is kept in a
`std::atomic<DLIterateFunction>`. The first call stores it, and later calls
load it. `updatePHDRCache()`, which `doris_main` runs at startup, already goes
through this function, so the pointer is resolved before worker threads exist.
I used an atomic rather than a function-local `static` on purpose. `dlsym` can
re-enter the interposer (the #65428 chain, through jemalloc profiling), and
re-entering a static initializer would deadlock. With the atomic, a re-entrant
or concurrent first call just resolves the same address again, as it does today.
- **Results:** A standalone benchmark throws and catches exceptions from N
threads. It is built with the real `phdr_cache.cpp` from master and from this
branch, with `-rdynamic` so `_Unwind_Find_FDE` reaches the interposer, and with
`-Wl,--wrap=dlsym` to count lookups. It ran in
`apache/doris:build-env-ldb-toolchain-latest` (clang 20, glibc 2.17), 20,000
throws per thread, after one `updatePHDRCache()`:
| threads | `dlsym` calls, master | `dlsym` calls, this PR | time, master
| time, this PR |
|---:|---:|---:|---:|---:|
| 1 | 140,000 | 0 | 0.028 s | 0.020 s |
| 8 | 1,120,000 | 0 | 0.404 s | 0.185 s |
| 32 | 4,480,000 | 0 | 2.198 s | 0.941 s |
On master, each exception costs 7 `dlsym` calls. With this PR it costs
none. The host was an 8-vCPU VM under x86 emulation, so only the direction and
the scaling with thread count matter, not the absolute times.
- **Applicability and cost:** This helps every exception unwind outside
`ScopedPHDRCacheRead`, not just Hyperscan. The cost is one atomic load per
call. The cached value is glibc's function, not a snapshot, so ordinary callers
still see the live loader list. ASAN builds do not compile the interposer.
### Testing
- `be/test/common/phdr_cache_test.cpp`
(`PhdrCacheTest.DefaultLoaderViewIsLiveWhileScopedViewUsesSnapshot`) checks
that the default path still sees a DSO opened after the first call, and that
the scoped path uses the snapshot. I compiled it standalone with the real
`phdr_cache.cpp` and the image's prebuilt gtest, in the image above. It passes
with both the master file and this branch.
- The benchmark above, run locally, shows the `dlsym` count dropping from 7
per throw to 0.
- `clang-format` 16.0.6: no changes.
- I did not run the full BE build or `run-be-ut.sh`.
### Notes for Reviewers
This does not claim to fix #65428. If the first `dl_iterate_phdr` call comes
from jemalloc initialization before `updatePHDRCache()` runs, that call still
goes through `dlsym` as it does today. After startup the hot path no longer
calls `dlsym` at all.
### Release note
Fix a BE slowdown, introduced when the `dl_iterate_phdr` interposer was
enabled, in queries that throw many C++ exceptions across scanner threads, such
as constant `REGEXP` patterns compiled by Hyperscan.
### Check List (For Author)
- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Partially verified: the full BE build and
`run-be-ut.sh` were not run. The changed file and its unit test were compiled
and run standalone in the Doris build image.
### AI Disclosure
- Tool: Claude Code (Claude Opus 5.5)
- Generated: code, the verification harness, this description
- Human review: not stated
- Prompts: The author asked for unclaimed Doris issues to work on and chose
this one.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]