Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure page faults from a c program

I am comparing a few system calls where I read/write from/to memory. Is there any API defined to measure page faults (pages in/out) in C ?

I found this library libperfstat.a but it is for AIX, I couldn't find anything for linux.

Edit: I am aware of time & perf-stat commands in linux, just exploring if there is anything available for me to use inside the C program.

like image 381
brokenfoot Avatar asked Apr 25 '14 20:04

brokenfoot


People also ask

How do you get a major page fault?

A major page fault is an exception that occurs when a process attempts to access memory in a way that exceeds its permissions. For example, if the program attempts to access data from an unmapped region of physical memory, or if it writes beyond the end of allocated virtual address space, a major page fault occurs.

What is page faults in operating system?

In computing, a page fault (sometimes called PF or hard fault) is an exception that the memory management unit (MMU) raises when a process accesses a memory page without proper preparations. Accessing the page requires a mapping to be added to the process's virtual address space.

Is page fault an interrupt?

So basically, a page fault is an error, an interrupt is a signal and you can use an interrupt to detect a page fault.


2 Answers

It is not an API as such, however I have had a lot of success by rolling my own and reading /proc/myPID/stat from within my C program which includes page fault statistics for my process, this allows me to monitor counts in real time as my program runs and store these however I like.

Remember that doing so can cause page faults in itself so there will be some inaccuracy but you will get a general idea.

See here for details of the format of the file: https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_MRG/1.3/html/Realtime_Reference_Guide/chap-Realtime_Reference_Guide-Memory_allocation.html

like image 147
Vality Avatar answered Sep 20 '22 14:09

Vality


There is getrusage function (SVr4, 4.3BSD. POSIX.1-2001; but not all fields are defined in standard). In linux there are several broken fields, but man getrusage lists several interesting fields:

long   ru_minflt;        /* page reclaims (soft page faults) */
long   ru_majflt;        /* page faults (hard page faults) */

long   ru_inblock;       /* block input operations */
long   ru_oublock;       /* block output operations */

The rusage is also reported in wait4 (only usable in external program). This one is used by /usr/bin/time program (it prints minor/major pagefault counts).

like image 42
osgx Avatar answered Sep 22 '22 14:09

osgx