Skip to contentSkip to conversation

gprof cannot profile multi-threaded programs

DISCUSSION Mohit Aron 2 participants 2 messages Jan 31, 2001

lore
Share & tools
lore

Mohit Aron

author
#0 lore raw

Hi,
I'm using Linux-2.2 and discovered a problem with the profiling of
a multi-threaded program (uses Linux pthreads). Basically, upon compiling the program with '-pg' option, running it and invoking gprof on the gmon.out file only shows the profile information corresponding to the computation done by the first thread (that starts in main()). The computation performed by any other thread (created using pthread_create()) is not accounted for.

I analyzed the problem to be the following. Linux uses periodic SIGPROF signals for profiling (Linux doesn't use the profil system call used in other OS's like Solaris where the kernel does the profiling on behalf of the process). All profile information is collected in the context of the signal handler for the SIGPROF signal in Linux. Unfortunately, any thread that's created using pthread_create() does not get these periodic SIGPROF signals. Hence any thread other than the first thread is not profiled. The fix is to use setitimer() system call immediately in the thread startup function for any new thread to make the SIGPROF signal to be delivered at the designated interrupt frequency (every 10ms). With this fix, the profile produced by gprof reflects the overall computation done by all threads in the process. A more general fix would be to fix the kernel to make any new threads inherit the setitimer() settings for the parent thread.

Does anyone know if this problem has already been fixed ? If so, please send me a pointer to the patch. Thanks,

- Mohit
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#1 lore raw
Quoted 5 lines · Mohit Aron

I analyzed the problem to be the following. Linux uses periodic SIGPROF signals for profiling (Linux doesn't use the profil system call used in other OS's like Solaris where the kernel does the profiling on behalf of the process). All profile information is collected in the context of the signal handler for the SIGPROF signal in Linux. Unfortunately, any thread that's created using

Quoted 8 lines

pthread_create() does not get these periodic SIGPROF signals. Hence any thread other than the first thread is not profiled. The fix is to use setitimer() system call immediately in the thread startup function for any new thread to make the SIGPROF signal to be delivered at the designated interrupt frequency (every 10ms). With this fix, the profile produced by gprof reflects the overall computation done by all threads in the process. A more general fix would be to fix the kernel to make any new threads inherit the setitimer() settings for the parent thread.

You have the same problem when doing fork(). Only the parent will get cpu usage info. I have to call setitimer() too, to make it work properly.

I complained about it a few days ago, but didn't get a reply yet.

Kurt

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/