I am trying to write a program in C to run under Linux. Basically I am trying to draw a statistic that should show like: pid, number of process, page fault(major/minor) and total number of page faults.

val, pid, pagefault, number of processes, total number of pages faults(Majpr+Minor)

1     127    major           1                          2323

for an idea I took the code solution from Generate major page faults and its code:

#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>

int main(int argc, char ** argv) {
  int fd = open(argv[1], O_RDONLY);
  struct stat stats;
  fstat(fd, &stats);
  posix_fadvise(fd, 0, stats.st_size, POSIX_FADV_DONTNEED);
  char * map = (char *) mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED) {
    perror("Failed to mmap");
    return 1;
  int result = 0;
  int i;
  for (i = 0; i < stats.st_size; i++) {
    result += map[i];
  munmap(map, stats.st_size);
  return result;

this code does but giving too much things. I also saw this link sol: https://stackoverflow.com/questions/23302763/measure-page-faults-from-a-c-program but failed to make my way that how shd i get the page faults(Major/Minor). Anyone who can tell me how to get major and minor faults?

  • I would expect any profiler to provide this info. For one perf will give you major and minor faults using perf stat. – user1794469 Sep 8 '15 at 14:40
  • umm how to use or any example give? – Malik Sep 8 '15 at 15:13
  • i made some search like brendangregg.com/perf.html but example are not in language C – Malik Sep 8 '15 at 15:15
  • Sorry i'm on a phone so i cant give a proper answer but this page should get you started. perf.wiki.kernel.org/index.php/Tutorial#Counting_with_perf_stat – user1794469 Sep 8 '15 at 15:18
  • it looks like that i cannot write in C language. is it? i am trying to write in C language. – Malik Sep 9 '15 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.