Chapter 7 Memory Hierarchy
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 7 Memory Hierarchy Outline Memory hierarchy The basics of caches Measuring and improving cache performance Virtual memory A common framework for memory hierarchy 2 Technology Trends Capacity Speed (latency) Logic: 4x in 1.5 years 4x in 3 years DRAM: 4x in 3 years 2x in 10 years Disk: 4x in 3 years 2x in 10 years DRAM Year Size Cycle Time 19801000:1! 64 Kb2:1! 250 ns 1983 256 Kb 220 ns 1986 1 Mb 190 ns 1989 4 Mb 165 ns 1992 16 Mb 145 ns 1995 64 Mb 120 ns Performance 1000 100 10 1 1980 Processor Memory Latency Gap 1981 1982 1983 1984 1985 1986 1987 1988 1989 Moore 1990 1991 1992 ’ 1993 s Law 1994 1995 1996 1997 (grows 50% / year) performance gap: Processor 1998 1999 2000 Year - memory (2X/10 yrs) 9%/yr. DRAM (2X/1.5 yr) 60%/yr. Proc Solution: Memory Hierarchy An Illusion of a large, fast, cheap memory –Fact: Large memories slow, fast memories small –How to achieve: hierarchy, parallelism An expanded view of memory system: Processor Control Memory Memory M M e e Memory Datapath m m o o r r y y Speed: Fastest Slowest Size: Smallest Biggest Cost: Highest Lowest Memory Hierarchy: Principle At any given time, data is copied between only two adjacent levels: –Upper level: the one closer to the processor Smaller, faster, uses more expensive technology –Lower level: the one away from the processor Bigger, slower, uses less expensive technology Block: basic unit of information transfer –Minimum unit of information that can either be present or not present in a level of the hierarchy Lower Level To Processor Upper Level Memory Memory Block X From Processor Block Y 6 Why Hierarchy Works? Principle of Locality: –Program access a relatively small portion of the address space at any instant of time –90/10 rule: 10% of code executed 90% of time Two types of locality: –Temporal locality: if an item is referenced, it will tend to be referenced again soon –Spatial locality: if an item is referenced, items whose addresses are close by tend to be referenced soon Probability of reference 0 address space 2n - 1 How Does It Work? Temporal locality: keep most recently accessed data items closer to the processor Spatial locality: move blocks consists of contiguous words to the upper levels Processor Control Tertiary Secondary Storage Main Second Storage (Disk) O R C Memory n (Disk) e Level a - g C c (DRAM) Datapath i Cache s h h t e i e p (SRAM) r s Speed (ns): 1‘s 10‘s 100‘s 10,000,000‘s 10,000,000,000‘s (10’s ms) (10’s sec) Size (bytes): 100‘s K’s M’s G’s T’s Levels of Memory Hierarchy Capacity Upper Level Access Time Staging Cost Transfer Unit faster CPU Registers Registers 100s Bytes <10s ns Instr. Operands prog./compiler Cache 1-8 bytes K Bytes Cache 10-100 ns cache controller $.01-.001/bit Blocks 8-128 bytes Main Memory M Bytes Memory 100ns-1us OS $.01-.001 Pages 512-4K bytes Disk G Bytes Disk ms user/operator 10-3 - 10-4 cents Files Mbytes Tape Larger infinite Tape Lower Level sec-min 10-6 How Is the Hierarchy Managed? Registers <-> Memory –by compiler (programmer?) cache <-> memory –by the hardware memory <-> disks –by the hardware and operating system (virtual memory) –by the programmer (files) Memory Hierarchy Technology Random access: –Access time same for all locations –DRAM: Dynamic Random Access Memory High density, low power, cheap, slow Dynamic: need to be refreshed regularly Addresses in 2 halves (memory as a 2D matrix): –RAS/CAS (Row/Column Access Strobe) Use for main memory –SRAM: Static Random Access Memory Low density, high power, expensive, fast Static: content will last (forever until lose power) Address not divided Use for caches Comparisons of Various Technologies Memory Typical access $ per GB technology time in 2004 SRAM 0.5 –5 ns $4000 –$10,000 DRAM 50 –70 ns $100 –$200 Magnetic 5,000,000 – $0.05 –$2 disk 20,000,000 ns Memory Hierarchy Technology Performance of main memory: –Latency: related directly to Cache Miss Penalty Access Time: time between request and word arrives Cycle Time: time between requests –Bandwidth: Large Block Miss Penalty (interleaved memory, L2) Non-so-random access technology: –Access time varies from location to location and from time to time, e.g., disk, CDROM Sequential access technology: access time linear in location (e.g., tape) Memory Hierarchy: Terminology Hit: data appears in upper level (Block X) –Hit rate: fraction of memory access found in the upper level –Hit time: time to access the upper level RAM access time + Time to determine hit/miss Miss: data needs to be retrieved from a block in the lower level (Block Y) –Miss Rate = 1 - (Hit Rate) –Miss Penalty: time to replace a block in the upper level + time to deliver the block to the processor (latency + transmit time) Hit Time << Miss Penalty Lower Level To Processor Upper Level Memory Memory Block X From Processor Block Y 4 Questions for Hierarchy Design Q1: Where can a block be placed in the upper level? => block placement Q2: How is a block found if it is in the upper level? => block identification Q3: Which block should be replaced on a miss? => block replacement Q4: What happens on a write? => write strategy 15 Memory System Design Workload or Benchmark programs Processor reference stream <op,addr>, <op,addr>,<op,addr>,<op,addr>, . op: i-fetch, read, write Memory Optimize the memory system organization $ to minimize the average memory access time for typical workloads Mem Summary of Memory Hierarchy Two different types of locality: –Temporal Locality (Locality in Time) –Spatial Locality (Locality in Space) Using the principle of locality: –Present the user with as much memory as is available in the cheapest technology. –Provide access at the speed offered by the fastest technology. DRAM is slow but cheap and dense: –Good for presenting users with a BIG memory system SRAM is fast but expensive, not very dense: –Good choice for providing users FAST accesses Outline Memory hierarchy The basics of caches (7.2) Measuring and improving cache performance Virtual memory A common framework for memory hierarchy 18 Levels of Memory Hierarchy Capacity Upper Level Access Time Staging Cost Transfer Unit faster CPU Registers Registers 100s Bytes <10s ns Instr. Operands prog./compiler Cache 1-8 bytes K Bytes Cache 10-100 ns cache controller $.01-.001/bit Blocks 8-128 bytes Main Memory M Bytes Memory 100ns-1us OS $.01-.001 Pages 512-4K bytes Disk G Bytes Disk ms user/operator 10-3 - 10-4 cents Files Mbytes Tape Larger infinite Tape Lower Level sec-min 10-6 Basics of Cache Our first example: direct-mapped cache Block Placement : –For each item of data at the lower level, there is exactly one location in cache where it might be –Address mapping: modulo number of blocks Block identification : Tag and valid –How to know if an item is in cache? bit 1 0 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 0 0 0 –If it is, how do we find it? 1 1 1 1 20 0 0 0 0 1 0 0 1 0 1 0 1 0 0 1 0 1 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 0 0 1 1 1 1 0 1 M e m o r y Accessing a Cache •1K words, 1-word block: –Cache index: lower 10 bits –Cache tag: upper 20 bits –Valid bit (When start up, valid is 0) Fig. 7.7 21 Hits and Misses Read hits: this is what we want! Read misses –Stall CPU, freeze register contents, fetch block from memory, deliver to cache, restart –Block replacement ? Write hits: keep cache/memory consistent? –Write-through: write to cache and memory at same time => but memory is very slow! –Write-back: write to cache only (write to memory when that block is being replaced) Need a dirty bit for each block 22 Hits and Misses Write misses: –Write-allocated: read block into cache, write the word low miss rate, complex control, match with write-back –Write-non-allocate: write directly into memory high miss rate, easy control, match with write-through DECStation 3100 uses write-through, but no need to consider hit or miss on a write (one block has only one word) index the cache using bits 15-2 of the address write bits 31-16 into tag, write data, set valid 23 write data into main memory Miss Rate Miss rate of Instrinsity FastMATH for SPEC2000 Benchmark: In trin s ity Instruction D a ta E ffe c tiv e F a stM A T m is s ra te m is s c o m b in e d H ra te m is s ra te 0 .4 % 11 .4 % 3 .2 % Fig. 7.10 24 Avoid Waiting for Memory in Write Through Cache Processor DRAM Write Buffer Use a write buffer (WB): –Processor: writes data into cache and WB –Memory controller: write WB data to memory Write buffer is just a FIFO: –Typical number of entries: 4 Memory system designer’s nightmare: –Store frequency > 1 / DRAM write cycle –Write buffer saturation => CPU stalled Exploiting Spatial Locality (I) •Increase block sizeAddress for(showing b spatialit positions) locality 31 16 15 4 3 2 1 0 16 12 2 Byte Hit Data Tag offset Index Block offset 16 bits 128 bits Total no.