Cache placement policies


A CPU cache is a memory which holds the recently utilized data by the processor. A block of memory cannot necessarily be placed randomly in the cache and may be restricted to a single cache line or a set of cache lines by the cache placement policy. In other words, the cache placement policy determines where a particular memory block can be placed when it goes into the cache.
There are three different policies available for placement of a memory block in the cache: direct-mapped, fully associative, and set-associative. Originally this space of cache organizations was described using the term "congruence mapping".

Direct-mapped cache

In a direct-mapped cache structure, the cache is organized into multiple sets with a single cache line per set. Based on the address of the memory block, it can only occupy a single cache line. The cache can be framed as a column matrix.

To place a block in the cache

Consider a main memory of 16 kilobytes, which is organized as 4-byte blocks, and a direct-mapped cache of 256 bytes with a block size of 4 bytes.
Since each cache block is of size 4 bytes, the total number of sets in the cache is 256/4, which equals 64 sets.
The incoming address to the cache is divided into bits for Offset, Index and Tag.
Offset corresponds to the bits used to determine the byte to be accessed from the cache line.
In the example, the offset bits are 2 which are used to address the 4 bytes of the cache line.
Index corresponds to bits used to determine the set of the Cache.
In the example, the index bits are 6 which are used to address the 64 sets of the cache.
Tag corresponds to the remaining bits.
In the example, there are 14 – = 6 tag bits, which are stored in tag field to match the address on cache request.
Address 0x0000 maps to block 0 of the memory and occupies the set 0 of the cache.
Address 0x0004 maps to block 1 of the memory and occupies the set 1 of the cache.
Similarly, address 0x00FF maps to block 63 of the memory and occupies the set 63 of the cache.
Address 0x0100 maps to block 64 of the memory and occupies the set 0 of the cache.

Fully associative cache

In a fully associative cache, the cache is organized into a single cache set with multiple cache lines. A memory block can occupy any of the cache lines. The cache organization can be framed as row matrix.

To place a block in the cache

Consider a main memory of 16 kilobytes, which is organized as 4-byte blocks, and a fully associative cache of 256 bytes and a block size of 4 bytes.
Since each cache block is of size 4 bytes, the total number of sets in the cache is 256/4, which equals 64 sets or cache lines.
The incoming address to the cache is divided into bits for offset and tag.
Offset corresponds to the bits used to determine the byte to be accessed from the cache line.
In the example, the offset bits are 2 which are used to address the 4 bytes of the cache line and the remaining bits form the tag.
In the example, the tag bits are 12, which are stored in the tag field of the cache line to match the address on cache request.
Since any block of memory can be mapped to any cache line, the memory block can occupy one of the cache lines based on the replacement policy.

Set-associative cache

Set-associative cache is a trade-off between direct-mapped cache and fully associative cache.
A set-associative cache can be imagined as a matrix. The cache is divided into ‘n’ sets and each set contains ‘m’ cache lines. A memory block is first mapped onto a set and then placed into any cache line of the set.
The range of caches from direct-mapped to fully associative is a continuum of levels of set associativity.
Many processor caches in today's designs are either direct-mapped, two-way set-associative, or four-way set-associative.

To place a block in the cache

Consider a main memory of 16 kilobytes, which is organized as 4-byte blocks, and a 2-way set-associative cache of 256 bytes with a block size of 4 bytes.
Since each cache block is of size 4 bytes and is 2-way set-associative, the total number of sets in the cache is 256/, which equals 32 sets.
In this example, there are 2 offset bits, which are used to address the 4 bytes of a cache line; there are 5 index bits, which are used to address the 32 sets of the cache; and there are 7 = tag bits, which are stored in tag to match against addresses from cache requests.
Address 0x0000 maps to block 0 of the memory and occupies the set 0 of the cache. The block occupies one of the cache lines of the set 0 and is determined by the replacement policy for the cache.
Address 0x0004 maps to block 1 of the memory and occupies one of the cache lines of the set 1 of the cache.
Similarly, address 0x00FF maps to block 63 of the memory and occupies one of the cache lines of the set 31 of the cache.
Address 0x0100 maps to block 64 of the memory and occupies one of the cache lines of the set 0 of the cache.

Two-way skewed associative cache

Other schemes have been suggested, such as the skewed cache, where the index for way 0 is direct, as above, but the index for way 1 is formed with a hash function. A good hash function has the property that addresses which conflict with the direct mapping tend not to conflict when mapped with the hash function, and so it is less likely that a program will suffer from an unexpectedly large number of conflict misses due to a pathological access pattern. The downside is extra latency from computing the hash function. Additionally, when it comes time to load a new line and evict an old line, it may be difficult to determine which existing line was least recently used, because the new line conflicts with data at different indexes in each way; LRU tracking for non-skewed caches is usually done on a per-set basis. Nevertheless, skewed-associative caches have major advantages over conventional set-associative ones.

Pseudo-associative cache

A true set-associative cache tests all the possible ways simultaneously, using something like a content addressable memory. A pseudo-associative cache tests each possible way one at a time. A hash-rehash cache and a column-associative cache are examples of a pseudo-associative cache.
In the common case of finding a hit in the first way tested, a pseudo-associative cache is as fast as a direct-mapped cache, but it has a much lower conflict miss rate than a direct-mapped cache, closer to the miss rate of a fully associative cache.