SummaryLibrary interposition is a useful technique for tuning performance, collecting runtime statistics, or debugging applications. This article offers helpful tips and tools for working with the technique, and gets you started on your own interposer.
Most of today's applications use shared libraries and dynamic linking, especially for such system libraries as the standard C library ( With dynamic linking, you can intercept any function call an application makes to any shared library. Once you intercept it, you can do whatever you want in that function, as well as call the real function the application originally intended to call.
Performance tuning is one use of this technology. Even if you have access to profilers and other development tools, or the application's source code itself, having your own library interposer puts you completely in control. You can see exactly what you're doing and make adjustments at any time. Building and running your first interposer
To use library interposition, you need to create a special shared library and
set the
Let's create a simple interposer for Here's the source for this interposer:
Now let's build and run this interposer, using % cc -o malloc_interposer.so -G -Kpic malloc_interposer.c % setenv LD_PRELOAD $cwd/malloc_interposer.so % ls -l malloc_interposer.so malloc(64) is called malloc(52) is called malloc(1072) is called -rwxr-xr-x 1 gregn 5224 Aug 3 15:21 malloc_interposer.so* % unsetenv LD_PRELOAD
Without access to the source code of
Note that Collecting runtime statistics
Here's a more practical example of library interposition on Here's the source code:
Note that we round up all memory-request sizes to the next power of two. To obtain the name of the current executable, we use the
We can run this interposer with the CDE editor % setenv LD_PRELOAD $cwd/malloc_hist.so % dtpad malloc_hist.c % unsetenv LD_PRELOAD Here are the results.
% cat /tmp/malloc_histogram.dtpad.15203
prog_name=dtpad.15203
******** malloc **********
1 76
2 105
4 450
8 667
16 2047
32 620
64 158
128 39
256 33
512 22
1024 32
2048 10
4096 14
8192 46
32768 2
131072 3
******** calloc **********
1 0
2 0
4 1676
8 40
16 21
32 12
64 34
128 4
256 2
512 0
1024 3
8192 7
******** realloc **********
1 0
2 0
4 2
8 2
16 11
32 11
64 14
128 1
256 0
512 0
If the application invokes more than one executable, you'll get a histogram in the
Such histograms can be quite useful in application performance tuning. We now know that
This tool has been used with many real applications. It revealed that most of one application's
First of all, most
Second, it's possible to create your own memory allocator specially geared towards small blocks. It can be made a lot faster than the system's default Fixing a bug
This is a true story. A major mechanical CAD application stopped working with Solaris 2.6, but continued to work with Solaris 2.5.1. Debugging showed that the reason for failure was a call to
The reason it didn't work was a bug in X. It could also be considered a bug in the application, because using In any case, we needed a quick workaround until the bug was fixed.
The application in question was old and complex. It called More ideasNow that you know how to build and use library interposers, here are some other things you can have them do::
Using the library interposers described in this article, you can monitor your applications' patterns of system-resource consumption and provide useful feedback to application developers. Acknowledgements
I'd like to thank two of my colleagues at Sun Microsystems: Bart Smaalders, who wrote the original version of the interposer to collect About the authorGreg Nakhimovsky is a member of technical staff at Sun Microsystems. He works with independent software vendors, making sure their applications run well on Sun systems. He has 20 years of industry experience, developing, performance tuning, and supporting technical computer applications on various computer systems. | ||||||||
|
| ||||||||||||