Avoiding page faults
A page fault occurs when an application reads or writes to memory that is not committed to physical memory. It is impossible (or very hard) to predict when a page fault will happen, so they are another source of non-determinism in computers.
Fortunately, there is a function that allows you to commit all the memory used by the process and lock it down so that it cannot cause a page fault. It is mlockall(2)
. These are its two flags:
MCL_CURRENT
: This locks all pages currently mapped.MCL_FUTURE
: This locks pages that are mapped in later.
You usually call mlockall
during the startup of the application with both flags set to lock all current and future memory mappings.
Tip
MCL_FUTURE
is not magic, in that there will still be a non-deterministic delay when allocating or freeing heap memory using malloc()
/free()
or mmap()
. Such operations are best done at startup and not in the main control loops.
Memory allocated on the stack is trickier...