简体   繁体   English

如何使用 KCachegrind 和 Callgrind 仅测量我的代码的一部分?

[英]How to use KCachegrind and Callgrind to measure only parts of my code?

I want to use valgrind to analyze my code.我想使用valgrind来分析我的代码。 The problem is, that I have a huge startup sequence which I'm not interested in.问题是,我有一个我不感兴趣的巨大启动序列。

I found defines in the valgrind/callgrind.h that should help me:我在valgrind/callgrind.h中找到了应该对我有帮助的定义:

  • CALLGRIND_START_INSTRUMENTATION CALLGRIND_START_INSTRUMENTATION
  • CALLGRIND_STOP_INSTRUMENTATION CALLGRIND_STOP_INSTRUMENTATION
  • CALLGRIND_DUMP_STATS CALLGRIND_DUMP_STATS

According to this article I have to execute valgrind with the following options:根据这篇文章,我必须使用以下选项执行valgrind

valgrind --tool=callgrind --instr-atstart=no ./application

When I do this two files are created:当我这样做时,会创建两个文件:

  • callgrind.out.16060 callgrind.out.16060
  • callgrind.out.16060.1 callgrind.out.16060.1

I then want to use kcachegrind to visualize my results.然后我想使用 kcachegrind 来可视化我的结果。 This works great but the makros for the skipping of my startup-sequence seem to do nothing.这很好用,但跳过我的启动序列的 makros 似乎什么也没做。 What do I have to do to measure the performance only in places where I want to?我必须做什么才能仅在我想要的地方衡量性能?

I got it now, but I'm not 100% sure why.我现在明白了,但我不是 100% 确定为什么。 I will try to describe my code a bit:我将尝试稍微描述一下我的代码:

I have an Application class that is responsible for a lot of subsystems.我有一个负责很多子系统的 Application 类。 In my original attempt I tried to measure the performance inside the Application like this:在我最初的尝试中,我尝试像这样测量应用程序内部的性能:

int main(int argc, char *argv[])
{
    Application a(argc, argv);
    return a.exec();
}

void Application::Application(int &argc, char **argv)
{
    m_pComplexSystem = new ComplexSystem();
    m_pComplexSystem->configure();

    CALLGRIND_START_INSTRUMENTATION;
    m_Configurator->start();    
}

Application::~Application()
{
    CALLGRIND_STOP_INSTRUMENTATION;
    CALLGRIND_DUMP_STATS;
    m_pComplexSystem ->stop();

    delete m_pComplexSystem;
    m_pComplexSystem = 0;
}

For some reason the defines were ignored and I got the performance measures of the whole constructor and everything that was done in the configure() call of the ComplexSystem member.由于某种原因,定义被忽略了,我得到了整个构造函数的性能度量以及在 ComplexSystem 成员的 configure() 调用中完成的所有事情。

So now I use this code that seems to work:所以现在我使用这个似乎有效的代码:

int main(int argc, char *argv[])
{
    Application a(argc, argv);

    CALLGRIND_START_INSTRUMENTATION;
    int result = a.exec();
    CALLGRIND_STOP_INSTRUMENTATION;
    CALLGRIND_DUMP_STATS;
    return result;
}

Although it is not exactly the same as my original attempt, I can start looking for slow functions now.虽然和我最初的尝试不完全一样,但我现在可以开始寻找慢功能了。

Let's assume you have the following open-source program:假设您有以下开源程序:

int main()
{
    function1();

    function2();
    
    return 0;
}

Let's assume you want to execute Callgrind on only function2() .假设您只想在function2()上执行Callgrind

One approach is to insert Callgrind macros around function2() , and do the recompilation of the program (please compare with the above):一种方法是在function2()周围插入Callgrind宏,并重新编译程序(请与上面的比较):

#include <valgrind/callgrind.h>
int main()
{
    function1();

    CALLGRIND_START_INSTRUMENTATION;
    CALLGRIND_TOGGLE_COLLECT;
        function2();
    CALLGRIND_TOGGLE_COLLECT;
    CALLGRIND_STOP_INSTRUMENTATION;

    return 0;
}

In some cases, callgrind.h may not be found, see here for a similar problem .在某些情况下,可能找不到callgrind.h ,请参阅此处了解类似问题 The likely solution is to install/compile valgrind-devel , see this answer .可能的解决方案是安装/编译valgrind-devel ,请参阅此答案

Finally, you will need to add two new options to your callgrind commands, eg:最后,您需要向callgrind命令添加两个新选项,例如:

valgrind --tool=callgrind \
    --collect-atstart=no --instr-atstart=no \ #new options
    <program>

This answer is an extension of this entry .这个答案是 这个条目的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM