0

I want to experiment with hard page faults so I need a program to produce them and some tools to profile what is happening. However, every program I find/write produces no hard faults.

This is a typical program that should do the trick. Alloc a lot of memory (more than the physical, 16GB in my case), write at least 1 byte of each page, and read all the pages. At some point on the first loop old pages should start swapping to disk to make room for more pages, and reading them again on the second loop should produce hard faults since they should be on the disk.

//gcc -Wall main.c && ./a.out

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

#define PAGE_SIZE 4096
#define PAGES 262144
#define REQUESTS 32

int main(void){
  char *arr[REQUESTS];
  int sum = 0;

  for (int rq = 0; rq < REQUESTS; rq++){ 
    arr[rq] = malloc(PAGE_SIZE * PAGES); // 1 GB
    if (!arr[rq]){
      perror("malloc");
      return 0;
    }

    for (int i = 0; i < PAGES; ++i) 
      arr[rq][i * PAGE_SIZE] = 0;
  }

  for (int rq = 0; rq < REQUESTS; rq++)
    for (int j = 0; j < PAGES; j++)
      sum += arr[rq][j * PAGE_SIZE];

  return sum;
}

This is the output of /usr/bin/time -l ./a.out

       39.21 real        12.36 user        22.92 sys
10898886656  maximum resident set size
         0  average shared memory size
         0  average unshared data size
         0  average unshared stack size
  15545872  page reclaims
         0  page faults
         0  swaps
         0  block input operations
         0  block output operations
         0  messages sent
         0  messages received
         0  signals received
         1  voluntary context switches
     73193  involuntary context switches

From man libuv

...
uint64_t ru_minflt; /* page reclaims (soft page faults) (X) */
uint64_t ru_majflt; /* page faults (hard page faults) */
...

Activity Monitor shows page ins: 0 which I suppose means hard faults

htop shows lots of major faults (hard faults I guess) and no minor fault

MINFLT  MAJFLT  VIRT    RES     MEM%    TIME+   Command
0       8726220 36.1G   9.6G    59.7    0:18.50 ./a.out

Even though htop shows major faults, It's strange it shows no minor faults. So at this point, I don't know what tool to trust in.

(I'm working on Mac Catalina)

Any help will be appreciated.

0 Answers0