gprof seems to fail to collect data from my program. Here is my command line:
g++ -Wall -O3 -g -pg -o fftw_test fftw_test.cpp -lfftw3 -lfftw3_threads -lm && ./fftw_test
你的程序使用了fftw库,可能几乎全部都是fftw库调用。运行时间是多少?你的程序可能太快了,无法使用gprof进行分析。
更新:库可能无法被gprof看到,因为它是在没有启用gprof分析的情况下编译的。
GNU gprof有两个部分。首先,它会在使用-pg
选项(带有mcount函数调用-https://en.wikipedia.org/wiki/Gprof)编译的c/cpp文件中插入函数调用记录,以获取调用者/被调用者信息。其次,它会将额外的分析库链接到可执行文件中,以添加定期采样来查找哪些代码执行时间更长。采样是通过profil(setitimer)完成的。Setitimer分析具有有限的分辨率,无法解决小于10毫秒或1毫秒(每秒100或1000个样本)的间隔。
在你的例子中,fftw库可能是没有插装的,因此其中没有mcount
调用。它仍然可以被采样部分捕获,但只能针对程序的主线程(https://en.wikipedia.org/wiki/Gprof -“通常只分析应用程序的主线程”)。
perf
分析器没有使用mcount
进行仪器化(当使用-g
选项记录时,它从堆栈展开中获取被调用者/调用者),但它具有更好的统计/采样变量(它可以使用硬件PMU计数器),没有100或1000 Hz的限制,并且正确支持(分析)线程。尝试perf record -F1000 ./fftw_test
(使用1 kHz采样频率),然后使用perf report
或perf report > report.txt
。还有一些GUI / HTML前端可用于perf:https://github.com/KDAB/hotspot https://github.com/jrfonseca/gprof2dot
对于更好的setitimer风格的分析器,请查看google-perftools https://github.com/gperftools/gperftools中的“CPU PROFILER”。
======
通过您的测试,我在Debian 8.6 Linux内核版本3.16.0-4-amd64 x86_64机器上使用g++(Debian 4.9.2-10)获得了一些gprof结果,gprof是“GNU gprof(Debian GNU Binutils)2.27”
$ cat gprof_test.cpp
#include <cstdlib>
#include <ctime>
#include <iostream>
int main()
{
std::srand( std::time( 0 ) );
double sum = 0.0;
for ( int i = 0; i < 100000000; ++i )
sum += std::rand() / ( double ) RAND_MAX;
std::cout << sum << '\n';
return 0;
}
$ g++ -Wall -O3 -g -pg -o gprof_test gprof_test.cpp && time ./gprof_test
5.00069e+06
real 0m0.992s
$ gprof -b gprof_test gmon.out
Flat profile:
Each sample counts as 0.01 seconds.
no time accumulated
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 _GLOBAL__sub_I_main
所以,在这个1秒的示例中,gprof没有捕获任何时间样本,并且没有关于调用库的信息(它们是在没有使用-pg
编译的情况下编译的)。添加一些包装函数并禁止内联优化后,我从gprof获得了一些数据,但库时间没有被计算在内(它看到了2秒运行时间中的0.72秒):
$ cat *cpp
#include <cstdlib>
#include <ctime>
#include <iostream>
int rand_wrapper1()
{
return std::rand();
}
int rand_scale1()
{
return rand_wrapper1() / ( double ) RAND_MAX;
}
int main()
{
std::srand( std::time( 0 ) );
double sum = 0.0;
for ( int i = 0; i < 100000000; ++i )
sum+= rand_scale1();
std::cout << sum << '\n';
return 0;
}
$ g++ -Wall -O3 -fno-inline -g -pg -o gprof_test gprof_test.cpp && time ./gprof_test
real 0m2.345s
$ gprof -b gprof_test gmon.out
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ns/call ns/call name
80.02 0.57 0.57 rand_scale1()
19.29 0.71 0.14 100000000 1.37 1.37 rand_wrapper1()
2.14 0.72 0.02 frame_dummy
0.00 0.72 0.00 1 0.00 0.00 _GLOBAL__sub_I__Z13rand_wrapper1v
0.00 0.72 0.00 1 0.00 0.00 __static_initialization_and_destruction_0(int, int) [clone .constprop.0]
Call graph
granularity: each sample hit covers 2 byte(s) for 1.39% of 0.72 seconds
index % time self children called name
<spontaneous>
[1] 97.9 0.57 0.14 rand_scale1() [1]
0.14 0.00 100000000/100000000 rand_wrapper1() [2]
-----------------------------------------------
0.14 0.00 100000000/100000000 rand_scale1() [1]
[2] 19.0 0.14 0.00 100000000 rand_wrapper1() [2]
而且perf可以看到所有的部分:
$ perf record ./gprof_test
0
[ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.388 MB perf.data (~16954 samples) ]
$ perf report |more
25.91% gprof_test gprof_test [.] rand_scale1()
21.65% gprof_test libc-2.19.so [.] __mcount_internal
13.88% gprof_test libc-2.19.so [.] _mcount
12.54% gprof_test gprof_test [.] main
9.35% gprof_test libc-2.19.so [.] __random_r
8.40% gprof_test libc-2.19.so [.] __random
3.97% gprof_test gprof_test [.] rand_wrapper1()
2.79% gprof_test libc-2.19.so [.] rand
1.41% gprof_test gprof_test [.] mcount@plt
0.03% gprof_test [kernel.kallsyms] [k] memset
std::rand
函数中占据 100% 减 epsilon 的时间,这个函数没有被使用 -g 编译,是吗?所以实质上在你的代码中不会发生任何采样。 - Mike Dunlavey