Docsity

Cache Structure & Replacement Policies: Fully Associative, Direct Mapped, Set Associative, Study notes of Operating Systems

Partial preview of the text

Download Cache Structure & Replacement Policies: Fully Associative, Direct Mapped, Set Associative and more Study notes Operating Systems in PDF only on Docsity!

Cache structure

Each row in the cache is called a line:

  • Valid bit: Does this line hold data?
  • Dirty bit: Has the data been modified?
  • Tag: What address are we holding?
  • Data: The actual cached data.

Typically lines store a number of words.

Key question - which memory words can go in which lines?

Fully Associative Caches

Any word of memory can occupy any cache line.

When we access memory, search the tags for the required address. If it’s found, we hit the cache. If not, we miss.

For this to be efficient, we must be able to search the tags in parallel. This requires special (= expensive) hardware.

Say we have 32 bit addresses, and each line holds 32 bytes. An address is divided as:

If a valid line has the correct tag, the offset indicates which byte of the line we require.

Replacement policy

Initially, every valid bit is 0. The cache fills as memory is accessed.

If we miss and the cache is full, we need to decide which address to evict from the cache:

  • First in, First out (FIFO) Be fair in access.
  • Least Frequently Used (LFU)
  • Random
  • Optimal Evict the address whose next access is furthest in the future – unimplementable.
  • Least Recently Used (LRU) Principle that history repeats itself

In practice, LRU is often used – it’s easy to implement and quite good.

Fully Associative, cont’d

Since any cache line can be used for any word, fully associative caches make very ef- ficient use of available cache space.

With a good replacement policy, and pattern of data accesses to data that fits in the cache is likely to see a performance gain.

Parallel searching for the tag requires content accessible memory, aka associative memory. Nontrivial hardware support is required.

The tags are large, and consume consider- able memory.

For large caches, the high hardware cost and complexity become prohibitive.

Direct mapped example

Consider a cache with 8 lines of 2 bytes each, for a system with 256 bytes of memory:

  • Read address 01011101: hit, 0x
  • Read address 11011100: miss load 11011100, 11011101 into index 110, displacing current contents.
  • Read address 01100011: miss write dirty contents of 001, then load line

Direct mapped, cont’d

Direct mapped caches are easy to implement and don’t require special support.

Since the index consumes part of the address, less tag space is required.

The use of the cache may not be extremely efficient, as we cannot place cached data freely.

Consider moving along a pair of arrays in memory. If the base addresses occupy the same cache line, so do elements at the same index in each array.

If we’re unlucky, almost every cache access will be a miss, with each load evicting the other array’s data!

Set associative example

Consider a 2-way set associative cache with 8 lines of 2 bytes each, for a system with 256 bytes of memory:

  • Read address 01011101: hit, 0x
  • Read address 11011100: miss load 11011100, 11011101 into index 10
  • Read address 01100011: miss load 01100010, 01100011 into index 01, evicting one of the lines.