Page Table and TLB Simulator
Prerequisites: Virtual Memory, Pages, and TLB, Page Faults and Latency
Every load the AAPL feed handler issues names a virtual address, which must become a physical one before the data cache can be searched. Simulate that step: a page table, a TLB in front of it, demand paging behind it, and huge pages.
- Frame: a page-sized block of physical memory (the article's physical page), numbered from 0.
- Page number and offset:
vpn = vaddr / page_size(integer division), and the offset isvaddr % page_size. Sincepage_sizeis a power of two, the split is a right shift by log2(page_size) and a mask of the low bits. - Physical address: with page
vpnmapped to framepfn,paddr = pfn * page_size + offset.
With 4096-byte pages, address 8292 is byte 100 of page 2. If page 2 is mapped to frame 5, the physical address is 5 * 4096 + 100 = 20580.
What to implement
process_commands(page_size, frame_count, tlb_size, huge_ratio, commands)returns the printed lines in command order. Each command is one line.- On stdin, the first four lines are
page_size,frame_count,tlb_size, andhuge_ratio; the fifth is the number of commands, one per line after it. - Every number in the input and the output is a decimal integer.
- There are three parts, each building on the previous ones. One solution must pass all ten tests.
| Parameter | Meaning |
|---|---|
page_size | Size of a page in bytes, a power of two |
frame_count | Number of frames in physical memory |
tlb_size | Most entries the TLB can hold |
huge_ratio | Pages per huge page; matters only in Part 3 |
| Command | Meaning | Prints |
|---|---|---|
MAP vpn pfn | Map page vpn to frame pfn | nothing |
UNMAP vpn | Remove page vpn | nothing |
ACCESS vaddr | Translate vaddr | one line, or two after a page fault (Part 2) |
TLB | Show the TLB | TLB and its entries |
RESERVE vpn count | Make count pages valid without frames (Part 2) | nothing |
MAP_HUGE vpn pfn | Map one huge page (Part 3) | nothing |
STATS | Report the counters (Part 3) | STATS hits=a misses=b minor=c major=d evictions=e |
Part 1: Page table and TLB
Page table
- The page table maps page numbers to frames. A frame is free when no page is mapped to it.
MAP vpn pfnsets pagevpn's entry topfn, replacing any entry it had.pfnis either free or already pagevpn's frame.UNMAP vpnremoves pagevpn's entry, and does nothing for a page without one.- A replaced or removed entry frees its old frame.
TLB
- The TLB caches translations: one entry per page, holding the page number and its frame.
ACCESS vaddrlooks in the TLB first. On a TLB hit it printsTLB_HIT paddr.- On a miss, if the page table maps the page, the simulator walks the table, adds the page's entry to the TLB, and prints
TLB_MISS paddr. - If the page has no entry, the access prints
SEGFAULT vaddrand changes nothing. A real kernel would stop the process; the simulator continues with the next command. - A miss that adds to a TLB already holding
tlb_sizeentries first removes the least recently used (LRU) entry, whose last use is the oldest. An entry is used when it is added and on each hit. - No stale translations:
MAPandUNMAPremove pagevpn's TLB entry if it has one, even whenMAPgives the page the frame it already had. The remaining entries keep their order.
Output of TLB
TLB vpn:pfn vpn:pfn ...
- One
vpn:pfnper entry, from most to least recently used, separated by single spaces. - An empty TLB prints
TLB empty.
Limits
- At most 105 commands.
page_sizeis between 2 and 220.frame_countandtlb_sizeare between 1 and 105.huge_ratiois a power of two between 2 and 512.- Every
vaddris between 0 and 248 - 1, so addresses, page numbers, and physical addresses can pass 231. Store them in 64-bit integers (std::int64_t). - Every
pfnnames an existing frame. Thecounts of allRESERVEcommands add up to at most 105.
Tests 1–3 cover this part. They use only MAP, UNMAP, ACCESS, and TLB.
Part 2: Demand paging and page eviction
With demand paging, a page can be valid before it has a frame.
RESERVE vpn countmakes pagesvpnthroughvpn + count - 1valid without mapping them, the waymallocreserves a virtual range. Pages already valid are unchanged.- A page is valid from the first
MAPorRESERVEthat names it until anUNMAPnames it. A valid page is resident while the page table maps it to a frame. - An
ACCESSto a page that is not valid still printsSEGFAULT vaddr. - In every part,
MAPnames a page that is either not valid or resident, never one that is valid without a frame.
An ACCESS to a valid page that is not resident is a page fault:
- Pick a frame. Take the lowest-numbered free frame if there is one. Otherwise evict the least recently used resident page: remove its page-table and TLB entries, write its contents to disk, and take its frame.
- Map the faulting page to that frame and print the fault line.
- Retry the access. The new mapping has no TLB entry yet, so the retry misses, adds the entry, and prints
TLB_MISS paddras in Part 1.
Output of a faulting ACCESS: two lines.
MINOR_FAULT vpn pfn TLB_MISS paddr
- The first line is
MINOR_FAULT vpn pfn, orMAJOR_FAULT vpn pfnif the page's contents are on disk because it was evicted. - If step 1 evicted a page, the first line ends with
EVICT victim, wherevictimis that page's number.
Minor or major
- A reserved page's first touch is a minor fault: the kernel only hands it a zeroed frame.
- An evicted page comes back with a major fault, because its contents must be read from disk. Once resident again, its contents count as in memory, so it takes another major fault only after another eviction.
UNMAPdiscards a page's disk copy along with its mapping. A page that is unmapped, reserved again, and accessed takes a minor fault.- Step 1 removes the victim's TLB entry before step 3 adds the new one, so the new entry pushes out another TLB entry only if the TLB is still full.
Eviction order
- Page eviction keeps its own recency order, separate from the TLB's.
- A resident page is used when
MAPor a page fault maps it, and on everyACCESSthat translates through it, TLB hit or miss. - The victim is the resident page whose last use is the oldest.
So a page whose TLB entry keeps hitting is recently used, even though those accesses never walk the page table.
Tests 4–7 cover this part.
Part 3: Huge pages and statistics
A huge page covers huge_ratio consecutive pages with one page-table entry and one TLB entry, so one entry reaches huge_ratio times as much memory. The article's 2 MB huge page is 512 pages of 4 KB.
MAP_HUGE vpn pfn maps pages vpn through vpn + huge_ratio - 1 to frames pfn through pfn + huge_ratio - 1, in order. The input guarantees:
vpnandpfnare multiples ofhuge_ratio.- None of those pages is valid beforehand, and all of those frames are free.
- No later command maps, unmaps, or reserves a page inside a huge page.
- At least one frame always lies outside every huge page.
Huge-page rules
- An
ACCESSinside a huge page looks up the huge page's single TLB entry, which hits for every address it covers. On a miss, the simulator walks the table and adds that one entry. - The physical address is
pfn * page_size + (vaddr - vpn * page_size), wherevpnandpfnare the huge page's first page and first frame. - Huge-page entries share the TLB, its capacity, and its LRU order with ordinary entries.
- The
TLBline prints a huge page's entry asHvpn:pfn, again with its first page and first frame. - Huge pages stay locked in memory, as
mlockwould keep them. They never fault, are never evicted, never appear in the page eviction order, and their frames are never free.
Output of STATS
STATS hits=a misses=b minor=c major=d evictions=e
Each field counts since the start:
a:TLB_HITlines.b:TLB_MISSlines, including those printed by a retry after a page fault.candd:MINOR_FAULTandMAJOR_FAULTlines.e: evicted pages.
Tests 8–10 cover this part.
4096 8 2 4 10 MAP 2 5 MAP 7 1 ACCESS 8292 ACCESS 8200 ACCESS 12288 ACCESS 28700 MAP 0 3 ACCESS 40 TLB ACCESS 8192
TLB_MISS 20580 TLB_HIT 20488 SEGFAULT 12288 TLB_MISS 4124 TLB_MISS 12328 TLB 0:3 7:1 TLB_MISS 20480
The first four lines are the configuration: 4096-byte pages, 8 frames, a TLB of 2 entries, and a huge-page ratio this example does not use.
Address 8292 is byte 100 of page 2, mapped to frame 5, so the first access misses and translates to 5 * 4096 + 100 = 20580. Address 8200 is also in page 2, so it hits.
Address 12288 is byte 0 of page 3, which has no entry.
Address 28700 is byte 28 of page 7, and the TLB now holds pages 7 and 2. The access to page 0 needs a third entry, so the least recently used one, page 2, is removed. The TLB line lists page 0 then page 7.
That is why the last access, to page 2 again, misses even though page 2 was used twice.