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

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_size bytes. The cache holds cache_size / line_size lines.
  • Set-associative: the lines are grouped into num_sets = cache_size / (line_size * associativity) sets of associativity lines. Every address belongs to exactly one set, and a lookup searches only that set.
  • Way: one slot in a set. With associativity 1 the cache is direct-mapped. When associativity * line_size equals cache_size it 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) or W addr (a write, Part 2). It touches only the line that contains addr.
  • addr is hexadecimal with a 0x prefix and lowercase digits, such as 0x1f40.
  • prefetch is 0 until Part 3.
  • On stdin, the first four lines are cache_size, line_size, associativity, and prefetch. 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.
LinePrinted when
HIT set taga demand access finds its tag among its set's ways
MISS set taga demand miss, and the set still has an empty way
MISS set tag EVICT olda demand miss evicts tag old from the same set
... WRITEBACKappended after EVICT old if the evicted line is dirty (Part 2)
HIT set tag PREFETCHEDthe first demand hit on a prefetched line (Part 3)
PREFETCH set tag, with the same EVICT and WRITEBACK suffixesthe prefetcher fills a line (Part 3)
TOTAL hits H misses M evictions Eonce, after the last command
  • The set index prints in decimal.
  • Tags print in lowercase hexadecimal with a 0x prefix and no leading zeros (tag zero is 0x0), the format of Python's hex().

Splitting an address

FieldFormulaMeaning
offsetaddress % line_sizethe byte within its line
set index(address / line_size) % num_setswhich set the line belongs to
tag(address / line_size) / num_setsthe 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 next log2(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 0x88 is 10 00 1000 in binary: offset 8, set 0, tag 0x2.

Part 1: Reads and LRU replacement

  1. The cache starts empty.
  2. 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.
  3. If the set holds fewer than associativity lines, the new line takes an empty way and nothing is evicted.
  4. 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 EVICT with the evicted tag.
  5. A hit and a fill both make the line the most recently used (MRU) line of its set.
  6. 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, and associativity are powers of two.
  • 4 <= line_size <= 4096, 1 <= associativity <= 16, and line_size * associativity <= cache_size <= 2^20, so num_sets is 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 the 0x prefix.

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.

  1. W addr looks up its line exactly as R does, counts as a hit or a miss, and updates recency the same way.
  2. A write miss first brings the line in, evicting exactly as a read miss does, then marks the new line dirty.
  3. A line filled by a read starts clean. A read hit leaves the dirty bit as it is.
  4. A line stays dirty until it is evicted, however many times it is written, so it is written back once.
  5. Memory receives the line only when a dirty line is evicted. That eviction appends WRITEBACK, as in MISS 0 0x2 EVICT 0x1 WRITEBACK. Evicting a clean line prints no WRITEBACK.
  6. A dirty line that is evicted and later brought back by a read is clean again.
  7. 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.

  1. After every demand miss on line number L, from R or W, the prefetcher brings line L + 1 into the cache if that line is not already there.
  2. Line L + 1 has set (L + 1) % num_sets and tag (L + 1) / num_sets, so it usually lands in the next set. When line L sits in the last set, line L + 1 wraps to set 0 with a tag one higher.
  3. The prefetch runs after the demand miss is handled. Its PREFETCH set tag line follows the MISS line, with EVICT old and WRITEBACK appended under the same rules as a demand fill.
  4. 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.
  5. A prefetch is not an access: it counts as neither a hit nor a miss, and never triggers another prefetch.
  6. If line L + 1 is already cached, the prefetcher does nothing, prints nothing, and leaves that line's recency unchanged.
  7. A demand hit never triggers a prefetch.
  8. A prefetch hit is the first demand access, read or write, that hits a prefetched line. It prints HIT set tag PREFETCHED and clears the mark, so later hits print a plain HIT.
  9. 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
  • P counts prefetch fills; Q counts prefetch hits.

Tests 8–10 cover this part.

Example
Input
128
16
2
0
6
R 0x0
R 0x40
R 0x4
R 0x80
R 0x44
R 0x88
Output
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
Explanation

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.