【问题标题】:Record instruction addresses of page faults with perf用 perf 记录缺页的指令地址
【发布时间】:2019-07-11 09:14:01
【问题描述】:

我想使用perf 获取导致主要页面错误的指令的地址。 我有一个简单的程序:

#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>


int main(int argc, char* argv[]) {
    int fd = open("path to large file several Gb", O_RDONLY);
    struct stat st;
    fstat(fd, &st);
    void* ptr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    const uint8_t* data = (const uint8_t*) ptr;

    srand(time(NULL));
    size_t i1 = ((double) rand() / RAND_MAX) * st.st_size;
    size_t i2 = ((double) rand() / RAND_MAX) * st.st_size;
    size_t i3 = ((double) rand() / RAND_MAX) * st.st_size;

    printf("%x[%lu], %x[%lu], %x[%lu]\n", data[i1], i1, data[i2], i2, data[i3], i3);

    munmap(ptr, st.st_size);
    close(fd);
    return 0;
}

我使用gcc -g -O0 main.c 编译它 并运行perf record -e major-faults -g -d ./a.out

接下来我使用perf report -g 打开生成的报告 报告说有 3 个主要的页面错误(这是正确的), 但我无法理解导致页面错误的指令地址。 报告如下:

# To display the perf.data header info, please use --header/--header-only options.
#
#
# Total Lost Samples: 0
#
# Samples: 3  of event 'major-faults'
# Event count (approx.): 3
#
# Children      Self  Command  Shared Object     Symbol                
# ........  ........  .......  ................  ......................
#
   100.00%     0.00%  a.out    libc-2.23.so      [.] __libc_start_main 
              |
              ---__libc_start_main
                 main

   100.00%   100.00%  a.out    a.out             [.] main              
              |
              ---0x33e258d4c544155
                 __libc_start_main
                 main

   100.00%     0.00%  a.out    [unknown]         [.] 0x033e258d4c544155
              |
              ---0x33e258d4c544155
                 __libc_start_main
                 main

a.out 不包含地址0x33e258d4c544155 或以155 结尾的内容。

问题是如何获取导致页面错误的指令地址?

【问题讨论】:

  • 欢迎来到 SO - 很好的第一个问题!感谢您提供完整的描述以复制它。

标签: linux perf


【解决方案1】:

由于某种原因,我无法重现您的示例,即我没有收到任何带有 major-faults 事件的样本。但我可以用一个不同的例子来解释。

pref report 输出具有误导性,它不是三个事件,而是显示三个堆栈级别。使用perf script 更容易理解 - 它显示了实际事件(包括它们的堆栈)。条目如下所示(对每个样本重复):

a.out 22107 14721.378764:   10000000 cycles:u: 
            5653c1afb134 main+0x1b (/tmp/a.out)
            7f58bb1eeee3 __libc_start_main+0xf3 (/usr/lib/libc-2.29.so)
        49564100002cdb3d [unknown] ([unknown])

现在您可以看到函数堆栈,其中包含虚拟指令地址、最近的符号以及与符号的偏移量。如果你想自己摆弄地址,你可以运行perf script --show-mmap-events,它告诉你:

a.out 22107 14721.372233: PERF_RECORD_MMAP2 22107/22107: [0x5653c1afb000(0x1000) @ 0x1000 00:2b 463469 624179165]: r-xp /tmp/a.out
                                                          ^ Base         ^ size    ^ offset                             ^ file

然后,您可以通过减去基数 0x5653c1afb000 并加上偏移量 0x1000 来计算 0x5653c1afb134 - 您将获得指令的地址或文件中的返回地址。

您还看到 0x49564100002cdb3d 未映射,无法解析 - 它只是基于帧指针的堆栈展开的垃圾。你可以忽略它。您也可以使用--call-graph dwarf--call-graph lbr,它们似乎显示出更合理的堆栈来源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多