0

I am trying to explore the page fault behavior of Linux. I made an lxc container with restricting the memory to 1GB (by adding 'lxc.cgroup.memory.limit_in_bytes = 1G' to /etc/lxc/default.conf). Then, I ran a simple code which accesses 2GB amount of data.

int main() {
  char* buf = malloc(1024*1024*1024);
  char* buf2 = malloc(1024*1024*1024);
  if (buf == 0 || buf2 == 0) {
    printf("Malloc failed!\n");
    return 0;
  }
  int i,j,k;
  for (i=0; i<1024; i++)
    for (j=0; j<1024; j++)
      for (k=0; k<1024; k++)
        buf[i*1024*1024 + j*1024 + k] = i+j+k;
  for (i=0; i<1024; i++)
    for (j=0; j<1024; j++)
      for (k=0; k<1024; k++)
        buf2[i*1024*1024 + j*1024 + k] = i+j+k;
  free(buf);
  free(buf2);
  while(1);
  return 0;
}

The code is compiled with -O0 and ran inside the container. When the program reaches the while(1);, I check how many page fault it experienced with

ps -eo maj_flt,cmd | grep a.out

Where a.out is the compiled executable. Sometimes I get 200~300 page faults; however, sometimes I only see 10~20 page faults. Because memory is only 1G, I think at least always 1G/4K = 256K page fault should be happening. Why am I only seeing 10~20 page fault sometimes? I confirmed my Linux uses 4K pages as default.

I am new to Linux. Any insights will be very helpful! Thank you.

1
  • you may want to run your test with /usr/bin/time -v /path/to/executable - it will display page faults (hard vs soft), etc. for the entire process run
    – ivanivan
    Sep 26, 2019 at 20:41

3 Answers 3

1

I figured out the problem.

  1. A major problem with my code was that on first write to the malloc'ed page, page fault does not occur because Linux does not have to read an empty page from the disk. I changed the code so that it runs the looping part of the code twice.
  2. Also, I disabled Linux readahead (by echo "0" >> /proc/sys/vm/page-cluster)

With the two changes, I was able to see roughly 2G / 4K = 524,288 page faults (precisely 524,304).

0

Why would you see page faults on memory being written for the first time for every page? It's more-or-less the worst possible result for the virtual memory subsystem. As a result any optimization, caching, preloading, detecting access pattern, etc. is going to reduce that number. I would be surprised if ANY operating system reliably gave you the calculated 256 thousand page faults if you tried this.

0

You compiled it with optimization enabled and the compiler removed your loops, clang will transform your entire program to:

int main() {
  while(1);
}
1
  • I compiled with -O0, which is all optimization turned off
    – kwmaeng
    Sep 26, 2019 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .