Project 19: Virtual Memory Simulator
Project 19: Virtual Memory Simulator
Make virtual memory visible by simulating page tables, a TLB, and page replacement on real address traces.
Quick Reference
| Attribute | Value |
|---|---|
| Difficulty | Advanced |
| Time Estimate | ~2 weeks |
| Language | C (Alternatives: C++, Rust) |
| Prerequisites | Comfortable with bitfields/masks, basic data structures |
| Key Topics | Page tables, TLB, page faults, replacement policies, dirty/writeback |
| CS:APP Chapters | 9 |
1. Learning Objectives
By completing this project, you will:
- Translate virtual addresses to physical frames using a multi-level page table model
- Implement a TLB cache and quantify hit rates vs. page-table-walk costs
- Implement FIFO/LRU/Clock replacement and compare fault/writeback tradeoffs
- Produce a reproducible report from a trace file (stats + optional visualization)
2. Project Specification
2.1 What You Will Build
A CLI called vmsim that consumes an address trace and simulates:
- Configurable page size (e.g., 4 KiB)
- Configurable physical frames (e.g., 4, 64, 1024)
- Optional TLB with capacity and replacement policy
- Page replacement policies: FIFO, LRU, Clock
- Dirty tracking and writeback accounting
2.2 Example Output
$ ./vmsim --frames=64 --page-size=4096 --tlb=32 --replace=clock trace.txt
accesses=100000 tlb_hits=87234 tlb_misses=12766
page_hits=99012 page_faults=988 writebacks=234
3. Solution Architecture
3.1 Data Model
- Page table: represent levels explicitly (arrays) or sparsely (hash maps) depending on trace size.
- Frame table: per-frame metadata
{vpn, valid, dirty, refbit, last_used}. - TLB: fixed-size entries
{vpn, pfn, valid, dirty/ref}+ policy metadata.
3.2 Address Breakdown (Typical x86-64-style)
Virtual Address:
[ VPN (high bits) | Offset (low bits) ]
page_size = 2^k => offset = k bits
Your simulator doesnโt need to exactly match a real CPUโs 4-level structure, but it should mimic the idea: hierarchical translation + caching.
4. Phased Implementation
- Parse trace lines into
{op, vaddr}and compute{vpn, offset} - Single-level page table + FIFO replacement
- Dirty bit + writeback accounting
- LRU via timestamps/counters (or an LRU list)
- Clock algorithm with reference bit
- Add a TLB, measure impact, and produce a summary report
5. Testing Strategy
- Tiny hand-written traces where you can predict faults/hits exactly.
- Property tests: frame count 1 behaves like โevery new vpn faultsโ.
- Cross-check replacement logic with an independent reference implementation for small traces.
6. Extensions
- Visualize โworking setโ size over time.
- Model copy-on-write and shared pages (two processes, same vpn mapping).
- Add huge pages and compare TLB hit rates.
7. Reference Reading
- CS:APP 3e โ Ch. 9 (Virtual Memory)
- Operating Systems: Three Easy Pieces โ memory chapters (replacement)