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 contiguous words; the block is the transfer unit between memory and cache.
- The cache has 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 , the last 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 words (), the address has word field and block address .
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:
Address fields, from the most significant: tag, line ( bits), word ( bits).
Example. With lines, block goes into line .
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 (); blocks of 4 bytes (). Fields: word 2, line 14, tag .
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 ( bits) and the word field is bit.
Set-associative mapping ( ways)
Formula (set-associative mapping with ways). The lines are divided into sets of lines. A block goes into a fixed set, in any line of the set:
Address fields: tag, set ( bits), word.
Example. With lines and ways: sets; block goes into set .
"Two-way" means two lines per set, not two sets. With it is direct mapping, with it is fully associative.
Example: 12-bit addresses, 1-byte word, 16-byte cache, 2-byte blocks, 2 ways: lines , sets , so word 1 bit, set 2 bits, tag 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 , line (= block) of bytes, ways and byte-addressed memory:
Example. With bytes, bytes, ways and -bit addresses: lines , sets , word bit, set field bits, tag bits.
- The address bits are of the memory size; if the tag is known, the maximum memory managed is 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 words; cache of lines with tag, valid bit and (write-back) dirty bit. Address: word field of bits (selects the word only, never decides hit or miss), the rest is the block address. Example: , address : word , block .
Mapping
- Direct: ; fields tag, line (), word. Hit if the tag matches. One comparison, but thrashing. Block 19, : line 3.
- Fully associative: any line; fields tag, word; all tags compared (small caches, TLB, 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 →).
- -way set associative: sets, ; is direct, is fully associative. Block 19, 8 lines, 2 ways: , set 3.
Fields formula
; ; word ; set field ; tag . Example , , , 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.