Virtual Memory.

Slides:



Advertisements
Similar presentations
9.4 Page Replacement What if there is no free frame?
Advertisements

Module 10: Virtual Memory
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Chapter 9: Virtual Memory
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Virtual Memory. 2 What is virtual memory? Each process has illusion of large address space –2 32 for 32-bit addressing However, physical memory is much.
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Module 9: Virtual Memory
Module 10: Virtual Memory Background Demand Paging Performance of Demand Paging Page Replacement Page-Replacement Algorithms Allocation of Frames Thrashing.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
Virtual-Memory Management
Gordon College Stephen Brinton
Ken Birman. How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines all.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
1 Virtual Memory vs. Physical Memory So far, all of a job’s virtual address space must be in physical memory However, many parts of programs are never.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 9: Virtual Memory Background.
Chapter 10: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 10: Virtual Memory.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
Thrashing and Memory Management
1 Virtual Memory Management B.Ramamurthy Chapter 10.
OS Spring’04 Virtual Memory: Page Replacement Operating Systems Spring 2004.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Virtual Memory. 2 Last Week Memory Management Increase degree of multiprogramming –Entire process needs to fit into memory Dynamic Linking and Loading.
Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Virtual Memory Prof. Van Renesse & Sirer. Segments Note: overloaded term… Chunks of virtual address space Access Protection – User/Supervisor – Read/Write/Execute.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Page 19/17/2015 CSE 30341: Operating Systems Principles Optimal Algorithm  Replace page that will not be used for longest period of time  Used for measuring.
Part 8: Virtual Memory. Silberschatz, Galvin and Gagne ©2005 Virtual vs. Physical Address Space Each process has its own virtual address space, which.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples Operating.
Operating Systems CMPSC 473 Virtual Memory Management (3) November – Lecture 20 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
Virtual Memory The memory space of a process is normally divided into blocks that are either pages or segments. Virtual memory management takes.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 9: Virtual-Memory Management.
CS307 Operating Systems Virtual Memory Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
Silberschatz, Galvin and Gagne  Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory.
Virtual Memory The address used by a programmer will be called a virtual address or logical address. An address in main memory is called a physical address.
CSE 153 Design of Operating Systems Winter 2015 Lecture 12: Page Replacement.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
1 Page Replacement Algorithms. 2 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging  Load pages into memory only when.
10.1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples.
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
Virtual Memory. 2 Last Week Memory Management Increase degree of multiprogramming –Entire process needs to fit into memory Dynamic Linking and Loading.
1 Content Lecture 8 Virtual memory 1.Virtual memory concept 2.Paging on demand 3.Page replacement 4.Algorithm LRU and it’s approximation 5.Process memory.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
COS 318: Operating Systems Virtual Memory Paging.
Chapter 9: Virtual Memory
Logistics Homework 5 will be out this evening, due 3:09pm 4/14
Virtual Memory.
CSE 120 Principles of Operating
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Demand Paged Virtual Memory
VM Page Replacement Hank Levy.
Chapter 6 Virtual Memory
There’s not always room for one more. Brian Bershad
Operating Systems CMPSC 473
Lecture 9: Caching and Demand-Paged Virtual Memory
Module 9: Virtual Memory
There’s not always room for one more. Brian Bershad
Virtual Memory.
Presentation transcript:

Virtual Memory

Goals for Today Virtual memory Mechanism Policy How does it work? What to replace? How much to fetch?

What is virtual memory? Each process has illusion of large address space 232 for 32-bit addressing However, physical memory is much smaller How do we give this illusion to multiple processes? Virtual Memory: some addresses reside in disk page table Physical memory disk Virtual memory page 0 page 1 page 2 page 3 page 4 page N

Virtual memory Separates users logical memory from physical memory. Only part of the program needs to be in memory for execution Logical address space can therefore be much larger than physical address space Allows address spaces to be shared by several processes Allows for more efficient process creation

Swapping vs Paging Swapping Paging Loads entire process in memory , runs it, exit Is slow (for big, long-lived processes) Wasteful (might not require everything) Paging Runs all processes concurrently, taking only pieces of memory (specifically, pages) away from each process Finer granularity, higher performance Paging completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory The verb “to swap” is also used to refer to pushing contents of a page out to disk in order to bring other content from disk; this is distinct from the noun “swapping”

How does VM work? Modify Page Tables with another bit (“is present”) 1 If page in memory, is_present = 1, else is_present = 0 If page is in memory, translation works as before If page is not in memory, translation causes a page fault 1 2 3 32 :P=1 4183 :P=0 177 :P=1 5721 :P=0 Disk Mem Page Table

Page Faults On a page fault: OS finds a free frame, or evicts one from memory (which one?) Want knowledge of the future? Issues disk request to fetch data for page (what to fetch?) Just the requested page, or more? Block current process, context switch to new process (how?) Process might be executing an instruction When disk completes, set present bit to 1, and current process in ready queue

Steps in Handling a Page Fault

What to replace? What happens if there is no free frame? find a suitable page in memory, swap it out Page Replacement When process has used up all frames it is allowed to use OS must select a page to eject from memory to allow new page The page to eject is selected using the Page Replacement Algo Goal: Select page that minimizes future page faults

Modified/Dirty Bits Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk, non-modified pages can always be brought back from the original source Process text segments are rarely modified, can bring pages back from the program image stored on disk

Page Replacement

Page Replacement Algorithms Random: Pick any page to eject at random Used mainly for comparison FIFO: The page brought in earliest is evicted Ignores usage OPT: Belady’s algorithm Select page not used for longest time LRU: Evict page that hasn’t been used the longest Past could be a good predictor of the future MRU: Evict the most recently used page LFU: Evict least frequently used page

First-In-First-Out (FIFO) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in memory at a time per process): 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 4 frames: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Belady’s Anomaly: more frames  more page faults 1 1 4 5 2 2 1 3 9 page faults 3 3 2 4 1 1 5 4 2 2 1 10 page faults 5 3 3 2 4 4 3

FIFO Illustrating Belady’s Anomaly

Optimal Algorithm Replace page that will not be used for longest period of time 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 How do you know this? Used for measuring how well your algorithm performs 1 4 6 page faults 2 3 4 5

Example: FIFO, OPT Reference stream is A B C A B D A D B C OPTIMAL A B C A B D A D B C B toss C 5 Faults toss A or D A B C D FIFO A B C A B D A D B C B toss A 7 Faults toss ?

OPT Approximation In real life, we do not have access to the future page request stream of a program No crystal ball, no way to know definitively which pages a program will access So we need to make a best guess at which pages will not be used for the longest time

Least Recently Used (LRU) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Counter implementation Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter When a page needs to be changed, look at the counters to determine which are to change 1 1 1 1 5 2 2 2 2 2 3 5 5 4 4 4 4 3 3 3

Implementing Perfect LRU On reference: Time stamp each page On eviction: Scan for oldest frame Problems: Large page lists Timestamps are costly Approximate LRU LRU is already an approximation! 13 t=4 t=14 t=5 0xffdcd: add r1,r2,r3 0xffdd0: ld r1, 0(sp) 14 14

LRU: Clock Algorithm Each page has a reference bit Algorithm: Problem: Set on use, reset periodically by the OS Algorithm: FIFO + reference bit (keep pages in circular list) Scan: if ref bit is 1, set to 0, and proceed. If ref bit is 0, stop and evict. Problem: Low accuracy for large memory R=1 R=1 R=0 R=0 R=1 R=0 R=1 R=1 R=1 R=0 R=0

LRU with large memory Solution: Add another hand What if angle small? Leading edge clears ref bits Trailing edge evicts pages with ref bit 0 What if angle small? What if angle big? Sensitive to sweeping interval and angle Fast: lose usage information Slow: all pages look used R=1 R=0

Other Algorithms MRU: Remove the most recently touched page Works well for data accessed only once, e.g. a movie file Not a good fit for most other data, e.g. frequently accessed items LFU: Remove page with lowest count No track of when the page was referenced Use multiple bits. Shift right by 1 at regular intervals. MFU: remove the most frequently used page LFU and MFU do not approximate OPT well

Allocating Pages to Processes Global replacement Single memory pool for entire system On page fault, evict oldest page in the system Problem: lack of performance isolation Local (per-process) replacement Have a separate pool of pages for each process Page fault in one process can only replace pages from its own process Problem: might have idle resources

Thrashing Def: Excessive rate of paging Why does it occur? May stem from lack of resources More likely, caused by bad choices of the eviction algorithm Keep throwing out page that will be referenced soon So, they keep accessing memory that is not there Why does it occur? Poor locality, past != future There is reuse, but process does not fit model Too many processes in the system

Working Set Peter Denning, 1968 He uses this term to denote memory locality of a program Def: pages referenced by process in last  time-units comprise its working set For our examples, we usually discuss WS in terms of , a “window” in the page reference string. But while this is easier on paper it makes less sense in practice! In real systems, the window should probably be a period of time, perhaps a second or two.

Working Sets The working set size is num pages in the working set the number of pages touched in the interval [t-Δ+1..t]. The working set size changes with program locality. during periods of poor locality, you reference more pages. Within that period of time, you will have a larger working set size. Goal: keep WS for each process in memory. E.g. If  WSi for all i runnable processes > physical memory, then suspend a process

Working Set Approximation Approximate with interval timer + a reference bit Example:  = 10,000 Timer interrupts after every 5000 time units Keep in memory 2 bits for each page Whenever a timer interrupts copy and sets the values of all reference bits to 0 If one of the bits in memory = 1  page in working set Why is this not completely accurate? Cannot tell (within interval of 5000) where reference occured Improvement = 10 bits and interrupt every 1000 time units

Using the Working Set Used mainly for prepaging Pages in working set are a good approximation In Windows processes have a max and min WS size At least min pages of the process are in memory If > max pages in memory, on page fault a page is replaced Else if memory is available, then WS is increased on page fault The max WS can be specified by the application The max is also modified then window is minimized! Let’s see the task manager

Page Fault Frequency Thrashing viewed as poor ratio of fetch to work PFF = page faults / instructions executed if PFF rises above threshold, process needs more memory not enough memory on the system? Swap out. if PFF sinks below threshold, memory can be taken away

Working Sets and Page Fault Rates stable transition

OS and Paging Process Creation: Process Execution Page Faults Allocate space and initialize page table for program and data Allocate and initialize swap area Info about PT and swap space is recorded in process table Process Execution Reset MMU for new process Flush the TLB Bring processes’ pages in memory Page Faults Process Termination Release pages