Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 is vaddr % page_size. Since page_size is 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 vpn mapped to frame pfn, 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, and huge_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.
ParameterMeaning
page_sizeSize of a page in bytes, a power of two
frame_countNumber of frames in physical memory
tlb_sizeMost entries the TLB can hold
huge_ratioPages per huge page; matters only in Part 3
CommandMeaningPrints
MAP vpn pfnMap page vpn to frame pfnnothing
UNMAP vpnRemove page vpnnothing
ACCESS vaddrTranslate vaddrone line, or two after a page fault (Part 2)
TLBShow the TLBTLB and its entries
RESERVE vpn countMake count pages valid without frames (Part 2)nothing
MAP_HUGE vpn pfnMap one huge page (Part 3)nothing
STATSReport the counters (Part 3)STATS hits=a misses=b minor=c major=d evictions=e

Part 1: Page table and TLB

Page table

  1. The page table maps page numbers to frames. A frame is free when no page is mapped to it.
  2. MAP vpn pfn sets page vpn's entry to pfn, replacing any entry it had. pfn is either free or already page vpn's frame.
  3. UNMAP vpn removes page vpn's entry, and does nothing for a page without one.
  4. A replaced or removed entry frees its old frame.

TLB

  1. The TLB caches translations: one entry per page, holding the page number and its frame.
  2. ACCESS vaddr looks in the TLB first. On a TLB hit it prints TLB_HIT paddr.
  3. 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.
  4. If the page has no entry, the access prints SEGFAULT vaddr and changes nothing. A real kernel would stop the process; the simulator continues with the next command.
  5. A miss that adds to a TLB already holding tlb_size entries 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.
  6. No stale translations: MAP and UNMAP remove page vpn's TLB entry if it has one, even when MAP gives the page the frame it already had. The remaining entries keep their order.

Output of TLB

TLB vpn:pfn vpn:pfn ...
  • One vpn:pfn per entry, from most to least recently used, separated by single spaces.
  • An empty TLB prints TLB empty.

Limits

  • At most 105 commands.
  • page_size is between 2 and 220. frame_count and tlb_size are between 1 and 105. huge_ratio is a power of two between 2 and 512.
  • Every vaddr is 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 pfn names an existing frame. The counts of all RESERVE commands 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 count makes pages vpn through vpn + count - 1 valid without mapping them, the way malloc reserves a virtual range. Pages already valid are unchanged.
  • A page is valid from the first MAP or RESERVE that names it until an UNMAP names it. A valid page is resident while the page table maps it to a frame.
  • An ACCESS to a page that is not valid still prints SEGFAULT vaddr.
  • In every part, MAP names 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:

  1. 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.
  2. Map the faulting page to that frame and print the fault line.
  3. Retry the access. The new mapping has no TLB entry yet, so the retry misses, adds the entry, and prints TLB_MISS paddr as 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, or MAJOR_FAULT vpn pfn if the page's contents are on disk because it was evicted.
  • If step 1 evicted a page, the first line ends with EVICT victim, where victim is 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.
  • UNMAP discards 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

  1. Page eviction keeps its own recency order, separate from the TLB's.
  2. A resident page is used when MAP or a page fault maps it, and on every ACCESS that translates through it, TLB hit or miss.
  3. 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:

  • vpn and pfn are multiples of huge_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

  1. An ACCESS inside 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.
  2. The physical address is pfn * page_size + (vaddr - vpn * page_size), where vpn and pfn are the huge page's first page and first frame.
  3. Huge-page entries share the TLB, its capacity, and its LRU order with ordinary entries.
  4. The TLB line prints a huge page's entry as Hvpn:pfn, again with its first page and first frame.
  5. Huge pages stay locked in memory, as mlock would 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_HIT lines.
  • b: TLB_MISS lines, including those printed by a retry after a page fault.
  • c and d: MINOR_FAULT and MAJOR_FAULT lines.
  • e: evicted pages.

Tests 8–10 cover this part.

Example
Input
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
Output
TLB_MISS 20580
TLB_HIT 20488
SEGFAULT 12288
TLB_MISS 4124
TLB_MISS 12328
TLB 0:3 7:1
TLB_MISS 20480
Explanation

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.