Sub topic 3
Cache Basics: Blocks, Lines, Hits, and Misses
Working model and prerequisites
Assume you know binary and hexadecimal numbers, powers of two, byte-addressable memory, word size, and the basic idea that a CPU fetches instructions and data from memory. You should also be comfortable with simple ratios and averages.
A cache is a small, fast memory between the CPU and a larger, slower memory. It stores copies of recently used memory data. The cache works because programs usually show locality:
- Temporal locality means that recently used data is likely to be used again soon.
- Spatial locality means that data near recently used data is likely to be used soon.
The cache does not usually store one byte at a time. It transfers a fixed-size group of adjacent bytes called a block. A block in the cache is held in a cache line, which also stores metadata such as a tag and a valid bit.
Hits and misses
When the CPU requests an address, the cache checks whether the block containing that address is present.
- A cache hit occurs when the requested block is present. The cache returns the requested byte or word quickly.
- A cache miss occurs when the block is absent. The system fetches the block from the next memory level, places it in the cache, and then supplies the requested data.
The time needed to obtain data after a miss is the miss penalty. It includes the time to access the next level and often the time to transfer the complete block.
| Event | Block present? | Result |
|---|---|---|
| Hit | Yes | Return data from the cache |
| Miss | No | Fetch the block, install it, then return data |
The first access to a block is often a miss, but later accesses to the same block can hit. Accesses to nearby addresses can also hit because one miss brings in several adjacent bytes.
Block size and cache lines
If the block size is 16 bytes, an access to address 0x2047 belongs to the block whose address range is 0x2040 through 0x204F. The cache fetches that whole range, even if the CPU needs only one byte.
Larger blocks can improve spatial locality because one miss brings in more neighboring data. However, large blocks also consume more cache capacity and take longer to transfer. They can even fetch data that the program never uses.
A cache line normally has three important parts:
- Data, containing the block copied from memory.
- A tag, identifying which memory block the line contains.
- A valid bit, indicating whether the line contains usable data.
Initially, valid bits are usually zero. Therefore, the first reference to each line cannot be treated as a hit.
A small trace
The following program counts hits for a fully associative cache with two lines. It uses the block number as the lookup key and replaces the oldest entry when the cache is full.
from collections import deque
def count_hits(block_trace, capacity):
cache = deque()
hits = 0
for block in block_trace:
if block in cache:
hits += 1
else:
if len(cache) == capacity:
cache.popleft()
cache.append(block)
return hits, len(block_trace) - hits
trace = [3, 3, 7, 3, 8, 7]
print(count_hits(trace, 2)) # (2, 4)The repeated block 3 produces a hit while it remains in the cache. The later reference to 7 misses because block 7 was evicted when block 8 arrived.
Exercise
A cache starts empty and has four lines. Each line holds one block. For the block trace [2, 5, 2, 9, 5], how many hits and misses occur if no replacement is needed?
Show answer
There are 2 hits and 3 misses. The second references to blocks 2 and 5 hit; the first references to 2, 5, and 9 miss.