ยง19.1โ€“19.2TLB Basic Algorithm โ€ฆ Example: Accessing An Array

Part I OSTEP pp. 199โ€“202 ยท ~4 min read

  • tlb
  • tlb hit
  • tlb miss

Pagingโ€™s extra memory reference โ€” before every fetch, load, and store โ€” is prohibitively slow. Time to make the common case fast.

The Crux: How To Speed Up Address Translation

How can we speed up address translation, and generally avoid the extra memory reference that paging seems to require? What hardware support is required? What OS involvement is needed?

Help comes from the OSโ€™s old friend, the hardware: a translation-lookaside buffer (TLB) โ€” part of the MMU, and simply a hardware cache of popular virtual-to-physical translations (a better name would be address-translation cache). On every reference the hardware checks the TLB first; only when the translation is absent does anyone touch the page table. TLBs, in a real sense, make virtual memory possible.

19.1 TLB Basic Algorithm

The hardwareโ€™s control flow (linear page table, hardware-managed TLB โ€” Figure 19.1):

VPN = (VirtualAddress & VPN_MASK) >> SHIFT
(Success, TlbEntry) = TLB_Lookup(VPN)
if (Success == True)    // TLB Hit
    if (CanAccess(TlbEntry.ProtectBits) == True)
        Offset   = VirtualAddress & OFFSET_MASK
        PhysAddr = (TlbEntry.PFN << SHIFT) | Offset
        Register = AccessMemory(PhysAddr)
    else
        RaiseException(PROTECTION_FAULT)
else                    // TLB Miss
    PTEAddr = PTBR + (VPN * sizeof(PTE))
    PTE = AccessMemory(PTEAddr)
    if (PTE.Valid == False)
        RaiseException(SEGMENTATION_FAULT)
    else if (CanAccess(PTE.ProtectBits) == False)
        RaiseException(PROTECTION_FAULT)
    else
        TLB_Insert(VPN, PTE.PFN, PTE.ProtectBits)
        RetryInstruction()

A hit forms the physical address in a few cycles โ€” the TLB sits right next to the core. A miss pays the old price (the page-table access, an extra memory reference or more), installs the translation, and retries the instruction โ€” which now hits. The premise, as with all caches: misses are rare, hits are the common case.

19.2 Example: Accessing An Array

Ten 4-byte integers at virtual address 100, in an 8-bit address space with 16-byte pages (4-bit VPN, 4-bit offset):

Figure 19.2: ten 4-byte ints starting at VA 100, spread over three of sixteen 16-byte pages
VPN 00โ€“05 (unused here)VPN 00VPN 06 โ€” a[0] a[1] a[2] โ“˜VPN 06VPN 07 โ€” a[3] a[4] a[5] a[6] โ“˜VPN 07VPN 08 โ€” a[7] a[8] a[9] โ“˜VPN 08VPN 09โ€“15 (unused here)VPN 09

click a region marked โ“˜ for details

Now run for (i = 0; i < 10; i++) sum += a[i]; against a cold TLB โ€” counting only the array accesses:

The ยง19.2 trace, live: click a[0] through a[9] in order and watch the TLB fill (frame numbers illustrative: PFN = VPN + 16)

virtual address

100

VPN 6 ยท offset 4

โ†’

MMU: TLB first, page table only on a miss

no access yet โ€” click a preset (or โ–ถ access)

โ†’
โ€ฆ
TLB slotVPNPFNvalidnote
0โ€”โ€”0
1โ€”โ€”0
2โ€”โ€”0
3โ€”โ€”0

The presets are a[0]โ€ฆa[9] (VAs 100โ€“136). Run them in order: miss, hit, hit, miss, hit, hit, hit, miss, hit, hit โ€” 70%, from a stone-cold TLB, on data never touched before. Thatโ€™s spatial locality. Now run the ten again WITHOUT resetting: 100% โ€” temporal locality. Then reset and try any order you like.

miss, hit, hit, miss, hit, hit, hit, miss, hit, hit โ€” a 70% hit rate on the very first pass, thanks to spatial locality : array elements pack tightly onto pages, so only the first touch of each page misses. Doubling the page size would halve the misses; with realistic 4KB pages, dense array code misses roughly once per thousand accesses. And if the program loops over the array again soon after? Ten hits out of ten โ€” temporal locality , the quick re-reference of recently-used items.

Tip: Use Caching When Possible

Caching is one of the most fundamental performance techniques in computer systems โ€” the common case made fast by exploiting locality, temporal and spatial. So why not build one giant cache and keep everything in it? Physics. A fast cache must be small โ€” speed-of-light and related constraints see to it โ€” and any large cache is by definition slow. Weโ€™re stuck with small, fast caches; the art is using them well.

Check yourself

1.What exactly is a TLB, and where does it sit in the translation story?

2.Ten sequential accesses to the array (three pages: VPN 06, 07, 08) with a cold TLB. What's the pattern, and why isn't it 0% hits?

3.Walk the TLB-miss path in the hardware-managed algorithm (Figure 19.1).

4.Two knobs improve the array loop's TLB behavior further: bigger pages, and running the loop again soon. Why does each help?

5.If caches are so effective, why not build one big enough to hold all translations (or all data) and be done?

5 questions