使用 --call-stack fp 的 perf 记录无法展开主函数

perf record with --call-stack fp fails to unwind main function

我有一个让 CPU 忙碌的 C++ 测试程序:

#include <cstdint>
#include <iostream>

// Linear-feedback shift register
uint64_t lfsr1(uint64_t max_ix)
{
    uint64_t start_state = 0xACE1u;  /* Any nonzero start state will work. */
    uint64_t lfsr = start_state;
    uint64_t bit;                    /* Must be 16-bit to allow bit<<15 later in the code */

    for (uint64_t ix = 0; ix < max_ix; ++ix)
    {   /* taps: 16 14 13 11; feedback polynomial: x^16 + x^14 + x^13 + x^11 + 1 */
        bit = ((lfsr >> 0) ^ (lfsr >> 1) ^ (lfsr >> 3) ^ (lfsr >> 4)) & 1 /* & 1u */;
        lfsr = (lfsr >> 1) | (bit << 15);
    }
    return lfsr;
}

int main() {
    std::cout << lfsr1(1717986914ull) << "\n";
}

我用 g++ -g -O3 -fno-omit-frame-pointer cpu.cpp -o cpu.bin 编译它,然后 运行 用 perf record -F 100 --call-graph fp -- ./cpu.bin 编译它,第二次用 dwarf 而不是 fp.

fpperf script 输出中,我可以看到

cpu.bin 23435 1535437.021156:   42706947 cycles: 
            5617daf4b7a1 main+0x31 (…/cpu.bin)
            7f9a95088bf7 __libc_start_main+0xe7 (/lib/x86_64-linux-gnu/libc-2.27.so)
         3fe258d4c544155 [unknown] ([unknown])

而对于 dwarf,它是

cpu.bin 23443 1535441.101859:   42952079 cycles: 
            55a3b4ffd7a1 lfsr1+0x31 (inlined)
            55a3b4ffd7a1 main+0x31 (…/cpu.bin)
            7f00bcc8ebf6 __libc_start_main+0xe6 (/lib/x86_64-linux-gnu/libc-2.27.so)
            55a3b4ffd829 _start+0x29 (…/cpu.bin)

似乎 fp__libc_start_main 中偏移了一个字节,这导致它错过了最后的展开步骤。如何解决?

正如彼得在他的评论中所说,当使用带有帧指针的 glibc 版本时,问题会自行解决。在 Ubuntu 20.04 上,有这样一个 glibc 的包。

sudo apt install libc6-prof
# To use this library:
env LD_LIBRARY_PATH=/lib/libc6-prof/x86_64-linux-gnu perf record …

然后,[unknown] 按预期解析为 _start

来源:https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1908307