CSE 153 Design of Operating Systems Winter 2015 Lecture 12: Page Replacement.

Slides:



Advertisements
Similar presentations
VM Page Replacement Hank Levy.
Advertisements

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 补充:什么叫页面抖动?
9/3/20141 VM Paging Example May 3, 2000 Instructor: Gary Kimura.
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
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
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.
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
CS444/CS544 Operating Systems Virtual Memory 4/06/2007 Prof. Searleman
OS Spring’04 Virtual Memory: Page Replacement Operating Systems Spring 2004.
Page Replacement Algorithms
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
Virtual Memory.
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.
Lecture Topics: 11/17 Page tables TLBs Virtual memory flat page tables
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.
Virtual Memory Questions answered in this lecture: How to run process when not enough physical memory? When should a page be moved from disk to 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.
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
CENG334 Introduction to Operating Systems
CS703 - Advanced Operating Systems
Memory Management and Virtual Memory - 2
CSE 120 Principles of Operating
Module 9: Virtual Memory
EECE.4810/EECE.5730 Operating Systems
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
Demand Paged Virtual Memory
VM Page Replacement Hank Levy.
CSE 451: Operating Systems Autumn 2003 Lecture 11 Demand Paging and Page Replacement Hank Levy Allen Center
CSE451 Introduction to Operating Systems Winter 2009
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
Lecture 9: Caching and Demand-Paged Virtual Memory
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
CSE451 Introduction to Operating Systems Winter 2011
Module 9: Virtual Memory
CSE 451: Operating Systems Autumn 2009 Module 11 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Ed Lazowska
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
CSE 451: Operating Systems Winter 2007 Module 11 Virtual Memory, Page Faults, Demand Paging, and Page Replacement Ed Lazowska
Presentation transcript:

CSE 153 Design of Operating Systems Winter 2015 Lecture 12: Page Replacement

Announcements l Homework 2 will be posted later tonight u Due in class next Thursday u Longer/more challenging than HW1 l Project 2 deadlines extended to give you more time to work with Tas u Project 1 grades were much better than I expected CSE 153 – Lecture 12 – Page Replacement2

3 Memory Management l Mechanisms for memory management u Physical and virtual addressing (1) u Techniques: Partitioning, paging, segmentation (1) u Page table management, TLBs, VM tricks (2) l Policies u Page replacement algorithms (3)

CSE 153 – Lecture 12 – Page Replacement4 Lecture Overview l Review paging and page replacement l Survey page replacement algorithms l Discuss local vs. global replacement l Discuss thrashing

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

CSE 153 – Lecture 12 – Page Replacement6 Demand Paging (Process) l Demand paging is also used when a process first starts up l When a process is created, it has u A brand new page table with all valid bits off u No pages in memory l When the process starts executing u Instructions fault on code and data pages u Faulting stops when all necessary code and data pages are in memory u Only code and data needed by a process needs to be loaded u This, of course, changes over time…

CSE 153 – Lecture 12 – Page Replacement7 Page Replacement l When a page fault occurs, the OS loads the faulted page from disk into a page frame of memory l At some point, the process has used all of the page frames it is allowed to use u This is likely (much) less than all of available memory l When this happens, the OS must replace a page for each page faulted in u It must evict a page to free up a page frame l Page replacement algorithm determines how this is done

CSE 153 – Lecture 12 – Page Replacement8 Locality to the Rescue l Recall that virtual memory works because of locality u Temporal and spatial u Work at different scales: for cache, at a line level, for VM, at page level, and even at larger scales l All paging schemes depend on locality u What happens if a program does not have locality? u High cost of paging is acceptable, if infrequent u Processes usually reference pages in localized patterns, making paging practical

CSE 153 – Lecture 12 – Page Replacement9 Evicting the Best Page l The goal of the replacement algorithm is to reduce the fault rate by selecting the best victim page to remove l The best page to evict is the one never touched again u Will never fault on it l Never is a long time, so picking the page closest to “never” is the next best thing u Evicting the page that won’t be used for the longest period of time minimizes the number of page faults u Proved by Belady l We’re now going to survey various replacement algorithms, starting with Belady’s

CSE 153 – Lecture 12 – Page Replacement10 Belady’s Algorithm l Belady’s algorithm u Idea: Replace the page that will not be used for the longest time in the future u Optimal? How would you show? u Problem: Have to predict the future l Why is Belady’s useful then? u Use it as a yardstick/upper bound u 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 u What’s a good lower bound? »Random replacement is often the lower bound

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

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

CSE 153 – Lecture 12 – Page Replacement13 Approximating LRU l LRU approximations use the PTE reference bit u Keep a counter for each page u At regular intervals, for every page do: »If ref bit = 0, increment counter »If ref bit = 1, zero the counter »Zero the reference bit u The counter will contain the number of intervals since the last reference to the page u The page with the largest counter is the least recently used l Some architectures don’t have a reference bit u Can simulate reference bit using the valid bit to induce faults

P1P2P3P4 t = 0000X t = 1010X t = 2000X t = 3001X t = 4000X t = 5X001 LRU Approximation CSE 153 – Lecture 12 – Page Replacement14 P1P2P3P4 t = t = t = t = t = t = Reference bitsLRU counter Problem: Overhead of one counter value per page

CSE 153 – Lecture 12 – Page Replacement15 LRU Clock (Not Recently Used) l Not Recently Used (NRU) – Used by Unix u Replace page that is “old enough” u Arrange all of physical page frames in a big circle (clock) u 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 n What is the minimum “age” if ref bit is off? »If the ref bit is on, turn it off and go to next page u Arm moves quickly when pages are needed u Low overhead when plenty of memory u If memory is large, “accuracy” of information degrades »What does it degrade to? »One fix: use two hands (leading erase hand, trailing select hand)

LRU Clock CSE 153 – Lecture 12 – Page Replacement16 P1: 1 P2: 1 P3: 1 P8: 0 P7: 0 P6: 0 P5: 1 P4: 0 P1: 0 P2: 0 P3: 0 P8: 1 P7: 0 P6: 0 P5: 1 P4: 0

Example: gcc Page Replace CSE 153 – Lecture 12 – Page Replacement17

Example: Belady’s Anomaly CSE 153 – Lecture 12 – Page Replacement18

CSE 153 – Lecture 12 – Page Replacement19 Fixed vs. Variable Space l In a multiprogramming system, we need a way to allocate memory to competing processes l Problem: How to determine how much memory to give to each process? u 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 n Some processes may do well while others suffer u Variable space algorithms »Process’ set of pages grows and shrinks dynamically »Global replacement n One process can ruin it for the rest

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

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

Example: gcc Working Set CSE 153 – Lecture 12 – Page Replacement22

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

CSE 153 – Lecture 12 – Page Replacement24 Page Fault Frequency (PFF) l Page Fault Frequency (PFF) is a variable space algorithm that uses a more ad-hoc approach u Monitor the fault rate for each process u 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) u If the fault rate is below a low threshold, take away memory »Should fault more »But not always l Hard to use PFF to distinguish between changes in locality and changes in size of working set

CSE 153 – Lecture 12 – Page Replacement25 Thrashing l Page replacement algorithms avoid thrashing u When most of the time is spent by the OS in paging data back and forth from disk u No time spent doing useful work (making progress) u In this situation, the system is overcommitted »No idea which pages should be in memory to reduce faults »Could just be that there isn’t enough physical memory for all of the processes in the system »Ex: Running Windows95 with 4 MB of memory… u Possible solutions »Swapping – write out all pages of a process »Buy more memory

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

CSE 153 – Lecture 12 – Page Replacement27 Next time… l File systems u Chapters 11, 12, and 13