Page fault notes

I have been doing some page fault analysis lately, and I decided to share what I have learned.

First, there are two basic types of page faults -- soft faults and hard faults.

A hard fault is a true page fault where the operating system needs to read data in from the disk in order to satisfy the request. These are the most costly faults.

Soft faults are relatively inexpensive. These occur when a process tries to access the virtual address, and the operating system can satisfy the request without reading in the page from disk. This can happen with pages that the program wants to be zero (called ‘demand zero’ pages), when a page is written to for the first time (‘copy on write’ pages) or if the page is already in memory somewhere else. The last situation occurs if a file is memory mapped into multiple processes or into multiple locations of the same process, and one of the other file references has already caused the data to be in physical memory.

The operating system keeps data on how many page faults have happened, and what kinds of faults are occurring. Tracing these counters could be a good way to look at the performance characteristics of your application. You can use the internal NtQuerySystemInformation API to retrieve the SYSTEM_PERFORMANCE_INFORMATION structure, which contains these counters. Unfortunately, this API is not documented, so you will need to buy a copy of Windows NT/2000 Native API Reference by Gary Nebbett if you want to do this (or search the web).

Alternatively, you can use the GetProcessMemoryInfo (https://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/getprocessmemoryinfo.asp) API to get the page fault count for a particular process. Unfortunately, this counter does not distinguish between hard faults and soft fault. Another interesting thing to note is that this API will exclude some internal operating system faults that your application is causing.