HOWTO: using gprof with multithreaded applications
What is gprof?
gprof is the GNU Profiler, a tool used when tracking which functions are eating CPU in your program. Anyway, you should already be familiar with it if you got interested in this page.
One problem with gprof under certain kernels (such as Linux) is that it doesn’t behave correctly with multithreaded applications. It actually only profiles the main thread, which is quite useless.
Workaround
There is an easy, but surprisingly not very widespread fix for this annoying gprof behaviour. Basically, gprof uses the internal ITIMER_PROF timer which makes the kernel deliver a signal to the application whenever it expires. So we just need to pass this timer data to all spawned threads.
Example
It wouldn’t be too hard to put a call to setitimer in each function spawned by a thread, but I thought it would be more elegant to implement a wrapper for pthread_create.
Daniel Jönsson enhanced my code so that it could be used in a preload library without having to modify the program. It can also be very useful for libraries that spawn threads without warning, such as libSDL. The result code is shown below and can be downloaded (gprof-helper.c):
转载链接
HOWTO: using gprof with multithreaded applications
gprof-helper.c

本文介绍如何使用GNU Profiler (gprof) 工具有效分析多线程应用程序的CPU使用情况。通常,gprof在某些内核下如Linux,只能分析主线程,但通过设置ITIMER_PROF定时器并将其传递给所有派生线程,可以解决这一问题。文章提供了一个优雅的解决方案,即为pthread_create实现一个包装器,并附带了预加载库中使用的代码示例。
3638

被折叠的 条评论
为什么被折叠?



