University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);

Slides:



Advertisements
Similar presentations
Virtual Memory. The Limits of Physical Addressing CPU Memory A0-A31 D0-D31 “Physical addresses” of memory locations Data All programs share one address.
Advertisements

May 7, A Real Problem  What if you wanted to run a program that needs more memory than you have?
Computer Organization CS224 Fall 2012 Lesson 44. Virtual Memory  Use main memory as a “cache” for secondary (disk) storage l Managed jointly by CPU hardware.
Lecture 34: Chapter 5 Today’s topic –Virtual Memories 1.
1 A Real Problem  What if you wanted to run a program that needs more memory than you have?
Virtual Memory October 25, 2006 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs class16.ppt.
Carnegie Mellon 1 Virtual Memory: Concepts : Introduction to Computer Systems 15 th Lecture, Oct. 14, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Today Virtual memory (VM) Overview and motivation
©UCB CS 162 Ch 7: Virtual Memory LECTURE 13 Instructor: L.N. Bhuyan
Virtual Memory October 29, 2007 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs class16.ppt.
Virtual Memory Topics Virtual Memory Access Page Table, TLB Programming for locality Memory Mountain Revisited.
Carnegie Mellon 1 Virtual Memory: Concepts / : Introduction to Computer Systems 16 th Lecture, Oct. 21, 2014 Instructors: Greg Ganger, Greg.
Virtual Memory: Concepts
Virtual & Dynamic Memory Management Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
University of Washington Today Lab 5 out  Puts a *lot* together: pointers, debugging, C, etc.
Carnegie Mellon 1 Saint Louis University Virtual Memory CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides adapted from Bryant.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
1 Seoul National University Virtual Memory: Systems.
University of Washington Memory and Caches I The Hardware/Software Interface CSE351 Winter 2013.
1 Virtual Memory: Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Carnegie Mellon 1 Virtual Memory: Concepts Instructor: Rabi Mahapatra (TAMU) Slides: Randy Bryant and Dave O’Hallaron (CMU)
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
November 23, A Real Problem  What if you wanted to run a program that needs more memory than you have?
1 Some Real Problem  What if a program needs more memory than the machine has? —even if individual programs fit in memory, how can we run multiple programs?
University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?
Multilevel Caches Microprocessors are getting faster and including a small high speed cache on the same chip.
Virtual Memory.  Next in memory hierarchy  Motivations:  to remove programming burdens of a small, limited amount of main memory  to allow efficient.
University of Washington Exceptional Control Flow The Hardware/Software Interface CSE351 Winter 2013.
1 Chapter Seven CACHE MEMORY AND VIRTUAL MEMORY. 2 SRAM: –value is stored on a pair of inverting gates –very fast but takes up more space than DRAM (4.
CS2100 Computer Organisation Virtual Memory – Own reading only (AY2015/6) Semester 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts Slides adapted from Bryant.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Virtual Memory: Concepts CENG331 - Computer Organization.
Virtual Memory Topics Motivations for VM Address translation Accelerating translation with TLBs CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory Ch. 8 & 9 Silberschatz Operating Systems Book.
Virtual Memory Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs.
Virtual Memory October 14, 2008 Topics Address spaces Motivations for virtual memory Address translation Accelerating translation with TLBs lecture-14.ppt.
CSE 153 Design of Operating Systems Winter 2015 Lecture 11: Paging/Virtual Memory Some slides modified from originals by Dave O’hallaron.
1 Virtual Memory (I). 2 Outline Physical and Virtual Addressing Address Spaces VM as a Tool for Caching VM as a Tool for Memory Management VM as a Tool.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
University of Washington Indirection in Virtual Memory 1 Each process gets its own private virtual address space Solves the previous problems Physical.
Alan L. Cox Virtual Memory Alan L. Cox Some slides adapted from CMU slides.
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
CS161 – Design and Architecture of Computer
Memory COMPUTER ARCHITECTURE
Virtual Memory Samira Khan Apr 25, 2017.
CS161 – Design and Architecture of Computer
Section 9: Virtual Memory (VM)
From Address Translation to Demand Paging
Section 9: Virtual Memory (VM)
Today How was the midterm review? Lab4 due today.
Virtual Memory.
Today How’s Lab 3 going? HW 3 will be out today
Virtual Memory I CSE 351 Autumn 2017
CSE 153 Design of Operating Systems Winter 2018
CS 105 “Tour of the Black Holes of Computing!”
CSE 153 Design of Operating Systems Winter 2018
Virtual Memory: Concepts /18-213/14-513/15-513: Introduction to Computer Systems 17th Lecture, October 23, 2018.
ECE Dept., University of Toronto
Instructors: Majd Sakr and Khaled Harras
Morgan Kaufmann Publishers Memory Hierarchy: Virtual Memory
Virtual Memory Nov 27, 2007 Slide Source:
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Virtual Memory I CSE 351 Winter 2019
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
Cache writes and examples
Instructor: Phil Gibbons
Presentation transcript:

University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100); c.setGals(17); float mpg = c.getMPG(); get_mpg: pushq %rbp movq %rsp, %rbp... popq %rbp ret Java: C: Assembly language: Machine code: Computer system: OS: Data & addressing Integers & floats Machine code & C x86 assembly programming Procedures & stacks Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C

University of Washington Again: Processes Definition: A process is an instance of a running program  One of the most important ideas in computer science  Not the same as “program” or “processor” Process provides each program with two key abstractions:  Logical control flow  Each process seems to have exclusive use of the CPU  Private virtual address space  Each process seems to have exclusive use of main memory How are these illusions maintained?  Process executions interleaved (multi-tasking) – done…  Address spaces managed by virtual memory system – now! 2

University of Washington Virtual Memory (VM) Overview and motivation VM as tool for caching Address translation VM as tool for memory management VM as tool for memory protection 3

University of Washington Memory as we know it so far… is virtual! Programs refer to virtual memory addresses  movl (%ecx),%eax  Conceptually memory is just a very large array of bytes  Each byte has its own address  System provides address space private to particular “process” Allocation: Compiler and run-time system  Where different program objects should be stored  All allocation within single virtual address space But…  We probably don’t have exactly 2 w bytes of physical memory.  We certainly don’t have 2 w bytes of physical memory for every process.  We have multiple processes that usually should not interfere with each other, but sometimes should share code or data. 4 FF∙∙∙∙∙∙F 00∙∙∙∙∙∙0

University of Washington Problem 1: How Does Everything Fit? 5 64-bit addresses can address several exabytes (18,446,744,073,709,551,616 bytes) Physical main memory offers a few gigabytes (e.g. 8,589,934,592 bytes) ? 1 virtual address space per process, with many processes… (Actually, it’s smaller than that dot compared to virtual memory.)

University of Washington Problem 2: Memory Management 6 Physical main memory What goes where? stack heap.text.data … Process 1 Process 2 Process 3 … Process n x

University of Washington Problem 3: How To Protect 7 Physical main memory Process i Process j Problem 4: How To Share? Physical main memory Process i Process j

University of Washington How can we solve these problems? 8

University of Washington Indirection “Any problem in computer science can be solved by adding another level of indirection.” –David Wheeler, inventor of the subroutine (a.k.a. procedure) Without Indirection With Indirection Name Thing Name Thing 9 What if I want to move Thing?

University of Washington Indirection Indirection: the ability to reference something using a name, reference, or container instead the value itself. A flexible mapping between a name and a thing allows changing the thing without notifying holders of the name. Without Indirection With Indirection Examples of indirection:  Domain Name Service (DNS): translation from name to IP address  phone system: cell phone number portability  snail mail: mail forwarding  911: routed to local office  Dynamic Host Configuration Protocol (DHCP): local network address assignment  call centers: route calls to available operators, etc. Name Thing Name Thing 10

University of Washington Indirection in Virtual Memory 11 Each process gets its own private virtual address space Solves the previous problems Physical memory Virtual memory Process 1 Process n mapping

University of Washington Address Spaces Virtual address space: Set of N = 2 n virtual addresses {0, 1, 2, 3, …, N-1} Physical address space: Set of M = 2 m physical addresses (n >= m) {0, 1, 2, 3, …, M-1} Every byte in main memory has:  one physical address  zero, one, or more virtual addresses 12

University of Washington Mapping P2’s Virtual Address Space Physical Memory Disk A virtual address can be mapped to either physical memory or disk. 13 P1’s Virtual Address Space

University of Washington A System Using Physical Addressing 14 Used in “simple” systems with (usually) just one process:  embedded microcontrollers in devices like cars, elevators, and digital picture frames 0: 1: M-1: Main memory CPU 2: 3: 4: 5: 6: 7: Physical address (PA) Data word 8:... 4

University of Washington A System Using Virtual Addressing 15 Physical addresses are completely invisible to programs. Used in all modern desktops, laptops, servers, smartphones… One of the great ideas in computer science 0: 1: M-1: Main memory MMU 2: 3: 4: 5: 6: 7: Physical address (PA) Data word 8:... CPU Virtual address (VA) CPU Chip Memory Management Unit

University of Washington VM and the Memory Hierarchy Think of virtual memory as array of N = 2 n contiguous bytes. Pages of virtual memory are usually stored in physical memory, but sometimes spill to disk.  Pages are another unit of aligned memory (size is P = 2 p bytes)  Each virtual page can be stored in any physical page. 16 PP 2 m-p -1 Physical memory Empty on disk Virtual Page 0 VP 1 VP 2 n-p -1 Virtual memory unallocated in memory on disk unallocated in memory on disk Physical Page 0 PP 1 Empty in memory 0 N-1 M-1 0

University of Washington or: Virtual Memory as DRAM Cache for Disk Think of virtual memory as an array of N = 2 n contiguous bytes stored on a disk. Then physical main memory is used as a cache for the virtual memory array  The cache blocks are called pages (size is P = 2 p bytes) PP 2 m-p -1 Physical memory Empty Uncached VP 0 VP 1 VP 2 n-p -1 Virtual memory Unallocated Cached Uncached Unallocated Cached Uncached PP 0 PP 1 Empty Cached 0 N-1 M-1 0 Virtual pages (VPs) stored on disk Physical pages (PPs) cached in DRAM 17

University of Washington Memory Hierarchy: Core 2 Duo 18 Disk Main Memory L2 unified cache L1 I-cache L1 D-cache CPUReg 2 B/cycle8 B/cycle16 B/cycle1 B/30 cyclesThroughput: Latency:100 cycles14 cycles3 cyclesmillions ~4 MB 32 KB ~4 GB~500 GB Not drawn to scale Miss penalty (latency): 33x Miss penalty (latency): 10,000x SRAM Static Random Access Memory DRAM Dynamic Random Access Memory

University of Washington Virtual Memory Design Consequences Large page size: typically 4-8 KB, sometimes up to 4 MB Fully associative  Any virtual page can be placed in any physical page  Requires a “large” mapping function – different from CPU caches Highly sophisticated, expensive replacement algorithms in OS  Too complicated and open-ended to be implemented in hardware Write-back rather than write-through 19

University of Washington Address Translation 20 How do we perform the virtual -> physical address translation? 0: 1: M-1: Main memory MMU 2: 3: 4: 5: 6: 7: Physical address (PA) Data word 8:... CPU Virtual address (VA) CPU Chip

University of Washington Address Translation: Page Tables A page table is an array of page table entries (PTEs) that maps virtual pages to physical pages. 21 null Memory resident page table (DRAM) Physical memory (DRAM) VP 7 VP 4 Virtual memory (disk) Valid Physical page number or disk address PTE 0 PTE 7 PP 0 VP 2 VP 1 PP 3 VP 1 VP 2 VP 4 VP 6 VP 7 VP 3 How many page tables are in the system? One per process stored in physical memory managed by HW (MMU), OS

University of Washington Address Translation With a Page Table 22 Virtual page number (VPN)Virtual page offset (VPO) Physical page number (PPN) Physical page offset (PPO) Virtual address (VA) Physical address (PA) Valid Physical page number (PPN) Page table base register (PTBR) Page table Page table address for process Valid bit = 0: page not in memory (page fault) In most cases, the hardware (the MMU) can perform this translation on its own, without software assistance This feels familiar…

University of Washington Page Hit Page hit: reference to VM byte that is in physical memory null Memory resident page table (DRAM) Physical memory (DRAM) VP 7 VP 4 Virtual memory (disk) Valid Physical page number or disk address PTE 0 PTE 7 PP 0 VP 2 VP 1 PP 3 VP 1 VP 2 VP 4 VP 6 VP 7 VP 3 Virtual address 23

University of Washington Page Fault Page fault: reference to VM byte that is NOT in physical memory 24 null Memory resident page table (DRAM) Physical memory (DRAM) VP 7 VP 4 Virtual memory (disk) Valid Physical page number or disk address PTE 0 PTE 7 PP 0 VP 2 VP 1 PP 3 VP 1 VP 2 VP 4 VP 6 VP 7 VP 3 Virtual address What happens when a page fault occurs?

University of Washington User writes to memory location That portion (page) of user’s memory is currently on disk Page handler must load page into physical memory Returns to faulting instruction: mov is executed again! Successful on second try int a[1000]; main () { a[500] = 13; } 80483b7:c d d movl $0xd,0x8049d10 User ProcessOS exception: page fault Create page and load into memory returns movl 25 Fault Example: Page Fault

University of Washington Handling Page Fault Page miss causes page fault (an exception) null Memory resident page table (DRAM) Physical memory (DRAM) VP 7 VP 4 Virtual memory (disk) Valid Physical page number or disk address PTE 0 PTE 7 PP 0 VP 2 VP 1 PP 3 VP 1 VP 2 VP 4 VP 6 VP 7 VP 3 Virtual address 26

University of Washington Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) null Memory resident page table (DRAM) Physical memory (DRAM) VP 7 VP 4 Virtual memory (disk) Valid Physical page number or disk address PTE 0 PTE 7 PP 0 VP 2 VP 1 PP 3 VP 1 VP 2 VP 4 VP 6 VP 7 VP 3 Virtual address 27

University of Washington Handling Page Fault Page miss causes page fault (an exception) Page fault handler selects a victim to be evicted (here VP 4) null Memory resident page table (DRAM) Physical memory (DRAM) VP 7 VP 3 Virtual memory (disk) Valid Physical page number or disk address PTE 0 PTE 7 PP 0 VP 2 VP 1 PP 3 VP 1 VP 2 VP 4 VP 6 VP 7 VP 3 Virtual address 28

University of Washington 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! null Memory resident page table (DRAM) Physical memory (DRAM) VP 7 VP 3 Virtual memory (disk) Valid Physical page number or disk address PTE 0 PTE 7 PP 0 VP 2 VP 1 PP 3 VP 1 VP 2 VP 4 VP 6 VP 7 VP 3 Virtual address 29

University of Washington Why does it work? 30

University of Washington Why does VM work on RAM/disk? Locality. Virtual memory works well for avoiding disk accesses because of locality  Same reason that L1 / L2 / L3 caches work The set of virtual pages that a program is “actively” accessing at any point in time is called its working set  Programs with better temporal locality will have smaller working sets If (working set size of one process < main memory size):  Good performance for one process after compulsory misses But if SUM(working set sizes of all processes) > main memory size:  Thrashing: Performance meltdown where pages are swapped (copied) between memory and disk continuously. CPU always waiting or paging.  Full quote: “Every problem in computer science can be solved by adding another level of indirection, but that usually will create another problem.” 31