我有一个模型代码,kcachegrind/callgrind会报告奇怪的结果。它是一种调度函数。dispatcher是从4个地方调用的;每个调用都说明要运行哪个实际的do_J函数(因此first2将只调用do_1和do_2,依此类推)
源代码(这是实际代码的模型)
#define N 1000000
int a[N];
int do_1(int *a) { int i; for(i=0;i<N/4;i++) a[i]+=1; }
int do_2(int *a) { int i; for(i=0;i<N/2;i++) a[i]+=2; }
int do_3(int *a) { int i; for(i=0;i<N*3/4;i++) a[i]+=3; }
int do_4(int *a) { int i; for(i=0;i<N;i++) a[i]+=4; }
int dispatcher(int *a, int j) {
if(j==1) do_1(a);
else if(j==2) do_2(a);
else if(j==3) do_3(a);
else do_4(a);
}
int first2(int *a) { dispatcher(a,1); dispatcher(a,2); }
int last2(int *a) { dispatcher(a,4); dispatcher(a,3); }
int inner2(int *a) { dispatcher(a,2); dispatcher(a,3); }
int outer2(int *a) { dispatcher(a,1); dispatcher(a,4); }
int main(){
first2(a);
last2(a);
inner2(a);
outer2(a);
}用gcc -O0编译;用valgrind --tool=callgrind调用;用kcachegrind和qcachegrind-0.7调用。
下面是该应用程序的完整调用图。所有到do_J的路径都通过dispatcher,这很好( do_1只是因为太快而被隐藏起来,但它确实在这里,只是留给do_2)

让我们关注do_1并检查谁调用了它(这张图片是不正确的):

这非常奇怪,我认为,只有first2和outer2调用了do_1,但不是全部。
这是callgrind/kcachegrind的限制吗?我怎样才能得到准确的带权重的调用图(与每个函数的运行时间成正比,有没有它的孩子)?
发布于 2011-10-30 17:52:21
是的,这是callgrind格式的限制。它不存储完整的跟踪;它只存储父子调用信息。
有一个google-perftools项目,其中包含pprof/libprofiler.so CPU profiler,http://google-perftools.googlecode.com/svn/trunk/doc/cpuprofile.html。libprofiler.so可以获取带有调用跟踪的配置文件,并且它将以完整的回溯存储每个跟踪事件。pprof是libprofile输出到图形格式或callgrind格式的转换器。在完整视图中,结果将与kcachegrind中的结果相同;但如果您将重点放在某些函数上,例如使用pprof的选项焦点的do_1;当您将焦点放在函数上时,它将显示准确的调用树。
https://stackoverflow.com/questions/7488793
复制相似问题