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 ?