English 中文(简体)
Callgrind main()包含成本远低于100%
原标题:Callgrind main() inclusive cost much smaller than 100%

我介绍了一些在Linux上运行的非常简单的C++程序。main()的包含成本远不是100%,大约是3.83%。我使用callgrind正确吗?我有callgrind_annotation的输出,下面粘贴了--inclusive=yes

这个程序叫做heap,它进行简单的堆排序

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]
问题回答

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

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

If your program uses threads, the top function of threads 1..n will be not a main (each thread can separate stack; and chain of function calls from main is stored in stack of thread 0).

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





相关问题
Java: Comparing memory heap dumps in Netbeans

How do I compare memory heap dumps in Netbeans? What I have done is to configure my project to use profiling, then added several profiling points at chosen lines of code (similar to setting ...

Tools to evaluate callgrind s call profiles?

Somehow related to this question, which tool would you recommend to evaluate the profiling data created with callgrind? It does not have to have a graphical interface, but it should prepare the ...

Java: Finding out what is using all the memory

I have a java application that runs out of memory, but I have no idea which code is allocating the memory. Is there an application with which I can check this? I use Eclipse.

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签