Sub topic 4
Hit ratio, miss ratio and miss penalty
This chapter assumes you know what a cache block, a hit and a miss are (from the previous chapter), can work with powers of two, and can compute a weighted average. Here we turn "hits and misses" into numbers: the hit ratio, the miss ratio and the miss penalty. These are the inputs to the average memory access time formula in the next chapter.
Counting hits and misses
Every memory reference the CPU makes to the cache ends in exactly one of two outcomes.
- A hit means the requested block is in the cache and the data is returned from it.
- A miss means the block is not in the cache. The block must be fetched from the next level, and then the data is delivered.
Given a trace of N references with H hits and M misses, N = H + M. All the metrics in this chapter come from these counts.
Hit ratio and miss ratio
The hit ratio (also called hit rate) is the fraction of references that hit:
hit ratio h = H / N
The miss ratio (miss rate) is the fraction that miss:
miss ratio m = M / N = 1 − h
Both are numbers between 0 and 1. GATE questions often give them as percentages, such as "a hit rate of 90%". Convert to a fraction before you put the value in a formula.
def ratios(hits, misses):
total = hits + misses
h = hits / total
return h, 1 - h
print(ratios(90, 10)) # (0.9, 0.09999999999999998)
print(ratios(2, 2)) # (0.5, 0.5)The first line prints a floating-point artifact (0.0999...). Treat it as 0.1. In an exam you work with exact fractions anyway.
Miss penalty
The miss penalty is the extra time a miss costs compared with a hit. It is the time to fetch the block from the next level of the hierarchy and deliver it to the CPU. Its size depends on three things:
- The access time of the next level (for example, main memory).
- The block size, because a larger block takes more transfers to move.
- The width of the bus between the levels.
Suppose main memory takes 100 ns to start an access and then delivers 4 bytes every 10 ns after that. A 16-byte block needs four transfers. The fetch time is then 100 + (4 − 1) × 10 = 130 ns, or 100 + 4 × 10 = 140 ns if the first transfer is also counted separately. Read the question carefully. GATE statements usually say outright how block transfer time is counted, and you must follow their model rather than your own.
Two common ways of stating the miss penalty need to be kept apart.
| Style | Meaning | Example |
|---|---|---|
| Miss penalty as extra time | Time added on top of the hit time | Hit time 2 ns, miss penalty 50 ns, so a miss costs 52 ns in total |
| Miss time as total time | Total time for an access that misses | A miss takes 52 ns in total, and the hit time is already included |
Most textbooks use the first style: AMAT = hit time + miss ratio × miss penalty. Some GATE questions give the second style (the full time of a miss). The two forms agree only if you subtract the hit time correctly. The next chapter works through this in detail.
Why a few percent of misses matter
A small miss ratio does not mean a small effect. Memory is much slower than cache, so misses dominate the average time.
def avg_time(t_hit, miss_ratio, miss_penalty):
return t_hit + miss_ratio * miss_penalty
# Cache hit 1 ns, memory penalty 100 ns
for m in (0.0, 0.01, 0.05, 0.10):
print(m, avg_time(1, m, 100))The output is:
0.0 1.0
0.01 2.0
0.05 6.0
0.1 11.0A 1% miss ratio doubles the average access time. A 10% miss ratio makes the system eleven times slower than a perfect cache. This is why designers work so hard on the hit ratio.
Worked example: from a trace
A cache is direct-mapped with four lines and a block size of one byte. The CPU reads addresses 0, 1, 2, 3, 0, 1, 2, 3, 4, 0.
- Addresses 0, 1, 2, 3 each miss the first time. These are four misses, and they fill all four lines.
- The second pass over 0, 1, 2, 3 hits four times.
- Address 4 maps to line 0 (4 mod 4 = 0), so it misses and replaces address 0.
- Address 0 then maps to line 0 again, and it misses because address 4 evicted it.
Total: 10 references, 4 hits, 6 misses. Hit ratio = 4/10 = 0.4 and miss ratio = 0.6.
You can check this with a short simulation.
def direct_mapped_trace(addresses, num_lines):
lines = [None] * num_lines
hits = misses = 0
for a in addresses:
idx = a % num_lines
if lines[idx] == a:
hits += 1
else:
misses += 1
lines[idx] = a
return hits, misses
h, m = direct_mapped_trace([0, 1, 2, 3, 0, 1, 2, 3, 4, 0], 4)
print(h, m, h / (h + m)) # 4 6 0.4Common errors
- Using percent as a whole number. A 95% hit ratio is 0.95. Using 95 in the formula gives absurd results.
- Mixing up the ratios. If the question gives the miss ratio, do not put it where the formula expects the hit ratio. Check that h + m = 1.
- Confusing miss penalty with miss time. Decide whether the number includes the hit time before you add anything.
- Ignoring block size. A miss fetches a whole block, so the penalty grows with block size, even though the miss ratio usually falls because of spatial locality.
- Mixing instruction and data caches. If a question gives separate hit ratios for instruction and data references, weight each by its share of all references.
Combining ratios for separate streams
When a program issues some references to an instruction cache and others to a data cache, the overall hit ratio is a weighted average. Suppose 75% of references are instruction fetches with hit ratio 0.98, and 25% are data accesses with hit ratio 0.90.
overall h = 0.75 × 0.98 + 0.25 × 0.90 = 0.735 + 0.225 = 0.96
def combined_hit_ratio(parts):
# parts: list of (fraction_of_references, hit_ratio)
assert abs(sum(f for f, _ in parts) - 1.0) < 1e-9
return sum(f * h for f, h in parts)
print(combined_hit_ratio([(0.75, 0.98), (0.25, 0.90)])) # about 0.96The fractions must add up to 1. The weights are shares of references, not shares of hits.
Local and global ratios
With more than one cache level, a ratio can be measured against two different totals.
- The local miss ratio of a level is its misses divided by the references that reach that level.
- The global miss ratio is its misses divided by all references made by the CPU.
If L1 misses 10% of all references and L2 misses 20% of the references it receives, the L2 local miss ratio is 0.2 and the L2 global miss ratio is 0.1 × 0.2 = 0.02. The chapter on multilevel caches uses these terms. For now, always ask what the denominator is.
Summary of formulas
| Quantity | Formula |
|---|---|
| Hit ratio | h = H / N |
| Miss ratio | m = M / N = 1 − h |
| Combined hit ratio | Σ (fraction of references × hit ratio of that stream) |
| Global miss ratio of L2 | (L1 miss ratio) × (L2 local miss ratio) |
| Cost of a miss | hit time + miss penalty (when the penalty is stated as extra time) |
Exercise
A cache is direct-mapped with 4 lines and a block size of 1 byte. The CPU reads the addresses 0, 4, 0, 4, 1, 1 in this order, starting with an empty cache. Find the hit ratio and miss ratio.
Show answer
Addresses 0 and 4 both map to line 0 (0 mod 4 and 4 mod 4). Address 1 maps to line 1. Step by step: 0 misses (cold); 4 misses and evicts 0; 0 misses and evicts 4; 4 misses and evicts 0; 1 misses (cold); 1 hits. That is 1 hit and 5 misses out of 6 references. Hit ratio = 1/6, about 0.167. Miss ratio = 5/6, about 0.833.
Exercise
80% of references go to a cache with hit ratio 0.95 and 20% go to another cache with hit ratio 0.70. What is the overall hit ratio? If the hit time is 2 ns and the miss penalty is 60 ns for both, what is the average access time?
Show answer
Overall hit ratio = 0.8 × 0.95 + 0.2 × 0.70 = 0.76 + 0.14 = 0.90. Miss ratio = 0.10. Average time = 2 + 0.10 × 60 = 8 ns.
Exercise
L1 has a miss ratio of 5% of all CPU references. L2 receives only the L1 misses, and its local miss ratio is 40%. What is the global miss ratio of L2, and what fraction of all references go to main memory?
Show answer
Global miss ratio of L2 = 0.05 × 0.40 = 0.02, or 2%. Main memory is accessed only when L2 misses, so 2% of all references reach it.