Sub topic 6
Multilevel, Hashed and Inverted Page Tables
This chapter assumes you can already split a virtual address into page number and offset, and that you know a page table maps each page to a frame. Those are the paging and TLB ideas from the earlier chapters. Here we deal with a problem they create: the page table itself can be too big.
Why a single-level table fails
Take a 32-bit virtual address and 4 KB pages. The offset takes 12 bits, so the page number takes 20 bits. The table needs 2^20 entries. With 4-byte entries that is 4 MB for every process, even if the process uses only a few pages.
The cost grows quickly with address width. A 64-bit address with 4 KB pages has 2^52 pages, so a flat table is impossible. Three designs address this:
- Multilevel tables page the page table itself.
- Hashed tables look up entries by hashing the page number.
- Inverted tables keep one entry per physical frame instead of one per virtual page.
Multilevel (hierarchical) page tables
The idea is to treat the 4 MB table as ordinary data and split it into page-sized pieces. For the 32-bit example:
- The 4 MB table divides into 4 MB / 4 KB = 1024 pieces, each exactly one page.
- An outer page table with 1024 entries points to those pieces. It takes 1024 × 4 B = 4 KB, which is one page.
- The 20-bit page number splits into 10 bits for the outer index and 10 bits for the inner index.
The virtual address becomes 10 | 10 | 12 bits.
The savings come from sparse address spaces. Inner tables are allocated only for regions the process actually uses. Suppose a process has 8 MB of code and data at the bottom of memory and a 4 MB stack at the top, all aligned to 4 MB boundaries. Each inner table maps 1024 × 4 KB = 4 MB, so the process needs 3 inner tables plus the outer table. That is 4 × 4 KB = 16 KB, compared with 4 MB for a flat table.
Counting levels
Every table should fit in one page. This gives a formula:
- Entries per page = page size / entry size. Call its base-2 logarithm k, the number of index bits one level can resolve.
- Page number bits = virtual address bits − offset bits.
- Levels = ceil(page number bits / k).
def levels(va_bits, page_bytes, pte_bytes):
offset = page_bytes.bit_length() - 1 # log2(page size)
vpn = va_bits - offset # page-number bits
per_level = (page_bytes // pte_bytes).bit_length() - 1
n = -(-vpn // per_level) # ceiling division
return offset, vpn, per_level, n
print(levels(32, 4096, 4)) # (12, 20, 10, 2)
print(levels(46, 8192, 4)) # (13, 33, 11, 3)
print(levels(40, 4096, 4)) # (12, 28, 10, 3)
print(levels(48, 4096, 8)) # (12, 36, 9, 4)The 46-bit case divides exactly: 33 bits / 11 bits per level is 3 levels. The 40-bit case does not divide: 28 bits needs 3 levels of 10 bits, and the 2 extra bits are unused. The usual convention is to give the outermost table the leftover bits, here 8 bits (28 − 20), so the top table is smaller than a page. Check what the question says about this.
Splitting an address
This code splits the address 0x12345678 for the 10/10/12 layout:
va = 0x12345678
offset = va & 0xFFF # low 12 bits
inner = (va >> 12) & 0x3FF # next 10 bits
outer = va >> 22 # top 10 bits
print(f"outer={outer:#x} inner={inner:#x} offset={offset:#x}")
# outer=0x48 inner=0x345 offset=0x678The page number 0x12345 is the same as before. The two index fields are just its upper and lower 10 bits.
Cost in memory accesses
With n levels, a TLB miss costs n table accesses plus one data access, which is (n + 1) × m for memory time m. The TLB hides most of this. Using the chapter's effective access time (EAT) form:
EAT = h × (t + m) + (1 − h) × (t + (n + 1) × m)
For a two-level table with t = 10 ns, m = 100 ns and h = 0.9:
EAT = 0.9 × 110 + 0.1 × (10 + 300) = 99 + 31 = 130 ns.
The same machine with a single-level table would give 120 ns. The extra 10 ns is the price of the smaller table, and it shrinks as the hit ratio rises.
Hashed page tables
Hashed tables are common for address spaces wider than 32 bits. The hardware or OS hashes the virtual page number to pick a bucket. Each bucket holds a chain of entries, and each entry stores:
- the virtual page number,
- the frame number,
- a pointer to the next entry in the chain.
A lookup hashes the page number, then walks the chain comparing the stored page numbers with the requested one. A match gives the frame. No match means the page is not mapped, which is a page fault. The expected cost is about one access when chains are short. The table size follows the number of mapped pages, not the size of the address space.
Because the page number is stored in each entry, the comparison is exact. This is what makes hashing safe. Two pages that hash to the same bucket do not get confused.
A variant called a clustered page table lets each entry cover a block of consecutive pages, for example 16. This reduces chain overhead when memory use is sparse but locally dense.
Inverted page tables
A normal table has one entry per virtual page, per process. An inverted table flips this: there is one entry per physical frame, and one table for the whole system. Entry i describes frame i and holds:
- the process ID (pid) of the owner,
- the virtual page number stored in that frame,
- control bits such as valid and protection.
Translation searches for the entry whose pair (pid, page) matches the request. The index of that entry is the frame number. If no entry matches, the page is not in memory.
The table size depends only on physical memory. Take 4 GB of RAM with 4 KB frames, which gives 2^20 frames. With 8-byte entries the table is 2^20 × 8 B = 8 MB for the entire system. That total does not change with the number of processes or the width of the virtual address.
The costs are:
- A linear search through up to 2^20 entries is far too slow. Real systems put a hash table in front, keyed by (pid, page), so a lookup hashes to a short chain of inverted-table indexes.
- Each frame has exactly one (pid, page) entry, so sharing one frame between processes at different virtual addresses is hard.
- Pages that are not in memory need a separate structure, such as an external per-process table on disk, to record where they live.
Comparison
| Scheme | One entry per | Table size depends on | Lookup | Main weakness |
|---|---|---|---|---|
| Single level | Virtual page | Address space size | 1 access | Huge for large address spaces |
| Multilevel | Mapped region (inner tables) | Pages actually used | n accesses on a TLB miss | More accesses per miss |
| Hashed | Mapped page | Number of mapped pages | About 1 plus chain walk | Collisions, chain length |
| Inverted | Physical frame | Physical memory size | Needs hash or search | Sharing is hard |
Common errors
- Forgetting that the entry size sets how many entries fit in a page. A page with 8-byte entries holds half as many entries as one with 4-byte entries, which changes the bits per level and often the number of levels.
- Counting levels from the virtual address size instead of from the page-number bits. The offset bits never take part in indexing.
- Using the page size where the page table size is asked, or the reverse. A table that fits in one page is only one case.
- Adding the data access twice, or leaving it out. An n-level miss costs n + 1 accesses.
- Sizing an inverted table by the number of virtual pages. It is sized by the number of frames, and each entry must also hold the pid.
- Assuming an inverted table makes translation faster. It saves space, and the lookup needs extra hashing to stay fast.
Exercises
Exercise
A system has a 46-bit virtual address, 8 KB pages and 4-byte page table entries. Every page table must fit in exactly one page. How many levels are needed, and how many index bits does each level use?
Show answer
Offset = 13 bits, so the page number has 46 − 13 = 33 bits. A page holds 8 KB / 4 B = 2^11 entries, so each level resolves 11 bits. The number of levels is 33 / 11 = 3, with 11 index bits at each level.
Exercise
A two-level page table is used with a TLB. The TLB lookup takes 20 ns, one memory access takes 100 ns, and the TLB hit ratio is 80%. What is the effective access time?
Show answer
A hit costs 20 + 100 = 120 ns. A miss costs 20 + 3 × 100 = 320 ns, which is the TLB lookup, two table accesses and one data access. EAT = 0.8 × 120 + 0.2 × 320 = 96 + 64 = 160 ns.
Exercise
A machine has 2 GB of physical memory, 4 KB pages, and an inverted page table whose entries are 8 bytes each. How large is the table, and does the size change when the number of processes doubles?
Show answer
Frames = 2 GB / 4 KB = 2^19. The table size is 2^19 × 8 B = 4 MB. It does not change with the number of processes, because there is one entry per physical frame for the whole system.