Locality and Average Access Time
A cache is a bet on how programs behave. This page says what the bet is, and puts a number on what it pays: the average access time.
You Will Be Able To
- Name the two kinds of locality, and say how a cache uses each.
- Work out an average access time from the hit time, the miss rate and the miss penalty.
- Work out the average access time of two cache levels, from counts and from rates.
- Lesson
- S3.2
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
Two programs make sixteen reads each, on the same chip. One waits 96 cycles for them, and the other waits 336. The cache, the memory and the clock are the same. Only the order of the addresses differs.
Before You Start
You need S0.7: a cache, a hit, a miss, a line, and two boxes that fight over one line.
You also need fractions and percent: 4 misses in 16 reads is a quarter, or 25 percent.
| Question | |
|---|---|
| 1 | What is locality, and what are its two kinds? |
| 2 | Why does a cache line hold a block of several neighbouring words? |
| 3 | What are the hit time, the miss rate and the miss penalty? |
| 4 | How is the average access time worked out? |
| 5 | What does a second cache level change in that sum? |
The Idea: Programs Are Predictable, and the Bet Has a Price
S0.7 ended on a bet: the program will soon ask for the same address again. Programs keep that promise in two ways. The habit as a whole is called locality, and its two kinds have names.
The first is temporal locality. A word used now is likely to be used again soon. A loop fetches the same instructions on every round, and a counter is read and written again and again. A cache wins here simply by keeping what was used.
The second is spatial locality. When a word is used, its neighbours are likely to be used soon. Instructions run one after another, and an array is walked from one end to the other. A cache that keeps single words gains nothing from this. So a real cache line holds a block: several neighbouring words, brought in together on one miss.
How much does the bet pay? Every read pays for a look in the cache. A read that misses pays for the slow memory on top. So the cost of an average read is the first price, plus the second price times how often it is paid. That number is the average access time.
One more step follows from S0.7, where size cost speed. A cache cannot be both large and fast. So a chip puts a small fast cache first and a larger, slower one behind it. The sum stays the same. Only the second price changes: it becomes the average access time of the cache behind.
New Words
| Word | Meaning |
|---|---|
| Locality | The habit of programs to use a small part of their addresses for a while, and not all addresses evenly. |
| Temporal locality | A word used now is likely to be used again soon. |
| Spatial locality | The neighbours of a word used now are likely to be used soon. |
| Block of a cache | The group of neighbouring words that one line holds. Here it is four words. |
| Address trace | The list of addresses that a program reads, in order. |
| Hit rate and miss rate | The part of all reads that hit, and the part that miss. The two add up to 1. |
| Hit time | The time for a look in the cache. Every read pays it. |
| Miss penalty | The extra time that a miss costs, on top of the hit time. |
| Average access time | The total time of all reads, divided by the number of reads. |
| Memory hierarchy | Memories in a row from small and fast to large and slow. A read stops at the first one that has its word. |
| Cache level | One cache in a row of caches. Level 1 is next to the processor, and level 2 stands behind it. |
| L1 and L2 | Short names for the level-1 cache and the level-2 cache. |
One Picture: The Prices of One Read
- Processor
- Memory
- Bus
- Every read pays the first price
Each read looks in the cache first. That costs the hit time, 1 cycle here, whether the word is there or not.
- A miss pays a second price
On a miss the read goes on to memory. That costs the miss penalty, 20 cycles here, on top of the first price.
- A line holds a block of neighbours
On a miss the cache brings in four neighbouring words, not one. A program that reads the next word a moment later finds it there.
- A second level in between
A larger cache stands behind the first. It is slower, 4 cycles, but it holds far more lines.
- A read stops at the first level that has its block
Most reads stop at level 1. Some go on to level 2. Only the few that miss twice pay for memory.
Read the diagram as text
- Processor: one read at a time
- Level-1 cache: small: 1 cycle
- Level-2 cache: larger: 4 cycles more
- Memory: large: 20 cycles more
- One line: a block of four neighbouring words
- Processor → Level-1 cache: every read
- Level-1 cache → Level-2 cache: only a miss
- Level-2 cache → Memory: only a miss here too
- Level-1 cache → Memory: a miss, with one level
- Level-1 cache → One line: a miss fills a whole line
The prices of 1, 4 and 20 cycles are choices of this page. Real ones are in "At Scale".
The Smallest Example, Worked by Hand
A program walks an array: it reads the sixteen words at addresses 64 to 79, once each. The cache has lines of four words. A look in the cache costs 1 cycle, and a miss costs 20 more.
| Reads | Block | What happens | Misses | Hits |
|---|---|---|---|---|
| 64, 65, 66, 67 | 16 | the first read is a miss and brings in all four words | 1 | 3 |
| 68, 69, 70, 71 | 17 | the same | 1 | 3 |
| 72, 73, 74, 75 | 18 | the same | 1 | 3 |
| 76, 77, 78, 79 | 19 | the same | 1 | 3 |
The block number is the address divided by four, with the remainder dropped.
Now the bill. All 16 reads pay 1 cycle: 16 cycles. The 4 misses pay 20 more each: 80 cycles. The total is 96 cycles, so the average access time is 96 / 16 = 6 cycles.
The same number comes from rates. The miss rate is 4 / 16 = 0.25. The average is 1 + 0.25 × 20 = 6 cycles. Every read pays the 1. A quarter of them pay the 20.
Now two levels. A loop goes four times round six blocks: 24 reads. The level-1 cache has only four lines, so some of the six blocks fight over a line, as in S0.7. It misses 18 times. The level-2 cache has sixteen lines and holds all six blocks. It misses only on the first round: 6 times.
| Level | Reads that get this far | Cycles each | Cycles |
|---|---|---|---|
| Level-1 cache | 24 | 1 | 24 |
| Level-2 cache | 18 | 4 | 72 |
| Memory | 6 | 20 | 120 |
| Total | 216 |
The average access time is 216 / 24 = 9 cycles. With no level-2 cache, the 18 misses would all go to memory: 24 + 18 × 20 = 384 cycles, or 16 for each read.
The Rules
Rule 1: Programs reuse addresses, and they use neighbours.
The first is temporal locality. The second is spatial locality.
Rule 2: A line holds a block of neighbours.
One miss brings in the whole block, so the next reads nearby are hits.
Rule 3: Every read pays the hit time. A miss pays the miss penalty as well.
So a faster cache helps every read, and a faster memory helps only the misses.
Rule 4: Average access time = hit time + miss rate × miss penalty.
It equals the total time of all reads, divided by the number of reads.
Rule 5: With two levels, the miss penalty of level 1 is the average access time of level 2.
Level 2 has a hit time and a miss rate of its own. Its miss rate counts only the reads that reach it.
Rule 6: A cache saves time only as far as the trace has locality.
On addresses that are far apart and never repeat, every read is a miss.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.1. It covers the two kinds of locality and the levels of a memory hierarchy. It also defines hit rate, miss rate, hit time and miss penalty.
The same book, section 5.4: the average memory access time, and a second cache level that lowers the miss penalty.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, sections 8.1 and 8.2: the average memory access time, with the formula for more than one level.
The cache of four lines with blocks of four words is our own teaching example. So are the level-2 cache of sixteen lines and the prices of 1, 4 and 20 cycles.
Books differ in one detail. Some count the time of a miss as the penalty alone. This page always adds the penalty to the hit time, as rule 3 says.
Now Run It
The lab at the end of this page is a counting model of this cache. It holds no data. For each read it decides hit or miss, and adds up the cycles. It runs three traces of reads.
array reads=16 L1_misses=4 to_memory=4 cycles=96 average=6.00
loop reads=20 L1_misses=4 to_memory=4 cycles=100 average=5.00
scattered reads=16 L1_misses=16 to_memory=16 cycles=336 average=21.00Rules 2 and 4 at work: the row "array" is the first hand table. It has 4 misses in 16 reads, 96 cycles, and an average of 6.00.
Rule 1 at work: the row "loop" reads the same four words five times round. Only the first round misses. That is temporal locality, and the average falls to 5.00.
Rule 6 at work: the row "scattered" reads sixteen words that are far apart, once each. Every read is a miss, and the average is 21.00: the hit time and the whole penalty.
Now the loop over six blocks from the second hand table, first with one level and then with two:
six-block reads=24 L1_misses=18 to_memory=18 cycles=384 average=16.00
six-block reads=24 L1_misses=18 to_memory=6 cycles=216 average=9.00Rule 5 at work: level 1 misses 18 times in both rows. With level 2 behind it, only 6 reads go to memory, and the average falls from 16.00 to 9.00.
This lab leaves things out. It counts reads only, and it holds no data. Writes, and what a cache does with them, come in the later lessons of this module.
Why It Is Built This Way
First, the block. Here are the three traces again, in a cache whose lines hold one word each, as in S0.7:
array reads=16 L1_misses=16 to_memory=16 cycles=336 average=21.00
loop reads=20 L1_misses=4 to_memory=4 cycles=100 average=5.00
scattered reads=16 L1_misses=16 to_memory=16 cycles=336 average=21.00The loop still does well, because temporal locality needs no block. The array walk falls from 6.00 to 21.00: it never reads a word twice, so only its neighbours could have helped.
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A line holds a block of neighbours | A line holds one word | Spatial locality becomes hits. A cache of the same size has fewer lines, so more addresses fight over each. |
| A small first level | One large cache | A short hit time, which every read pays. More reads go on to the next level. |
| A second level behind the first | A miss goes straight to memory | A far smaller penalty for most misses. More area, and one more look for the reads that miss twice. |
The Trap: The Cache That Made It Slower
A team adds a level-2 cache to speed up a program that looks up records all over a large table. The program gets slower. Here is the scattered trace again, now with two levels:
scattered reads=16 L1_misses=16 to_memory=16 cycles=400 average=25.00With one level the average was 21.00. With two it is 25.00. Every read is a miss in level 1 and a miss in level 2, and then goes to memory anyway. The level-2 cache added its 4 cycles to every read and saved none.
Rule 6 said so. A cache is paid for by locality, and this trace has none. Before adding a cache, look at the trace it must serve. A hit rate measured on one program says nothing about another.
At Scale
This section looks ahead. The sum of this page is used unchanged on real chips, with other numbers.
Real sizes
A block is often 64 bytes. A level-1 cache holds some tens of kibibytes and answers in a few cycles. Memory can cost a hundred cycles or more.
A third level
Large chips add a level that several cores share: The Cache Hierarchy of a Core Cluster.
Instructions and data apart
Level 1 is usually two caches, one for instructions and one for data, because their traces differ.
Why a read missed
A miss can be a first use, a cache that is too small, or two blocks that fight over a line. Later lessons of this module tell them apart.
Try It
Run the lab. Check the row "array" against the first hand table.
Set WORDS_PER_LINE to 1. Predict the three averages before you run it.
Set TMEM to 40. Work out the new average of the array walk by rule 4, then run it.
array reads=16 L1_misses=4 to_memory=4 cycles=96 average=6.00 loop reads=20 L1_misses=4 to_memory=4 cycles=100 average=5.00 scattered reads=16 L1_misses=16 to_memory=16 cycles=336 average=21.00Check Yourself
Common Mistakes
Wrong Take the miss penalty alone as the time of a miss.
A read that misses has paid the hit time first.
Fix Add them: hit time plus miss rate times miss penalty.
Wrong Apply the miss rate of level 2 to all reads.
It counts only the reads that reach level 2.
Fix Multiply it by the miss rate of level 1, or count the reads at each level.
Wrong Expect a cache to help any program.
It is paid for by locality. With no locality, every read is a miss.
Fix Measure the miss rate on the trace that matters.
Remember
- Temporal locality: a word is used again soon. Spatial locality: its neighbours are used soon.
- A line holds a block of neighbours, so one miss serves several reads.
- Average access time = hit time + miss rate × miss penalty.
- With two levels, the miss penalty of level 1 is the average access time of level 2.