Ray's Knowledge Base
Unlock

A dlopen on the data path under GOTCHA re-binds every wrapped symbol on the app's thread

PitfallVerified 2 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A preloaded tracer that uses GOTCHA wrappers slows the application far more than its own per-call work explains. Moving the tracer's formatting off the app thread and removing its locks changes nothing. A profile cut to the application's measured phase shows the app's main thread in libgotcha symbol lookup (lookup_gnu_hash_symbol, strhash, lookup_exported_symbol, lookup_hashtable) and in the dynamic loader, while a variant that skips the tracer's work after the hook shows none.

A second form: a library the application uses (not the tracer) calls dlopen(..., RTLD_NOLOAD) or reopens a library that is already loaded, and each call costs about 1.7 ms. GOTCHA_DEBUG=1 prints "User called dlopen" for each one.

Cause#

GOTCHA wraps dlopen itself (src/gotcha_dl.c, dlopen_wrapper, in 1.0.10 and on master). On every call it calls the real dlopen, then retries prepare_symbol for every binding not found yet (notfound_binding_table), then rewrites GOTs. It does this even for RTLD_NOLOAD and for a library that is already loaded. The GOT rewrite skips libraries already at the current generation, so the cost is the retry of the not-found bindings. A shim that wraps thousands of names, most of them in libraries never loaded, pays that on every dlopen.

So there are two sources. A tracer that resolves a library lazily (for example dlopen("libmlx5.so.1", RTLD_NOLOAD) and dlsym on its first completion) pays it on the application's thread, in the middle of the measured phase. And a third-party library can call dlopen inside the measured phase, which the tracer cannot move. A profile of the whole process hides both: startup re-binds dominate the samples in every variant alike.

Fix#

Do every dlopen, dlsym and first device-clock read of the tracer at setup (where the tracer registers the object, such as at CQ creation), never on the first data-path call:

// in register_cq, once per CQ at setup
if (auto* get = cqe_clock::resolve()) {
  struct mlx5dv_clock_info ci {};
  (void)get(context, &ci);
}

For dlopen calls the tracer does not own, wrap dlopen in front of GOTCHA's own wrapper. Bind it as a separate GOTCHA tool with a higher priority than GOTCHA's internal one (which uses the unset priority, -1). In the wrapper, call the real dlopen, then compare glibc's count of loaded objects (dlpi_adds from dl_iterate_phdr) with the count at the last rescan. If it did not move, return the handle at once. If it moved, reopen the same name through GOTCHA's chain with flags | RTLD_NOLOAD, so GOTCHA rescans once and maps nothing new. Get the real dlopen with dlvsym(RTLD_NEXT, "dlopen", "GLIBC_2.34") (then 2.2.5, then 2.17): GOTCHA's dlsym returns a wrapper for "dlopen", so dlsym(RTLD_NEXT) can return your own wrapper.

A narrow binding list (only the names the workload calls) also removes the cost, but per workload.

To find it, profile only the data phase: record with call stacks, then slice the samples by time to the measured window and split them by thread (perf script -F tid,time,ip,sym,dso -G).

Evidence#

dpucrumbs' ibverbs completion shim on a BlueField-3 RDMA proxy (buddy). A whole-run profile showed about 77 percent GOTCHA lookup in both the full shim and a hook-only variant. Cut to the 1 s data phase, the proxy's main thread had about 78 GOTCHA samples at 2999 Hz (about 26 ms) with the full shim and 0 in the hook-only variant. After moving the resolve and the first clock read to CQ setup, the default cell's median went from 0.170 s to 0.144 s (untraced 0.119 s), with all 100 message links still matched.

VPIC/BDCATS with an HDF5 deflate filter on the BF3 engine, one rank, 2124 shim bindings: three dlopen calls in the timed read (HDF5 reopening its preloaded plugin, DOCA's dlopen(libibverbs.so.1, RTLD_NOLOAD) and dlopen(libmlx5.so.1, RTLD_NOLOAD) in device open) cost about 50 ms per process, +5.8 percent. A 86-name binding list gave -0.2 percent. The dlopen guard with the full list gave -0.2 percent [-1.7, +2.3], the same records, and a test of 20 reopens and one NOLOAD call showed 0 rescans (1.70 ms each before).

Also found by searching

  • lookup_gnu_hash_symbol strhash hot in perf of a traced process
  • interposition shim slows the app far more than its per-call work explains
  • how do I keep a GOTCHA-based tracer cheap on the data path
  • first completion is slow under an LD_PRELOAD tracer
  • dlopen with RTLD_NOLOAD takes milliseconds under GOTCHA
  • GOTCHA_DEBUG shows "User called dlopen" for a library that is already loaded