
CS 485: Systems Programming Virtual Memory CS 485G-006: Systems Programming Lectures 18 and 19: 7-9 Mar 2016 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 1 CS 485: Systems Programming Today Address spaces VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Memory mapping Address translation Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 2 CS 485: Systems Programming A System Using Physical Addressing Main memory 0: 1: Physical address 2: (PA) 3: CPU 4: 4 5: 6: 7: 8: ... M-1: Data word Used in “simple” systems like embedded microcontrollers in devices like cars, elevators, and digital picture frames Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 3 CS 485: Systems Programming A System Using Virtual Addressing Main memory 0: CPU Chip 1: 2: Virtual address Physical address (VA) (PA) 3: CPU MMU 4: 4100 4 5: 6: 7: 8: ... M-1: Data word Used in all modern servers, laptops, and smart phones One of the great ideas in computer science Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 4 CS 485: Systems Programming Address Spaces Linear address space: Ordered set of contiguous non-negative integer addresses: {0, 1, 2, 3 … } Virtual address space: Set of N = 2n virtual addresses {0, 1, 2, 3, …, N-1} Physical address space: Set of M = 2m physical addresses {0, 1, 2, 3, …, M-1} Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 5 CS 485: Systems Programming Why Virtual Memory (VM)? Uses main memory efficiently . Use RAM as a cache for parts of a virtual address space . Supports paging: transferring data between RAM and disk Simplifies memory management . Each process gets the same uniform linear address space Isolates address spaces . One process can’t interfere with another’s memory . User program cannot access privileged kernel information and code Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 6 CS 485: Systems Programming Today Address spaces VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Memory mapping Address translation Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 7 CS 485: Systems Programming VM as a Tool for Caching Conceptually, virtual memory is an array of N contiguous bytes stored on disk. The contents of the array on disk are cached in physical memory (DRAM cache) . These cache blocks are called pages (size is P = 2p bytes) Virtual memory Physical memory 0 VP 0 Unallocated 0 VP 1 Cached Empty PP 0 Uncached PP 1 Unallocated Empty Cached Uncached Empty Cached PP 2m-p-1 M-1 VP 2n-p-1 Uncached N-1 Virtual pages (VPs) Physical pages (PPs) stored on disk cached in DRAM Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 8 CS 485: Systems Programming DRAM Cache Organization DRAM cache organization driven by the enormous miss penalty . DRAM (main memory) is about 10x slower than SRAM (cache) . Disk is about 10,000x slower than DRAM Consequences . Large page (block) size: typically 4 KB, sometimes 4 MB . Fully associative: any VP can be placed in any PP . Highly sophisticated, expensive replacement algorithms . Too complicated and open-ended to be implemented in hardware . Write-back rather than write-through . Data written into RAM is not immediately written to disk Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 9 CS 485: Systems Programming Enabling Data Structure: Page Table A page table is an array of page table entries (PTEs) that maps virtual pages to physical pages. Per-process kernel data structure in DRAM Physical memory Physical page (DRAM) number or VP 1 Valid disk address PP 0 VP 2 PTE 0 null 0 VP 7 1 VP 4 PP 3 1 0 1 0 null Virtual memory 0 (disk) PTE 7 1 VP 1 Memory resident VP 2 page table (DRAM) VP 3 VP 4 VP 6 VP 7 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 10 CS 485: Systems Programming Page Hit Page hit: reference to VM word that is in physical memory (DRAM cache hit) Physical memory Physical page Virtual address (DRAM) number or VP 1 Valid disk address PP 0 VP 2 PTE 0 null 0 VP 7 1 VP 4 PP 3 1 0 1 0 null Virtual memory 0 (disk) PTE 7 1 VP 1 Memory resident VP 2 page table (DRAM) VP 3 VP 4 VP 6 VP 7 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 11 CS 485: Systems Programming Page Fault Page fault: reference to VM word that is not in physical memory (DRAM cache miss) Physical memory Physical page (DRAM) Virtual address number or VP 1 Valid disk address PP 0 VP 2 PTE 0 null 0 VP 7 1 VP 4 PP 3 1 0 1 0 null Virtual memory 0 (disk) PTE 7 1 VP 1 Memory resident VP 2 page table (DRAM) VP 3 VP 4 VP 6 VP 7 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 12 CS 485: Systems Programming Handling Page Fault Page miss causes page fault (an exception) Physical memory Physical page (DRAM) Virtual address number or VP 1 Valid disk address PP 0 VP 2 PTE 0 null 0 VP 7 1 VP 4 PP 3 1 0 1 0 null Virtual memory 0 (disk) PTE 7 1 VP 1 Memory resident VP 2 page table (DRAM) VP 3 VP 4 VP 6 VP 7 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 13 CS 485: Systems Programming Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Physical memory Physical page (DRAM) Virtual address number or VP 1 Valid disk address PP 0 VP 2 PTE 0 null 0 VP 7 1 VP 4 PP 3 1 0 1 0 null Virtual memory 0 (disk) PTE 7 1 VP 1 Memory resident VP 2 page table (DRAM) VP 3 VP 4 VP 6 VP 7 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 14 CS 485: Systems Programming Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Physical memory Physical page (DRAM) Virtual address number or VP 1 Valid disk address PP 0 VP 2 PTE 0 null 0 VP 7 1 VP 3 PP 3 1 1 0 0 null Virtual memory 0 (disk) PTE 7 1 VP 1 Memory resident VP 2 page table (DRAM) VP 3 VP 4 VP 6 VP 7 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 15 CS 485: Systems Programming Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) Offending instruction is restarted: page hit! Physical memory Physical page (DRAM) Virtual address number or VP 1 Valid disk address PP 0 VP 2 PTE 0 null 0 VP 7 1 VP 3 PP 3 1 1 0 0 null Virtual memory 0 (disk) PTE 7 1 VP 1 Memory resident VP 2 page table (DRAM) VP 3 VP 4 Key point: Waiting until the miss to copy the page to VP 6 DRAM is known as demand paging VP 7 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 16 CS 485: Systems Programming Allocating Pages Allocating a new page (VP 5) of virtual memory. Physical memory Physical page (DRAM) number or VP 1 Valid disk address PP 0 VP 2 PTE 0 null 0 VP 7 1 VP 3 PP 3 1 1 0 0 Virtual memory 0 (disk) PTE 7 1 VP 1 Memory resident VP 2 page table (DRAM) VP 3 VP 4 VP 5 VP 6 VP 7 Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 17 CS 485: Systems Programming Locality to the Rescue Again! Virtual memory seems terribly inefficient, but it works because of locality. At any point in time, programs tend to access a set of active virtual pages called the working set . Programs with better temporal locality will have smaller working sets If (working set size < main memory size) . Good performance for one process after compulsory misses If ( SUM(working set sizes) > main memory size ) . Thrashing: Performance meltdown where pages are swapped (copied) in and out continuously Adapted from slides by R. Bryant and D. O’Hallaron (http://csapp.cs.cmu.edu/3e/instructors.html) 18 CS 485: Systems Programming Today Address spaces VM as a tool for caching VM as a tool for memory management VM as a tool for memory protection Memory mapping Address translation Adapted from slides by R.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages47 Page
-
File Size-