Replacement: LRU, FIFO, Random and the Pseudo-LRU Tree

When a set is full, a new block pushes an old one out. Which one goes decides how many of the next reads hit, and how much logic every set must carry.

You Will Be Able To

  • Say what LRU, FIFO and random replacement each give up, and what each must remember.
  • Follow a pseudo-LRU tree of three bits to its way, and update it after a use.
  • Show a trace on which the tree and true LRU choose differently.
Lesson
S3.16
Time
28 min
Runs on
Your browser
Check
6 questions

A set holds four blocks. One of them is read in every round of a loop. A fifth block arrives, and the cache throws out exactly that busy block. The next round starts with a miss that nobody expected.

Before You Start

You need S3.4: sets and ways, and a set that is full. You need S3.2: temporal locality.

You also need to count orders: three things can stand in a row in six ways.

Five questions this page answers
Question
1When must a cache choose a block to replace, and why does the choice matter?
2What does LRU replace, and what must it remember?
3What do FIFO and random replace, and what do they save?
4How does a pseudo-LRU tree point at a way, and how does a use change it?
5When does the tree choose differently from true LRU?

The Idea: Choosing the Block That Leaves

A miss brings a block in. If its set has an empty way, the block goes there. If the set is full, one block must leave. The block that leaves is called the victim, and the rule that picks it is the replacement policy. A direct-mapped cache has no choice to make. Every cache with ways has.

The best victim is the block that will not be needed for the longest time. Nobody knows the future, so the cache guesses from the past. Temporal locality says: what was used a moment ago will be used again soon. So the block that was used longest ago is the best guess. That policy is LRU, for least recently used.

LRU is costly. The cache must know the full order in which the ways of a set were last used. It must also update that order on every hit. With two ways, that is one bit. With four ways there are 24 possible orders, and with eight there are 40,320.

Two policies go the other way and remember almost nothing. FIFO, first in first out, replaces the block that came in first, whether it was used since or not. Random replaces any block. Neither looks at hits at all.

Between them stands a clever compromise. A pseudo-LRU tree keeps three bits for a set of four ways. The bits point away from whatever was used last. Following them finds a block that was not used recently. It is usually the LRU block, and it is never the most recent one.

New Words

New words on this page
WordMeaning
VictimThe block that must leave a full set, to make room for a new one. Also the way that holds it.
Replacement policyThe rule by which a cache picks the victim.
LRULeast recently used. The policy that replaces the block whose last use is furthest in the past.
FIFOFirst in, first out. The policy that replaces the block that came into the set first.
Random replacementThe policy that replaces a way picked by a pseudo-random number.
Pseudo-LRUA policy that comes close to LRU with far fewer bits. This page uses the tree form.
Tree bitOne bit of a pseudo-LRU tree. It points at one of two halves: the half that was not used last.
Pair of waysTwo ways that share one tree bit. Four ways are two pairs.
Use of a wayA hit in that way, or a new block that comes into it.

One Picture: The Tree of Three Bits

Three Bits That Point at a WayStep through the picture. It starts with a full set and ends with a tree of three bits that picks the way to replace.
  • Processor
  • Control / FSM
  • Memory
  • Bus
Three Bits That Point at a WayStep through the picture. It starts with a full set and ends with a tree of three bits that picks the way to replace. component A new block its set is full component Top bit 0: left pair, 1: rightpair component Left bit 0: way 0, 1: way 1 component Right bit 0: way 2, 1: way 3 component Ways 0 and 1 the left pair component Ways 2 and 3 the right pair component A use of a way a hit, or a block thatcomes in which way goes?top is 0top is 1points at one waypoints at one wayturns the bits away
  1. component A new blockits set is full
    • which way goes? → Top bit
  2. component Ways 0 and 1the left pair
  3. component Ways 2 and 3the right pair
  4. component Top bit0: left pair, 1: right pair
    • top is 0 → Left bit
    • top is 1 → Right bit
  5. component Left bit0: way 0, 1: way 1
    • points at one way → Ways 0 and 1
  6. component Right bit0: way 2, 1: way 3
    • points at one way → Ways 2 and 3
  7. component A use of a waya hit, or a block that comes in
    • turns the bits away → Top bit
  1. A full set

    A new block arrives, and all four ways of its set hold a block. One of them must go. The question is which.

  2. The top bit picks a pair

    The four ways are split into two pairs. One bit, the top bit, points at the pair that was not used last.

  3. A second bit picks a way

    Each pair has a bit of its own. It points at the way of that pair that was not used last. Follow two bits, and you are at one way.

  4. A use turns the bits away

    Whenever a way is used, the two bits on its path are turned to point away from it. The top bit points at the other pair. The pair bit points at the other way.

  5. Three bits, not a full history

    The bit of the other pair is not touched. So the tree remembers who was last in each pair, and which pair was last. It does not know the full order of the four.

Read the diagram as text
  • A new block: its set is full
  • Top bit: 0: left pair, 1: right pair
  • Left bit: 0: way 0, 1: way 1
  • Right bit: 0: way 2, 1: way 3
  • Ways 0 and 1: the left pair
  • Ways 2 and 3: the right pair
  • A use of a way: a hit, or a block that comes in
  • A new block → Top bit: which way goes?
  • Top bit → Left bit: top is 0
  • Top bit → Right bit: top is 1
  • Left bit → Ways 0 and 1: points at one way
  • Right bit → Ways 2 and 3: points at one way
  • A use of a way → Top bit: turns the bits away

The pseudo-LRU tree of one set with four ways. The meaning of 0 and 1 is a choice of this page.

The Smallest Example, Worked by Hand

One set with four ways, all empty. Seven reads arrive: blocks 1, 2, 3, 4, then 1 again, then 5, then 3. First work out what true LRU does. The first four reads fill ways 0 to 3. The read of block 1 is a hit. Then block 5 needs a victim.

True LRU when block 5 arrives
WayHoldsLast used at readOrder
0block 15most recent
1block 22least recent: the victim
2block 33
3block 44

LRU replaces block 2. The last read, of block 3, is then a hit: 2 hits in all.

Now the tree, with all three bits at 0 to begin with. After each use, turn the top bit away from the pair that was used. Then turn the bit of that pair away from the way that was used. Leave the third bit alone.

The tree, read by read
ReadWay usedTopLeftRightThe tree now points at
block 1, miss0110right pair, way 2
block 2, miss1100right pair, way 2
block 3, miss2001left pair, way 0
block 4, miss3000left pair, way 0
block 1, hit0110right pair, way 2
block 5, miss2: the tree said so011left pair, way 1
block 3, miss1101right pair, way 3

When block 5 arrives the tree points at way 2, so block 3 goes. True LRU would have taken block 2 in way 1.

The tree made a different choice, and it cost a hit: the last read, of block 3, is now a miss. Look at why. After the hit on way 0 the top bit points at the right pair. Inside that pair, the right bit points at way 2. The tree never compared way 1 with ways 2 and 3.

Still, the tree did not pick badly. Its victim was the second oldest of the four. It can never pick the block that was used last, because every use turns the bits away from itself.

The Rules

  1. Rule 1: A miss uses an empty way first. Only a full set needs a victim.

    The replacement policy picks it.

  2. Rule 2: LRU replaces the block that was used longest ago.

    It must keep the full order of the ways of each set, and change it on every hit.

  3. Rule 3: FIFO replaces the block that came in first. Random replaces any block.

    Neither looks at hits. FIFO needs a small counter for each set, and random needs nothing for a set.

  4. Rule 4: A pseudo-LRU tree keeps one bit for each split in two.

    Four ways need three bits: one for the two pairs, and one inside each pair.

  5. Rule 5: Every use turns the bits on its path away from the way that was used.

    To find the victim, follow the bits from the top.

  6. Rule 6: The tree never picks the way that was used last, and it does not always pick the LRU way.

    It knows the last use inside each half, not the full order.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.4, "Choosing Which Block to Replace", and section 5.8, question 3. They give least recently used and random replacement, and say that exact tracking is too costly beyond a few ways. Section 5.8 also gives the approximation for four ways: one bit for the pair, and one bit inside each pair.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.3. It gives least recently used replacement, the use bit of a two-way cache, and a pseudo-LRU that splits the ways into two groups. Its summary names first-in-first-out and random.

The tree for four ways follows Patterson and Hennessy, section 5.8. The tree for eight ways under "At Scale" is the same idea with one more layer, given as common practice. Harris and Harris use another form: two groups of ways, and a random pick inside a group. Which value of a bit means left is our choice.

The trace, the set of four ways and the numbers of the random policy are our own teaching example.

Now Run It

The lab at the end of this page is test code for one set of four ways. It runs one trace of eleven reads three times: with LRU, with random and with FIFO. It prints a row for each read under LRU, and then the three totals.

Printed by the lab on Icarus. The trace is 1, 2, 3, 4, 1, 2, 5, 1, 2, 1, 4.
LRU    read 1: miss into way 0 | ways hold 1 0 0 0
LRU    read 2: miss into way 1 | ways hold 1 2 0 0
LRU    read 3: miss into way 2 | ways hold 1 2 3 0
LRU    read 4: miss into way 3 | ways hold 1 2 3 4
LRU    read 1: hit  in way 0 | ways hold 1 2 3 4
LRU    read 2: hit  in way 1 | ways hold 1 2 3 4
LRU    read 5: miss into way 2 | ways hold 1 2 5 4
LRU    read 1: hit  in way 0 | ways hold 1 2 5 4
LRU    read 2: hit  in way 1 | ways hold 1 2 5 4
LRU    read 1: hit  in way 0 | ways hold 1 2 5 4
LRU    read 4: hit  in way 3 | ways hold 1 2 5 4
LRU   : reads=11 hits=6
random: reads=11 hits=5
FIFO  : reads=11 hits=4

Rule 1 at work: the first four rows. Each miss takes the lowest empty way, and no policy is asked.

Rule 2 at work: the row "read 5". The set is full. Blocks 1 and 2 were just used, and block 4 came in after block 3. So block 3 in way 2 is the least recent, and it goes. Blocks 1, 2 and 4 are all read again later, and all hit.

Rule 3 at work: the last two rows. FIFO has 4 hits where LRU has 6. When block 5 arrived, FIFO threw out block 1, the first one in. Block 1 was the busiest block of the trace. Random has 5 hits here. With other random numbers it would have another count.

The lab does not hold the tree. You build it in the practice problem of this page. So here is the same trace through the answer to that problem, as recorded output:

Recorded from the reference of the practice problem, in a bench with one set. The last three fields are the tree bits after the read.
tree   read 1: miss into way 0 | ways hold 1 0 0 0 | top=1 left=1 right=0
tree   read 2: miss into way 1 | ways hold 1 2 0 0 | top=1 left=0 right=0
tree   read 3: miss into way 2 | ways hold 1 2 3 0 | top=0 left=0 right=1
tree   read 4: miss into way 3 | ways hold 1 2 3 4 | top=0 left=0 right=0
tree   read 1: hit  in way 0 | ways hold 1 2 3 4 | top=1 left=1 right=0
tree   read 2: hit  in way 1 | ways hold 1 2 3 4 | top=1 left=0 right=0
tree   read 5: miss into way 2 | ways hold 1 2 5 4 | top=0 left=0 right=1
tree   read 1: hit  in way 0 | ways hold 1 2 5 4 | top=1 left=1 right=1
tree   read 2: hit  in way 1 | ways hold 1 2 5 4 | top=1 left=0 right=1
tree   read 1: hit  in way 0 | ways hold 1 2 5 4 | top=1 left=1 right=1
tree   read 4: hit  in way 3 | ways hold 1 2 5 4 | top=0 left=1 right=0
tree  : reads=11 hits=6

Rules 4 and 5 at work: the row "read 5". The bits before it were top=1, left=0, right=0, from the row above. They point at the right pair and at way 2: the same victim as LRU. On this trace the tree gets 6 hits, as many as LRU, with three bits.

This lab leaves out everything but one set. A real cache keeps these bits for every set, and updates them in the same cycle as the hit.

Why It Is Built This Way

PolicyBits for a set of 4 waysBits for a set of 8 waysChanges on a hit?What it buys, and costs
LRU516yes, the whole orderThe best guess from the past. A lot of logic in the hit path.
Pseudo-LRU tree37yes, the bits on one pathClose to LRU. Sometimes a block that is not the oldest goes.
FIFO23noA counter and nothing else. A busy block is thrown out when its turn comes.
Random00noOne shared number generator. No worst trace, and no use of locality.

LRU must tell 24 orders of four ways apart, which takes 5 bits. For eight ways there are 40,320 orders, which takes 16 bits.

The numbers explain what real caches do. With two ways, LRU is one bit, and everybody uses it. With four ways or more, exact LRU costs more than it gains, and the tree takes its place.

The Trap: The Model That Kept Exact LRU

A verification engineer writes a model of the cache in software. The model keeps a list of blocks in order of use, which is exact LRU. The hardware has the tree. The two agree for thousands of reads, and then a test fails with a miss that the model calls a hit. Here is the trace of the hand example under exact LRU:

The lab, with the trace 1, 2, 3, 4, 1, 5, 3.
LRU    read 1: miss into way 0 | ways hold 1 0 0 0
LRU    read 2: miss into way 1 | ways hold 1 2 0 0
LRU    read 3: miss into way 2 | ways hold 1 2 3 0
LRU    read 4: miss into way 3 | ways hold 1 2 3 4
LRU    read 1: hit  in way 0 | ways hold 1 2 3 4
LRU    read 5: miss into way 1 | ways hold 1 5 3 4
LRU    read 3: hit  in way 2 | ways hold 1 5 3 4
LRU   : reads=7 hits=2

And here it is through the tree:

Recorded from the reference of the practice problem.
tree   read 1: miss into way 0 | ways hold 1 0 0 0 | top=1 left=1 right=0
tree   read 2: miss into way 1 | ways hold 1 2 0 0 | top=1 left=0 right=0
tree   read 3: miss into way 2 | ways hold 1 2 3 0 | top=0 left=0 right=1
tree   read 4: miss into way 3 | ways hold 1 2 3 4 | top=0 left=0 right=0
tree   read 1: hit  in way 0 | ways hold 1 2 3 4 | top=1 left=1 right=0
tree   read 5: miss into way 2 | ways hold 1 2 5 4 | top=0 left=1 right=1
tree   read 3: miss into way 1 | ways hold 1 3 5 4 | top=1 left=0 right=1
tree  : reads=7 hits=1

The two part at the row "read 5". LRU puts block 5 into way 1, and the tree puts it into way 2. One read later, block 3 is a hit for LRU and a miss for the tree. Rule 6 said this could happen.

Neither is wrong. They are two different policies, and the hardware is the one that counts. A model of a cache must copy the replacement policy bit for bit. If it does not, its hits and misses drift away from the real ones.

At Scale

This page1 set of 4 ways, 3 tree bits
A real cachehundreds of sets, a tree for each

This section looks ahead. The policies are the same in real caches, and the bookkeeping is repeated for every set.

  1. Eight ways

    A tree for eight ways has seven bits in three layers: one bit, then two, then four. A use turns three of them.

  2. Where the bits live

    The tree bits change on almost every read. They are kept in flip-flops or in a small fast memory beside the tags.

  3. Random in large caches

    In a large cache the gap between random replacement and LRU becomes small, and random is far simpler.

  4. Dirty blocks

    A victim that was written must be saved before it goes. A later lesson of this module is about that.

Try It

Run the lab. Follow the LRU rows, and check the victim of the read of block 5.

Work out by hand which block FIFO replaces when block 5 arrives. Then count its hits.

Change the last read from 4 to 3. Predict the new hits of LRU and of FIFO.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: LRU read 1: miss into way 0 | ways hold 1 0 0 0 LRU read 2: miss into way 1 | ways hold 1 2 0 0 LRU read 3: miss into way 2 | ways hold 1 2 3 0 LRU read 4: miss into way 3 | ways hold 1 2 3 4 LRU read 1: hit in way 0 | ways hold 1 2 3 4 LRU read 2: hit in way 1 | ways hold 1 2 3 4 LRU read 5: miss into way 2 | ways hold 1 2 5 4 LRU read 1: hit in way 0 | ways hold 1 2 5 4 LRU read 2: hit in way 1 | ways hold 1 2 5 4 LRU read 1: hit in way 0 | ways hold 1 2 5 4 LRU read 4: hit in way 3 | ways hold 1 2 5 4 LRU : reads=11 hits=6 random: reads=11 hits=5 FIFO : reads=11 hits=4

Check Yourself

Predict the outputThis run reads ten blocks through one set of four ways: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3. It does so with LRU, and then with FIFO. What does the last row say?
// One set of a cache with four ways. When the set is full, a policy picks the way that must go.
module tb;
  localparam WAYS = 4;
  logic valid [0:WAYS-1];
  int   holds [0:WAYS-1];          // the block in each way
  int   used  [0:WAYS-1];          // LRU: when the way was last used
  int   next_out;                  // FIFO: the way that was filled longest ago
  logic [3:0] dice;                // random: a shift register that makes pseudo-random numbers
  string policy;
  int now, reads, hits;
  bit quiet = 0;

  task automatic start(input string name);
    policy = name;
    for (int w = 0; w < WAYS; w++) valid[w] = 0;
    now = 0;  reads = 0;  hits = 0;  next_out = 0;  dice = 4'b1001;
  endtask

  task automatic read(input int block);
    int way, victim;
    now++;  reads++;
    way = -1;
    for (int w = 0; w < WAYS; w++) if (valid[w] && holds[w] == block) way = w;
    if (way >= 0) begin
      hits++;
      used[way] = now;                         // only LRU needs to know about a hit
      if (!quiet) $display("%-6s read %0d: hit  in way %0d | ways hold %0d %0d %0d %0d", policy, block, way, holds[0], holds[1], holds[2], holds[3]);
    end else begin
      victim = -1;
      for (int w = WAYS - 1; w >= 0; w--) if (!valid[w]) victim = w;      // an empty way first, the lowest one
      if (victim < 0) begin
        if (policy == "LRU") begin
          victim = 0;
          for (int w = 1; w < WAYS; w++) if (used[w] < used[victim]) victim = w;
        end else if (policy == "FIFO") begin
          victim = next_out;
        end else begin
          dice = {dice[2:0], dice[3] ^ dice[2]};
          victim = dice[1:0];
        end
      end
      if (policy == "FIFO") next_out = (victim + 1) % WAYS;
      valid[victim] = 1;  holds[victim] = block;  used[victim] = now;
      if (!quiet) $display("%-6s read %0d: miss into way %0d | ways hold %0d %0d %0d %0d", policy, block, victim, holds[0], holds[1], holds[2], holds[3]);
    end
  endtask

  task automatic trace;
    read(1); read(2); read(3); read(4);
    read(1); read(2);
    read(5);
    read(1); read(2); read(3);
  endtask

  initial begin
    for (int w = 0; w < WAYS; w++) holds[w] = 0;
    start("LRU");     trace;  $display("LRU   : reads=%0d hits=%0d", reads, hits);
    quiet = 1;
    start("FIFO");    trace;  $display("FIFO  : reads=%0d hits=%0d", reads, hits);
  end
endmodule
ConceptWhen does a cache need a replacement policy?
ConceptWhat must exact LRU remember for each set?
ConceptWhich of these policies ignores hits?
ConceptA full set of four ways has the tree bits top = 0, left = 0, right = 1. Which way is the victim?
ConceptWhy can a pseudo-LRU tree pick a way that is not the least recently used?

Common Mistakes

Wrong Treat pseudo-LRU as LRU in a model or a test.

The tree keeps three bits, not the full order, and sometimes picks another way.

Fix Model the tree bit for bit, with the same meaning of 0 and 1.

Wrong Update the replacement bits on a miss only.

A hit is a use too. Without it the policy turns into FIFO.

Fix Turn the bits away from the way on every hit and on every block that comes in.

Wrong Ask the policy while a way of the set is still empty.

It would throw out a good block for nothing.

Fix Fill an empty way first. Ask the policy only when the set is full.

Remember

  • A full set must give up a block. The replacement policy picks the victim.
  • LRU replaces the block used longest ago. It needs the full order of the ways, changed on every hit.
  • FIFO and random ignore hits and need almost no bits.
  • A pseudo-LRU tree keeps three bits for four ways. Every use turns the bits on its path away.

Practice

Report a problem with this page