Virtual Memory and the TLB
Every program uses pretend addresses that the hardware translates, a page at a time, into real ones. A small cache of translations, the TLB, makes that nearly free, and the average cost follows the same hit-time-plus-misses-times-penalty pattern as any cache, except that a page fault's penalty is so enormous it has to be vanishingly rare.
What the symbols mean
- Offset bits
Bits that pick a byte inside a page.
- Page size
Bytes per page, a power of two (4 KiB is typical).
- Virtual address bits
Bits in a virtual address.
What the symbols mean
- Effective access time
Average time per memory access, translation included.
- TLB time
Time to look up the TLB, paid on every access.
- Memory time
Time for one memory access.
- TLB hit rate
Fraction of accesses whose translation is in the TLB.
- Page-table levels
Memory reads needed to walk the page table on a TLB miss (1 for a simple table, 4 on many 64-bit machines).
What the symbols mean
- Page-fault rate
Fraction of accesses whose page isn't in memory at all.
- Fault service time
Time to bring the page in from SSD or disk: milliseconds.
- Normal access time
Time for an access whose page is in memory.
Explanation
Virtual memory is simply a layer of pretend addresses: every program behaves as if it had the whole memory to itself, starting at address , and the hardware quietly translates each of those virtual addresses into a real, physical one. Memory is chopped into fixed-size pages ( KiB is typical), and translation works a page at a time, using the same trick as a cache: split the address into a page number (which page) and an offset (which byte inside the page). Only the page number gets translated; the offset rides along unchanged. The TLB (translation lookaside buffer, a clumsy name for "a small cache of recent translations") makes that fast. The math here is the average-access-time idea from Hit Rate and AMAT, one more time.
Why bother? Three reasons. Isolation: one program can't touch another's memory, because the other program's pages simply aren't in its translation table. More memory than you have: pages nobody is using can sit on the SSD. And simplicity: every program can be built as if it starts at the same address.
Page number and offset. With pages of bytes, the low bits are the offset and the other bits are the virtual page number (VPN). For -bit addresses and KiB pages, and the VPN is bits, so there are , about a million, pages. Translation swaps the VPN for a physical frame number (a frame is a page-sized slot of real memory) and keeps the offset.
The page table, and why it's slow. The translations live in memory, in a page table with one entry per virtual page: entries of bytes is MiB, for every running program. Worse, reading the page table is itself a memory access, so a naive machine would pay two memory accesses for every one the program asks for. And -bit machines use a tree of tables, typically levels deep, so finding one translation (a page walk) can take extra reads.
The TLB is a cache for translations. It works for the reason every cache works: locality. A KiB page holds ints, so a loop walking an array uses the same translation times in a row. A TLB with just entries often hits more than of the time.
Effective access time. The effective access time (EAT) is the average time per memory access, translation included. It's an expected value again. Every access checks the TLB, . On a hit (probability ), one memory access gets the data: . On a miss, walk the page table first, memory reads, and then get the data: . Weight them and simplify:
Notice it's the same shape as AMAT: a floor you always pay, plus the miss rate times a penalty, and here the penalty is the page walk, . (Real machines also keep page-table entries in their data caches, which shortens the walk; in our problems each level is a full memory read unless the problem says otherwise.)
Page faults: when the page isn't there at all. Sometimes the page table says the page isn't in memory; it's out on the SSD or disk. Then the operating system has to stop the program and fetch it, a page fault. That takes milliseconds: ms is ns, eighty thousand times a ns memory access. With fault rate ,
Ask how small must be to keep the slowdown under , so EAT ns: gives , so . At most one fault in about accesses. That's why the fault rate must be tiny, and why a machine that runs out of RAM "thrashes": faults stop being rare and everything grinds almost to a halt.
A common mistake. It's tempting to treat a TLB miss and a page fault as the same thing, since both mean "the translation wasn't handy." They're wildly different. On a TLB miss, the translation exists in the page table in memory; the hardware walks it, costing a few hundred nanoseconds at most. On a page fault, the page isn't in memory at all; the operating system loads it from storage, costing milliseconds, ten thousand times worse. Another one is translating the offset. The offset never changes: a page and its frame are the same size, so byte of the page is byte of the frame.
There's a graph further down showing how the effective access time grows as the TLB hit rate drops.
See it
Worked examples
A machine has -bit virtual addresses, KiB pages and -byte page-table entries. How are the address bits split, and how big is one program's page table? If virtual page maps to physical frame , what physical address does become?
- 1
The offset picks a byte inside a page. A page is KiB bytes, so the offset takes bits.
A TLB lookup takes ns, a memory access takes ns, and the page table has one level. (a) Find the effective access time with a TLB hit rate. (b) A page fault takes ms to service. Ignoring the TLB, what's the largest page-fault rate that keeps the average access within of ns?
- 1
Start with a TLB hit. The translation comes straight from the TLB, then one memory access gets the data: ns.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A machine has -bit virtual addresses and KiB pages. How many bits is the virtual page number?
A TLB lookup takes ns, memory takes ns, the page table has one level, and the TLB hit rate is . What's the effective access time, in ns?
A -bit machine walks a -level page table on every TLB miss. The TLB takes ns, memory takes ns and the TLB hit rate is . What's the effective access time, in ns?
A -bit machine walks a -level page table on every TLB miss. The TLB takes ns and memory takes ns. What TLB hit rate keeps the effective access time at ns? Give it as a decimal.
A database scans a MiB table once, in order, on a machine whose TLB holds translations. With KiB pages, the first access to each page misses the TLB. How many TLB misses does the scan cause? (This is Locality's one-miss-per-block idea, with pages.)
Memory takes ns, and a page fault takes ms to service. To keep the average access within of ns, page faults can happen at most once in every how many accesses? (Answer to 3 significant figures.)
Which statement about TLB misses and page faults is right?
A machine has -bit virtual addresses, KiB pages and -byte page-table entries, with a single-level page table. How many MiB does one program's page table take?
The hardest one here. Get it, and you've got this lesson.
Every access first checks a ns TLB with a hit rate. On a TLB miss, the hardware walks a -level page table, and each of those reads goes through the data cache. The data access itself also goes through the cache. The cache has a ns hit time, a miss rate and a ns miss penalty. On top of that, one access in a million causes a page fault that takes ms. What's the effective access time, in ns?
Where it's used
Every phone, laptop and server runs on virtual memory, and the TLB numbers here drive real decisions. Databases and machine-learning frameworks ask the operating system for MiB or GiB "huge pages" so that a few dozen TLB entries cover gigabytes of data. Virtual machines add a second layer of translation, so a TLB miss can cost dozens of memory reads, which is why cloud providers care so much about TLB hit rates. And the page-fault arithmetic is why a computer that runs out of RAM suddenly becomes unusable: once faults stop being one in a million, each one costs as much as millions of ordinary accesses.