LRU Counter Implementation

In the Least Recently Used (LRU) Cache replacement policy, the cache block which has been unused for the longest time is replaced. In this way, LRU exploits temporal locality – a data location that is currently referenced will tend to be referenced again soon.

Procedure

Each block in an n-way set associative cache will have its own LRU counter. This counter can take any value from 0 to size of set - 1. At any given time, all LRU counter values are distinct within a set. The value 0 corresponds to the LRU (Least Recently Used) block. The value size of set - 1 corresponds to the MRU(Most Recently Used) block.

Block TypeCounter Value
Most Recently Used (MRU)size of set - 1
Least Recently Used (LRU)0

During block replacement, the block that is replaced becomes the MRU block and thus its counter value becomes size of set - 1. Only those counter values greater than the counter value of the block that is accessed get decremented by 1. All other blocks remain unchanged.

Consider a set with 4 blocks with the following counter values.

Cache Miss

In case of a cache miss, the Least Recently Used block needs to be replaced. This is indicated by the red block below.

The counter value of the block that has to be replaced is 0. So, all blocks with counter values greater than 0 have to be decremented by 1. The block that gets replaced will have the counter value 3. All counter values have to be distinct.

Another cache miss will replace the following red block

After replacement, the counter values will be

Another cache miss will replace the following red block

After replacement, the counter values will be

Cache Hit

In the case of a cache hit, the block that is hit will become the Most Recently Used. This is indicated by the blue block below

The counter value of the block that is hit is 3. So, all blocks with counter values greater than 3 (of which there are none) have to be decremented by 1. The block that is hit is updated with the counter value of 3. All counter values have to be distinct.

Another cache hit on the blue block.

The counter value of the block that is hit is 1. So, all blocks with counter values greater than 1 have to be decremented by 1. The block that is hit is updated with the counter value of 3. All counter values have to be distinct.

Another cache hit on the blue block.

The counter value of the block that is hit is 2. So, all blocks with counter values greater than 2 have to be decremented by 1. The block that is hit is updated with the counter value of 3. All counter values have to be distinct.

Cost and Energy Factor

A set of size k will require log_2k bits to keep track of all the counter values. In the above case, 2 bits to track 4 counter values. Each set has its own list of counter values. Thus, a set associative cache with n sets, will require n counters each of size log_2k bits.

For each cache access, in the worst case, upto n counters need to be changed.

Leave a Reply

Your email address will not be published. Required fields are marked *