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.

Five questions this page answers
Question
1What is locality, and what are its two kinds?
2Why does a cache line hold a block of several neighbouring words?
3What are the hit time, the miss rate and the miss penalty?
4How is the average access time worked out?
5What 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

New words on this page
WordMeaning
LocalityThe habit of programs to use a small part of their addresses for a while, and not all addresses evenly.
Temporal localityA word used now is likely to be used again soon.
Spatial localityThe neighbours of a word used now are likely to be used soon.
Block of a cacheThe group of neighbouring words that one line holds. Here it is four words.
Address traceThe list of addresses that a program reads, in order.
Hit rate and miss rateThe part of all reads that hit, and the part that miss. The two add up to 1.
Hit timeThe time for a look in the cache. Every read pays it.
Miss penaltyThe extra time that a miss costs, on top of the hit time.
Average access timeThe total time of all reads, divided by the number of reads.
Memory hierarchyMemories in a row from small and fast to large and slow. A read stops at the first one that has its word.
Cache levelOne cache in a row of caches. Level 1 is next to the processor, and level 2 stands behind it.
L1 and L2Short names for the level-1 cache and the level-2 cache.

One Picture: The Prices of One Read

What One Read Pays, Level by LevelStep through the picture. It starts with one cache and its two prices, and ends with two levels.
  • Processor
  • Memory
  • Bus
What One Read Pays, Level by LevelStep through the picture. It starts with one cache and its two prices, and ends with two levels. component Processor one read at a time component Level-1 cache small: 1 cycle component Level-2 cache larger: 4 cycles more component Memory large: 20 cycles more component One line a block of fourneighbouring words every readonly a missonly a miss here tooa miss, with one levela miss fills a wholeline
  1. component Processorone read at a time
    • every read → Level-1 cache
  2. component Level-1 cachesmall: 1 cycle
    • only a miss → Level-2 cache
    • a miss, with one level → Memory
    • a miss fills a whole line → One line
  3. component Memorylarge: 20 cycles more
  4. component One linea block of four neighbouring words
  5. component Level-2 cachelarger: 4 cycles more
    • only a miss here too → Memory
  1. 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.

  2. 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.

  3. 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.

  4. A second level in between

    A larger cache stands behind the first. It is slower, 4 cycles, but it holds far more lines.

  5. 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.

Sixteen reads of neighbours, one cache level
ReadsBlockWhat happensMissesHits
64, 65, 66, 6716the first read is a miss and brings in all four words13
68, 69, 70, 7117the same13
72, 73, 74, 7518the same13
76, 77, 78, 7919the same13

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.

Twenty-four reads, two cache levels
LevelReads that get this farCycles eachCycles
Level-1 cache24124
Level-2 cache18472
Memory620120
Total216

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

  1. Rule 1: Programs reuse addresses, and they use neighbours.

    The first is temporal locality. The second is spatial locality.

  2. Rule 2: A line holds a block of neighbours.

    One miss brings in the whole block, so the next reads nearby are hits.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

Printed by the lab on Icarus, one row for each trace. The cache has one level, with four lines of four words each.
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.00

Rules 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:

The same lab with another trace. First row: USE_L2 = 0. Second row: USE_L2 = 1.
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.00

Rule 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:

The same lab with WORDS_PER_LINE = 1.
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.00

The 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 choiceThe alternativeWhat the choice buys, and costs
A line holds a block of neighboursA line holds one wordSpatial locality becomes hits. A cache of the same size has fewer lines, so more addresses fight over each.
A small first levelOne large cacheA short hit time, which every read pays. More reads go on to the next level.
A second level behind the firstA miss goes straight to memoryA 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:

The same lab with USE_L2 = 1 and the scattered trace.
scattered reads=16 L1_misses=16 to_memory=16 cycles=400 average=25.00

With 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 page4 lines, 1 or 2 levels
An application processorthousands of lines, 3 levels

This section looks ahead. The sum of this page is used unchanged on real chips, with other numbers.

  1. 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.

  2. A third level

    Large chips add a level that several cores share: The Cache Hierarchy of a Core Cluster.

  3. Instructions and data apart

    Level 1 is usually two caches, one for instructions and one for data, because their traces differ.

  4. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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.00

Check Yourself

Predict the outputThis run reads the same four words ten times round, with one cache level. What does the lab print?
// Address traces through a small cache, and the bill for each trace. Reads only.
module tb;
  localparam WORDS_PER_LINE = 4;     // 1: a line holds one word, as in S0.7. 4: a line holds a block of four neighbours
  localparam LINES    = 4;         // lines of the level-1 cache
  localparam USE_L2   = 0;         // 1: a larger, slower cache stands behind the first
  localparam L2_LINES = 16;        // lines of the level-2 cache
  localparam T1 = 1, T2 = 4, TMEM = 20;   // cycles for level 1, level 2 and memory: choices of this lab

  // A line remembers which block it holds. The data is left out: this lab only counts.
  logic valid1 [0:LINES-1];     int held1 [0:LINES-1];
  logic valid2 [0:L2_LINES-1];  int held2 [0:L2_LINES-1];
  int reads, misses1, misses2, cycles;
  int block, line1, line2;

  task automatic start;            // empty caches, counters at 0
    for (int i = 0; i < LINES; i++)    valid1[i] = 0;
    for (int i = 0; i < L2_LINES; i++) valid2[i] = 0;
    reads = 0;  misses1 = 0;  misses2 = 0;  cycles = 0;
  endtask

  task automatic read(input int address);
    block = address / WORDS_PER_LINE;    // neighbours share one block
    line1 = block % LINES;               // the one line of level 1 that this block may use
    line2 = block % L2_LINES;
    reads++;
    cycles += T1;                        // every read looks in level 1
    if (!(valid1[line1] && held1[line1] == block)) begin
      misses1++;
      valid1[line1] = 1;  held1[line1] = block;
      if (USE_L2) begin
        cycles += T2;                    // a level-1 miss looks in level 2
        if (!(valid2[line2] && held2[line2] == block)) begin
          misses2++;
          valid2[line2] = 1;  held2[line2] = block;
          cycles += TMEM;                // a level-2 miss goes to memory
        end
      end else begin
        misses2++;
        cycles += TMEM;                  // no level 2: every miss goes to memory
      end
    end
  endtask

  task automatic report(input string name);
    $display("%-9s reads=%0d L1_misses=%0d to_memory=%0d cycles=%0d average=%0.2f",
             name, reads, misses1, misses2, cycles, $itor(cycles) / reads);
  endtask

  initial begin
    start;  repeat (10) for (int i = 0; i < 4; i++) read(100 + 5 * i);       // the same four words, ten times round
    report("loop");
  end
endmodule
ConceptA program walks an array from the first word to the last. Which kind of locality is that?
ConceptWhy does a cache line hold a block of several neighbouring words?
ConceptWhich of these does every read pay, hit or miss?
ConceptThe hit time is 1 cycle, the miss rate is 0.1 and the miss penalty is 30 cycles. What is the average access time?
ConceptA level-2 cache is added behind level 1. Which number of the sum for level 1 changes?

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.

Practice

Report a problem with this page