[英]Profiling library equivalent to gprof/callgrind
I'm looking for a C/C++ library with profiling capabilities such as gprof
, or callgrind
.我正在寻找具有分析功能的 C/C++ 库,例如
gprof
或callgrind
。
More precisely, I want its output to be equivalent to what callgrind
would issue, in order to pass it to third-party tools such as KCacheGrind.更准确地说,我希望它的输出与
callgrind
会发出的内容相同,以便将其传递给 KCacheGrind 等第三方工具。
The idea is to be able to design an aspect based on this library and to plug it into several applications we are developing in my team.这个想法是能够基于这个库设计一个方面,并将其插入我们团队正在开发的几个应用程序中。
The CPU profiler from gperftools can be attached to arbitrary executables using either LD_PRELOAD
or typical dynamic linkage. gperftools的CPU 分析器可以使用
LD_PRELOAD
或典型的动态链接附加到任意可执行文件。 It can output data in a callgrind-compatible format.它可以以与 callgrind 兼容的格式输出数据。
Let's assume that you'd like to profile an executable a.out
.假设您想分析一个可执行文件
a.out
。 Begin by linking it with -lprofiler
.首先将其与
-lprofiler
链接。 Afterwards run it with CPUPROFILE
env.然后使用
CPUPROFILE
env 运行它。 variable pointing to a name of a file where profiling data will be stored.指向存储分析数据的文件名的变量。 Data in the callgrind format can be obtained using
pprof
.可以使用
pprof
获取 callgrind 格式的数据。
CPUPROFILE=a.out.prof ./a.out
pprof --callgrind a.out a.out.prof
What's interesting is the fact that with CPUPROFILE
undefined your executable behaves normally.有趣的是,当
CPUPROFILE
未定义时,您的可执行文件行为正常。 As a result this profiler can be easily enabled without recompiling or relinking the application.因此,无需重新编译或重新链接应用程序即可轻松启用此分析器。
If for any reason you cannot alter the way the executable is linked you can still profile it by defining LD_PRELOAD
in a following fashion.如果由于任何原因您无法更改可执行文件的链接方式,您仍然可以通过以下方式定义
LD_PRELOAD
来对其进行分析。
LD_PRELOAD=/path/to/libprofiler.so CPUPROFILE=a.out.prof ./a.out
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.