c++ - Callgrind:分析我的代码的特定部分

标签 c++ profiling valgrind callgrind

我正在尝试通过消除我不关心的噪音和计算来分析(使用 Callgrind)我的代码的特定部分。 这是我想做的一个例子:

for (int i=0; i<maxSample; ++i) {
    //Prepare data to be processed...
    //Method to be profiled with these data
    //Post operation on the data
}

我的用例是回归测试,我想确保所讨论的方法仍然足够快(自上次实现以来,额外指令不到 10%)。 这就是为什么我想要更清晰的输出形式 Callgrind。 (我需要一个 for 循环来处理大量数据,以便对我要分析的方法的行为有一个很好的估计)

我的第一次尝试是将代码更改为:

for (int i=0; i<maxSample; ++i) {
    //Prepare data to be processed...
    CALLGRIND_START_INSTRUMENTATION;
    //Method to be profiled with these data
    CALLGRIND_STOP_INSTRUMENTATION;
    //Post operation on the data
}
CALLGRIND_DUMP_STATS;

添加 Callgrind 宏来控制检测。我还添加了 --instr-atstart=no 选项,以确保我只分析我想要的部分代码......

不幸的是,当我开始使用 callgrind 启动我的可执行文件时,使用此配置时,它永远不会结束...这不是缓慢的问题,因为完整的检测运行持续不到一分钟。

我也试过

for (int i=0; i<maxSample; ++i) {
    //Prepare data to be processed...
    CALLGRIND_TOGGLE_COLLECT;
    //Method to be profiled with these data
    CALLGRIND_TOGGLE_COLLECT;
    //Post operation on the data
}
CALLGRIND_DUMP_STATS;

(或 --toggle-collect="myMethod"选项) 但是 Callgrind 没有任何调用就给我返回了一个日志(KCachegrind 像雪一样白:(并且说零指令......)

我是否正确使用了宏/选项?知道我需要改变什么才能获得预期的结果吗?

最佳答案

我终于设法解决了这个问题......这是一个配置问题:

我保留了代码

for (int i=0; i<maxSample; ++i) {
    //Prepare data to be processed...
    CALLGRIND_TOGGLE_COLLECT;
    //Method to be profiled with these data
    CALLGRIND_TOGGLE_COLLECT;
    //Post operation on the data
}
CALLGRIND_DUMP_STATS;

但是使用 --collect-atstart=no (并且没有 --instr-atstart=no !!!)运行 callgrind 并且在合理的时间内(约 1 分钟)运行良好.

START/STOP 检测的问题是 callgrind 在每次迭代(每次 STOP)时都会转储一个文件(callgrind.out.#number),因此它真的很慢......(5 分钟后我只有 5000 次运行300 000 次迭代基准测试...不适合回归测试)。

关于c++ - Callgrind:分析我的代码的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13688185/

相关文章:

c++ - 为什么不采用非类型偏特化元组?

c++ - 为什么 Gazebo 不启动 ROS?

android - Android JNI中的高精度定时器

Erlang VM : scheduler runtime information

arm - Valgrind在ARM Cortex-A8中的问题 "configure: error: Unsupported host architecture"

c++ - 不匹配删除

c++ - 如何使用 openAL 从麦克风将实时音频输入记录到文件中? (里面的C++代码)

c++ - 如何设置 "this"线程的自定义名称?

profiling - 使用 Valgrind 获取指令配置文件

c++ - QThread:最简单代码的竞争条件