Presentation is loading. Please wait.

Presentation is loading. Please wait.

Page Replacement Algorithms

Similar presentations


Presentation on theme: "Page Replacement Algorithms"— Presentation transcript:

1 Page Replacement Algorithms

2 Overview Introduction Page replacement algorithms Paging issues

3 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? 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

4 When Will Paging Work? Paging will only work if it occurs rarely
Paging schemes depend on locality of reference Spatial locality Programs tend to use a small fraction of their memory, or Memory accesses are near memory accessed recently 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 Hence, overall cost of paging is not very high

5 Page Replacement Algorithms
Aim is to reduce page misses using smart replacement algorithms Page table, MMU, TLB handling are VM mechanisms Page replacement is a VM policy Several Algorithms Optimal Algorithm First In First Out (FIFO) Clock Least Recently Used (LRU) Working Set Clock Page Fault Frequency

6 The Optimal Page Replacement Algorithm
Select page that will not be needed for longest time Why is this algorithm optimal? Time Requests c a d b e b a b c d Page a Frames 1 b 2 c 3 d Page faults x is a page fault a, b, c, d are VPN inductive proof: Say you chose some other page to replace. By definition, it will generate a page fault earlier than this optimal algorithm. a a a a b b b b c c c c d d d d X a a a a a b b b b b c c c c c e e e e e X X

7 The Optimal Page Replacement Algorithm
Optimal algorithm is unrealizable 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 How? Run the program once Generate a log of all memory references Do we need all references? Use log to simulate various replacement algorithms Use optimal algorithm as a control case for evaluating other algorithms

8 First In First Out (FIFO)
Replace the page that has been in memory for the longest time (i.e., replace oldest page) Time Requests c a d b e b a b c a Page a Frames 1 b 2 c 3 d Page faults a a a b c c c c d d X a a a a b b b b e e e e d d d d X X c b e d X X c b e a X

9 FIFO Implementation Implementation Problem
Keep list of all page frames in memory E.g., add linked list in coremap 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 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 Faults may increase if algorithm is given more memory! Known as Belady’s Anomaly

10 Can We Do Better that FIFO?
We need to know page access pattern in the future 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? Tracking each memory access is very expensive Tracking page access requires hardware support

11 Tracking Page Accesses
Bits in page table entry allow tracking page accesses Referenced (R) bit Set by processor when page is read OR written Cleared by OS/software (not by hardware) Dirty (D) bit Set by processor when page is written OS/hardware must synchronize the TLB bits with PTE bits What if h/w doesn’t maintain these bits? 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 for hardware managed TLB, the hardware could use a write-through cache (i.e., it could update the page table entry reference/dirty bits when TLB reference/dirty bits are updated) or it could use a write-back cache (it could update the page table entry when the TLB entry is invalidated). For software managed TLB, the OS needs to synchronize the TLB R/D bits with PTE R/D bits

12 Clock Algorithm FIFO, while giving second chance to referenced pages
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: 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 4 1 5 2 3 frame # referenced bit From now on, we assume that the page table entry maintains a referenced and dirty bit for each page.

13 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 c a d b e b a b c d Page a Frames 1 b 2 c 3 d Page faults a a a a b b b b c c c c d d d d X a a a a b b b b e e e e d d d d X X a b e c X X a b d c X

14 LRU Implementation Updating LRU list on each memory access is too expensive Suppose MMU maintains a counter that is incremented each clock cycle When page is accessed MMU writes counter value to the page table entry This timestamp value is the time of last use 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

15 LRU Approximation OS can maintain a counter in software
Periodically (i.e., timer interrupt), increment counter In each page with referenced bit set, write counter Clear the referenced bit, why? On a page fault 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? Why clear the referenced bit? So that if page is not referenced after the counter is written, the counter will not be incremented Why is it approximate: Granularity of counter is the timer interrupt

16 Set of pages accessed (working set)
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 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 When someone asks, how much memory does a program need, they mean max(WS). Set of pages accessed (working set) # of unique memory references over time T

17 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 Each entry contains time of last use rather than just a referenced bit

18 WSClock On a page fault, clock sweeps over circular list
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 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 The following additions from clock algorithm: Update time-of-last-use field Compute age by comparing … If age of page, continue

19 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 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

20 Page Fault Frequency Algorithm
Measuring page fault frequency: 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 Choose a victim process whose PFF is lowest Use Clock, LRU to evict a page from victim process

21 Which Algorithm is Best?
Compare algorithms based on a reference string Run a program Look at which memory addresses are accessed Pages accessed: Eliminate duplicates , Why? Defines the reference string Use the same reference string for evaluating different page replacement algorithms Count total page faults a duplicate will not cause replacement for any algorithm.

22 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

23 Paging Issues Paging and I/O interaction Paging performance Thrashing

24 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 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 Must always remember to un-pin the page, e.g., after the I/O completes

25 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: Paging daemon: swap out, in advance Prefetching: swap in, in advance

26 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 Writes out dirty pages, marks them as free Frees enough pages until pool reaches high watermark Frames in pool still hold previous contents of pages Can be rescued if page is referenced before reallocation Issue: If a page is going to be written again, then it would have been better to not swap it

27 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? wasted disk accesses, memory

28 Thrashing Thrashing is a situation where OS spends most time paging data from disk, and user programs do not make much progress A livelock situation System is over-committed: Either, page replacement algorithm is not working Or, system doesn't have enough memory to hold working set of all currently running programs Solution Run more programs because CPU is idle - no! Swapping - suspend some programs for a while Buy more memory

29 Think Time What the optimal page replacement policy?
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?


Download ppt "Page Replacement Algorithms"

Similar presentations


Ads by Google