1. Home
  2. Operating Systems
  3. Paging & Address Translation (TLB)

Paging & Address Translation (TLB)

Split a virtual address into page number and offset, look the page up in the TLB or page table, and build the physical address. Then compute the effective access time.

Interactive 3DIntermediate12 min readOSUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

Every memory access a program makes uses a virtual address. The CPU must translate it to a physical address in RAM — billions of times per second. Watch the TLB make that fast. Press Translate.

Pseudocode

  1. page ← address ÷ page size; offset ← address mod page size
  2. if page is in the TLB: frame ← TLB[page] // hit: fast
  3. else: // TLB miss
  4. frame ← page table[page] // one extra memory access
  5. if that entry is invalid: page fault → load the page from disk
  6. store (page, frame) in the TLB, evicting the least recently used
  7. physical address ← frame × page size + offset
Read this model as text · 32 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. Every memory access a program makes uses a virtual address. The CPU must translate it to a physical address in RAM — billions of times per second. Watch the TLB make that fast. Press Translate.
  2. Virtual memory is split into pages of 256 bytes and RAM into frames of the same size. The page table says which frame holds each page. The TLB is a tiny, very fast cache of recent translations — it starts empty.
  3. #1: address 1030 → page = 1030 ÷ 256 = 4, offset = 1030 mod 256 = 6. In binary the top 3 bits are the page number and the low 8 bits the offset.
  4. TLB miss: page 4 is not among the 0 cached translations. We must read the page table in main memory (slower).
  5. Page table entry 4 → frame 0.
  6. Store page 4 → frame 0 in the TLB. Physical address = 0 × 256 + 6 = 6.
  7. #2: address 520 → page = 520 ÷ 256 = 2, offset = 520 mod 256 = 8. In binary the top 3 bits are the page number and the low 8 bits the offset.
  8. TLB miss: page 2 is not among the 1 cached translations. We must read the page table in main memory (slower).
  9. Page table entry 2 → frame 7.
  10. Store page 2 → frame 7 in the TLB. Physical address = 7 × 256 + 8 = 1800.
  11. #3: address 1100 → page = 1100 ÷ 256 = 4, offset = 1100 mod 256 = 76. In binary the top 3 bits are the page number and the low 8 bits the offset.
  12. TLB hit! Page 4 is in the TLB → frame 0. No need to read the page table from memory.
  13. Physical address = frame × 256 + offset = 0 × 256 + 76 = 76.
  14. #4: address 300 → page = 300 ÷ 256 = 1, offset = 300 mod 256 = 44. In binary the top 3 bits are the page number and the low 8 bits the offset.
  15. TLB miss: page 1 is not among the 2 cached translations. We must read the page table in main memory (slower).
  16. Page table entry 1 → frame 2.
  17. Store page 1 → frame 2 in the TLB. Physical address = 2 × 256 + 44 = 556.
  18. #5: address 2000 → page = 2000 ÷ 256 = 7, offset = 2000 mod 256 = 208. In binary the top 3 bits are the page number and the low 8 bits the offset.
  19. TLB miss: page 7 is not among the 3 cached translations. We must read the page table in main memory (slower).
  20. The page table says page 7 is not in RAM: a page fault. The OS loads it from disk into a free frame (frame 1) — this takes milliseconds.
  21. Page table entry 7 → frame 1 (just loaded).
  22. Store page 7 → frame 1 in the TLB. Physical address = 1 × 256 + 208 = 464.
  23. #6: address 1050 → page = 1050 ÷ 256 = 4, offset = 1050 mod 256 = 26. In binary the top 3 bits are the page number and the low 8 bits the offset.
  24. TLB hit! Page 4 is in the TLB → frame 0. No need to read the page table from memory.
  25. Physical address = frame × 256 + offset = 0 × 256 + 26 = 26.
  26. #7: address 600 → page = 600 ÷ 256 = 2, offset = 600 mod 256 = 88. In binary the top 3 bits are the page number and the low 8 bits the offset.
  27. TLB hit! Page 2 is in the TLB → frame 7. No need to read the page table from memory.
  28. Physical address = frame × 256 + offset = 7 × 256 + 88 = 1880.
  29. #8: address 760 → page = 760 ÷ 256 = 2, offset = 760 mod 256 = 248. In binary the top 3 bits are the page number and the low 8 bits the offset.
  30. TLB hit! Page 2 is in the TLB → frame 7. No need to read the page table from memory.
  31. Physical address = frame × 256 + offset = 7 × 256 + 248 = 2040.
  32. TLB hits: 4 of 8 (hit ratio 50%). With a TLB lookup of 20 ns and a memory access of 100 ns: EAT = h·(20+100) + (1−h)·(20+2·100) = 170 ns, versus 200 ns with no TLB at all (the 1 page fault would add milliseconds on top).

Try this in the 3D model

  • Press Translate. Why is the third address (1100) a TLB hit when the first two were misses?
  • Find the page fault. Which free frame does the OS choose for page 7?
  • Switch the TLB to 2 entries and translate again. How does the hit ratio, and therefore the EAT, change?
  • Press Random addresses a few times. Why do most addresses fall into just a few pages?

Why translate addresses?

Every program believes it owns a large, continuous block of memory starting at address 0. Its virtual (logical) addresses don’t directly name bytes in RAM. The operating system and the CPU’s memory management unit (MMU) translate each one to a physical address. This lets many programs share RAM safely, lets a program be bigger than RAM, and removes the external fragmentation of contiguous allocation.

Pages, frames and the page table

Virtual memory is cut into fixed-size pages, and RAM into frames of the same size. A page can live in any free frame. The page table stores, for every page, which frame holds it, or that the page is not in RAM at the moment.

A virtual address splits into two parts:

virtual address  = | page number | offset |
physical address = | frame number | offset |     (offset copied unchanged)

With 256-byte pages the offset is the low 8 bits. Address 1030 = 4 × 256 + 6 → page 4, offset 6. If page 4 is in frame 0, the physical address is 0 × 256 + 6 = 6.

The TLB: a cache for translations

The page table lives in main memory, so a naive translation costs an extra memory access for every access a program makes, doubling memory time. The Translation Lookaside Buffer (TLB) is a tiny, very fast cache inside the CPU that remembers recent page → frame translations:

  1. TLB hit: the frame comes straight from the TLB.
  2. TLB miss: read the page table in memory, then store the translation in the TLB, evicting an old entry (often the least recently used).
  3. Page fault: the page-table entry says the page is not in RAM. The OS loads it from disk, which takes milliseconds. Which page to throw out when RAM is full is the subject of page replacement.

Worked example

Page size 256 bytes, a 4-entry TLB that starts empty, and the page table from the 3D model (page 7 is not in RAM). Addresses: 1030, 520, 1100, 300, 2000, 1050, 600, 760.

Address Page, offset TLB Frame Physical address
1030 4, 6 miss 0 6
520 2, 8 miss 7 1800
1100 4, 76 hit 0 76
300 1, 44 miss 2 556
2000 7, 208 miss + page fault 1 (loaded) 464
1050 4, 26 hit 0 26
600 2, 88 hit 7 1880
760 2, 248 hit 7 2040

4 hits out of 8 gives a hit ratio of 50 %.

Effective access time (EAT)

With TLB time t, memory time m and hit ratio h:

EAT = h · (t + m) + (1 − h) · (t + 2m)

The classic exam numbers are t = 20 ns, m = 100 ns, h = 80 %: EAT = 0.8 × 120 + 0.2 × 220 = 140 ns. Without a TLB every access would take 2m = 200 ns. Real programs reach hit ratios above 99 % thanks to locality: loops and nearby data keep reusing the same few pages.

Code

PAGE = 256
page_table = [5, 2, 7, None, 0, None, 3, None]     # page -> frame (None = on disk)
tlb, TLB_SIZE = [], 4                               # (page, frame), most recent last

def translate(addr):
    page, offset = divmod(addr, PAGE)
    for entry in tlb:
        if entry[0] == page:                        # TLB hit
            tlb.remove(entry)
            tlb.append(entry)
            return entry[1] * PAGE + offset, "hit"
    if page_table[page] is None:                    # page fault: load from disk
        used = {f for f in page_table if f is not None}
        page_table[page] = min(set(range(8)) - used)
    frame = page_table[page]
    if len(tlb) == TLB_SIZE:
        tlb.pop(0)                                  # evict least recently used
    tlb.append((page, frame))
    return frame * PAGE + offset, "miss"

for a in [1030, 520, 1100, 300, 2000, 1050, 600, 760]:
    print(a, translate(a))
# 1030 (6, 'miss')   520 (1800, 'miss')   1100 (76, 'hit')   300 (556, 'miss') ...

Bigger page tables

With 4 KB pages and 48-bit addresses, a flat page table would need 2³⁶ entries per process. Real systems use multi-level page tables (x86-64 uses 4 or 5 levels), so only the parts that are actually used take memory. A TLB miss then costs several memory reads, which makes the TLB even more important.

Common mistakes

  • Translating the offset. Only the page number changes; the offset is copied unchanged.
  • Forgetting the extra memory access for the page table on a TLB miss. That is the 2m in the formula.
  • Mixing up a TLB miss (cheap, fixed by reading the page table) with a page fault (expensive, needs the disk).
  • Calculating the page number with the wrong page size. Page size 2ⁿ means the low n bits are the offset.

Complexity at a glance

Case / operationTimeWhy
TLB hitt + mTLB lookup, then the actual memory access.
TLB miss (page table in memory)t + 2mExtra memory access to read the page table.
Effective access timeh(t + m) + (1 − h)(t + 2m)h = TLB hit ratio.
Page faultmillisecondsThe page must come from disk — about 100,000× slower.

Quick check

Test yourself — pick an answer to see if you got it.

1. With 256-byte pages, which page and offset does virtual address 1030 have?

2. Page 4 is in frame 0, and the page size is 256 bytes. What is the physical address of virtual address 1030?

3. TLB lookup takes 20 ns, a memory access 100 ns, and the hit ratio is 80 %. What is the effective access time?

4. Why does a small TLB (64 entries or so) achieve hit ratios above 99 %?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Paging & Address Translation (TLB). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.