Sub topic 5
The TLB and Effective Access Time
This chapter assumes you know how paging splits a virtual address into a page number and an offset. It also assumes you know that a page table maps each page to a frame, and that you are comfortable with powers of two. Here you will learn why paging needs a cache for translations, how that cache works, and how to compute the average cost of a memory reference.
Why paging needs a cache
With a single-level page table in main memory, every reference by the CPU costs two memory accesses:
- Read the page-table entry (PTE) for the page, to find the frame number.
- Read or write the data at the physical address built from that frame number and the offset.
This doubles the cost of every load and store. With a k-level page table, the walk costs k accesses before the data access, so the total is k + 1.
Programs show locality of reference. They touch the same few pages again and again over short periods. A small, fast cache of recent translations therefore removes most page-table reads. That cache is the translation lookaside buffer (TLB).
How a TLB works
A TLB is a small hardware cache, usually 16 to 1024 entries. Each entry holds a page number (the tag) and the frame number it maps to. It is associative: hardware compares the requested page number against all tags at the same time, so a lookup takes one fast step rather than a search.
Each entry usually also holds the protection bits from the PTE, such as valid, read/write and user/supervisor. The MMU checks these bits on every access, even on a hit.
The translation steps are:
- The CPU issues a virtual address. The MMU splits it into page number p and offset d.
- The MMU looks up p in the TLB.
- On a hit, the frame number f comes straight from the TLB.
- On a miss, the MMU walks the page table in memory, gets f, and loads the entry into the TLB. If the TLB is full, it evicts an entry.
- The physical address is f combined with d, and the data access proceeds.
Context switches and ASIDs
A TLB entry is valid only for one process's page table. Page 5 of process A and page 5 of process B map to different frames. There are two ways to handle this:
- Flush the TLB on every context switch. This is simple, but the new process starts with a cold TLB and a burst of misses.
- Tag entries with an address-space identifier (ASID). A lookup matches only if both the page number and the current process's ASID match. No flush is needed, and entries from several processes can coexist.
On GATE questions, "TLB is flushed on context switch" signals that the hit ratio drops after each switch. Questions usually do not ask you to compute that effect.
Effective access time with a TLB
The effective access time (EAT) is the expected time per memory reference, averaged over hits and misses. Use these symbols:
- h is the TLB hit ratio.
- t is the TLB lookup time.
- m is the main-memory access time.
- k is the number of page-table levels (k = 1 for a single-level table).
A hit costs the TLB lookup plus one memory access: t + m. A miss costs the TLB lookup, then k page-table accesses, then the data access: t + k·m + m.
EAT = h · (t + m) + (1 − h) · (t + (k + 1) · m)Worked example
Take h = 0.90, t = 10 ns, m = 100 ns and a single-level table.
- Hit: 10 + 100 = 110 ns.
- Miss: 10 + 100 + 100 = 210 ns.
- EAT = 0.9 × 110 + 0.1 × 210 = 99 + 21 = 120 ns.
With a two-level table (k = 2), a miss costs 10 + 300 = 310 ns. Then EAT = 99 + 31 = 130 ns.
A shortcut
Expand the formula and collect terms:
EAT = t + m + (1 − h) · k · mEvery reference pays the TLB lookup and the data access. Only misses pay the extra k page-table reads. Check it on the example: 10 + 100 + 0.1 × 1 × 100 = 120 ns.
The shortcut also makes it easy to solve backwards for the hit ratio you need.
def eat(h, t, m, levels=1):
"""Effective access time with a TLB; a miss walks `levels` page-table levels."""
hit = t + m
miss = t + levels * m + m
return h * hit + (1 - h) * miss
def required_hit_ratio(target, t, m, levels=1):
"""Smallest hit ratio that achieves the target EAT."""
return 1 - (target - t - m) / (levels * m)
print(round(eat(0.9, 10, 100), 2)) # 120.0
print(round(eat(0.9, 10, 100, levels=2), 2)) # 130.0
print(round(required_hit_ratio(115, 10, 100), 2)) # 0.95The last line says: to get EAT down to 115 ns, the hit ratio must be at least 95%.
Variations that appear in questions
Question writers change the model in a few standard ways. Read the statement for these phrases.
| Wording in the question | How to adjust |
|---|---|
| "TLB access time is negligible" or "ignore TLB time" | Set t = 0. |
| "Page table has two (or three) levels" | Use k = 2 (or 3) in the miss cost. |
| "TLB lookup and memory access happen in parallel" or a cache hit replaces the memory access | Use the stated times. Do not add t where the question says it overlaps. |
| "Memory access time is m for each page-table access" | Each page-table level costs m. |
| Hit ratio given as a percentage | Convert it to a fraction before substituting. |
| "Page fault" also appears | Add a third case. That is covered in the chapter on page faults. |
If the question gives the miss time directly, such as "a TLB miss takes 220 ns in total", use that figure. Do not rebuild it from t and m.
TLB reach and what changes the hit ratio
The TLB reach is the amount of memory the TLB can map at once:
TLB reach = number of entries × page sizeA 64-entry TLB with 4 KB pages has a reach of 64 × 4 KB = 256 KB. If a program's working set is far larger than the reach, the hit ratio falls. You can raise the reach in two ways:
- Add more entries, which costs chip area and lookup speed.
- Use larger pages, such as 2 MB "huge pages". Each entry then covers more memory, but larger pages bring more internal fragmentation.
Locality also matters. A loop over a small array hits the TLB almost every time. A pointer-chasing walk over a huge linked structure misses often.
Common errors
- Forgetting the TLB lookup on a miss. The TLB is searched first in both cases, so a miss costs t plus the walk. A miss is not just "2m".
- Using 2m for a multilevel miss. With k levels the walk is k·m, and the data access is one more m. The miss cost is t + (k + 1)·m.
- Mixing up h and 1 − h. If the question gives the miss ratio, the hit ratio is its complement.
- Mixing units. Keep ns with ns. Convert microseconds or milliseconds first.
- Treating TLB reach as the TLB size in bytes. Reach is entries times page size. The entries themselves are tiny.
Exercises
Exercise
A system uses two-level paging. The TLB lookup takes 20 ns, each memory access takes 100 ns, and the TLB hit ratio is 95%. Find the effective access time.
Show answer
Hit cost = 20 + 100 = 120 ns. Miss cost = 20 + 2 × 100 + 100 = 320 ns. EAT = 0.95 × 120 + 0.05 × 320 = 114 + 16 = 130 ns. The shortcut gives the same result: 20 + 100 + 0.05 × 2 × 100 = 130 ns.
Exercise
With a single-level page table, a 4 KB page size, a 10 ns TLB and 100 ns memory, what hit ratio is needed for an EAT of at most 112 ns? Also, what is the reach of a 128-entry TLB?
Show answer
Use EAT = t + m + (1 − h) · k · m with k = 1: 112 = 110 + (1 − h) × 100, so (1 − h) = 0.02 and h = 0.98 (98%). The reach is 128 × 4 KB = 512 KB.