开发者

Profile shared object without recompiling main program

I currently am developing a shared lilbrary for loading into PostgreSQL (as C-Language functions, see here). Now I would like to profile the function in this library without recompiling PostgreSQL itself.

I tried callgrind using

valgrind --tool=callgrind path/to/postgres arguments-to-postgres

This gives me profiling information for PostgreSQL itself, but fails to log the shared library I am interested i开发者_如何学JAVAn.

I also tried sprof, but I have no idea how to get that one working.

Any ideas would be highly appriciated.

P.S.: Please do not suggest just pausing the application in a debugger. With function runtimes way below 0.01 seconds I need more detailled results.


Using callgrind should work as expected. To test this I setup a simple project using a simple library and main functionMakefile:

CFLAGS=-fpic
exe:exe.o lib.so
        cc -o exe exe.o lib.so
lib.so:lib.o
        cc -shared lib.o -o lib.so
clean:
        rm -f exe lib.so *.o

lib.c is a simple library containing 2 functions:

#include <stdio.h>
void someOtherFunction() { printf("someOtherFunction\n"); }
void someFunction() { printf("someFunction\n"); someOtherFunction(); }

exe.c is a very simple executable:

int someFunction();
void main() { someFunction(); }

Use the Makefile to build the executable and run it using valgrind like so:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD valgrind --tool=callgrind ./exe

If you examine the callgrind output, you will find the profiling data for both functions in the shared library. If you cannot see these functions you may be using a non-standard environment that does not support this functionality. I'm using Linux Mint 11 x64, with the latest patches.


Point 1: Valgrind seems to have issues with privilege escalation, which is VERY likely to be performed by Postgres, see http://www.mail-archive.com/valgrind-users@lists.sourceforge.net/msg02355.html

Point 2: Have you tried to prove (e.g. with strace) that your SL is actually loaded in the same process? Have you tried to --trace-children=yes?

Point 3: I've tried to modify the test by compiling exe.o and exe with -g0 and using dlopen to load the file, i.e.:

all: exe lib.so
exe : exe.c 
        cc -g0 -o exe exe.c -ldl

lib.so:lib.c
        cc -shared lib.c -o lib.so
clean:
        rm -f exe lib.so *.o

and

#include 
#include 

void main() { 
    void *handle;
    void (*p)();
    int i;

    handle = dlopen("./lib.so", RTLD_LAZY);
    if ( ! handle ) { printf("Object not found\n"); return; }
    p = dlsym(handle, "someFunction");
    if ( ! p ) { printf("Function not found\n"); return; }
    for (i = 0; i < 100; ++i) (*p)();
    dlclose(handle);
}

Works with callgrind. Maybe Postgres is not using -ldl to open the object files?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜