How to load all symbols from shared library on start up?

1.5k views Asked by At

Good day! I have a fork-on-connect daemon. After profiling it with perf tool, I found out that function "do_lookup_x" consume lot's of CPU time. All the function calls for shared-libraries functions are after fork.

Is there any way to lookup all symbols before fork?

1

There are 1 answers

1
Pynchia On BEST ANSWER

Setting the environment variable LD_BIND_NOW should help achieving just that.

Set it with

export LD_BIND_NOW=1

then execute your program.

Excerpt:

ELF platforms (Linux, Solaris, FreeBSD, HP-UX, IRIX, etc.) support lazy binding of procedure addresses, which is an optimization that yields better performance overall but a genuine problem for applications that need uniform performance after startup (eg: trading systems.) When an ELF application starts up, the loader (by default) initializes the Procedure Linkage Table (PLT) with a bunch of fix-up code that'll be run on the first invocation of each function. On the fix-up call, the function's position within the virtual address space is looked up and placed into the PLT so that future invocations of the function won't need to be looked up again.

The possible solutions:

If you care about latency after startup, there's a few things you can do:

Have a "warmup phase" of your application that does a dlsym() lookup on every function;
Use static libraries instead of shared libraries;
Set LD_BIND_NOW=1 and force the loader to do the PLT fixups at startup;
Use the "-z now" option on your linker (if you have it available).

Please see here for the full article and here for further info.