Linux是否因页面错误和tlb错误而不准确?

时间:2017-11-29 07:50:33

标签: perf tlb page-fault

我写了一个简单的程序来测试页面错误和tlb miss with perf。 代码如下。它按顺序写入1 GB数据并且是预期的 触发 1GB / 4KB = 256K tlb未命中和页面错误。

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

#define STEP 64
#define LENGTH (1024*1024*1024)
int main(){
    char* a = malloc(LENGTH);
    int i;
    for(i=0; i<LENGTH; i+=STEP){
            a[i] = 'a';
    }

    return 0;
}

然而,结果如下,远小于预期。 perf这么不精确吗?如果有人可以在他的机器上运行代码,我将非常感激。

 $ perf stat -e dTLB-load-misses,page-faults ./a.out

   Performance counter stats for './a.out':

         12299      dTLB-load-misses
          1070      page-faults

   0.427970453 seconds time elapsed

环境:Ubuntu 14.04.5 LTS,内核4.4.0; gcc 4.8.4 glibc 2.19。没有编译标志。

CPU是Intel(R)Xeon(R)CPU E5-2640 v2 @ 2.00GHz。

1 个答案:

答案 0 :(得分:0)

内核预取故障页面,至少在它有模式证据之后。无法找到关于算法的明确参考,但也许https://github.com/torvalds/linux/blob/master/mm/readahead.c是查看正在发生的事情的起点。我会寻找其他性能计数器来捕获这种机制的行为。