Salta al contenuto
Note per Studenti Cache Memory Report

Cache Memory

In questa pagina 11
In questa pagina 4

The cache is a small, fast memory (SRAM) between the CPU and main memory. It holds copies of recently used blocks and exploits locality (see Memory Hierarchy and LocalityCharacteristics of memories (location, capacity, transfer unit, access method, performance, volatility); the cost-capacity-speed trade-off and the levels of the hierarchy; temporal and spatial locality; hit, miss and average access time with worked calculations.Memory Hierarchy and Locality →). It is managed entirely by the hardware: the program does not see it.

Blocks, lines, tags

  • Main memory is divided into blocks of KK contiguous words; the block is the transfer unit between memory and cache.
  • The cache has mm lines; each line holds one block, plus a tag saying which block it is, a valid bit and, with write-back, a dirty bit.
  • The number of lines is much smaller than the number of blocks: several blocks compete for the same lines.

Definition (word field and block address). If K=2wK = 2^w, the last ww bits of the address indicate the word inside the block (word field), the other bits are the block address. The first address of a block has the word field equal to zero.

Example. With blocks of K=4K=4 words (w=2w=2), the address 13=1101213=1101_2 has word field 0101 and block address 112=311_2=3.

In calculations "word" is the addressable unit: if memory is byte-addressed and the word is a byte, the word field counts the bytes in the block.

Direct mapping

Formula (direct mapping). Each block can go into one line only:

line=(block address) mod m\text{line} = (\text{block address}) \bmod m

Address fields, from the most significant: tag, line (log⁡2m\log_2 m bits), word (ww bits).

Example. With m=8m=8 lines, block 1919 goes into line 19 mod 8=319\bmod8=3.

Operation: the line field selects the line; if it is empty it is a miss; if it is full the line tag is compared with the address tag: equal means hit, different means miss and the block present is replaced. The word field plays no role in deciding hit or miss: it only selects the word in the block to send to the CPU.

Example: 16 MB memory byte-addressed gives 24-bit addresses; cache with 16 K lines (2142^{14}); blocks of 4 bytes (w=2w = 2). Fields: word 2, line 14, tag 24−14−2=824 - 14 - 2 = 8.

Pros: very simple, a single comparison. Cons: two blocks used alternately that map to the same line evict each other (thrashing) even if the rest of the cache is empty.

Fully associative mapping

Definition (fully associative mapping). A block can go into any line. Address fields: tag, word. The tag is compared with all lines in parallel (associative memory): expensive, used only for very small caches (for example the TLB, see Virtual MemoryVirtual and physical addresses, pages and frames; translation with the page table and field calculation; page fault and page replacement; TLB and effective access time; protection and sharing; comparison with the cache.Virtual Memory →).

Example. With 12-bit addresses and blocks of 2 bytes the tag is the block address (1111 bits) and the word field is 11 bit.

Set-associative mapping (nn ways)

Formula (set-associative mapping with nn ways). The mm lines are divided into v=m/nv = m/n sets of nn lines. A block goes into a fixed set, in any line of the set:

set=(block address) mod v\text{set} = (\text{block address}) \bmod v

Address fields: tag, set (log⁡2v\log_2 v bits), word.

Example. With m=8m=8 lines and 22 ways: v=8/2=4v=8/2=4 sets; block 1919 goes into set 19 mod 4=319\bmod4=3.

"Two-way" means two lines per set, not two sets. With n=1n = 1 it is direct mapping, with n=mn = m it is fully associative.

Example: 12-bit addresses, 1-byte word, 16-byte cache, 2-byte blocks, 2 ways: lines 16/2=816/2 = 8, sets 8/2=48/2 = 4, so word 1 bit, set 2 bits, tag 12−2−1=912 - 2 - 1 = 9 bits.

Replacement

With set-associative or fully associative mapping, when the set is full a line to free must be chosen:

  • LRU (least recently used): the one used least recently (with 2 ways one bit per set is enough);
  • FIFO: the one loaded first, regardless of later accesses;
  • LFU: the one used least often;
  • random: surprisingly little worse than LRU.

If there are free lines in the set one of them is used (in exercises the convention is stated, for example "the free line with the lowest index").

Writing

Policy On a write hit Pros / cons
write-through writes to the cache and to memory memory always up to date; lots of traffic (a write buffer is used)
write-back writes only to the cache and sets the dirty bit to 1 little traffic; when a dirty line is replaced, the whole block must be written back to memory before loading the new one

On a write miss:

  • write-allocate: the block is first brought into the cache, then written (typical with write-back);
  • no-write-allocate: the write goes only to memory (typical with write-through).

In complete exercises one traces for each access: address, fields, hit/miss, line and set involved, line contents (tag, data, dirty bit) and changes to memory.

Block size

Larger blocks exploit spatial locality better, but for the same capacity they reduce the number of lines and increase conflicts; each miss also takes longer to transfer. An intermediate optimum exists: with a cache of 8 words, blocks of 2 words give fewer misses than blocks of 1 or of 4.

Multilevel and split caches

  • L1 small and very fast, often split into instruction cache and data cache (so instruction fetch and data access do not compete for the cache, useful in a pipelined processor); L2 and L3 larger and unified, L3 shared among cores.
  • Causes of misses: compulsory (first access to a block), capacity (the cache is too small for the data in use), conflict (too many blocks in the same line or set; they disappear with full associativity).

Reducing misses

Type of miss Remedy Cost of the remedy
compulsory larger blocks (exploit spatial locality), prefetch blocks that are too large increase conflicts and transfer time
capacity larger cache more area, more cost, slower access
conflict more ways (higher associativity), victim cache more tags compared in parallel, longer access cycle

No remedy is free: each one moves the problem (larger blocks reduce compulsory misses but, for equal capacity, reduce the number of lines).

Typical address-field questions

Formula (address fields of a cache). With capacity CC, line (= block) of KK bytes, nn ways and byte-addressed memory:

lines=CK,sets=linesn,word=log⁡2K,set field=log⁡2(sets),tag=address bits−set field−word.\text{lines} = \frac{C}{K}, \quad \text{sets} = \frac{\text{lines}}{n}, \quad \text{word} = \log_2 K, \quad \text{set field} = \log_2(\text{sets}), \quad \text{tag} = \text{address bits} - \text{set field} - \text{word}.

Example. With C=16C=16 bytes, K=2K=2 bytes, n=2n=2 ways and 1212-bit addresses: lines =8=8, sets =4=4, word =1=1 bit, set field =2=2 bits, tag =12−2−1=9=12-2-1=9 bits.

  • The address bits are log⁡2\log_2 of the memory size; if the tag is known, the maximum memory managed is 2tag+set field+word2^{\text{tag} + \text{set field} + \text{word}} bytes.
  • Two addresses fall in the same line (direct mapping) if the line-field bits are equal and the tag is different; if the tag is also the same they are in the same block.
  • With several processors (or DMA) writing the same datum, write-back creates the coherence problem: memory may hold an old value while a cache holds the new one; it is solved by invalidating or updating the other copies.

Typical mistakes

  • Confusing the word size with the width of the word field: the word field depends on the number of words in the block.
  • Confusing the number of ways with the number of sets.
  • Using the word field to decide hit or miss.
  • With write-back, forgetting to write the replaced dirty block back to memory (and to write all of it, not only the modified word).

Versione ripasso

SRAM between CPU and main memory, hardware-managed. Memory in blocks of K=2wK = 2^w words; cache of mm lines with tag, valid bit and (write-back) dirty bit. Address: word field of ww bits (selects the word only, never decides hit or miss), the rest is the block address. Example: K=4K=4, address 13=1101213 = 1101_2: word 0101, block 33.

Mapping

Fields formula

lines=C/K\text{lines} = C/K; sets=lines/n\text{sets} = \text{lines}/n; word =log⁡2K= \log_2 K; set field =log⁡2(sets)= \log_2(\text{sets}); tag =address bits−set−word= \text{address bits} - \text{set} - \text{word}. Example C=16C = 16, K=2K = 2, n=2n = 2, 12 bits: 8 lines, 4 sets, word 1, set 2, tag 9. Same line (direct): same line bits, different tag.

Replacement and writing

  • LRU, FIFO, LFU, random.
  • Write-through: cache and memory (write buffer; usually no-write-allocate). Write-back: cache only, dirty bit; replacing a dirty line writes back the whole block (usually write-allocate).

Blocks, levels, misses

  • Larger blocks: more spatial locality but fewer lines; there is an optimum.
  • L1 split (instructions and data), L2/L3 unified, L3 shared.
  • Misses: compulsory (larger blocks, prefetch), capacity (larger cache), conflict (more ways, victim cache).
  • Write-back with several writers (processors, DMA) raises coherence: invalidate or update copies.

Typical mistakes: word size is not the word field width; ways are not sets; word field does not decide hit or miss; a dirty replaced block must be written back whole.

Teoria collegata