Sub topic 4
Hit Ratio, Miss Ratio, and Miss Penalty
Prerequisites and the basic measures
Assume you know binary numbers, powers of two, byte-addressable memory, and how a CPU reads instructions and data. You should also know that a cache stores copies of blocks from a slower memory.
A cache access has one of two outcomes:
- A hit occurs when the requested block is already in the cache.
- A miss occurs when the requested block is not in the cache.
For a sequence of memory accesses:
\[ \text{Hit ratio}=\frac{\text{number of hits}}{\text{total accesses}} \]
\[ \text{Miss ratio}=\frac{\text{number of misses}}{\text{total accesses}} \]
Because every access is either a hit or a miss:
\[ \text{Miss ratio}=1-\text{Hit ratio} \]
For example, suppose a program makes 1,000 cache accesses. The cache supplies 920 of them, and 80 require a slower memory.
| Quantity | Calculation | Result |
|---|---|---|
| Hit ratio | 920 / 1,000 | 0.92 or 92% |
| Miss ratio | 80 / 1,000 | 0.08 or 8% |
| Check | 0.92 + 0.08 | 1.00 |
A high hit ratio is useful because most accesses use the fast cache. However, even a small miss ratio can affect performance when the slower memory takes much longer.
Hit time and miss penalty
Hit time is the time needed to check the cache and return data when the access is a hit. Miss penalty is the additional time needed after a miss is detected. It usually includes accessing the next memory level, transferring the block, and updating the cache.
Do not confuse miss penalty with the total time of a miss. If the cache takes 2 nanoseconds to check and the miss penalty is 50 nanoseconds, a miss takes approximately:
\[ 2+50=52\text{ ns} \]
The common single-level average memory access time formula is:
\[ \text{AMAT}=\text{Hit time}+(\text{Miss ratio}\times\text{Miss penalty}) \]
With a 2 ns hit time, an 8% miss ratio, and a 50 ns miss penalty:
\[ \text{AMAT}=2+(0.08\times50)=6\text{ ns} \]
The miss penalty is multiplied by the miss ratio because only misses pay that extra cost.
def amat(hit_time, miss_ratio, miss_penalty):
return hit_time + miss_ratio * miss_penalty
print(amat(2, 0.08, 50)) # 6.0 nanosecondsReading a cache trace
To calculate the ratios from a reference string, count each access as a hit or miss. A reference string is the ordered sequence of memory blocks requested by a program.
When solving numerical questions, first identify the total number of accesses. Then count hits and misses, convert percentages to decimals, and apply the correct formula. A common error is using the hit ratio in the penalty term. The penalty applies to misses, so the formula uses the miss ratio.
Exercise
A cache has a hit time of 1 ns, a hit ratio of 90%, and a miss penalty of 30 ns. What is the average memory access time?
Show answer
The miss ratio is 1 - 0.90 = 0.10. AMAT = 1 + (0.10 × 30) = 4 ns.