virtual memory
play

Virtual Memory Philipp Koehn 25 April 2018 Philipp Koehn Computer - PowerPoint PPT Presentation

Virtual Memory Philipp Koehn 25 April 2018 Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018 Recall: Process Address Space 1 Kernel memory ffffffff User stack Stack pointer Memory-mapped region for shared


  1. Virtual Memory Philipp Koehn 25 April 2018 Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  2. Recall: Process Address Space 1 Kernel memory ffffffff User stack Stack pointer Memory-mapped region for shared libraries Run time heap (created by malloc) Read/write segment (.data / .bss) Loaded from executable Read-only code segment (.init, .text., .rodata) 400000 0 Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  3. Virtual Memory 2 • Abstraction of physical memory • Purpose – appearance of more available memory than physically exists (DRAM) – handles disk caching / loading – insulates memory of each process • Page table: maps from virtual address to physical addresses • Memory management unit (MMU): hardware implementation of address translation Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  4. Warning 3 • This is going to get very complex • Closely tied with multi-tasking (multiple processes) • Partly managed by hardware, partly managed by software Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  5. 4 virtual addressing Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  6. Physical Addressing 5 CPU chip Main memory 0: 1: 2: Physical address (PA) CPU 3: 4: 5: 6: 7: … Data • So far, assumed CPU addresses physical memory Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  7. Virtual Addressing 6 CPU chip Main memory Virtual Address Physical 0: address translation address 1: (VA) (PA) 2: CPU MMU 3: 4: 5: 6: 7: … Data • Memory management unit (MMU): maps virtual to physical addresses Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  8. Address Space 7 N = 2 n bytes, e.g., 256TB • Virtual memory size: M = 2 m bytes, e.g., 16GB • Physical memory size: P = 2 p bytes, e.g., 4KB • Page (block of memory): • A virtual address can be encoded in n bits Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  9. 8 caching Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  10. Caching... Again? 9 • Yes, we already discussed caching but: for on-chip cache of DRAM memory • Now – caching between RAM and disk – driven by a large virtual memory address space – to avoid unnecessary and duplicate loading • Jargon – previously "block", now "page" – now: "swapping" or "paging" Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  11. Mapping 10 Virtual memory Physical memory 0: unallocated 1: cached 2: uncached empty 3: cached empty 4: unallocated 5: uncached 6: cached empty 7: unallocated 8: unallocated empty empty … 15: unallocated Virtual pages (VP) Physical pages (PP) stored on disk cached in DRAM Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  12. State of Virtual Memory Page 11 • Cached – allocated page – stored in physical memory • Uncached – allocated page – not in physical memory • Unallocated – not used by virtual memory system so far Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  13. Page Table 12 • Array of page table entries (PTE) • Valid bit – set if PTE currently maps to physical address (cached) – not set otherwise (uncached or unallocated) • Mapped address – if cached: physical address in DRAM – if not cached: physical address on disk Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  14. Page Table 13 Physical memory DRAM Page table VP1 VP6 0: 0 null VP3 1: 1 VP7 2: 0 3: 1 4: 0 null 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 VP6 VP7 Disk Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  15. Page Hit 14 Physical memory DRAM Virtual address Page table VP1 VP6 0: 0 null VP3 1: 1 VP7 2: 0 3: 1 4: 0 null 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 VP6 VP7 Disk Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  16. Page Fault 15 Physical memory DRAM Virtual address Page table VP1 VP6 0: 0 null VP3 1: 1 VP7 2: 0 3: 1 4: 0 null 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 VP6 VP7 • Valid bit = 0 Disk • Page not in RAM Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  17. Page Fault 16 Physical memory DRAM Virtual address Page table VP1 VP6 0: 0 null VP3 1: 1 VP7 2: 0 3: 1 4: 0 null 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 VP6 VP7 • Page is on disk Disk Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  18. Page Fault 17 Physical memory DRAM Virtual address Page table VP1 0: 0 null VP3 1: 1 VP7 2: 0 3: 1 4: 0 null 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 VP6 • Make space in RAM VP7 • Pre-empt "victim" page Disk • Typically out-dated cached page Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  19. Page Fault 18 Physical memory DRAM Virtual address Page table VP1 VP2 0: 0 null VP3 1: 1 VP7 2: 0 3: 1 4: 0 null 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 VP6 VP7 • Load page into RAM Disk Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  20. Page Fault 19 Physical memory DRAM Virtual address Page table VP1 VP2 0: 0 null VP3 1: 1 VP7 2: 1 3: 1 4: 0 null 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 VP6 VP7 • Update page table entry Disk Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  21. Allocating Pages 20 • What happens when we load a program? • We need to load its executable into memory • Similar: create data objects when program is running ("allocating" memory) Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  22. Allocating Page 21 Physical memory DRAM Page table VP1 VP6 0: 0 null VP3 1: 1 VP7 2: 0 3: 1 4: 0 null 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 VP6 VP7 • Identify space in virtual memory Disk Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  23. Allocating Page 22 Physical memory DRAM Page table VP1 VP6 0: 0 null VP3 1: 1 VP7 2: 0 3: 1 4: 0 5: 0 VP0 6: 1 VP1 7: 1 VP2 VP3 Valid Address VP4 VP5 • Map to data on disk VP6 VP7 – do not actual load Disk – just create page table entries – let virtual memory system handle loading ⇒ On-demand loading Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  24. Process Memory 23 • Nothing loaded at startup • Working set (or resident set) – pages of a process that are currently in DRAM – loaded by virtual memory system on demand • Thrashing – memory actively required by all processes larger than physically available – frequent swapping of memory to/from disk – very bad: slows down machine dramatically Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  25. 24 memory management Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  26. One Page Table per Process 25 Physical memory Process 1 0: 0 PP2 1: 1 VP1 2: 1 VP2 3: 0 PP7 Shared page Process 2 0 0: PP10 1: 1 VP1 2: 1 VP2 3: 0 Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  27. Process Address Space 26 Kernel memory ffffffff User stack Stack pointer Memory-mapped region for shared libraries Run time heap (created by malloc) Read/write segment (.data / .bss) Loaded from executable Read-only code segment (.init, .text., .rodata) 400000 0 Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  28. Simplified Linking 27 Data Code 400000 • Each process has its code in address 0x400000 • Easy linking: Linker can establish fixed addresses Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  29. Simplified Loading 28 • When loading process into memory... • Enter .data and .text section into page table • Mark them as invalid (= not actually in RAM) • Called memory mapping (more on that later) Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  30. Simplified Sharing 29 Physical memory Shared libraries • Shared libraries used by several processes e.g., stdio providing printf, e.g., scanf, open, close, ... Shared libraries • Not copied multiple times into RAM Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

  31. Simplified Memory Allocation 30 • Process may need more memory (e.g., malloc call) ⇒ New entry in page table • Mapped to arbitrary pages in physical memory • Do not have to be contiguous Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

Recommend


More recommend