为什么我在调用图中看到使用gperftools不存在的边?

时间:2014-07-10 04:22:13

标签: c++ call-graph gperftools

给出以下代码,调用f或g

#include <stdlib.h>
#include <stdio.h>

int f() {
    return 0;
}

int g() {
    return 1;
}

int main() {
    long sum = 0;
    for(int i = 0; i < 1000*1000*1000; i++) {
        int result;
        if(rand() % 2 == 0) {
            result = f();
        }
        else {
            result = g();
        }

        sum += result;
    }
    printf("%ld\n", sum);
}

我用

编译
g++ test.c -o doom -lprofiler -lunwind

运行
CPUPROFILE=./test.txt ./test

然后使用

生成一个gif
pprof --gif ./test ./test.txt > output.gif

我得到以下enter image description here

但是,我的边缘从f到g,f到自身,g到自身。没有优化(我再次尝试使用-O0),我也尝试过使用和不使用-lunwind。

为什么分析器会认为f调用g(和它自己的somtimes)?如何正确使用分析器?

0 个答案:

没有答案