360 likes | 572 Views
Page Replacement. CSE 2431: Introduction to Operating Systems Reading: §§9.2–9.6, [OSC]. Contents (I). Demand Paging Page Replacement Optimal FIFO LRU Belady ’ s Anomaly Page Replacement NFU Clock WSClock Working Set. Review. Page Table Paging mapping hardware
E N D
Page Replacement CSE 2431: Introduction to Operating Systems Reading: §§9.2–9.6, [OSC]
Contents (I) • Demand Paging • Page Replacement • Optimal • FIFO • LRU • Belady’s Anomaly • Page Replacement • NFU • Clock • WSClock • Working Set
Review • Page Table • Paging mapping hardware • TLB: cache of page table • TLB miss • Multi-level Paging • Inverted Page Table • Sharing and Protection
Paging Policies • Fetch Strategies • When should a page be brought into primary (main) memory from secondary (disk) storage. • Placement Strategies • When a page is brought into primary storage, where should it be put? • Replacement Strategies • Which page now in primary storage is to be evicted from primary storage when some other page is to be brought in and there is not enough room.
Demand Paging • Algorithm: NEVER bring a page into primary memory until its needed. • Page fault • Check if a valid virtual memory address. Kill job if not. • If valid reference, check if its cached in memory already (perhaps for other processes.) If so, skip to 7). • Find a free page frame. • Map address into disk block and fetch disk block into page frame. Suspend user process. • When disk read finished, add vm mapping for page frame. • If necessary, restart the faulted instruction.
VM Fault Ref Load M i Page table Free frame Demand Paging Example
Page Replacement • Find location of page on disk • Find a free page frame • If there is a free page frame, use it • Otherwise, select a page frame using the page replacement algorithm • Write the selected modified page to the disk and update any necessary tables • Read the requested page from the disk. • Restart the faulted instruction.
Issue: Eviction • Hopefully, kick out a less-useful page • Dirty pages require writing, clean pages don’t • Hardware has a dirty bit for each page frame indicating this page has been updated or not • Where do you write? To “swap space” • Goal: kick out the page that’s least useful • Problem: how do you determine usefulness? • Heuristic: temporal locality exists • Kick out pages that aren’t likely to be used again
Terminology • Reference string: the memory reference sequence generated by a program. • Paging– moving pages from (to) disk • Optimal – the best (theoretical) strategy • Eviction – throwing something out • Pollution – bringing in useless pages/lines
Page Replacement Strategies • The Principle of Optimality: Replace the page that will not be used again the farthest time in the future. • Random page replacement:Choose a page randomly • FIFO - First in First Out: Replace the page that has been in primary memory the longest • LRU - Least Recently Used: Replace the page that has not been used for the longest time • LFU - Least Frequently Used • Replace the page that is used least often • An approximation to LRU • NRU - Not Recently Used:Replace the page that is not used recently • Working Set:Keep in memory those pages that the process is actively using.
Principle of Optimality • Description: • Assume that each page can be labeled with the number of instructions that will be executed before that page is first referenced, i.e., we would know the future reference string for a program. • Then the optimal page algorithm would choose the page with the highest label to be removed from the memory. • This algorithm provides a basis for comparison with other schemes. • Impractical because it needs future references • If future references are known • Should not use demand paging • Should use pre-paging to allow paging to be overlapped with computation.
An Example for Optimality 12 references, 7 faults
FIFO 12 references, 9 faults
Expected Paging Behavior with IncreasingNumber of Page Frames
Belady’s Anomaly (for FIFO) FIFO with 4 physical pages 12 references, 10 faults As the number of page frames increase, so does the fault rate.
LRU 12 references, 10 faults
LRU and Anomalies LRU, 4 physical pages 12 references, 8 faults Anomalies cannot occur. Why?
LRU Issues • How to track “recency”? • Use time • Record time of reference with page table entry • Use counter as clock • Search for smallest time. • Use stack • Remove reference of page from stack (linked list) • Push it on top of stack • Both approaches require large processing overhead, more space, and hardware support.
NRU • Page Classes (R, M) • (0, 0): Neither referenced nor dirty • (0, 1): Not referenced (recently) but dirty • (1, 0): Referenced but clean • (1, 1): Referenced and dirty • Algorithm • Select a page from lowest class • If conflict, use random or FIFO. • More details: • Hardware sets up R and M for each memory access • OS periodically clear Rbit
NFU: A LRU Approximation • NFU (Not Frequently Used): Evict a page that is NOT frequently used; • LRU: evict a page that is LEAST recently used. • NFU implementation: simpler than LRU • A software counter is kept per page • The R bit (0/1) is added into the counter periodically • Directly added (never forget history) • Right-shift the counter one bit and add the R bit to the leftmost (aging) • 00110011 would be accessed more frequently than 00010111 • The page with the counter holding the lowest number is the least frequently used. • The value may not be unique. Use FIFO to resolve conflicts.
Second Chance (Clock) • Only one reference bit in the page table entry. • 0 initially • 1when a page is referenced • Pages are kept in FIFO order • Choose “victim” to evict • Select the head of FIFO • If page has reference bit set, reset bit, put it back to the tail of the list, and process the next page. • Keep processing until reach page with zero reference bit and page that one out. • Clock algorithm is a variant of second chance (use circular list to maintain the FIFO)
Frame Allocation: Minimum • How are the page frames allocated to individual virtual memories in a multi- programmed environment? • Simple case: allocate a minimum number of frames per process • Most instructions require two operands • Include an extra page for paging out and one for paging in. • Moves and indirection instructions might require more.
Equal Allocation • Allocate an equal number of frames per job • But jobs use memory unequally • High priority jobs have same number of page frames as low priority jobs • Degree of multiprogramming might vary
Proportional Allocation • Allocate a number of frames per job proportional to job size • Challenge: how do you determine job size: by run command parameters or dynamically?
As page frames per VM space decrease, the page fault rate increases. Page Fault Rate Curve
Thrashing • Computation has locality • As page frames decrease, the page frames available are not large enough to contain the locality of the process • The processes start faulting heavily • Pages that are read in, are used and immediately paged out
Thrashing and CPU Utilization • As the page fault rate goes up, processes get suspended on page out queues for the disk • System may try to optimize performance by starting new jobs • Starting new jobs will reduce the number of page frames available to each process, increasing the page fault requests • System throughput plunges
Working Set • The working set model assumes locality. • The principle of locality states that a program clusters its access to data and text temporally. • As the number of page frames increases above a threshold, the page fault rate will drop dramatically.
Working Set in Action • Algorithm • If # free page frames > working set of some suspended process, then activate process and map in all its working set • If working set size of some process increases and no page frame is free, suspend process and release all its pages • Moving window over reference string used for determination. • Keep track of working set.
Working Set Example Window size is Δ 12 references, 8 faults
Working Set Solution • Approximate working set model using timer, reference bit, and age. • Set timer to interrupt periodically to clear reference bit • Once fault happens, remove pages that have not been referenced and old enough (> )⟹outside of working set
Page Fault Frequency Version of Working Set • Assume that if the working set is correct there will not be many page faults. • If page fault rate increases beyond assumed knee of curve, then increase number of page frames available to process. • If page fault rate decreases below foot of knee of curve, then decrease number of page frames available to process.
WSClock Page Replacement Algorithm • Carr and Hennessy, 1981 • Used very widely (Linux) • Circular list as in the clock algorithm • Initially, list is empty. • As pages are loaded into memory, pages are added to the list • Each entry contains Time of last use as well as reference and dirty bits. • Algorithm: • At each page fault, the page pointed to by the clock hand is examined. • Repeat the following: • (Step 1) If reference bit is 1, then the page has been referenced during the current tick, so it is in working set and not ideal candidate to remove. Clear the reference bit and update the time of last use. • If reference bit is 0, then check if it is in working set window (i.e., if Current Time minus Time of last use is less than the working set window size time). • If page is not in the working set and the page is clean, then replace it. • If the page is not in working set and page is dirty, request write to disk, and move on to the next page in the circular list.
Page Size Again • Small pages • Reason: • Locality of reference tends to be small (256) • Less fragmentation • Problem • Require large page tables • Large pages • Reason • Small page table • I/O transfers have high seek time, so better to transfer more data per seek • Problem: • Internal fragmentation
Summary • Demand Paging • Page Replacement • Optimal • FIFO • LRU • Belady’s Anomaly • Page Replacement • NFU • Clock • WSClock • Working Set