Using LD_PRELOAD for testing of alternative implementations in large applications
I have recently been exploring techniques for optimising the speed of execution of a large, computationally very intensive application. Unfortunately, this application is extremely complex and time-consuming to rebuild from source which makes testing of alternative computational kernels very cumbersome.
Using the linux perf
tools it is quite easy to determine which
procedures consume most to the computational load of the application
and since we have access to the source code it is easy to determine
exactly what the interfaces to these procedure are.
In this situation, it is very convenient to develop a minimal library
containing just the routines that we’d want to replace from the target
application and then pre-load this library using the LD_PRELOAD
environment library. This variable is described as follows in the
ld.so manual page:
LD_PRELOAD A whitespace-separated list of additional,
user-specified, ELF shared libraries to be loaded before
all others. This can be used to selectively override
functions in other shared libraries. For setuid/setgid
ELF binaries, only libraries in the standard search
directories that are also setgid will be loaded.
Therefore using this environment application one can choose at run-time what functions to replace in the target application. This particularly useful for tests of alternative implementations of some functions because:
- It is not necessary to recompile the entire target application
- Many different alternative implementations can be tested with little overhead simply by building small shared libraries with the alternative implementations