Linux perf 未正确记录 libstdc++.so 中的函数

问题描述 投票:0回答:1

我正在使用 perf 来分析 C++ 程序。代码如下:

std::string generateRandomString() {
    // Initialize a random number generator
    std::random_device rd;
    std::mt19937 gen(rd());

    // Define a distribution for the length of the string (1 to 6)
    std::uniform_int_distribution<> lenDistribution(1, 6);
    int length = lenDistribution(gen);

    // Define characters that can be part of the string
    const std::string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    // Generate the random string
    std::uniform_int_distribution<> charDistribution(0, characters.size() - 1);
    std::string result;
    result.reserve(length);

    for (int i = 0; i < length; ++i) {
        result += characters[charDistribution(gen)];
    }
    return result;
}


void test_trie(){
    for (...) generateRandomString();
}

void test(){
    // do something
}

int main(){
    test_trie();
    test();
}

然后

  1. 我用
    g++ -g -O2 main.cpp -o secmaster_test
    编译它(
    -O0
    也尝试过)
  2. perf record -F 999 -g -- ./secmaster_test
  3. 对其进行分析
  4. 绘制火焰图。像这样

但我认为结果应该如下:

                                           | --- some random -----|
                  | ...something...        | generateRandomString | -- --|
                  | ----------test-------- | --------------test_trie-----|
                  | ---------------------main----------------------------|
|--startup code-- | -------------------secmaster_test------------------- | -- cleanup -- |

似乎

perf
不知何故不知道在
main()
中调用了有关随机的函数?

顺便说一句,我尝试过:

  1. 使用
    perf record --call-graph dwarf
  2. 使用
    perf record --call-graph fp

并且失败了

c++ profiler perf
1个回答
0
投票

来自wiki

  • 要获得有用的调用图,请使用
    -fno-omit-frame-pointer
  • 构建编译器

帧指针是 perf(和其他工具)“读取”调用堆栈的方式,没有它,即使是非内联函数也很难识别。

© www.soinside.com 2019 - 2024. All rights reserved.