Hit Rate and Average Memory Access Time
Every access pays the hit time, and the misses also pay a big penalty to go to slower memory, so the average is . Because the penalty is so much bigger than the hit time, a few percent of misses can make memory several times slower.
What the symbols mean
- Average memory access time
How long one memory access takes on average, in cycles or ns.
- Hit time
Time to check the cache and get the data when it's there.
- Miss rate
Fraction of accesses not found in the cache: m = 1 − h.
- Miss penalty
Extra time to fetch the block from the slower level below on a miss.
What the symbols mean
- Base CPI
Cycles per instruction if every access hit (hit time already included).
- Real CPI
Cycles per instruction with the memory stalls counted.
- Miss penalty
Stall cycles per miss.
Explanation
The hit rate is simply how often the data the processor wants is already sitting in the cache, the small, fast memory next to it. Say our cache hit rate is 95%: 95 of every 100 memory accesses are answered by the fast memory, and the other 5, the miss rate of 5%, have to wait on slow main memory. Average memory access time, or AMAT, is exactly what it sounds like: how long one memory access takes, on average. The formula has three plain pieces: the hit time (how long it takes to check the cache and get the data when it's there), the miss rate (how often it isn't there) and the miss penalty (the extra wait to fetch it from main memory when it isn't). That one line is the heart of the lesson; the rest is seeing why it's true and why the answer is so touchy.
The 95% example. With a -cycle hit time and a -cycle miss penalty, our hit rate of 95% gives
So the average access takes six times as long as a hit. That surprises most people, because 95% sounds nearly perfect.
Where the formula comes from. It's an expected value. Every access checks the cache first, so every access pays . Then, with probability , the data isn't there and the access pays the penalty on top. So an access takes with probability and with probability :
since . Think about why that shape makes sense: the hit time is a floor you always pay, and the misses add a tax on top of it, the miss rate times the penalty.
Why it climbs so steeply. The penalty is huge next to the hit time, often times bigger. So every of miss rate adds a whole extra cycle () on top of a -cycle hit. Going from 99% hits to 95% takes AMAT from cycles to , three times slower, even though the hit rate barely seemed to move. The trick is to think in misses, not hits: 99% to 95% hits is the miss rate going from 1% to 5%, five times as many misses. That's the number that matters. There's a graph further down that shows it.
Misses slow down the whole processor. Now connect this to CPU Time and Average CPI. Every instruction makes memory accesses: one to fetch the instruction itself, and one more for the fraction of instructions that load or store data. So
and each miss freezes the processor for the penalty (a stall). Those stall cycles add straight onto the CPI the processor would have with perfect caches:
The base CPI already includes the hit time, so only the penalty gets added.
A common mistake. It's tempting to think a 95% hit rate makes memory "95% as fast as the cache." It isn't. Times don't average the way percentages do: the 5% of slow accesses are each times slower, so they dominate, and the average is cycles, not something close to .
Another one: penalty or total miss time? Some problems give the total time of a miss, say cycles ( to check the cache plus to fetch). Then the average is , which works out to the same number. What goes wrong is mixing the two, , which forgets that the misses paid to check the cache too.
See it
Worked examples
A cache has a hit time of cycle, a miss penalty of cycles and a hit rate of . Find the AMAT. Then a redesign raises the hit rate to . How many times faster is the average access?
- 1
Turn the hit rate into a miss rate, because the formula runs on misses: .
A processor has when every access hits. Every instruction is fetched through an instruction cache with a miss rate, and of instructions are loads or stores that use a data cache with a miss rate. Both caches have a -cycle miss penalty. Find the real CPI, and how much faster the processor would be if its caches never missed.
- 1
Count misses per instruction from the instruction cache first. Every instruction is fetched exactly once, so that's misses per instruction.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A cache has a -cycle hit time, a miss rate and a -cycle miss penalty. What's the AMAT, in cycles?
A hardware counter reports cache accesses and misses. The hit time is cycle and the miss penalty is cycles. What's the AMAT, in cycles?
A designer wants an AMAT of cycles with a -cycle hit time and a -cycle miss penalty. What hit rate does the cache need? Give it as a decimal.
A cache has a hit rate, a -cycle hit time and a -cycle miss penalty. A student computes cycles. What's wrong?
A processor has and makes memory accesses per instruction (the fetch plus data accesses). Its unified cache misses of the time with a -cycle penalty. What's the real CPI?
A simulation runs instructions on a GHz processor with . The instruction cache misses of fetches, of instructions access data, the data cache misses of those, and every miss costs cycles. How many seconds does the simulation take?
A processor runs at GHz with and misses per instruction. Main memory takes ns per miss. The clock is doubled to GHz, but memory is no faster. How many times faster is the new processor? (Uses CPU Time.)
The hardest one here. Get it, and you've got this lesson.
A processor with a ns clock has (a cache hit fits in that one cycle) and makes memory accesses per instruction. Its cache misses of the time, and each miss waits ns for main memory. Option A is a bigger cache that misses only of the time, but it's slower, so the clock period has to stretch to ns. Main memory still takes ns. How many times faster is option A than the current design?
Where it's used
Performance engineers read hit rates straight off hardware counters (profilers report lines like "L1 miss rate 3.1%") and put them into exactly this formula to see whether a program is waiting on memory. The same math describes any cache, not just the one on the chip: a web server keeping popular pages in memory in front of a database (hit time ms, miss penalty ms), a content delivery network in front of an origin server, or your browser's cache. In every case the lesson is the same: the miss penalty is so large that the miss rate, not the hit rate, is the number to watch.