LRU algorithm, how many bits are needed to implement this algorithm?

I have a little question about the LRU algorithm. If you have a cache with four blocks, how many bits do you need to implement this algorithm?

+6
source share
3 answers

Http://www.powershow.com/view/95163-NzkyO/4_4_Page_replacement_algorithms_powerpoint_ppt_presentation has a nice slide panel that talks about different page replacement schemes. He also explains the implementation of LRU using the mxm matrix very well.

0
source

Assuming you mean the four-way associative-associative cache:
A β€œperfect” LRU will essentially assign each row an exact index in the order of use. You can also think of it as "age." Thus, each of the 4 elements will require an index of 2 bits (since we need to count 4 different ages), indicating its location in LRU order - this means that for each cache set, 2 bits * 4 paths.
In the general case of n ways, you will need the log2 (n) bit in the string or the n * log2 (n) bit in the set.

By the way, there are cheaper ways to achieve near-LRU behavior, see, for example, Pseudo LRU , which will require only 3 bits for the entire set in your case (or in general: #ways - 1 )

+6
source

The minimum number of bits for each set is the ceiling (log2 (N!)), Where N is the number of methods.

This is easily seen for four-sided associativity, noting that the MRU (A) block can be any of the four blocks, the almost MRU block can be any of the three remaining blocks (B ∈ {0,1,2, 3} and B β‰  A), an almost LRU block can be only one of the two remaining blocks (C ∈ {0,1,2,3} and C β‰  A and C β‰  B), and only one block is available for an LRU block. Therefore, the number of possible states in the aggregate is the product of the number of these independent states, i.e. 4! (or for the general case N!).

The B bit can encode 2 B states so B must be greater than or equal to log2 (the_number_of_states). For 24 states of quadrilateral associativity, five bits are needed.

(Increasing the number of bits can simplify the state machine used to manage this information, so the minimum number of bits needed may not match the actual number of bits used in real-world implementations.)

As an answer Leeor noted, the pseudo-LRU tree (which supports the tree of single-bit / two-way LRU variants) requires only N-1 bits. This pLRU is relatively simple to implement, so even with 4-sided associativity (where only two bits of memory are stored - 3 bits versus 5 bits), this form of pLRU can be attractive.

(With 8-position associativity, a true LRU requires 16 bits of status compared to 7 bits for the pLRU tree. Obviously, with higher associativity, a true LRU becomes more expensive, but it can still be useful to simplify the worst execution and it was chosen as a replacement policy for some fully associative TLBs.)

0
source

Source: https://habr.com/ru/post/957244/


All Articles