callgrind 分析是否受其他进程影响?

Is callgrind profiling influenced by other processes?

我想使用 callgrind 分析我的应用程序。现在,由于这需要很长时间,与此同时我继续在同一台机器上进行网页浏览、编译和其他密集型任务。

我对分析结果有偏见吗?我预料到了,因为 valgrind 使用 模拟 CPU,其他外部 进程不应干扰 valgrind 的执行。我说得对吗?

默认情况下,Callgrind 不记录任何与时间相关的信息,因此您可以预期所有收集的指标(大部分)独立于机器上的其他进程。正如 Callgrind manual 所述,

By default, the collected data consists of the number of instructions executed, their relationship to source lines, the caller/callee relationship between functions, and the numbers of such calls.

因此,Callgrind 报告的指标应该仅取决于程序在(模拟)CPU 上执行的指令 - 而不是这些指令花费的时间。事实上,很多时候 Callgrind 的输出可能有点误导,因为模拟的 CPU 可能与真实的不同(特别是在分支预测方面)。 Callgrind paper presented at ICCS 2004也很清楚这一点:

We note that the simulation is not able to predict consumed wall clock time, as this would need a detailed simulation of the microarchitecture.

然而,无论如何,模拟的 CPU 不受真实 CPU 正在做的事情的影响。 原因很简单。 就像你说的,你的程序根本没有在你的机器上执行。 相反,在 运行 时间,Valgrind 动态翻译您的程序,即将二进制文件反汇编为 "UCode" 用于模拟机器,添加分析代码(称为检测),然后生成执行模拟的二进制代码. 添加分析代码使得指令计数(在 Callgrind 中)、内存检查(在 Memcheck 中)和所有其他插件成为可能。

然而,其中存在转折。 在这样的动态模拟中,程序 运行 的隔离程度自然是有限的。 首先,您的程序可能会与其他程序交互。 虽然这样做所花费的时间无关紧要(因为它没有被考虑在内),但进程间通信的 return 代码肯定会发生变化,具体取决于系统中发生的其他事情。 其次,大多数系统调用需要 运行 未翻译,它们的 return 代码也可以更改——导致程序的执行路径不同,因此收集的指标略有不同。 (顺便说一句,Calgrind 提供了一个选项来记录系统调用期间花费的挂钟时间,这将始终受到系统中发生的其他事情的影响)。 有关这些限制的更多详细信息,请参阅 PhD Dissertation of Nicholas Nethercote ("Dynamic Binary Analysis and Instrumentation").