Experiments and implementations · Intermediate

Observing mmap and first touch

Observe Linux anonymous mappings by separating address-space reservation, first reads and first writes.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Prepare an isolated anonymous mapping
  2. Run the three phases
  3. Interpret faults without inventing disk reads
  4. Repeat while preserving the comparison conditions
  5. Ask Logeia about this articleCheck your understanding

Prepare an isolated anonymous mapping

Use Linux and Python 3 to create a sixteen-mebibyte private anonymous mapping. The example does not alter a file or a database. Reserving a virtual address range does not imply that every page already has a dedicated physical page backing it. The experiment separates mapping creation, the first read from each page and the first write to each page. Keep enough memory available under any container limit, or reduce size before running the program.

Run the three phases

Save the code and execute it with an ordinary Python interpreter. The page size comes from the operating system rather than a hard-coded 4,096-byte assumption. Reading checks that untouched anonymous memory supplies zeroes; writing changes one byte in each page. Compare counter differences between phases because the reported values are cumulative process statistics. Interpreter activity also contributes faults, so do not assert that the measured difference exactly equals the number of mapped pages.

python
import mmap, os, resource
page = os.sysconf("SC_PAGE_SIZE")
size = 16 * 1024 * 1024
m = mmap.mmap(-1, size, flags=mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS,
              prot=mmap.PROT_READ | mmap.PROT_WRITE)
def faults():
    r = resource.getrusage(resource.RUSAGE_SELF)
    return r.ru_minflt, r.ru_majflt
print("mapped", faults())
assert sum(m[i] for i in range(0, size, page)) == 0
print("read", faults())
for i in range(0, size, page):
    m[i] = 1
print("written", faults())
m.close()

Interpret faults without inventing disk reads

A rise in minor faults does not establish disk I/O. Providing zero-filled anonymous memory and preparing writable pages can require fault handling without fetching a page from storage. Major faults indicate paging activity requiring I/O, but this experiment does not guarantee an exact zero across the entire process in every environment. Other memory accesses and system conditions contribute to statistics. In particular, neither a minor fault nor the word fault by itself means that the program malfunctioned.

Repeat while preserving the comparison conditions

Repeat the page walk on the same mapping to contrast first touch with later access. Then vary the size while recording page size, huge-page behavior and memory pressure as relevant environmental conditions. This small anonymous-memory example cannot predict file-backed mapping latency or a production server’s memory behavior. Close the mapping when finished and retain the interpreter and kernel versions with any observations. Distinguish the mechanism being tested from the particular counter values obtained on one run.

For a second-access comparison, reuse the same mapping rather than creating a replacement. Remapping creates a new first-touch situation and changes the question being tested. If you reverse the read/write order, record it as a different experiment. Keep unrelated large allocations outside the measured phases so that the process-level counters are easier, though still not perfectly isolated, to interpret.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 16

Understand the systems behind the web · STEP 6

Prepare for AI systems · STEP 6

Choose a learning path