Set-Associative Cache Simulator
Prerequisites: The CPU Cache Hierarchy, Prefetching and Streaming Access, Cache Lines and Locality
A loop over MarketUpdate records reaches the hardware as a stream of loads and stores, one address each. Build the bookkeeping of one cache level and count that stream's hits, misses, and evictions exactly.
- Line: a block of
line_sizebytes. The cache holdscache_size / line_sizelines. - Set-associative: the lines are grouped into
num_sets = cache_size / (line_size * associativity)sets ofassociativitylines. Every address belongs to exactly one set, and a lookup searches only that set. - Way: one slot in a set. With
associativity1 the cache is direct-mapped. Whenassociativity * line_sizeequalscache_sizeit is fully associative (one set holds every line). - Demand access: a read or write the program makes, as opposed to the prefetches of Part 3.
What to implement
process_commands(cache_size, line_size, associativity, prefetch, commands)returns the printed lines, in order.- Each command is a demand access:
R addr(a read) orW addr(a write, Part 2). It touches only the line that containsaddr. addris hexadecimal with a0xprefix and lowercase digits, such as0x1f40.prefetchis 0 until Part 3.- On stdin, the first four lines are
cache_size,line_size,associativity, andprefetch. The fifth is the number of commands, and each remaining line is one command. - There are three parts, each building on the previous ones. One solution must pass all ten tests.
| Line | Printed when |
|---|---|
HIT set tag | a demand access finds its tag among its set's ways |
MISS set tag | a demand miss, and the set still has an empty way |
MISS set tag EVICT old | a demand miss evicts tag old from the same set |
... WRITEBACK | appended after EVICT old if the evicted line is dirty (Part 2) |
HIT set tag PREFETCHED | the first demand hit on a prefetched line (Part 3) |
PREFETCH set tag, with the same EVICT and WRITEBACK suffixes | the prefetcher fills a line (Part 3) |
TOTAL hits H misses M evictions E | once, after the last command |
- The set index prints in decimal.
- Tags print in lowercase hexadecimal with a
0xprefix and no leading zeros (tag zero is0x0), the format of Python'shex().
Splitting an address
| Field | Formula | Meaning |
|---|---|---|
| offset | address % line_size | the byte within its line |
| set index | (address / line_size) % num_sets | which set the line belongs to |
| tag | (address / line_size) / num_sets | the rest of the line number, stored in the way |
address / line_size(integer division) is the line number. The tag together with the set index identifies the line.- Every size is a power of two, so the fields are bit ranges: the low
log2(line_size)bits are the offset, the nextlog2(num_sets)bits the set index, and the high bits the tag. - Example: a 128-byte cache with 16-byte lines and 2 ways has 4 sets. Address
0x88is10 00 1000in binary: offset 8, set 0, tag0x2.
Part 1: Reads and LRU replacement
- The cache starts empty.
- A read searches its set's ways for its tag. If a way holds it, the access is a hit. Otherwise it is a miss, and the line is brought into the set.
- If the set holds fewer than
associativitylines, the new line takes an empty way and nothing is evicted. - If the set is full, its least recently used (LRU) line, the one whose most recent hit or fill is the oldest, is evicted and replaced. The miss line reports
EVICTwith the evicted tag. - A hit and a fill both make the line the most recently used (MRU) line of its set.
- Recency is kept per set. An access to one set never changes the order of another set's lines.
Output: one HIT or MISS line per access, then TOTAL with the counts of hits, misses, and evictions.
Limits
cache_size,line_size, andassociativityare powers of two.4 <= line_size <= 4096,1 <= associativity <= 16, andline_size * associativity <= cache_size <= 2^20, sonum_setsis a power of two of at least 1.- The cache holds at least two lines.
- At most 105 commands. Every address is below 232.
- Parse addresses into
std::uint64_t(64-bit unsigned).std::stoull(text, nullptr, 16)accepts the0xprefix.
Tests 1–4 cover this part. They use only R, and their prefetch is 0.
Part 2: Writes, dirty bits, and write-back
The cache is write-back: a write changes only the cached copy and sets the line's dirty bit, which records that memory's copy is now stale.
W addrlooks up its line exactly asRdoes, counts as a hit or a miss, and updates recency the same way.- A write miss first brings the line in, evicting exactly as a read miss does, then marks the new line dirty.
- A line filled by a read starts clean. A read hit leaves the dirty bit as it is.
- A line stays dirty until it is evicted, however many times it is written, so it is written back once.
- Memory receives the line only when a dirty line is evicted. That eviction appends
WRITEBACK, as inMISS 0 0x2 EVICT 0x1 WRITEBACK. Evicting a clean line prints noWRITEBACK. - A dirty line that is evicted and later brought back by a read is clean again.
- Dirty bits never change the victim: it is still the set's LRU line.
Tests 5–7 cover this part. Their prefetch is 0.
Part 3: A next-line prefetcher
With prefetch equal to 1, the cache runs a next-line prefetcher, the simplest form of the hardware prefetcher from Prefetching and Streaming Access.
- After every demand miss on line number
L, fromRorW, the prefetcher brings lineL + 1into the cache if that line is not already there. - Line
L + 1has set(L + 1) % num_setsand tag(L + 1) / num_sets, so it usually lands in the next set. When lineLsits in the last set, lineL + 1wraps to set 0 with a tag one higher. - The prefetch runs after the demand miss is handled. Its
PREFETCH set tagline follows theMISSline, withEVICT oldandWRITEBACKappended under the same rules as a demand fill. - Like a demand fill, the prefetched line takes an empty way or evicts its set's LRU line, and becomes the set's MRU line. It starts clean and carries a prefetched mark.
- A prefetch is not an access: it counts as neither a hit nor a miss, and never triggers another prefetch.
- If line
L + 1is already cached, the prefetcher does nothing, prints nothing, and leaves that line's recency unchanged. - A demand hit never triggers a prefetch.
- A prefetch hit is the first demand access, read or write, that hits a prefetched line. It prints
HIT set tag PREFETCHEDand clears the mark, so later hits print a plainHIT. - A prefetched line evicted before any demand access reaches it produces no prefetch hit.
Output: evictions caused by prefetches count in evictions, and TOTAL appends prefetches P prefetch_hits Q:
TOTAL hits H misses M evictions E prefetches P prefetch_hits Q
Pcounts prefetch fills;Qcounts prefetch hits.
Tests 8–10 cover this part.
128 16 2 0 6 R 0x0 R 0x40 R 0x4 R 0x80 R 0x44 R 0x88
MISS 0 0x0 MISS 0 0x1 HIT 0 0x0 MISS 0 0x2 EVICT 0x1 MISS 0 0x1 EVICT 0x0 HIT 0 0x2 TOTAL hits 2 misses 4 evictions 2
The first four lines are the configuration: a 128-byte cache with 16-byte lines and 2 ways, so 128 / (16 * 2) = 4 sets, and the prefetcher is off. With 16-byte lines and 4 sets, the tag is the address divided by 64.
All six addresses fall in set 0. 0x0 and 0x40 miss and fill both ways with tags 0x0 and 0x1. 0x4 is byte 4 of line 0, so it hits tag 0x0 and makes it the MRU line.
0x80 (tag 0x2) misses in a full set. The LRU line is now tag 0x1, so 0x1 is evicted even though it was filled after 0x0.
0x44 brings tag 0x1 back and evicts tag 0x0, which became the LRU line when 0x80 was filled. 0x88 hits tag 0x2.