Operating Systems ECE344 Page Replacement Algorithms Ashvin Goel ECE University of Toronto Overview Introduction Page replacement algorithms Paging issues 2 Introduction CPU generates page fault when it accesses address not in memory, OS allocates memory frames to programs on demand If no frame is available, OS needs to evict another page to free a frame Which page should be evicted? o o A page miss is similar to a TLB miss or a cache miss However, a miss may require accessing the disk So miss handling can be very expensive Disk access times are at least 1000x memory access times 3 When Will Paging Work? Paging will only work if it occurs rarely Paging schemes depend on locality of reference o Spatial locality Programs tend to use a small fraction of their memory, or Memory accesses are near memory accessed recently o Temporal locality Programs use same memory over short periods of time, or Memory accessed recently will be accessed again Programs normally have both kinds of locality o Hence, overall cost of paging is not very high 4 Page Replacement Algorithms Aim is to reduce page misses using smart replacement algorithms o o Page table, MMU, TLB handling are VM mechanisms Page replacement is a VM policy Several Algorithms o o o o o o Optimal Algorithm First In First Out (FIFO) Clock Least Recently Used (LRU) Working Set Clock Page Fault Frequency 5 The Optimal Page Replacement Algorithm Select page that will not be needed for longest time o Why is this algorithm optimal? Time Requests 0 1 c 2 a 3 d 4 b 5 e 6 b 7 a 8 b 9 c Page 0 Frames 1 2 3 a b c d a b c d a b c d a b c d a b c d a b c e a b c e a b c e a b c e a b c e Page faults X 10 d X 6 The Optimal Page Replacement Algorithm Optimal algorithm is unrealizable o o Don’t know the future of a program! Don’t know when a given page will be next needed However, it can be used to compare to compare the performance of other algorithms o How? 7 First In First Out (FIFO) Replace the page that has been in memory for the longest time (i.e., replace oldest page) Time Requests 0 Page 0 Frames 1 2 3 a b c d Page faults 1 c c 2 a 3 d 4 b 5 e 6 b 7 a 8 b 9 c 10 a a a c c d a b c d a b e d a b e d a b e d a b e d c b e d c b e a X X X 8 FIFO Implementation Implementation o Keep list of all page frames in memory E.g., add linked list in coremap o o When a frame is allocated (e.g., when page is mapped to a new frame), add frame to the front of list On a page fault, choose frame at end of list for replacement Problem o The oldest page may be needed again soon E.g., some page may be important throughout execution, when it gets old, replacing it will soon cause a page fault o Faults may increase if algorithm is given more memory! Known as Belady’s Anomaly 9 Can We Do Better that FIFO? We need to know page access pattern in the future o o We can take advantage of locality of reference Pages that have been accessed in the recent past are likely to be accessed again and should not be replaced How can page accesses be tracked? o o Tracking each memory access is very expensive Tracking page access requires hardware support 10 Tracking Page Accesses Bits in page table entry allow tracking page accesses o Referenced (R) bit Set by processor when page is read OR written Cleared by OS/software (not by hardware) o Dirty (D) bit Set by processor when page is written Cleared by OS/software (not by hardware) o OS/hardware must synchronize the TLB bits with PTE bits What if h/w doesn’t maintain these bits? o OS can simulate them When TLB read fault occurs – Set referenced bit, make page read-only (why read-only)? When TLB read-only or write fault occurs – Set referenced & dirty bits, make page writeable 11 Clock Algorithm FIFO, while giving second chance to referenced pages o o o Keep circular list of all page frames in memory Maintain a clock hand, from where frames are evicted New frames are added behind the clock hand On a page fault, when we need to evict a page frame: o Choose page starting from clock hand If referenced bit is set – Unset referenced bit, continue Else if referenced bit is not set – If page is dirty • Schedule page write, continue – Else page is clean • Select it for replacement, done Advance clock hand to next page 0 0 frame # 0 5 1 1 1 4 0 2 0 3 referenced bit 12 Least Recently Used (LRU) Replace page that has been used least recently using a list of pages kept sorted in LRU order c a b d a c b d d a c b b d a c e b d a b e d a a b e d b a e d c b a e d c b a Time Requests 0 1 c 2 a 3 d 4 b 5 e 6 b 7 a 8 b 9 c 10 d Page 0 Frames 1 2 3 a b c d a b c d a b c d a b c d a b c d a b e d a b e d a b e d a b e d a b e c a b d c X X Page faults X 13 LRU Implementation Updating LRU list on each memory access is too expensive Suppose MMU maintains a counter that is incremented each clock cycle o When page is accessed MMU writes counter value to the page table entry This timestamp value is the time of last use o On a page fault Software looks through the page table Identifies entry with the oldest timestamp Problem is MMU may not provide such a counter 14 LRU Approximation OS can maintain a counter in software Periodically (i.e., timer interrupt), increment counter o o On a page fault o o o In each page with referenced bit set, write counter Clear the referenced bit, why? Software looks through the page table Identifies entry with the oldest timestamp If several have oldest time, choose one arbitrarily Why does this method approximate LRU? 15 Working Set Model Working set is the set of pages a program needs currently To measure working set, look at the last time interval T o o o o T is also called working set interval WS(T) = { pages referenced in interval (now, now – T) } As T gets bigger, more pages are needed However, working set varies slowly after a while Set of pages accessed (working set) # of unique memory references over time T 16 WSClock Goal of algorithm is to keep working set in memory, so few page faults will occur due to locality of reference Variant of Clock algorithm o Each entry contains time of last use rather than just a referenced bit 17 WSClock On a page fault, clock sweeps over circular list o If referenced bit is set Update time-of-last-use field to current virtual time – Virtual time is time application has run Unset referenced bit, continue o Else if referenced bit is not set Compute age by comparing current time with time-of-last-use If age of the page is less than working set interval (T) – continue Else if page is dirty – Schedule page write, continue Else if page is clean – Select it for replacement, done Compare with Clock algorithm 18 Page Fault Frequency Working set clock requires knowing working set interval for each process Page fault frequency can provide estimate of working set needs of a program o o It declines as a program is assigned more pages Can be used to ensure fairness in working set allocation Too High: need to give this program some more frames Too Low: take some frames away, give to other programs 19 Page Fault Frequency Algorithm Measuring page fault frequency: o For each thread On each fault, count fault (f) – f=f+1 Every second, update faults/second (fe) via aging – fe = (1 – a) * fe + a * f, f = 0 – 0 < a < 1, when a -> 1, history is ignored, a is weighting factor Goal: Allocate frames so PFF is equal for programs o o Choose a victim process whose PFF is lowest Use Clock, LRU to evict a page from victim process 20 Which Algorithm is Best? Compare algorithms based on a reference string Run a program o Look at which memory addresses are accessed Pages accessed: 0000001222333300114444001123444 o Eliminate duplicates 012301401234, Why? o Defines the reference string Use the same reference string for evaluating different page replacement algorithms o Count total page faults 21 Summary Algorithm Comment Optimal Not implementable, but useful as a benchmark FIFO Might throw out important pages Clock Realistic LRU Excellent, but difficult to implement exactly Working Set Clock Efficient working set-base algorithm Page Fault Frequency Fairness in working set allocation 22 Paging Issues Paging and I/O interaction Paging performance Thrashing 23 Paging and I/O Interaction Example: A thread performs a read system call, suspends during I/O, then another thread runs, has a page fault o o A frame selected for eviction is the one that is involved in the read When I/O returns, OS will copy data from disk to new page! Solution: Each frame has a 'do not evict me' flag, called pinned page because the page is pinned/locked in memory and cannot be evicted during I/O o Must always remember to un-pin the page, e.g., after the I/O completes 24 Paging Performance Paging works best if there are plenty of free frames If all pages are full of dirty pages, then two disk operations are needed for each page fault, one to swap out the dirty page that is being invalidated, one to read in the new page Two methods for improving performance: o o Paging daemon: swap out, in advance Prefetching: swap in, in advance 25 Paging Daemon OS can use a paging thread/daemon to maintain a pool of free frames Daemon runs replacement algorithm periodically or when pool reaches low watermark o o Frames in pool still hold previous contents of pages o Writes out dirty pages, marks them as free Frees enough pages until pool reaches high watermark Can be rescued if page is referenced before reallocation Issue: o If a page is going to be written again, then it would have been better to not swap it 26 Prefetching Page faults can only be processed one page at a time, because disk has to be read one page at a time Suppose we can predict future page usage at current fault, then we can prefetch other pages What if we are wrong? 27 Thrashing Thrashing is a situation where OS spends most time paging data from disk, and user programs do not make much progress o System is over-committed: o o A livelock situation Either, page replacement algorithm is not working Or, system doesn't have enough memory to hold working set of all currently running programs Solution o o o Run more programs because CPU is idle - no! Swapping - suspend some programs for a while Buy more memory 28 Think Time What the optimal page replacement policy? o Is it achievable in practice? Why is it used? What is the problem with FIFO page replacement? What is the assumption used by most replacement policies to improve on FIFO? What is the working set of a process? Which of the policies described in these slides take working set into account? What happens when working set does not fit in memory? 29