The operating system has many mechanisms to operate on memory and using it in a proper way, one of them is page faults. In this article, we will look at what is a page fault and you should keep a track of it properly.
What is a page fault?
A page fault is an exception raised by a program when it tries to access a block of memory that is not present on RAM. Reacting to this fault, the operating system has to bring in the data that it needs to work on in RAM from disk and then it can move ahead.
Are page faults costly?
Page faults need to read the data from disk and then put it in the memory, this will take some CPU cycles as well as IO to accomplish this. Here is an answer on StackOverflow which says it takes around 2000~ CPU
cycles to get this done.
https://stackoverflow.com/questions/10223690/cost-of-a-page-fault-trap
You can also read this thread
https://www.reddit.com/r/linux/comments/24gj12/the_cost_of_linuxs_page_fault_handling/
Now say if your programs are frequently triggering page fault, how many CPU cycles this will consume, and how it will affect your system.
How you can see page faults?
You can look in the vmstat file for a page fault.
grep pgfault /proc/vmstat
This will be the total number of page faults that happened. You need to see the rate of page faults. You will get better stacks with the below command
sar -B 1 10
08:41:41 PM IST pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff
08:41:42 PM IST 0.00 0.00 1115.00 0.00 4538.00 0.00 0.00 0.00 0.00
08:41:43 PM IST 0.00 24.00 330.00 0.00 2956.00 0.00 0.00 0.00 0.00
08:41:44 PM IST 0.00 2628.00 507.00 0.00 2550.00 0.00 0.00 0.00 0.00
08:41:45 PM IST 0.00 0.00 1151.00 0.00 1524.00 0.00 0.00 0.00 0.00
08:41:46 PM IST 0.00 0.00 186.00 0.00 723.00 0.00 0.00 0.00 0.00
08:41:47 PM IST 0.00 0.00 198.00 0.00 494.00 0.00 0.00 0.00 0.00
08:41:48 PM IST 0.00 0.00 517.00 0.00 1056.00 0.00 0.00 0.00 0.00
08:41:49 PM IST 0.00 480.00 154.00 0.00 2338.00 0.00 0.00 0.00 0.00
08:41:50 PM IST 252.00 464.00 10383.00 2.00 6236.00 0.00 0.00 0.00 0.00
08:41:51 PM IST 0.00 0.00 675.00 0.00 339.00 0.00 0.00 0.00 0.00
Average: 25.20 359.60 1521.60 0.20 2275.40 0.00 0.00 0.00 0.00
You have to make sure that, your program doesn’t trigger a lot of page faults and that’s it for this article.
You can also read about https://serverfault.com/questions/273201/what-is-a-lot-of-page-faults
If you like the article please share and subscribe.