A cache simulator
The problem
Build a configurable cache simulator that replays a trace of memory addresses, and use it to find out how block size, associativity and replacement change the hit rate of a small program.
Requirements
- Read the cache size, block size and number of ways, and split every address into tag, index and offset
- Support direct-mapped, set-associative and fully associative caches with LRU replacement
- Count hits and misses and classify each miss as compulsory, capacity or conflict
- Generate traces for a row-by-row and a column-by-column walk of a 2-D array and compare them
Technologies
- Python 3
- Lists and dictionaries
- Bit shifts and masks
- AMAT formula
Step by step
- Write the function that splits an address into tag, index and offset.
- Implement a set with LRU order and test it on a short trace by hand.
- Add the miss classification and the hit rate and AMAT report.
- Run row-order and column-order traces and several cache shapes, and write down what you see.
Expected result
A program and a short table that show how cache shape and access order change hits, misses and average access time.
How it is evaluated
- Small traces give the same hits and misses as a hand simulation
- Direct-mapped is the special case of one way, with the same results
- Miss classes add up to the total number of misses
- The report explains each difference in terms of locality
Ways to improve it
- Add write-back with dirty bits and count the writebacks
- Add a second cache level and report AMAT
- Plot the hit rate against the cache size