1

I try to learn about the the memory management if Linux kernel, and I write the code like this:

//mem.c
#include <stdio.h>
#include <malloc.h>
#include <unistd.h>


void main()
{
    char *brk_end = NULL;
    char *p = sbrk(0);
    int nr = 10000, i = 0; 

    brk(p +  4096*nr);

    i = 0;
    for (i = 0; i < nr; i++) {
        *(p + (i*4096) + 100) = 1;
    }

}

For my opinion, each time I do *(p + (i*4096) + 100),page fault will occur( 4K page size). And I add a code to Linux Kernel like this to capture the page falt:

if (memcmp(current->comm, "mem", 3)) {
    g_counter++;
}

"mem" is my process's name, and I compile the kernel. Finally the g_counter indicates that this program actually triggers about 10000 times page fault.

For my opinion, the page fault must occurs after TLB miss, so the times of TLB miss should be equal to times of page fault. But when I try to use perf to detect program's TLB miss, i get another answer:

$perf stat -e dTLB-load-misses,iTLB-load-misses ./mem >/dev/null

 Performance counter stats for './mem':

             7,426      dTLB-load-misses                                            
               397      iTLB-load-misses                                            

       0.022220726 seconds time elapsed

Why the times of TLB miss is less than times of page fault ?

Hadi Brais
  • 22,259
  • 3
  • 54
  • 95
Nail Jay
  • 267
  • 3
  • 9
  • 2
    It is possible for page fault handler, which is invoked when non-mapped page is accessed, to map several pages at once. – Tsyvarev Oct 29 '18 at 09:11
  • What processor are you using? See [how to interpret perf iTLB-loads,iTLB-load-misses](https://stackoverflow.com/questions/49933319/how-to-interpret-perf-itlb-loads-itlb-load-misses). Potential duplicate of [What causes the DTLB_LOAD_MISSES.WALK_* performance events to occur?](https://stackoverflow.com/questions/52572922/what-causes-the-dtlb-load-misses-walk-performance-events-to-occur). `dTLB-load-misses` and `iTLB-load-misses` may not be counting TLB misses. – Hadi Brais Oct 29 '18 at 17:32
  • 1
    See also [Hardware cache events and perf](https://stackoverflow.com/questions/52170960/hardware-cache-events-and-perf). – Hadi Brais Oct 29 '18 at 17:35
  • 1
    `perf stat -e page-faults,dTLB-load-misses` will count both events on a vanilla kernel. You don't need to add your own instrumentation to count page faults. – Peter Cordes Oct 29 '18 at 19:26

0 Answers0