CS162 Operating Systems and Systems Programming Lecture 15. Page Allocation and Replacement

Similar documents
CS162 Operating Systems and Systems Programming Lecture 15. Page Allocation and Replacement

CS162 Operating Systems and Systems Programming Lecture 15. Page Allocation and Replacement

10/9/2013 Anthony D. Joseph and John Canny CS162 UCB Fall 2013

Page 1. Goals for Today" TLB organization" CS162 Operating Systems and Systems Programming Lecture 11. Page Allocation and Replacement"

CMPT 300 Introduction to Operating Systems. Page Replacement Algorithms

CS162 Operating Systems and Systems Programming Lecture 11 Page Allocation and Replacement"

3/3/2014! Anthony D. Joseph!!CS162! UCB Spring 2014!

CMPT 300 Introduction to Operating Systems

VIRTUAL MEMORY READING: CHAPTER 9

CS162 Operating Systems and Systems Programming Lecture 15. Page Allocation and Replacement. Page 1

CS162 Operating Systems and Systems Programming Lecture 16. Page Allocation and Replacement (con t) I/O Systems

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 15: Caching: Demand Paged Virtual Memory

Operating Systems (1DT020 & 1TT802) Lecture 9 Memory Management : Demand paging & page replacement. Léon Mugwaneza

Past: Making physical memory pretty

CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

CS162 Operating Systems and Systems Programming Lecture 16. Page Allocation and Replacement (con t) I/O Systems

Page 1. CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

Lecture#16: VM, thrashing, Replacement, Cache state

CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

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

Virtual Memory Design and Implementation

! What is virtual memory and when is it useful? ! What is demand paging? ! What pages should be. ! What is the working set model?

Virtual Memory - II. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - XVI. University at Buffalo.

Virtual Memory Design and Implementation

Virtual Memory. Virtual Memory. Demand Paging. valid-invalid bit. Virtual Memory Larger than Physical Memory

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

Chapter 6: Demand Paging

Operating Systems Virtual Memory. Lecture 11 Michael O Boyle

All Paging Schemes Depend on Locality. VM Page Replacement. Paging. Demand Paging

CS370 Operating Systems

CS 153 Design of Operating Systems Winter 2016

CS162 Operating Systems and Systems Programming Lecture 14. Caching (Finished), Demand Paging

!! What is virtual memory and when is it useful? !! What is demand paging? !! When should pages in memory be replaced?

Background. Demand Paging. valid-invalid bit. Tevfik Koşar. CSC Operating Systems Spring 2007

CSE 153 Design of Operating Systems

Basic Page Replacement

Chapter 9: Virtual Memory

ECE7995 Caching and Prefetching Techniques in Computer Systems. Lecture 8: Buffer Cache in Main Memory (I)

CS370 Operating Systems

CS370 Operating Systems

Chapter 8: Virtual Memory. Operating System Concepts

Page Replacement. (and other virtual memory policies) Kevin Webb Swarthmore College March 27, 2018

Lecture 12: Demand Paging

Module 9: Virtual Memory

Last class: Today: Paging. Virtual Memory

Chapter 9: Virtual Memory

Operating System Concepts

Virtual Memory. ICS332 Operating Systems

ADRIAN PERRIG & TORSTEN HOEFLER Networks and Operating Systems ( ) Chapter 6: Demand Paging

CSE 120 Principles of Operating Systems

Chapter 9: Virtual-Memory

Chapter 9: Virtual Memory. Chapter 9: Virtual Memory. Objectives. Background. Virtual-address address Space

Virtual Memory. Overview: Virtual Memory. Virtual address space of a process. Virtual Memory

CS Advanced Operating Systems Structures and Implementation Lecture 16. Virtual Memory (finished) Paging Devices.

Virtual Memory. Overview: Virtual Memory. Virtual address space of a process. Virtual Memory. Demand Paging

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

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

Operating System Principles: Memory Management Swapping, Paging, and Virtual Memory CS 111. Operating Systems Peter Reiher

CS420: Operating Systems

Memory Management: Virtual Memory and Paging CS 111. Operating Systems Peter Reiher

Principles of Operating Systems

Last Class: Demand Paged Virtual Memory

Reminder: Mechanics of address translation. Paged virtual memory. Reminder: Page Table Entries (PTEs) Demand paging. Page faults

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

Operating Systems. Operating Systems Sina Meraji U of T

Chapter 8: Virtual Memory. Operating System Concepts Essentials 2 nd Edition

CS510 Operating System Foundations. Jonathan Walpole

Paging algorithms. CS 241 February 10, Copyright : University of Illinois CS 241 Staff 1

Module 9: Virtual Memory

Virtual Memory COMPSCI 386

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

Where are we in the course?

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

Page Replacement Algorithms

Lecture 14 Page Replacement Policies

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

Page Replacement. 3/9/07 CSE 30341: Operating Systems Principles

Virtual Memory. 1 Administrivia. Tom Kelliher, CS 240. May. 1, Announcements. Homework, toolboxes due Friday. Assignment.

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

Lecture 14: Cache & Virtual Memory

CS 333 Introduction to Operating Systems. Class 14 Page Replacement. Jonathan Walpole Computer Science Portland State University

Virtual Memory Management

Last Class. Demand Paged Virtual Memory. Today: Demand Paged Virtual Memory. Demand Paged Virtual Memory

CS370 Operating Systems

Chapter 9: Virtual Memory

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

Virtual Memory - Overview. Programmers View. Virtual Physical. Virtual Physical. Program has its own virtual memory space.

Chapter 3: Virtual Memory ว ตถ ประสงค. Background สามารถอธ บายข อด ในการท ระบบใช ว ธ การจ ดการหน วยความจ าแบบเสม อนได

Lecture #15: Translation, protection, sharing

CS 333 Introduction to Operating Systems. Class 14 Page Replacement. Jonathan Walpole Computer Science Portland State University

Virtual Memory Design and Implementation

Swapping. Operating Systems I. Swapping. Motivation. Paging Implementation. Demand Paging. Active processes use more physical memory than system has

Virtual Memory Outline

Lecture 17. Edited from slides for Operating System Concepts by Silberschatz, Galvin, Gagne

Demand Paging. Valid-Invalid Bit. Steps in Handling a Page Fault. Page Fault. Transfer of a Paged Memory to Contiguous Disk Space

Page 1. Review: Address Segmentation " Review: Address Segmentation " Review: Address Segmentation "

Chapter 10: Virtual Memory. Background

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

Chapter 10: Virtual Memory

Chapter 9: Virtual Memory

Transcription:

S6 Operating Systems and Systems Programming Lecture 5 Page llocation and Replacement October, 006 Prof. John Kubiatowicz http://inst.eecs.berkeley.edu/~cs6 Review: emand Paging Mechanisms PT helps us implement demand paging Valid Page in memory, PT points at physical page Not Valid Page not in memory; use info in PT to find it on disk when necessary Suppose user references page with invalid PT? Memory Management Unit (MMU) traps to OS» Resulting trap is a Page Fault What does OS do on a Page Fault?:» hoose an old page to replace» If old page modified ( = ), write contents back to disk» hange its PT and any cached TL to be invalid» Load new page into memory from disk» Update page table entry, invalidate TL for new entry» ontinue thread from original faulting location TL for new page will be loaded when thread continued! While pulling pages off disk for one process, OS runs another process from ready queue» Suspended process sits on wait queue Lec 5. Review: Software-Loaded TL MIPS/Snake/Nachos TL is loaded by software High TL hit rate ok to trap to software to fill the TL, even if slower Simpler hardware and added flexibility: software can maintain translation tables in whatever convenient format How can a process run without hardware TL fill? Fast path (TL hit with valid=):» Translation to physical page done by hardware Slow path (TL hit with valid=0 or TL miss)» Hardware receives a TL Fault What does OS do on a TL Fault?» Traverse page table to find appropriate PT» If valid=, load page table entry into TL, continue thread» If valid=0, perform Page Fault detailed previously» ontinue thread verything is transparent to the user process: It doesn t know about paging to/from disk It doesn t even know about software TL handling Lec 5. User OS TL Faults Review: Transparent xceptions Faulting Inst Faulting Inst Load TL Faulting Inst Fetch page/ Load TL Hardware must help out by saving: Faulting instruction and partial state Processor State: sufficient to restart user thread» Save/restore registers, stack, etc Precise xception state of the machine is preserved as if program executed up to the offending instruction ll previous instructions completed Offending instruction and all following instructions act as if they have not even started ifficult with pipelining, out-of-order execution,... MIPS takes this position Modern techniques for out-of-order execution and branch prediction help implement precise interrupts Faulting Inst Lec 5.4

Goals for Today Steps in Handling a Page Fault Page Replacement Policies lock lgorithm N th chance algorithm Second-hance-List lgorithm Page llocation Policies Working Set/Thrashing Note: Some slides and/or pictures in the following are adapted from slides 005 Silberschatz, Galvin, and Gagne. Many slides generated from my lecture notes by Kubiatowicz. Lec 5.5 Lec 5.6 emand Paging xample Since emand Paging like caching, can compute average access time! ( ffective ccess Time ) T = Hit Rate x Hit Time + Miss Rate x Miss Time xample: Memory access time = 00 nanoseconds verage page-fault service time = 8 milliseconds Suppose p = Probability of miss, -p = Probably of hit Then, we can compute T as follows: T = ( p) x 00ns + p x 8 ms = ( p) x 00ns + p x 8,000,000ns = 00ns + p x 7,999,800ns If one access out of,000 causes a page fault, then T = 8. μs: This is a slowdown by a factor of 40! What if want slowdown by less than 0%? 00ns x. < T p <.5 x 0-6 This is about page fault in 400000! Lec 5.7 What Factors Lead to Misses? ompulsory Misses: Pages that have never been paged into memory before How might we remove these misses?» Prefetching: loading them into memory before needed» Need to predict future somehow! More later. apacity Misses: Not enough memory. Must somehow increase size. an we do this?» One option: Increase amount of RM (not quick fix!)» nother option: If multiple processes in memory: adjust percentage of memory allocated to each one! onflict Misses: Technically, conflict misses don t exist in virtual memory, since it is a fully-associative cache Policy Misses: aused when pages were in memory, but kicked out prematurely because of the replacement policy How to fix? etter replacement policy Lec 5.8

Page Replacement Policies Why do we care about Replacement Policy? Replacement is an issue with any cache Particularly important with pages» The cost of being wrong is high: must go to disk» Must keep important pages in memory, not toss them out FIFO (First In, First Out) Throw out oldest page. e fair let every page live in memory for same amount of time. ad, because throws out heavily used pages instead of infrequently used pages MIN (Minimum): Replace page that won t be used for the longest time Great, but can t really know future Makes good comparison case, however RNOM: Pick random page for every replacement Typical solution for TL s. Simple hardware Pretty unpredictable makes it hard to make real-time guarantees Lec 5.9 Replacement Policies (on t) LRU (Least Recently Used): Replace page that hasn t been used for the longest time Programs have locality, so if something not used for a while, unlikely to be used in the near future. Seems like LRU should be a good approximation to MIN. How to implement LRU? Use a list! Head Page 6 Page 7 Page Page Tail (LRU) On each use, remove page from list and place at head LRU page is at tail Problems with this scheme for paging? Need to know immediately when each page used so that can change position in list Many instructions for each hardware access In practice, people approximate LRU (more later) Lec 5.0 xample: FIFO Suppose we have page frames, 4 virtual pages, and following reference stream: onsider FIFO Page replacement: xample: MIN Suppose we have the same reference stream: onsider MIN Page replacement: Ref: Ref: FIFO: 7 faults. When referencing, replacing is bad choice, since need again right away Lec 5. MIN: 5 faults Where will be brought in? Look for page not referenced farthest in future. What will LRU do? Same decisions as MIN here, but won t always be true! Lec 5.

dministrivia Midterm I: Graded and handed out verage: 7.8, Std eviation:.7 4 0 8 6 4 0 5 40 45 50 55 60 65 70 75 80 85 90 95 00 Solutions are up on the Handouts page Regrades: We were lenient on grading Regrades will regrade whole exam and will not be lenient xception: adding mistakes Lec 5. When will LRU perform badly? onsider the following: LRU Performs as follows (same as FIFO here): Ref: very reference is a page fault! MIN oes much better: Ref: Lec 5.4 Graph of Page Faults Versus The Number of Frames One desirable property: When you add memory the miss rate goes down oes this always happen? Seems like it should, right? No: elady s anomaly ertain replacement algorithms (FIFO) don t have this obvious property! Lec 5.5 dding Memory oesn t lways Help Fault Rate oes adding memory reduce number of page faults? Yes for LRU and MIN Not necessarily for FIFO! (alled elady s anomaly) Ref: Ref: 4 fter adding memory: With FIFO, contents can be completely different In contrast, with LRU or MIN, contents of memory with X pages are a subset of contents with X+ Page Lec 5.6

Implementing LRU Perfect: Timestamp page on each reference Keep list of pages ordered by time of reference Too expensive to implement in reality for many reasons lock lgorithm: rrange physical pages in circle with single clock hand pproximate LRU (approx to approx to MIN) Replace an old page, not the oldest page etails: Hardware use bit per physical page:» Hardware sets use bit on each reference» If use bit isn t set, means not referenced in a long time» Nachos hardware sets use bit in the TL; you have to copy this back to page table when TL entry gets replaced On page fault:» dvance clock hand (not real time)» heck use bit: used recently; clear and leave alone 0 selected candidate for replacement Will always find a page or loop forever?» ven if all use bits set, will eventually loop around FIFO Lec 5.7 lock lgorithm: Not Recently Used Set of all pages in Memory Single lock Hand: dvances only on page fault! heck for pages not used recently Mark pages as not used recently What if hand moving slowly? Good sign or bad sign?» Not many page faults and/or find page quickly What if hand is moving quickly? Lots of page faults and/or lots of reference bits set One way to view clock algorithm: rude partitioning of pages into two groups: young and old Why not partition into more than groups? Lec 5.8 N th hance version of lock lgorithm N th chance algorithm: Give page N chances OS keeps counter per page: # sweeps On page fault, OS checks use bit:» clear use and also clear counter (used in last sweep)» 0 increment counter; if count=n, replace page Means that clock hand has to sweep by N times without page being used before page is replaced How do we pick N? Why pick large N? etter approx to LRU» If N ~ K, really good approximation Why pick small N? More efficient» Otherwise might have to look a long way to find free page What about dirty pages? Takes extra overhead to replace a dirty page, so give dirty pages an extra chance before replacing? ommon approach:» lean pages, use N=» irty pages, use N= (and write back to disk when N=) Lec 5.9 lock lgorithms: etails Which bits of a PT entry are useful to us? Use: Set when page is referenced; cleared by clock algorithm Modified: set when page is modified, cleared when page written to disk Valid: ok for program to reference this page Read-only: ok for program to read page, but not modify» For example for catching modifications to code pages! o we really need hardware-supported modified bit? No. an emulate it (S Unix) using read-only bit» Initially, mark all pages as read-only, even data pages» On write, trap to OS. OS sets software modified bit, and marks page as read-write.» Whenever page comes back in from disk, mark read-only Lec 5.0

lock lgorithms etails (continued) o we really need a hardware-supported use bit? No. an emulate it similar to above:» Mark all pages as invalid, even if in memory» On read to invalid page, trap to OS» OS sets use bit, and marks page read-only Get modified bit in same way as previous:» On write, trap to OS (either invalid or read-only)» Set use and modified bits, mark page read-write When clock hand passes by, reset use and modified bits and mark page as invalid again Remember, however, that clock is just an approximation of LRU an we do a better approximation, given that we have to take page faults on some reads and writes to collect use information? Need to identify an old page, not oldest page! nswer: second chance list Lec 5. Second-hance List lgorithm (VX/VMS) irectly Mapped Pages Marked: RW List: FIFO Page-in From disk New ctive Pages Overflow ccess New S Victims Split memory in two: ctive list (RW), S list (Invalid) ccess pages in ctive list at full speed Otherwise, Page Fault lways move overflow page from end of ctive list to front of Second-chance list (S) and mark invalid esired Page On S List: move to front of ctive list, mark RW Not on S list: page in to front of ctive list, mark RW; page out LRU victim at end of S list LRU victim Second hance List Marked: Invalid List: LRU Lec 5. Second-hance List lgorithm (con t) How many pages for second chance list? If 0 FIFO If all LRU, but page fault on every page reference Pick intermediate value. Result is: Pro: Few disk accesses (page only goes to disk if unused for a long time) on: Increased overhead trapping to OS (software / hardware tradeoff) With page translation, we can adapt to any kind of access the program makes Later, we will show how to use page translation / protection to share memory between threads on widely separated machines Question: why didn t VX include use bit? Strecker (architect) asked OS people, they said they didn t need it, so didn t implement it He later got blamed, but VX did OK anyway Lec 5. Set of all pages in Memory Free List Single lock Hand: dvances as needed to keep freelist full ( background ) Free Pages For Processes Keep set of free pages ready for use in demand paging Freelist filled in background by lock algorithm or other technique ( Pageout demon ) irty pages start copying back to disk when enter list Like VX second-chance list If page needed before reused, just return to active set dvantage: Faster for page fault an always use page (or pages) immediately on fault Lec 5.4

emand Paging (more details) oes software-loaded TL need use bit? Two Options: Hardware sets use bit in TL; when TL entry is replaced, software copies use bit back to page table Software manages TL entries as FIFO list; everything not in TL is Second-hance list, managed as strict LRU ore Map Page tables map virtual page physical page o we need a reverse mapping (i.e. physical page virtual page)?» Yes. lock algorithm runs through page frames. If sharing, then multiple virtual-pages per physical page» an t push page out to disk without invalidating all PTs Lec 5.5 llocation of Page Frames (Memory Pages) How do we allocate memory among different processes? oes every process get the same fraction of memory? ifferent fractions? Should we completely swap some processes out of memory? ach process needs minimum number of pages Want to make sure that all processes that are loaded into memory can make forward progress xample: IM 70 6 pages to handle SS MOV instruction:» instruction is 6 bytes, might span pages» pages to handle from» pages to handle to Possible Replacement Scopes: Global replacement process selects replacement frame from set of all frames; one process can take a frame from another Local replacement each process selects from only its own set of allocated frames Lec 5.6 Fixed/Priority llocation qual allocation (Fixed Scheme): very process gets same amount of memory xample: 00 frames, 5 processes process gets 0 frames Proportional allocation (Fixed Scheme) llocate according to the size of process omputation proceeds as follows: s i = size of process p i and S = Σs i m = total number of frames s a i = allocation for p i = i m S Priority llocation: Proportional scheme using priorities rather than size» Same type of computation as previous scheme Possible behavior: If process p i generates a page fault, select for replacement a frame from a process with lower priority number Perhaps we should use an adaptive scheme instead??? What if some application just needs more memory? Lec 5.7 Page-Fault Frequency llocation an we reduce apacity misses by dynamically changing the number of pages/application? stablish acceptable page-fault rate If actual rate too low, process loses frame If actual rate too high, process gains frame Question: What if we just don t have enough memory? Lec 5.8

Thrashing Locality In Memory-Reference Pattern If a process does not have enough pages, the pagefault rate is very high. This leads to: low PU utilization operating system spends most of its time swapping to disk Thrashing a process is busy swapping pages in and out Questions: How do we detect Thrashing? What is best response to Thrashing? Lec 5.9 Program Memory ccess Patterns have temporal and spatial locality Group of Pages accessed along a given time slice called the Working Set Working Set defines minimum number of pages needed for process to behave well Not enough memory for Working Set Thrashing etter to swap out process? Lec 5.0 Working-Set Model What about ompulsory Misses? Δ working-set window fixed number of page references xample: 0,000 instructions WS i (working set of Process P ) i = total set of pages referenced in the most recent Δ (varies in time) if Δ too small will not encompass entire locality if Δ too large will encompass several localities if Δ = will encompass entire program = Σ WS i total demand frames if > m Thrashing Policy: if > m, then suspend one of the processes This can improve overall system behavior by a lot! Lec 5. Recall that compulsory misses are misses that occur the first time that a page is seen Pages that are touched for the first time Pages that are touched after process is swapped out/swapped back in lustering: On a page-fault, bring in multiple pages around the faulting page Since efficiency of disk reads increases with sequential reads, makes sense to read several sequential pages Working Set Tracking: Use algorithm to try to track working set of application When swapping process back in, swap in working set Lec 5.

Summary Replacement policies FIFO: Place pages on queue, replace page at end MIN: Replace page that will be used farthest in future LRU: Replace page used farthest in past lock lgorithm: pproximation to LRU rrange all pages in circular list Sweep through them, marking as not in use If page not in use for one pass, than can replace N th -chance clock algorithm: nother approx LRU Give pages multiple passes of clock hand before replacing Second-hance List algorithm: Yet another approx LRU ivide pages into two groups, one of which is truly LRU and managed on page faults. Working Set: Set of pages touched by a process recently Thrashing: a process is busy swapping pages in and out Process will thrash if working set doesn t fit in memory Need to swap out a process Lec 5.