410 likes | 716 Views
Caching & Virtual Memory Systems Chapter 7. Caching To address bottleneck between CPU and Memory Direct Associative Set Associate Virtual Memory Systems To address bottleneck between Memory & Disk Organization Page faults Lookaside Buffer.
E N D
Caching & Virtual Memory SystemsChapter 7 Caching To address bottleneck between CPU and Memory Direct Associative Set Associate Virtual Memory Systems To address bottleneck between Memory & Disk Organization Page faults Lookaside Buffer Simplicity is the goal, but it is hard work to keep it simple!
Associative Mapped Caching • Direct mapped caching allows any given main memory block to be mapped into one unique cache location. • Associative mapped caching allows any given main memory block to be mapped into any cache location. How does the length of the tag for Associative compare with that for Direct?
31 30 . . . 13 12 11 . . . 2 1 0 Tag 22 8 Index Index V V V V Tag Tag Tag Tag Data Data Data Data 0 1 2 . . . 253 254 255 0 1 2 . . . 253 254 255 0 1 2 . . . 253 254 255 0 1 2 . . . 253 254 255 32 4x1 select Hit Data Four-Way Set Associative Cache • 28 = 256 sets each with four ways (each with one block) Byte offset
Used for tag compare Selects the set Selects the word in the block Increasing associativity Decreasing associativity Fully associative (only one set) Tag is all the bits except block and byte offset Direct mapped (only one way) Smaller tags Range of Set Associative Caches • For a fixed size cache, each increase by a factor of two in associativity doubles the number of blocks per set (i.e., the number or ways) and halves the number of sets – decreases the size of the index by 1 bit and increases the size of the tag by 1 bit Tag Index Block offset Byte offset
Costs of Set Associative Caches • When a miss occurs, which block do we pick for replacement? • Least Recently Used (LRU): the block replaced is the one that has been unused for the longest time • Must have hardware to keep track of when each way’s block was used relative to the other blocks in the set • For 2-way set associative, takes one bit per set→ set the bit when a block is referenced (and reset the other way’s bit) • N-way set associative cache costs • N comparators (delay and area) • MUX delay (set selection) before data is available • Data available after set selection (and Hit/Miss decision). In a direct mapped cache, the cache block is available before the Hit/Miss decision • So its not possible to just assume a hit and continue and recover later if it was a miss
Benefits of Set Associative Caches • The choice of direct mapped or set associative depends on the cost of a miss versus the cost of implementation Data from Hennessy & Patterson, Computer Architecture, 2003 • Largest gains are in going from direct mapped to 2-way (20%+ reduction in miss rate) Note: Figure 7.30 in your text does not seems to be very different.
Multiple Caches • Virtually all modern computers employ caching • In fact, they employ multiple caches. Why? • But there organizations (organization, associativity, size) differ • “In most cases”, they follow a memory pyramid
Summary: The Cache Design Space • Several interacting dimensions • cache size • block size • associativity • replacement policy • write-through vs write-back • The optimal choice is a compromise • depends on access characteristics • workload • use (I-cache, D-cache, TLB) • depends on technology / cost • Simplicity often wins Cache Size Associativity Block Size
Summary: What happens on a Cache write? • Write-through – The information is written to both the block in the cache and to the block in the next lower level of the memory hierarchy • Write-through is always combined with a write buffer so write waits to lower level memory can be eliminated (as long as the write buffer doesn’t fill) • Write-back – The information is written only to the block in the cache. The modified cache block is written to main memory only when it is replaced. • Need a dirty bit to keep track of whether the block is clean or dirty • Pros and cons of each? • Write-through: read misses don’t result in writes (so are simpler and cheaper) • Write-back: repeated writes require only one write to lower level Cache Processor DRAM
Improving Cache Performance • Reduce the time to hit in the cache • smaller cache • direct mapped cache • smaller blocks • for writes • Write-through • Write-back • Reduce the miss rate • bigger cache • more flexible placement (increase associativity) • larger blocks (16 to 64 bytes typical) • Use “victim cache” – small buffer holding most recently discarded blocks
Improving Cache Performance • Reduce the miss penalty • smaller blocks • use a write buffer to hold dirty blocks being replaced so don’t have to wait for the write to complete before reading • check write buffer (and/or victim cache) on read miss – may get lucky • for large blocks fetch critical word first • use multiple cache levels – L2 cache not tied to CPU clock rate • faster backing store/improved memory bandwidth • wider buses • memory interleaving, page mode DRAMs
Virtual Memory System To address bottleneck between Memory & Disk • Discussing the problem • Organization • Page faults • Lookaside Buffer
Inclusive– what is in L1$ is a subset of what is in L2$ is a subset of what is in MM that is a subset of is in SM 4-8 bytes (word) 8-32 bytes (block) 1 to 4 blocks 1,024+ bytes (disk sector = page) Back to The Memory Hierarchy • Take advantage of the principle of locality to present the user with as much memory as is available in the cheapest technology at the speed offered by the fastest technology Processor Increasing distance from the processorin accesstime L1$ L2$ Main Memory Secondary Memory (Relative) size of the memory at each level
Virtual Memory System vs Caching • Caching is essentially a architecture / hardware problem • Virtual memory Systems are an Operating System problem with a requirement for architecture / hardware support • Why?
Recall Memory Differences Technology Access Time Cost/GB SRAM 0.5 – 5 ns $4,000 - $10,000 DRAM 50 – 70 ns $100 - $200 Disk 5 – 20 ms $0.50 - $2 DRAM access 40 times SRAM access Disk access 1,000,000 times DRAM access Note: Block reads/writes can be done much faster (maybe 1000 times)
Virtual Memory • Use main memory as a “cache” for secondary memory • Allows efficient and safe sharing of memory among multiple programs • Provides the ability to easily run programs larger than the size of physical memory • Simplifies loading a program for execution by providing for code relocation (i.e., the code can be loaded anywhere in main memory) • What makes it work? – again the Principle of Locality • A program is likely to access a relatively small portion of its address space during any period of time • Each program is compiled into its own address space – a “virtual” address space • During run-time each virtual address must be translated to a physical address (an address in main memory)
Two Programs Sharing Physical Memory • A program’s address space is divided into pages (all one fixed size) or segments (variable sizes) • The starting location of each page (either in main memory or in secondary memory) is contained in the program’s page table Program 1 virtual address space main memory Program 2 virtual address space
Translation Physical page number Page offset 29 . . . 12 11 0 Physical Address (PA) Address Translation • A virtual address is translated to a physical address by a combination of hardware and software • So each memory request first requires an address translation from the virtual space to the physical space • A virtual memory miss (i.e., when the page is not in physical memory) is called a page fault Virtual Address (VA) 31 30 . . . 12 11 . . . 0 Virtual page number Page offset
Address Translation Mechanisms Virtual page # Offset Physical page # Offset Physical page base addr V 1 1 1 1 1 1 0 1 0 1 0 Main memory Page Table (in main memory) Disk storage
miss VA PA Trans- lation Cache Main Memory CPU hit data Virtual Addressing with a Cache • It takes an extra memory access to translate a VA to a PA • This makes memory (cache) accesses very expensive (if every access was really two accesses) • The hardware fix is to use a Translation Lookaside Buffer (TLB) – a small cache that keeps track of recently used address mappings to avoid having to do a page table lookup
Physical page base addr V Tag 1 1 1 0 1 TLB Making Address Translation Fast Virtual page # Physical page base addr V 1 1 1 1 1 1 0 1 0 1 0 Main memory Page Table (in physical memory) Disk storage
Virtual Addressing with Look Ahead Buffer Intrinsity Design
Translation Lookaside Buffers (TLBs) • Just like any other cache, the TLB can be organized as fully associative, set associative, or direct mapped V Virtual Page # Physical Page # Dirty Ref Access • TLB access time is typically smaller than cache access time (because TLBs are much smaller than caches) • TLBs are typically not more than 128 to 256 entries even on high end machines
hit ¾ t ¼ t miss VA PA TLB Lookup Cache Main Memory CPU miss hit Trans- lation data A TLB in the Memory Hierarchy • A TLB miss – is it a page fault or merely a TLB miss? • If the page is loaded into main memory, then the TLB miss can be handled (in hardware or software) by loading the translation information from the page table into the TLB • Takes 10’s of cycles to find and load the translation info into the TLB • If the page is not in main memory, then it’s a true page fault • Takes 1,000,000’s of cycles to service a page fault • TLB misses are much more frequent than true page faults
TLB Event Combinations Yes – what we want! Yes – although the page table is not checked if the TLB hits Yes – TLB miss, PA in page table Yes – TLB miss, PA in page table, but data not in cache Yes – page fault Impossible – TLB translation not possible if page is not present in memory Impossible – data not allowed in cache if page is not in memory
The Hardware/Software Boundary • What parts of the virtual to physical address translation is done by or assisted by the hardware? • Translation Lookaside Buffer (TLB) that caches the recent translations • TLB access time is part of the cache hit time • May allot an extra stage in the pipeline for TLB access • Page table storage, fault detection and updating • Page faults result in interrupts that are then handled by the OS • Hardware must support (i.e., update appropriately) Dirty and Reference bits (e.g., ~LRU) in the Page Tables • Disk placement • Bootstrap (e.g., out of disk sector 0) so the system can service a limited number of page faults before the OS is even loaded
Summary • The Principle of Locality: • Program likely to access a relatively small portion of the address space at any instant of time. • Temporal Locality: Locality in Time • Spatial Locality: Locality in Space • Caches, TLBs, Virtual Memory all understood by examining how they deal with the four questions • Where can block be placed? • How is block found? • What block is replaced on miss? • How are writes handled? • Page tables map virtual address to physical address • TLBs are important for fast translation
Next class • Talk about the IA 64 Processor Superscaler Processor