Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.

Slides:



Advertisements
Similar presentations
VM Page Replacement Hank Levy.
Advertisements

Chapter 11 – Virtual Memory Management
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Virtual Memory: Page Replacement
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Virtual Memory Operating System Concepts chapter 9 CS 355
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.
Virtual Memory.
CSE451 Introduction to Operating Systems Winter 2012 Paging Mark Zbikowski Gary Kimura.
CSE 451: Operating Systems Autumn 2013 Module 12 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Ed Lazowska
Copyright ©: Lawrence Angrave, Vikram Adve, Caccamo 1 Virtual Memory III.
Chapter 101 Cleaning Policy When should a modified page be written out to disk?  Demand cleaning write page out only when its frame has been selected.
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.
CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
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.
Paging Algorithms Vivek Pai / Kai Li Princeton University.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
OS Spring’04 Virtual Memory: Page Replacement Operating Systems Spring 2004.
Page Replacement Algorithms
Memory Management 2 Tanenbaum Ch. 3 Silberschatz Ch. 8,9.
CSS430 Virtual Memory Textbook Ch9
Virtual Memory.
Lecture Topics: 11/17 Page tables TLBs Virtual memory flat page tables
Copyright ©: Lawrence Angrave, Vikram Adve, Caccamo 1 Virtual Memory.
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.
Lecture 11 Page 1 CS 111 Online Virtual Memory A generalization of what demand paging allows A form of memory where the system provides a useful abstraction.
Lecture Topics: 11/24 Sharing Pages Demand Paging (and alternative) Page Replacement –optimal algorithm –implementable algorithms.
CSC 360, Instructor: Kui Wu Memory Management II: Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory.
Page Buffering, I. Pages to be replaced are kept in main memory for a while to guard against poorly performing replacement algorithms such as FIFO Two.
CSE 153 Design of Operating Systems Winter 2015 Lecture 12: Page Replacement.
1 Page Replacement Algorithms. 2 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging  Load pages into memory only when.
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
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. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
IT 344: Operating Systems Module 11 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Chia-Chi Teng 265 CTB.
COS 318: Operating Systems Virtual Memory Paging.
CS 333 Introduction to Operating Systems Class 14 – Page Replacement
© 2013 Gribble, Lazowska, Levy, Zahorjan
CS703 - Advanced Operating Systems
CSE 120 Principles of Operating
Module 9: Virtual Memory
Chapter 9: Virtual-Memory Management
CSE 451: Operating Systems Autumn 2012 Module 12 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Ed Lazowska
5: Virtual Memory Background Demand Paging
VM Page Replacement Hank Levy.
CSE 451: Operating Systems Autumn 2003 Lecture 11 Demand Paging and Page Replacement Hank Levy Allen Center
There’s not always room for one more. Brian Bershad
CSE 451: Operating Systems Autumn Demand Paging and Page Replacement
CSE 451: Operating Systems Winter 2003 Lecture 11 Demand Paging and Page Replacement Hank Levy 412 Sieg Hall 1.
Operating Systems CMPSC 473
CSE 153 Design of Operating Systems Winter 19
CSE 451: Operating Systems Spring 2006 Module 11 Virtual Memory, Page Faults, Demand Paging, and Page Replacement John Zahorjan
Module 9: Virtual Memory
CSE 451: Operating Systems Autumn 2009 Module 11 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Ed Lazowska
Chapter 9: Virtual Memory CSS503 Systems Programming
CSE 451: Operating Systems Autumn 2010 Module 11 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Ed Lazowska
There’s not always room for one more. Brian Bershad
Demand Paging We’ve hinted that pages can be moved between memory and disk this process is called demand paging is different than swapping (entire process.
CSE 451: Operating Systems Winter 2006 Module 11 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Ed Lazowska
Virtual Memory.
CSE 451: Operating Systems Winter 2007 Module 11 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Ed Lazowska
Presentation transcript:

Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement

Review For a memory access instruction Does it use a virtual address or physical address? What can happen? Best case What if you are unlucky? Demand paging What is it? Page fault What is it? Why does it happen? Who handles it? How costly is it? ECE344: Page Replacement Ding Yuan 2

Demand Paging Algorithm Algorithm NEVER brings a page into main memory until it is needed 1.Page fault 2.Check if a valid virtual memory addr. Kill proc. if not. 3.If valid address, check if it’s cached in memory already (perhaps by other processes). If so, skip to 7. How can this be possible? 4.Find a free page frame. If no free page available, choose one to evict (which one? focus of this lecture) If the victim page is dirty, write it out to disk first 5.Suspend user process, map address into disk block and fetch disk block into page frame 6.When disk read finished, add vm mapping for page frame 7.If necessary, restart process. ECE344: Page Replacement Ding Yuan 3

4 Demand Paging (detail) Some Pages are evicted to disk when memory is full Pages loaded from disk when referenced again References to evicted pages cause a TLB miss PTE was invalid, causes fault OS allocates a page frame, reads page from disk When I/O completes, the OS fills in PTE, marks it valid, and restarts faulting process Dirty vs. clean pages Actually, only dirty pages (modified) need to be written to disk Clean pages do not – but you need to know where on disk to read them from again

Issue: Eviction Hopefully, kick out a less-useful page Goal: kick out the page that’s least useful Problem: how do you determine utility? Kick out pages that aren’t likely to be used again Heuristic: temporal locality exists ECE344: Page Replacement Ding Yuan 5

Page Replacement Strategies The Principle of Optimality Replace the page that will not be used again the farthest time in the future Random replacement Choose a page randomly FIFO – First In First Out Replace the page that has been in memory the longest LRU – Least Recently Used Replace the page that has not been used for the longest time NRU – Not Recently Used An approximation to LRU ECE344: Page Replacement Ding Yuan 6

7 Belady’s Algorithm Known as the optimal page replacement algorithm because it has the lowest fault rate for any page reference sequence Idea: Replace the page that will not be used for the longest time in the future Problem: Have to predict the future! Why is Belady’s useful then? Use it as a yardstick Compare implementations of page replacement algorithms with the optimal to gauge room for improvement If optimal is not much better, then algorithm is pretty good If optimal is much better, then algorithm could use some work Random replacement is often the lower bound

Optimal Example ECE344: Page Replacement Ding Yuan 8 12 references, 7 faults Miss rate: 7/12 Hit rate: 5/12

ECE344: Page Replacement Ding Yuan 9 First-In First-Out (FIFO) FIFO is an obvious algorithm and simple to implement Maintain a list of pages in order in which they were paged in On replacement, evict the one brought in longest time ago Why might this be good? Maybe the one brought in the longest ago is not being used Why might this be bad? Then again, maybe it’s not We don’t have any info to say one way or the other FIFO suffers from “Belady’s Anomaly” The fault rate might actually increase when the algorithm is given more memory (very bad)

FIFO ECE344: Page Replacement Ding Yuan references, 9 faults Miss rate: 9/12 Hit rate: 3/12

Intuitive Paging Behavior with Increasing Number of Page Frames ECE344: Page Replacement Ding Yuan 11

Belady’s Anomaly (for FIFO) ECE344: Page Replacement Ding Yuan references, 10 faults

ECE344: Page Replacement Ding Yuan 13 Least Recently Used (LRU) LRU uses reference information to make a more informed replacement decision Idea: We can’t predict the future, but we can make a guess based upon past experience On replacement, evict the page that has not been used for the longest time in the past (Belady’s: future) When does LRU do well? When does LRU do poorly? Implementation To be perfect, need to time stamp every reference (or maintain a stack) – much too costly So we need to approximate it

LRU ECE344: Page Replacement Ding Yuan 14 Evict A (Least Recent) Evict B (Least Recent) 12 references, 10 faults No Belady’s anomaly why?

Approximating LRU: NRU NRU: Evict a page that is NOT recently used; LRU: evict a page that is LEAST recently used NRU Implementation: simpler than LRU uses reference bit a counter is kept per page At regular intervals, for every page do: if ref bit = 0, increment counter if ref bit = 1, zero the counter zero the reference bit The counter will contain the number of intervals since the last reference to the page The page with the largest counter is the least recently used ECE344: Page Replacement Ding Yuan 15

ECE344: Page Replacement Ding Yuan 16 LRU Clock Used by Unix Replace page that is “old enough” Arrange all of physical page frames in a big circle (clock) A clock hand is used to select a good LRU candidate Sweep through the pages in circular order like a clock If the ref bit is off, it hasn’t been used recently What is the minimum “age” if ref bit is off? If the ref bit is on, turn it off and go to next page Arm moves quickly when pages are needed Low overhead when plenty of memory

ECE344: Page Replacement Ding Yuan 17 LRU Clock

ECE344: Page Replacement Ding Yuan 18 LRU Clock 0

ECE344: Page Replacement Ding Yuan 19 LRU Clock 0 0

ECE344: Page Replacement Ding Yuan 20 LRU Clock 0 0 Victim page

Switching Gear So far, all we have talked about is memory management for a single process What about multiple processes? If we just use “demand paging” for each process, why do we care? ECE344: Page Replacement Ding Yuan 21

Thrashing and CPU utilization As the page fault rate goes up, processes get suspended on page queues for the disk The system may try to optimize performance by starting new jobs But is it always good? Starting new jobs will reduce the number of page frames available to each process, increasing the page fault requests System throughput plunges ECE344: Page Replacement Ding Yuan 22

ECE344: Page Replacement Ding Yuan 23 Fixed vs. Variable Space In a multiprogramming system, we need a way to allocate memory to competing processes Problem: How to determine how much memory to give to each process? Fixed space algorithms Each process is given a limit of pages it can use When it reaches the limit, it replaces from its own pages Local replacement Some processes may do well while others suffer Variable space algorithms Process’ set of pages grows and shrinks dynamically Global replacement One process can ruin it for the rest

Working Set The working set model assumes locality The principle of locality states that a program clusters its access to data and text temporarily As the number of page frames increases above some threshold, the page fault rate will drop dramatically ECE344: Page Replacement Ding Yuan 24

ECE344: Page Replacement Ding Yuan 25 Working Set Model A working set of a process is used to model the dynamic locality of its memory usage Defined by Peter Denning in 60s Definition WS(t,w) = {pages P such that P was referenced in the time interval (t, t-w)} t – time, w – working set window (measured in page refs) A page is in the working set (WS) only if it was referenced in the last w references

Working Set Size vs. Page Faults ECE344: Page Replacement Ding Yuan 26

ECE344: Page Replacement Ding Yuan 27 Working Set Size The working set size is the number of pages in the working set The number of pages referenced in the interval (t, t-w) The working set size changes with program locality During periods of poor locality, you reference more pages Within that period of time, the working set size is larger Intuitively, want the working set to be the set of pages a process needs in memory to prevent heavy faulting Each process has a parameter w that determines a working set with few faults Denning: Don’t run a process unless working set is in memory

ECE344: Page Replacement Ding Yuan 28 Working Set Problems Problems How do we determine w? How do we know when the working set changes? Too hard to answer So, working set is not used in practice as a page replacement algorithm However, it is still used as an abstraction The intuition is still valid When people ask, “How much memory does Firefox need?”, they are in effect asking for the size of Firefox’s working set

ECE344: Page Replacement Ding Yuan 29 Page Fault Frequency (PFF) Page Fault Frequency (PFF) is a variable space algorithm that uses a more ad-hoc approach Monitor the fault rate for each process If the fault rate is above a high threshold, give it more memory So that it faults less But not always (FIFO, Belady’s Anomaly) If the fault rate is below a low threshold, take away memory Should fault more But not always Hard to use PFF to distinguish between changes in locality and changes in size of working set

ECE344: Page Replacement Ding Yuan 30 Summary Page replacement algorithms Belady’s – optimal replacement (minimum # of faults) FIFO – replace page loaded furthest in past LRU – replace page referenced furthest in past Approximate using PTE reference bit LRU Clock – replace page that is “old enough” Working Set – keep the set of pages in memory that has minimal fault rate (the “working set”) Page Fault Frequency – grow/shrink page set as a function of fault rate Multiprogramming Should a process replace its own page, or that of another?