A cache (also refer to as L1, L2 or L3 cache) is a SRAM near the CPU that help improving DRAM data access. In the other hand, a page cache (or disk cache) is a transparent cache for the pages originating from a secondary storage device such as a hard disk drive. Additionnaly, the linux operating system build the page cache by using unused DRAM memory.
I found many code around which can produce cache miss on SRAM (ie. L1, L2 or L3), I am interested on a C code to generated cache miss at the level of the page cache (or is it better to says page-fault?).
My understanding is that such a code have to deal with file system operations such as read and write. So I design a code to read data from file with the O_DIRECT flag, the idea is that each read operation will request access to the disk a therefore cause a cache-miss in the page cache. Here is the code where /tmp/data is a 4 KB (a size of a page) file full of 0 created using dd if=/dev/zero of=/tmp/data count=4K
:
#include <stdio.h>
#include <time.h>
#define DATA_SIZE 10000
int main(){
char page_data[DATA_SIZE];
clock_t begin, end;
double time_spent;
int lap;
int fd;
fd = open("/tmp/data", "O_DIRECT", "r");
for(lap=0; lap < 10; lap++){
begin = clock();
read(fd, page_data, DATA_SIZE);
end = clock();
time_spent = (double) (end - begin); // / CLOCKS_PER_SEC;
printf("lap %d : time spent : %f data : %s \n", lap, time_spent, page_data);
lseek(fd, 0, SEEK_SET);
}
close(fd);
return 0;
}
Here is the output of the code with additionnal information provided by the perf-tool
perf stat -B -e cpu-clock,task-clock,task-clock,cycles,context-swtiches,cpu-mogrations,page-faults,cache-references,cache-misses ./read_a_file
lap 0 : time spent : 1.000000 data :
lap 1 : time spent : 1.000000 data :
lap 2 : time spent : 1.000000 data :
lap 3 : time spent : 0.000000 data :
lap 4 : time spent : 0.000000 data :
lap 5 : time spent : 0.000000 data :
lap 6 : time spent : 1.000000 data :
lap 7 : time spent : 0.000000 data :
lap 8 : time spent : 0.000000 data :
lap 9 : time spent : 1.000000 data :
Performance counter stats for './read_a_file':
0,282623 cpu-clock (msec)
0,282361 task-clock (msec) # 0,024 CPUs utilized
0 cycles # 0,000 GHz
3 context-switches # 0,011 M/sec
0 cpu-migrations # 0,000 K/sec
54 page-faults # 0,191 M/sec
0 cache-references # 0,000 K/sec
0 cache-misses # 0,000 K/sec
0,011596405 seconds time elapsed
The problem is that perf shows that no cache-misses occured during the code which I found surprising even if I can not explain why. Otherwise 54 page-faults occured, which I also find strange since my code only ask for 1 file (which size is a page size) only 10 times (not 54 time).
So my question is how to design a better code to systematically generated a cache-miss on page cache (or page-fault) ?
Any comment is welcome