系統 - Debian - Memory - 如何使用KCacheGrind查看呼叫順序



main.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
void test1(void)
{
    for (int x = 0; x < 100; x++);
}
 
void test2(void)
{
    usleep(1);
}
 
void test3(void)
{
    usleep(100);
}
 
int main(int argc, char** argv)
{
    while (1) {
        test1();
        test2();
        test3();
    }
}

編譯、執行

$ gcc main.c -o main -ggdb
$ valgrind --tool=callgrind ./main
    ==7937== Callgrind, a call-graph generating cache profiler
    ==7937== Copyright (C) 2002-2017, and GNU GPL'd, by Josef Weidendorfer et al.
    ==7937== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
    ==7937== Command: ./main
    ==7937== 
    ==7937== For interactive control, run 'callgrind_control -h'.

按下Ctrl + C結束並且執行如下程式

$ kcachegrind callgrind.out.xxx