Sub topic 13
Compulsory, Capacity and Conflict Misses
This chapter assumes you can split an address into tag, index and offset, and that you know how direct-mapped, fully associative and set-associative caches place blocks. It also assumes you know LRU replacement. Here you will learn to say why a cache missed, and which design change would have prevented it.
Why classify misses
A miss counts the same in the AMAT formula whatever caused it. The cause still matters, because each cause has a different cure. Adding associativity does nothing for a miss that happens because the program never touched the block before. A larger cache does nothing for a miss caused by two blocks fighting over one line.
The classification is often called the "three Cs":
| Miss type | Cause | Typical remedy |
|---|---|---|
| Compulsory (cold) | First reference to a block. | Larger blocks, prefetching. |
| Capacity | The working set is larger than the whole cache. | Larger cache. |
| Conflict | Too many blocks map to the same set, though the cache has free space elsewhere. | Higher associativity, better placement. |
The three definitions
Compulsory misses
A compulsory miss happens on the first access to a block. No cache of any size or organization can avoid it, because the block has never been loaded. For a given trace and block size, the number of compulsory misses equals the number of distinct blocks referenced. It does not depend on cache size or associativity.
Capacity misses
A capacity miss happens when a block was loaded earlier and then evicted, because the program touched more distinct blocks than the cache can hold. Picture a fully associative cache with no placement restrictions. It still misses on this reference, because it ran out of room.
Conflict misses
A conflict miss happens when a block was evicted only because of the placement rule. A fully associative cache of the same total size would still hold it. Conflict misses exist only in direct-mapped and set-associative caches. A fully associative cache has none by definition.
The classification rule
The standard operational test uses a reference model: a fully associative cache with the same number of lines and the same block size, using LRU. Run it alongside the real cache. For each reference that misses in the real cache:
- If the block has never been referenced before, the miss is compulsory.
- Otherwise, if the fully associative model also misses, the miss is capacity.
- Otherwise, the model hits, so the miss is conflict.
Worked example by hand
Take a direct-mapped cache with 4 lines and 1-word blocks. The references are block numbers 0, 8, 0, 6, 8. The index is the block number mod 4, so blocks 0 and 8 both map to line 0, and block 6 maps to line 2.
| Ref | Line | Real cache | Seen before? | Fully associative (4 lines) | Result |
|---|---|---|---|---|---|
| 0 | 0 | miss | no | n/a | compulsory |
| 8 | 0 | miss, evicts 0 | no | n/a | compulsory |
| 0 | 0 | miss, evicts 8 | yes | hit (0 still resident) | conflict |
| 6 | 2 | miss | no | n/a | compulsory |
| 8 | 0 | miss, evicts 0 | yes | hit (8 still resident) | conflict |
The totals are 5 misses: 3 compulsory, 0 capacity and 2 conflict. Only 3 of the 4 lines were ever in use, so capacity was never the problem.
Simulating the classification
This program runs the real cache and the fully associative LRU reference model side by side. It prints the number of hits and of each miss type.
from collections import OrderedDict
def classify(addresses, block_size, num_sets, ways):
capacity = num_sets * ways
sets = [OrderedDict() for _ in range(num_sets)]
full = OrderedDict() # fully associative LRU reference model
seen = set()
counts = {"hit": 0, "compulsory": 0, "capacity": 0, "conflict": 0}
for a in addresses:
block = a // block_size
idx = block % num_sets
tag = block // num_sets
s = sets[idx]
real_hit = tag in s
full_hit = block in full
# update the real cache (LRU within the set)
if real_hit:
s.move_to_end(tag)
else:
if len(s) == ways:
s.popitem(last=False)
s[tag] = True
# update the reference model (LRU over all lines)
if full_hit:
full.move_to_end(block)
else:
if len(full) == capacity:
full.popitem(last=False)
full[block] = True
if real_hit:
counts["hit"] += 1
elif block not in seen:
counts["compulsory"] += 1
elif not full_hit:
counts["capacity"] += 1
else:
counts["conflict"] += 1
seen.add(block)
return counts
# Worked example: direct-mapped, 4 lines
print(classify([0, 8, 0, 6, 8], 1, 4, 1))
# {'hit': 0, 'compulsory': 3, 'capacity': 0, 'conflict': 2}
# Cyclic scan of 3 blocks through a 2-line fully associative cache
print(classify([0, 1, 2, 0, 1, 2], 1, 1, 2))
# {'hit': 0, 'compulsory': 3, 'capacity': 3, 'conflict': 0}
# Blocks 0 and 4 collide in a direct-mapped cache with 4 sets...
print(classify([0, 4, 0, 4], 1, 4, 1))
# {'hit': 0, 'compulsory': 2, 'capacity': 0, 'conflict': 2}
# ...but coexist in a 2-way cache with 2 sets (same 4 lines in total...
# here 2 sets x 2 ways)
print(classify([0, 4, 0, 4], 1, 2, 2))
# {'hit': 2, 'compulsory': 2, 'capacity': 0, 'conflict': 0}The second run shows a capacity-only pattern. A loop over 3 blocks with only 2 lines under LRU evicts each block just before it is needed again, so every repeat reference misses. No placement rule is involved, so the conflict count is 0.
The third and fourth runs show a conflict-only pattern. The trace uses only 2 blocks, far below the capacity of 4 lines. Moving from direct-mapped to 2-way associativity removes both conflict misses and turns them into hits.
Edge cases and gotchas
- Fully associative caches have no conflict misses. Every miss there is compulsory or capacity. In GATE questions this gives a shortcut: misses in the fully associative cache are compulsory plus capacity, and conflict misses are the extra misses of a restricted cache over that count.
- The classification depends on the reference model. The standard model is fully associative with LRU. A different replacement policy in the reference can move a miss between capacity and conflict. Unless a question says otherwise, use LRU.
- A real hit can occur where the model misses. In the simulator, the real cache and the model are updated independently. Consider a direct-mapped cache with 2 lines and a trace that touches one set once, then many blocks of the other set. The first block stays in the real cache, but a fully associative LRU model with 2 lines would have evicted it. Only real misses are classified, so this does not break the rule. It does mean "conflict misses = real misses minus model misses" can come out negative for particular traces.
- Compulsory misses depend on block size. A larger block brings in neighboring words, so the number of distinct blocks falls and so do compulsory misses. Changing block size changes the trace's block numbers, so recount.
- Prefetching hides compulsory misses. It does not remove the first reference to the data. It moves the load earlier, so the CPU does not wait for it.
How design changes shift the miss mix
| Change | Compulsory | Capacity | Conflict |
|---|---|---|---|
| Larger cache (same block size and associativity) | unchanged | decreases | may decrease |
| Higher associativity (same size) | unchanged | unchanged | decreases |
| Larger block (same cache size) | decreases | may increase, since there are fewer lines | may increase, since there are fewer sets |
The last row is why block size has a sweet spot. A very large block cuts compulsory misses through spatial locality, but with a fixed cache size it leaves fewer lines. Blocks then compete more, and the miss ratio rises again.
Exercises
Exercise
A fully associative cache has 2 lines, 1-word blocks and LRU replacement. The block reference string is 0, 1, 2, 0. Classify each miss and give the totals.
Show answer
Blocks 0, 1 and 2 are each referenced for the first time, so those three misses are compulsory. At the final reference to 0, the cache holds 1 and 2, because 0 was the least recently used block and was evicted when 2 arrived. This is a miss on a block seen before, and the fully associative model is the cache itself, so it also misses. That makes it a capacity miss. Totals: 4 misses, 3 compulsory, 1 capacity, 0 conflict, 0 hits.
Exercise
A direct-mapped cache has 4 lines and 1-word blocks. The block reference string is 3, 7, 3, 7, 3, 7. Compute the hits and the miss counts by type. Then say what changes if the cache becomes 2-way set-associative with the same 4 lines.
Show answer
Blocks 3 and 7 both map to line 3 (3 mod 4 and 7 mod 4), so every reference evicts the other block. All 6 references miss. The first access to each of 3 and 7 is compulsory, so 2 compulsory. The remaining 4 are on blocks seen before, and a 4-line fully associative cache holds both blocks, so they are conflict misses. Totals: 0 hits, 2 compulsory, 0 capacity, 4 conflict. With a 2-way cache and 2 sets, the index is the block number mod 2, so 3 and 7 both go to set 1. That set has two ways and holds both blocks, so only the 2 compulsory misses remain and the other 4 references hit.