Page faults: the kernel's on-demand help

Video 041 of 140 in Virtualization Foundations, in the unit “Memory: RAM, addresses and virtual memory”. 4:09, with English and Hindi audio.

A page fault sounds like an error, but most of them are normal. It happens when a program touches a page of memory that is not yet ready in RAM, and the kernel fixes the problem so that the program can carry on. This article explains what a page fault is, why the kernel sets up memory only when it is first used, the difference between minor and major faults, and how to check the fault counts on an Ubuntu server.

What you will learn

What a page fault is

A page fault is the processor pausing a program to hand a memory request to the kernel. This happens when the page table has no usable answer for an address. The page table is the table that maps the addresses a program uses to real RAM.

The kernel looks at the cause, fixes what it can, and the program resumes at the same instruction. The program does not notice the fault, because the pause is brief.

Minor faults, major faults and invalid accesses

Why the kernel sets up memory only when it is used

The kernel sets up memory lazily, page by page, when a program first touches each page. A program that starts writing into new memory causes a minor fault on the first touch of each page, because nothing is mapped yet.

Pages that a program never touches are never set up, so no work or RAM is spent on them. A large database can ask for a big memory area and still start without setting up every page at once.

When faults become a problem

You notice slow work, a rising major fault count, and a program that crashes.

How to check page faults on Ubuntu

On Ubuntu, the ps command can show the fault counts of one process. Its minor and major columns count faults since that process started. This command shows the counts for your current shell.

Show the minor and major fault counts for this shell
ps -o pid,min_flt,maj_flt,comm -p $

For the whole system, the vmstat file in the proc folder keeps running totals since the machine booted. A rising major count while work slows is a sign to check whether a disk, or swap, is the cause.

Show the system-wide fault totals since boot
grep -E 'pgfault|pgmajfault' /proc/vmstat

Try it yourself

You need an Ubuntu machine. You will see the fault counts for your current shell, then the system-wide totals since the machine booted.

  1. Show the minor and major fault counts for the current shell, since it started.

    ps -o pid,min_flt,maj_flt,comm -p $

    What you should see: Minor and major fault counts for this shell, since it started.

  2. Show the system-wide fault totals since boot: all faults, and the major ones.

    grep -E 'pgfault|pgmajfault' /proc/vmstat

    What you should see: System-wide totals since boot: all faults, and the major ones.

Terms

Page fault
A pause in which the processor hands a memory request to the kernel, because the page table has no usable entry for an address.
Minor fault
A page fault where the data is already in RAM and only the mapping needs to be added.
Major fault
A page fault where the data must first be read from a disk, so the program waits.
Swap
Disk space used to hold memory pages when RAM runs short.

Check yourself

Is a minor page fault a sign that something is wrong?

No. The data is already in RAM, so the kernel only adds the mapping. That is quick and normal.

Why does a major page fault make a program wait?

The data must first be read from a disk, either from a file or from swap, so the program waits for that read to finish.

What should you check when work is slow and the major fault count keeps rising?

Check whether a disk or swap is the cause, because programs are waiting on disk reads.

Previous: The TLB: a cache for address translation. Next: Swap: when RAM overflows onto disk.