Callgrind main()包含成本远低于100%

时间:2011-02-16 15:57:49

标签: profiling valgrind callgrind

我描述了一些在Linux上运行的非常简单的C ++程序。 main()的所有成本都远远低于100%,如3.83%。我正确使用callgrind吗?我将callgrind_annotate的输出与--inclusive=yes粘贴在下方。

该程序称为堆,它执行简单的堆排序。我使用的命令是

valgrind --tool=callgrind ./heap

然后,我输入

callgrind_annotate --inclusive=yes callgrind.out.25434

输出:

`--------------------------------------------------------------------------------

Profile data file 'callgrind.out.25434' (creator: callgrind-3.6.0)

`--------------------------------------------------------------------------------

I1 cache:
D1 cache:
LL cache:
Timerange: Basic block 0 - 361578
Trigger: Program termination
Profiled target:  ./heap (PID 25434, part 1)
Events recorded:  Ir
Events shown:     Ir
Event sort order: Ir
Thresholds:       99
Include dirs:
User annotated:
Auto-annotation:  off

`--------------------------------------------------------------------------------

       Ir
`--------------------------------------------------------------------------------

2,552,558  PROGRAM TOTALS

`--------------------------------------------------------------------------------

       Ir  file:function

`--------------------------------------------------------------------------------

2,552,558  ???:0x00000810 [/lib/ld-2.7.so]

2,515,793  ???:0x00000a60 [/lib/ld-2.7.so]

2,515,219  ???:0x00015270 [/lib/ld-2.7.so]

2,514,780  ???:0x000021e0 [/lib/ld-2.7.so]

2,456,164  ???:0x0000b2f0 [/lib/ld-2.7.so]

2,256,719  ???:0x00009e40 [/lib/ld-2.7.so]

1,702,371  ???:0x00009ac0 [/lib/ld-2.7.so]

  657,883  ???:0x000098e0 [/lib/ld-2.7.so]

  367,045  ???:0x00017040 [/lib/ld-2.7.so]

   33,170  ???:0x080483e0 [/home/test/heap]

   33,036  ???:0x0000ce60 [/lib/ld-2.7.so]

   31,347  ???:0x0000e850 [/lib/ld-2.7.so]

   30,706  ???:(below main) [/lib/libc-2.7.so]

   30,071  ???:0x00008570 [/lib/ld-2.7.so]

   27,954  ???:0x0000f500 [/lib/ld-2.7.so]

   27,758  ???:0x0000ca30 [/lib/ld-2.7.so]

   21,366  ???:0x0001767b [/lib/ld-2.7.so]

1 个答案:

答案 0 :(得分:4)

main()不是调用图中的顶级函数。 glibc中有一个_start函数,它会调用main(),它也会从main获取返回值。还有(对于动态链接程序=几乎所有)一个ELF解释器,也称为动态链接器(运行时):/ lib / ldl -linux.so(这个名字在linux中使用,在其他Unix中用于/ lib /ld.so)。链接器将加载并初始化应用程序所需的所有动态库;它在_start之前被操作系统调用。

main之前做了什么?加载库(打开库文件,解析其标题,mmap,在内存中采用新位置的代码=重定位处理),并初始化它们(动态和逻辑链接的库都需要这个;注意glibc = libc也是库) 。每个库都可能有一个代码,该代码将在加载库(__attribute__((constructor))或全局对象的非平凡构造函数)之后立即启动。此外,glibc可以注册一些在main之后运行的函数(例如,通过atexit();如果main正常返回,它们将由_start调用),并且库可能具有全局对象的析构函数。

如果您的程序使用线程,则线程1 .. n的顶部函数将不是main(每个线程可以分隔堆栈;来自main的函数调用链是存储在堆栈中 线程0)。

在您的示例中,我们看到/lib/ld-*.so,它是一个动态链接器。似乎,您的应用程序执行时间太短而无法正确分析,并且它使用大量动态库。

相关问题