The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

Similar documents
Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

Virtual Memory. CSCI 315 Operating Systems Design Department of Computer Science

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Chapter 8 Memory Management

Chapter 8 Main Memory

Virtual Memory: Mechanisms. CS439: Principles of Computer Systems February 28, 2018

Memory Management. Dr. Yingwu Zhu

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Module 8: Memory Management

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Chapter 8: Memory-Management Strategies

Multi-Process Systems: Memory (2) Memory & paging structures: free frames. Memory & paging structures. Physical memory

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

Chapter 9 Memory Management

Memory Management Cache Base and Limit Registers base limit Binding of Instructions and Data to Memory Compile time absolute code Load time

CS 5523 Operating Systems: Memory Management (SGG-8)

Goals of Memory Management

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

Virtual Memory. Virtual Memory

Memory Management. Memory Management

Memory Management. Contents: Memory Management. How to generate code? Background

Chapter 8: Main Memory

8.1 Background. Part Four - Memory Management. Chapter 8: Memory-Management Management Strategies. Chapter 8: Memory Management

CS6401- Operating System UNIT-III STORAGE MANAGEMENT

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

CS307 Operating Systems Main Memory

Module 9: Memory Management. Background. Binding of Instructions and Data to Memory

Logical versus Physical Address Space

Chapter 8: Main Memory

Chapter 9 Memory Management Main Memory Operating system concepts. Sixth Edition. Silberschatz, Galvin, and Gagne 8.1

Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Chapter 8: Memory- Management Strategies

a process may be swapped in and out of main memory such that it occupies different regions

Memory management: outline

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

Memory management: outline

Processes and Virtual Memory Concepts

Part Three - Memory Management. Chapter 8: Memory-Management Strategies

Chapter 8: Main Memory

Recall from Tuesday. Our solution to fragmentation is to split up a process s address space into smaller chunks. Physical Memory OS.

Module 8: Memory Management

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

CS5460: Operating Systems Lecture 14: Memory Management (Chapter 8)

Basic Memory Management

Chapter 8: Main Memory

Lecture 8 Memory Management Strategies (chapter 8)

Operating System Support

Virtual Memory. Chapter 8

Principles of Operating Systems

Memory Management. CSCI 315 Operating Systems Design Department of Computer Science

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Address Translation. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

COS 318: Operating Systems. Virtual Memory and Address Translation

198:231 Intro to Computer Organization. 198:231 Introduction to Computer Organization Lecture 14

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

VIRTUAL MEMORY II. Jo, Heeseung

Chapter 9: Virtual-Memory

Chapter 9: Memory Management. Background

CS 153 Design of Operating Systems Winter 2016

Virtual Memory Outline

CS399 New Beginnings. Jonathan Walpole

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

Recap: Memory Management

Lecture #15: Translation, protection, sharing

Readings and References. Virtual Memory. Virtual Memory. Virtual Memory VPN. Reading. CSE Computer Systems December 5, 2001.

Chapter 8: Memory Management

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Motivation. Memory Management. Memory Management. Computer Hardware Review

Address Translation. Tore Larsen Material developed by: Kai Li, Princeton University

Computer Systems. Virtual Memory. Han, Hwansoo

CS307: Operating Systems

16 Sharing Main Memory Segmentation and Paging

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 8: MEMORY

Chapter 8: Memory Management Strategies

Chapter 3 - Memory Management

CSE 153 Design of Operating Systems

Main Memory (Part II)

CS 3733 Operating Systems:

File Systems. OS Overview I/O. Swap. Management. Operations CPU. Hard Drive. Management. Memory. Hard Drive. CSI3131 Topics. Structure.

Main Memory. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & APPS 1

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

Virtual Memory 1. To do. q Segmentation q Paging q A hybrid system

Chapter 8 Main Memory

I.-C. Lin, Assistant Professor. Textbook: Operating System Principles 7ed CHAPTER 8: MEMORY

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

Objectives and Functions Convenience. William Stallings Computer Organization and Architecture 7 th Edition. Efficiency

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

Operating Systems. Paging... Memory Management 2 Overview. Lecture 6 Memory management 2. Paging (contd.)

Operating Systems. Operating Systems Sina Meraji U of T

Transcription:

The Virtual Memory Abstraction Memory Management Physical Memory Unprotected address space Limited size Shared physical frames Easy to share data Virtual Memory Programs are isolated Arbitrary size All programs loaded at Sharing is possible Address spaces: Physical and Virtual Physical address space consists of the collection of memory addresses supported by the hardware Virtual address space consists of the collection of addresses that the process can touch Note: CPU generates virtual addresses Address Translation A function that maps pid, virtual address into physical address Virtual a486d9 function implemented through a combination of hw and sw Advantages: protection relocation data sharing multiplexing Physical 5e3a7

Protection Relocation At all times, the functions used by different processes map to disjoint ranges The range of the function used by a process can change over time p j Relocation Data Sharing The range of the function used by a process can change over time Map different virtual addresses of different processes to the same physical address 4d26a p j 5e3a7 119af3

Contiguity Contiguity Contiguous addresses in the domain need not map to contiguous addresses in the codomain Contiguous addresses in the domain need not map to contiguous addresses in the codomain Multiplexing Multiplexing The domain (set of virtual addresses) that map to a given range of physical addresses can change over time The domain (set of virtual addresses) that map to a given range of physical addresses can change over time

Multiplexing Multiplexing The domain (set of virtual addresses) that map to a given range of physical addresses can change over time The domain (set of virtual addresses) that map to a given range of physical addresses can change over time Multiplexing One idea, many implementations The domain (set of virtual addresses) that map to a given range of physical addresses can change over time Base and limit Segment table maps variable-sized ranges of contiguous VAs to a range of contiguous PAs Page table map fixed-size ranges of contiguous VAs to fixed sized ranges of contiguous PAs Paged segmentation Multilevel page tables Inverted page table It s all just a lookup Virtual Address Physical Address a394 1 56bb3 1 24421 unmapped unmapped unmapped ffffff d82a4

Base & Limit On Base & Limit MAX sys Memory Exception Logical no addresses CPU + yes Implementation HW Add base and bound registers to CPU SW Add base and bound registers to PCB On context switch, change B&B (privileged) 5 1 Limit Register Base Register Physical addresses 15 1 p s physical address space Contiguous Allocation: contiguous virtual addresses are mapped to contiguous physical addresses Protection is easy, but sharing is hard Two copies of emacs: want to share code, but have data and stack distinct Managing heap and stack dynamically is hard We want them as far as as possible in virtual address space, but Contiguous allocation: multiple variable partitions OS keeps track of empty blocks ( holes ) Initially, one big hole! Over time, a queue of processes (with their memory requirements) and a list of holes OS decides which process to load in memory next Once process is done, it releases memory OS queue p 9 p 1 p 11 OS p 1 p 6 p 4 p 2 Strategies for Contiguous Memory Allocation First Fit Allocate first big-enough hole Next Fit As first fit, but start to search where you previously left off Best Fit Allocate smallest big-enough hole

Fragmentation Fragmentation External fragmentation Unusable memory between units of allocation OS queue OS p 1 External fragmentation Unusable memory between units of allocation OS p 1 p 9 p 6 p 11 Internal fragmentation Unusable memory within a unit of allocation p 6 p 11 p 4 p 4 p 2 p 2 External fragmentation Fragmentation Unusable memory between units of allocation OS p 1 Eliminating External Fragmentation: Compaction Relocate programs to coalesce holes OS p 1 Internal fragmentation Unusable memory within a unit of allocation p 6 Internal Fragmentation p 11 Problem with I/O Pin job in memory while it is performing I/O p 6 p 11 p 4 Do I/O in OS buffers p 2 p 2

Eliminating External Fragmentation: Swapping E Pluribus Unum Preempt processes and reclaim their memory Move images of suspended processes to backing store Code From a user s perspective, a process is a collection of distinct logical address spaces Heap Ready Running OS Ready queue Global vars Librares Suspended Waiting p 2 swan Suspended queue Semaphores/condition queues p 1 swap out Stack E Pluribus Unum Implementing Segmentation From a user s perspective, a process is a collection of distinct logical address spaces We call these logical address spaces segments Heap Code Global vars Librares Stack Contiguous mapping of addresses within segment Holes in Virtual address space: a problem? Think of address as (s, o) s is the segment number o is the offset within the segment CPU s o s STBR base Segment Table Base Register Logical addresses limit Segment table generalizes base & limit Limit Memory Exception no + p s segment yes Base 5 1 MAX sys Physical 15 addresses 1

On Segmentation Paging Sharing a segment is easy! Protection bits control access to shared segments External fragmentation base limit 4 6 4 1 13 Allocate VA & PA memory in fixed-sized chunks (pages and frames, respectively) memory allocation can use a bitmap typical size of page/frame: 4KB to 16KB Each process maintains a segment table, which is saved to PCB on a context switch Fast? How do we enlarge a segment? 29 2 25 2 32 5 13 1 23 25 27 29 31 32 Gives illusion of contiguity but adjacent pages in VA need not map to contiguous frames in PA Of course, now internal fragmentation 37 Virtual address Virtual address 32 bits 2 bits 12 bits Two components page number offset within page Two components page number - how many pages in the VA offset within page - how large is a page? To access a piece of data extract page number extract offset translate page number to frame number access data at offset in frame

Virtual address Basic Paging Implementation 2 bits 12 bits Two components page number - how many pages in the VA offset within page - how large is a page? To access a piece of data extract page number extract offset translate page number to frame number access data at offset in frame 2 2-1 Page table 4 3 2 1 8 4 6 1 2 CPU p o PTBR p Page Table f Page Table Base Register Logical addresses Memory Exception no Frame size yes f o Physical addresses f Speeding things up Sharing CPU p o page # frame # Memory Exception no TLB hit f o yes Physical addresses f Page Table Process Physical Memory Page Table Process 1 Processes share pages by mapping virtual pages tot he same memory frame code segments of processes running same program can share pages with executables TLB TLB miss f PTBR p Segment Table Base Register EAT: (1+ɛ)α+(2+ɛ)(1 α) =2+ɛ α ( α : hit ratio) Fine tuning using protection bits (rwx)

Page table Memory Protection Used valid/invalid bit to indicate which mappings are active 15 14 13 12 11 1 9 8 7 6 5 4 3 2 1 4 i 7 i 2 i i 7 v 6 i 5 v 4 i 2 i i 3 v 4 v v 6 v 1 v 2 v Memory frames Protection bits Caching disabled Referenced Modified Valid/ invalid 11 2 9 4 5 1 3 What happens on a TLB miss? Can be handled in software or hardware Software TLB generates trap Switch to kernel mode OS does translation OS loads new TLB entry and returns from trap On Context Switch Flush TLB or add PID tag to TLB add a CPU register change PID register on context switch Hardware HW includes PTB register HW follows pointer and does look un page table Exception handler invoked only if no/bad mapping/permission On Context Switch change value stored in PTB register flush TLB = Space overhead Two sources data structure overhead (the page table!) fragmentation How large should a page be? Overhead for paging: (#entries x sizeofentry) + (# segments x pagesize/2) = ((VA_Size/pagesize) x sizeofentry) + (# segments x pagesize/2) Size of entry enough bits to identify physical page (log 2 (PA_Size / page size)) should include control bits (valid, modified, referenced, etc) usually word or byte aligned Computing paging overhead 1 MB maximum VA, 1 KB page, 3 segments (program, stack, heap) ((2 2 / 2 1 ) x sizeofentry) + (3 x 2 9 ) If I know PA is 64 KB then sizeofentry = 6 bits (2 6 frames) + control bits if 3 control bits, byte aligned size of entry: 16 bits

Oops Multi-level Paging What is the size of the page table for a machine with 64-bit addresses and a page size of 4KB? Good news much of the space is unused Use a smarter data structure to capture page table tree! PTE PTE 1 PTE 2 (null) PTE 3 (null) PTE 4 (null) PTE 5 (null) PTE 6 (null) PTE 7 (null) PTE 8 (1K - 9) null PTEs 32 bit address space 4Kb pages 4 bytes PTE PTE PTE 123 PTE PTE 123 123 null PTEs PTE 123 VP VP 123 VP 124 VP 247 Gap 123 unallocated pages VP 9215 unallocated pages 2K pages code/data 6K pages unallocated 123 pages unallocated 1 page for stack PTBR Structure virtual address space as a tree Virtual address of a SPARC p 1 p 1 p 2 p 3 o 8 6 6 12 255 1 63 63 v p 2 p 3 16K 8K 4K Examples Examples Two level paging Two level paging 2 bits 12 bits 2 bits 12 bits Outer page table fits in a page Rest of page table allocated in page-size chunks

Examples Examples Two level paging 1 bits 1 bits 12 bits Outer page table fits in a page Rest of page table allocated in page-size chunks internal fragmentation (where?) increased TLB miss time 64-bit VA; 2K page; 4 byte/entry How many levels? each page table includes 512 entries (2 9 ) number of pages = 2 64 /2 11 number of levels - 53/9 = 6 (rounded up) The Challenge of Large Address Spaces Page Registers (aka Inverted Page Tables) With large address spaces (64-bits) page tables become cumbersome 5/6 levels of tables A new approach---make tables proportional to the size of the physical, not the virtual, address space virtual address space is growing faster than physical For each frame, a register containing Residence bit is the frame occupied? Page # of the occupying page Protection bits Catch? An example 16 MB of memory Page size: 4k # of frames: 496 Used by page registers (8 bytes/ register): 32 KB Overhead: 2% Insensitive to size of virtual memory

Basic Inverted Page Table Architecture Where have all the pages gone? Searching 32KB of registers on every memory reference is not fun CPU pid p offset f offset Physical Memory If the number of frames is small, the page registers can be placed in an associative memory---but search pid p f Large associative memories are expensive hard to access in a single cycle consume lots of power Inverted Page Table The BIG picture The BIG picture CPU vaddr Translator Box paddr Physical memory CPU vaddr Vaddr if match data Virtually addressed cache if no match if no match vpage TLB ppage if match if no match Main memory Paddr data PTBR (per process) vpage Segment and page table lookup dictionary if match Physically ppage addressed cache

Time Overhead Demand Paging Average Memory Access Time (AMAT) AMAT = T L1 + (P L1miss x T L1miss ) T L1miss = T TLB + (P TLBmiss x T TLBmiss ) + T L2 +!!!! (P L2miss x T mem ) T TLBmiss = #references x (T L2 + P L2miss x T mem ) To fill TLB Code pages are stored in a file on disk some are currently residing in memory most are not Data and stack pages are also stored in a file OS determines what portion of VAS is mapped in memory this file is typically invisible to users file only exists while a program is executing Creates mapping on demand Page-Fault Handling Page-Fault Handling References to a non-mapped page (i in page table) generate a page fault CPU Handling a page fault: Processor runs interrupt handler OS blocks running process OS finds a free frame OS schedules read of unmapped page When read completes, OS changes page table OS restarts faulting process from instruction that caused page fault 1 OS 2 i Page Table 5 3 Secondary Storage free frame 61 4 Physical Memory References to a non-mapped page (i in page table) generate a page fault CPU Handling a page fault: Processor runs interrupt handler OS blocks running process OS finds a free frame OS schedules read of unmapped page When read completes, OS changes page table OS restarts faulting process from instruction that caused page fault 1 OS 61 v Page Table 2 6 Secondary Storage 5 3 free frame 61 4 Physical Memory

Taking a Step Back Page replacement Physical and virtual memory partitioned into equal-sized units (respectively, frames and pages) Size of VAS decoupled to size of physical memory No external fragmentation Minimizing page faults is key to good performance Local vs Global replacement Local: victim chosen from frames of faulty process fixed allocation per process Global: victim chosen from frames allocated to any process variable allocation per process Many replacement policies Random, FIFO, LRU, Clock, Working set, etc Goal is minimizing number of page faults FIFO Replacement FIFO Replacement First block loaded is first replaced Low overhead Commonly used First block loaded is first replaced Low overhead Commonly used F F1 F2 F3 a b a d g a f d g a f c b g a a a a a b b b b d d g a b b d d g g f 1 2 3 4 5 6 7 8 9 1 11 12 13 14 M M H M M H M a b a d g a f d g a f c b g F a a a a a a b b b d d g f a F1 b b b b b d d d g g f a c F2 d d d g g g f f a c b F3 g g f f f a a c b g 1 2 3 4 5 6 7 8 9 1 11 12 13 14 M M H M M H M H H M H M M M

LRU Replacement Replace block referenced least recently Reference stack referenced block moved to top of stack on page fault, block on bottom of stack is replacedand new block is placed on top of stack Difficult to implement a b a d g a f d g a f c b g F a b a d g a f d g a f c b g F1 a b a d g a f d g a f c b F2 b a d g a f d g a f c F3 b b d g a f d g a f 1 2 3 4 5 6 7 8 9 1 11 12 13 14 M M H M M H M H H H H M M M Clock Replacement First-In-Not-Used -First-Out replacement Like FIFO, but add a used bit (*) for each queue entry and make queue circular Clock hand points to orange frame a b a d g a f d g a f c b g F a a a* a* a* a* a a a a* a* a a g F1 b b b b b f f f f f* f f f F2 d d d d d* d* d* d* c c c F3 g g g g g* g* g* g b b 1 2 3 4 5 6 7 8 9 1 11 12 13 14 M M H M M H M H H H H M M M Optimal Replacement Replace block referenced furthest in future Minimum number of faults Impossible to implement a b a d g a f d g a f c b g F a a a a a a a a a a a c b b F1 b b b b b f f f f f f f f F2 d d d d d d d d d d d d F3 g g g g g g g g g g 1 2 3 4 5 6 7 8 9 1 11 12 13 14 M M H M M H M H H H H M M H T = 4 Working Set Replacement Global replacement policy WS t = set of pages referenced in (t-t+1, t) A page is replaced at t if it does not belong to WS t pages not necessarily replaced at page fault time! adapts allocation to changes in locality a b a d g a f d g a f c b g F a a a a a a a a a a a a a g F1 b b b b d d d d d d c c c F2 d d g g g g g g g b b F3 g f f f f f f f f 1 2 3 4 5 6 7 8 9 1 11 12 13 14 M M H M M H M H H H H M M M

Thrashing If too much multiprogramming, pages tossed out while needed one program touches 5 pages with enough pages, 1ns/ref if too few and faults every 5th reference 1ms for disk IO Degree of Multiprogramming one reference now costs 2ms: 2, times slowdown CPU Utilization Thrashing T = 3 a b a d g a f d g a f c b g F a a a a a a a a g g g c c c F1 b b b g g g d d d f f f g F2 d d d f f f a a a b b 1 2 3 4 5 6 7 8 9 1 11 12 13 14 M M H M M H M M M M M M M M