The TLB: a cache for address translation

Video 040 of 140 in Virtualization Foundations, in the unit “Memory: RAM, addresses and virtual memory”. 4:17, with English and Hindi audio.

Every memory access needs an address translation, which turns the address a program uses into a real place in RAM. Walking the full page tables each time would make memory far slower. So the processor keeps a small cache of recent translations, called the TLB. This article explains why that shortcut is needed, how a hit and a miss differ, why bigger pages help a large database, and how to list the TLB events that the processor offers on Ubuntu.

What you will learn

Why the processor needs a shortcut

Think of the TLB as a short notebook kept beside the processor. Before the processor walks the tables, it checks the notebook. A look in the notebook takes far less time than a walk through the tables, and every translation found there saves that walk.

How the TLB answers a translation request

The processor first checks the TLB. That stands for translation lookaside buffer, and it is a small cache of recent translations.

A hit is quick, because the cached answer is used. A miss is much slower, because the tables must be walked. A program that keeps reusing a few pages sees mostly hits. A program that jumps across a huge range sees many misses.

The TLB is small, so it holds only a limited number of answers. When it is full, an old entry makes room for a new one. The processor does this for every address it uses, so even a small gain on each access adds up over a busy program.

How bigger pages help a large database

One remedy for many misses is bigger pages. A page of two mebibytes, which is 2,048 kibibytes, covers 512 times as much memory as a four-kibibyte page, and it uses only one TLB entry.

Bigger pages are not free either. A page is handled as one whole unit, so a program that uses only part of a large page still ties up the whole of it. Scattered access still causes misses, so the pattern of access matters as much as the page size.

What goes wrong with the TLB

A program that jumps around a large memory can be slow even though the processor looks busy, because the processor is waiting on table walks. If you see slow work with a busy processor, look at the access pattern first.

How to list TLB events on Ubuntu

The perf tool comes from a package, so install it first. Once it is installed, its list option shows the events that the processor offers. Filtering the list for TLB shows the event names, which you can then count.

List the TLB events that perf can count
perf list | grep -i tlb

Counting the events is a first step only. A high number of TLB misses, read alongside slow work, tells you that address translation may be the cost to study.

Terms

TLB
Translation lookaside buffer, a small cache inside the processor that holds recent address translations.
Table walk
The steps the processor takes through the page tables to find the real place of an address in RAM.
Mebibyte (MiB)
One thousand twenty-four kibibytes, which is 1,048,576 bytes.

Check yourself

What happens when the TLB already holds the answer for an address?

The processor uses the cached translation and goes straight to RAM, with no table walk.

Why do bigger pages help a large database?

One TLB entry covers much more memory, so fewer entries are needed. The pattern of access still matters.

Why must old TLB entries be cleared when the kernel changes a mapping?

Otherwise the processor would use a translation that is no longer true, and the wrong memory could be used.

Previous: Pages and page tables. Next: Page faults: the kernel's on-demand help.