Locality A principle that makes having a memory hierarchy a good idea If an item is referenced, temporal locality: it will tend to be referenced again soon spatial locality: nearby items will tend to be referenced soon. Why does code have locality? Our initial focus: two levels (upper, lower) block: minimum unit of data hit: data requested is in the upper level miss: data requested is not in the upper level Two issues: How do we know if a data item is in the cache? If it is, how do we find it? Our first example: block size is one word of data "direct mapped" For each item of data at the lower level, there is exactly one location in the cache where it might be. e.g., lots of items at the lower level share locations in the upper level Direct Mapped Direct Mapped Mapping: address is modulo the number of blocks in the cache For MIPS: Address (showing bit positions) Byte offset Valid = What kind of locality are we taking advantage of?
Direct Mapped s vs. Misses Taking advantage of spatial locality: V 8 bits Address (showing bit positions) 6 5 8 8 Byte offset 5 bits Block offset 56 entries Read hits this is what we want! Read misses stall the, fetch block from memory, deliver to cache, restart Write hits: can replace data in cache and memory (write-through) write the data only into the cache (write-back the cache later) = 6 Mux Write misses: read the entire block into the cache, then write the word 5 6 Hardware Issues Performance Make reading multiple words easier by using banks of memory Increasing the block size tends to decrease miss rate: % 5% Multiplexor Miss rate % 5% % 5% % bank bank bank bank 5% % 6 6 56 b. Wide memory organization c. Interleaved memory organization Block size (bytes) KB 8 KB 6 KB 6 KB 56 KB a. One-word-wide memory organization It can get a lot more complicated... 7 Use split caches because there is more spatial locality in code: Block size in Instruction miss Effective combined Program words miss rate rate miss rate gcc 6.%.% 5.%.%.7%.9% spice.%.%.%.%.6%.% 8
Performance Decreasing miss ratio with associativity Simplified model: execution time = (execution cycles + stall cycles) cycle time stall cycles = # of instructions miss ratio miss penalty Two ways of improving performance: decreasing the miss ratio decreasing the miss penalty One-way set associative (direct mapped) Block 5 6 7 Set Set Four-way set associative Two-way set associative Eight-way set associative (fully associative) What happens if we increase block size? 9 Compared to direct mapped, give a series of references that: results in a lower miss ratio using a -way set associative cache results in a higher miss ratio using a -way set associative cache assuming we use the least recently used replacement strategy An implementation Performance 9 8 8 5% V V V V % KB 5 9% KB 5 55 6% KB 8 KB -to- multiplexor % One-way 6 KB KB Two-way 6 KB 8 KB Four-way Eight-way Associativity
Decreasing miss penalty with multilevel caches Complexities Add a second level cache: often primary cache is on the same chip as the processor use SRAMs to add another cache above primary memory (DRAM) miss penalty goes down if data is in nd level cache Example: CPI of. on a 5 Ghz machine with a 5% miss rate, ns DRAM access Adding nd level cache with 5ns access time decreases miss rate to.5% Using multilevel caches: try and optimize the hit time on the st level cache try and optimize the miss rate on the nd level cache Not always easy to understand implications of caches: 8 6 8 6 6 8 56 5 8 96 Theoretical behavior of vs. 6 8 8 6 6 8 56 5 8 96 Observed behavior of vs. Complexities Virtual Here is why: Main memory can act as a cache for the secondary storage (disk) 5 Virtual addresses Address translation Physical addresses 8 6 6 8 56 5 8 96 Disk addresses system performance is often critical factor multilevel caches, pipelined processors, make it harder to predict outcomes Compiler optimizations to increase locality sometimes hurt ILP Difficult to predict best algorithm: need experimental data Advantages: illusion of having more physical memory program relocation protection 5 6
Pages: virtual memory blocks Page faults: the data is not in memory, retrieve it from disk huge miss penalty, thus pages should be fairly large (e.g., KB) reducing page faults is important (LRU is worth the price) can handle the faults in software instead of hardware using write-through is too expensive so we use writeback Virtual address 9 8 7 5 9 8 Virtual page number Page offset Translation 9 8 7 5 9 8 Physical page number Page offset Physical address 7