我想用valgrind分析我的代码。问题是,我有一个我不感兴趣的巨大启动序列。
我在中找到了定义valgrind/callgrind.h这应该对我有帮助:
- CALLGRIND_START_INSTRUMENTATION
- CALLGRIND_STOP_INSTRUMENTATION
- CALLGRIND_DUMP_STATS
根据本文 http://zariko.taba.free.fr/c++/callgrind_profile_only_a_part.html我必须执行valgrind有以下选项:
valgrind --tool=callgrind --instr-atstart=no ./application
当我这样做时,会创建两个文件:
- 呼叫研磨.out.16060
- callgrind.out.16060.1
然后我想使用 kcachegrind 来可视化我的结果。这很有效,但是用于跳过我的启动序列的 makros 似乎没有任何作用。我需要做什么才能只在我想要测量的地方测量性能?
假设您有以下开源程序:
int main()
{
function1();
function2();
return 0;
}
假设您要执行Callgrind
仅开启function2()
.
一种方法是插入Callgrind
周围的宏function2()
,并重新编译程序(请与上面进行比较):
#include <valgrind/callgrind.h>
int main()
{
function1();
CALLGRIND_START_INSTRUMENTATION;
CALLGRIND_TOGGLE_COLLECT;
function2();
CALLGRIND_TOGGLE_COLLECT;
CALLGRIND_STOP_INSTRUMENTATION;
return 0;
}
在某些情况下,callgrind.h
可能找不到,参见这里有类似的问题 https://stackoverflow.com/questions/32048354/valgrind-fatal-error-memcheck-h-no-such-file-or-directory。可能的解决方案是安装/编译valgrind-devel
, see 这个答案 https://stackoverflow.com/a/32049682/3393574.
最后,您需要将两个新选项添加到您的callgrind
命令,例如:
valgrind --tool=callgrind \
--collect-atstart=no --instr-atstart=no \ #new options
<program>
这个答案是一个延伸这个条目 https://developer.mantidproject.org/ProfilingWithValgrind.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)