如何在启动时从共享库加载所有符号?

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

美好的一天! 我有一个 fork-on-connect 守护进程。使用 perf 工具对其进行分析后,我发现函数 "do_lookup_x" 消耗了很多 CPU 时间。共享库函数的所有函数调用都在 fork 之后。

有没有办法在fork之前查找所有符号?

设置环境变量 LD_BIND_NOW 应该有助于实现这一目标。

设置为

export LD_BIND_NOW=1

然后执行你的程序。

摘录:

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.

可能的解决方案:

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).

请参阅 here for the full article and here 了解更多信息。