c++valgrindcallgrindkcachegrind

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


I want to use valgrind to analyze my code. 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:

According to this article I have to execute valgrind with the following options:

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

When I do this two files are created:

I then want to use kcachegrind to visualize my results. This works great but the makros for the skipping of my startup-sequence seem to do nothing. What do I have to do to measure the performance only in places where I want to?


Solution

  • I got it now, but I'm not 100% sure why. I will try to describe my code a bit:

    I have an Application class that is responsible for a lot of subsystems. 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.

    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.