Operating Systems ECE 344 Page Replacement Algorithms Ashvin

  • Slides: 33
Download presentation
Operating Systems ECE 344 Page Replacement Algorithms Ashvin Goel ECE University of Toronto

Operating Systems ECE 344 Page Replacement Algorithms Ashvin Goel ECE University of Toronto

Overview q Introduction q Page replacement algorithms q Paging issues 2

Overview q Introduction q Page replacement algorithms q Paging issues 2

Introduction q q CPU generates page fault when it accesses an address not in

Introduction q q CPU generates page fault when it accesses an 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 o However, a miss may require accessing the disk o § So miss handling can be very expensive § Disk access latency can be close to a million times slower! – 20 ns versus 10 ms 3

Paging Effectiveness relies on Memory Access Locality q Paging will only work if it

Paging Effectiveness relies on Memory Access Locality q Paging will only work if it occurs rarely q 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 q Programs normally have both kinds of locality o Hence, overall cost of paging is not very high 4

Page Replacement Algorithms q Objective: minimize number of page misses Page table, MMU, TLB

Page Replacement Algorithms q Objective: minimize number of page misses Page table, MMU, TLB handling are VM mechanisms o Page replacement is a VM policy o q Several Algorithms o o o Optimal Algorithm First In First Out (FIFO) Least Recently Used (LRU) Clock Working Set Clock Page Fault Frequency 5

The Optimal Page Replacement Algorithm q Select page that will not be needed for

The Optimal Page Replacement Algorithm q Select page that will not be needed for longest time o Why is this algorithm optimal? (page numbers: a, b, c, d, e, …) 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 e a b c e Page faults X 10 d X 6

The Optimal Page Replacement Algorithm q Optimal algorithm is the best page replacement algo

The Optimal Page Replacement Algorithm q Optimal algorithm is the best page replacement algo q But it is unrealizable Don’t know the future of a program! o Don’t know when a given page will be next needed o q However, it can be used to compare the performance of other algorithms o How? 7

First In First Out (FIFO) q Replace the page that has been in memory

First In First Out (FIFO) q 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 e d a b e d c b e a X X X 8

FIFO Implementation q Implementation o Keep list of all page frames in memory §

FIFO Implementation q Implementation o 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), move the frame to the front of list o On a page fault, choose frame at end of list for replacement o q 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? q We need to know page reference pattern

Can We Do Better that FIFO? q We need to know page reference pattern in the future We can take advantage of locality of reference o Pages that have been accessed in the recent past are likely to be accessed again and should not be replaced o q How can page accesses be tracked? Tracking each memory access is very expensive o Ideally, tracking page access requires hardware support o 10

Recall: Page Table Entry Bits Page table entry 31 12 11 frame number (PFN)

Recall: Page Table Entry Bits Page table entry 31 12 11 frame number (PFN) unused U R 0 D C W V Valid Writable Cacheable Dirty Referenced User 1 bit could be used for COW 11

Tracking Page Accesses q Bits in page table entry allow tracking page accesses o

Tracking Page Accesses q 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 q 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 protection fault, or write fault occurs – Set referenced & dirty bits, make page writeable 12

Least Recently Used (LRU) q Replace page that has been used least recently using

Least Recently Used (LRU) q 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 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 e d a b e c a b d c X X Page faults X 13

LRU Implementation q q Updating LRU list on each memory access is too expensive

LRU Implementation q q Updating LRU list on each memory access is too expensive Suppose MMU maintains a counter that is incremented at 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 q Problem is MMU may not provide such a counter 14

LRU Approximation q OS can maintain a counter in software q Periodically (i. e.

LRU Approximation q OS can maintain a counter in software q Periodically (i. e. , timer interrupt) o Increment counter in each page that has referenced bit set § Write counter value § Clear the referenced bit, why? q On a page fault Software looks through the page table o Identifies entry with the oldest timestamp o If several have oldest time, choose one arbitrarily o q Why does this method approximate LRU? 15

Clock Algorithm q FIFO, while giving second chance to referenced pages Keep circular list

Clock Algorithm q FIFO, while giving second chance to referenced pages Keep circular list of all page frames in memory o Maintain a clock hand, from where frames are evicted o Newly allocate frames is added behind the clock hand o q 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, go to next § Else if referenced bit is not set – If page is dirty • Schedule page write, go to next 0 0 frame # 0 5 1 1 1 4 0 2 – Else page is clean • Select it for replacement, done § Next: advance clock hand to next page 0 3 referenced bit 16

Working Set Model q Working set is the set of pages a program needs

Working Set Model q Working set is the set of pages a program needs currently To measure working set, look at the last time interval T o T is also called working set interval WS(T) = { pages accessed in interval (now, now – T) } o As T gets bigger, more pages are needed o However, working set varies slowly after a while o Set of pages accessed (working set) q # of unique memory references over time T 17

Example: Working Set of gcc over time From https: //cseweb. ucsd. edu/classes/fa 07/cse 120/replace.

Example: Working Set of gcc over time From https: //cseweb. ucsd. edu/classes/fa 07/cse 120/replace. pdf 18

WSClock q q Goal of algorithm is to keep working set in memory, so

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

WSClock q On a page fault, clock sweeps over circular list o If referenced

WSClock q 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 q Compare with Clock algorithm 20

Page Fault Frequency q q Working set clock requires knowing working set interval for

Page Fault Frequency q q 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 o Can be used to ensure fairness in working set allocation o Too High: need to give this program some more frames Too Low: take some frames away, give to other programs 21

Page Fault Frequency Algorithm q Measuring page fault frequency: o For each thread §

Page Fault Frequency Algorithm q 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 for all threads – fe = (1 – a) * fe + a * f, f = 0 – 0 < a < 1, when a -> 1, history is ignored, a is weighting factor q Goal: Allocate frames so PFF is equal for programs q Two step process: Choose a victim process whose PFF is lowest (global replacement) o Within victim process, use a previously described algorithm like WS/Clock or LRU to evict a page (local replacement) o 22

Which Algorithm is Best? q Compare algorithms based on a reference string q Run

Which Algorithm is Best? q Compare algorithms based on a reference string q Run a program o Look at which virtual memory addresses are accessed § Pages accessed: 0000001222333300114444001123444 o Eliminate duplicates § 012301401234, Why? o q Defines the reference string Use the same reference string for evaluating different page replacement algorithms o Count total page faults 23

Example: gcc From https: //cseweb. ucsd. edu/classes/fa 07/cse 120/replace. pdf 24

Example: gcc From https: //cseweb. ucsd. edu/classes/fa 07/cse 120/replace. pdf 24

Summary Algorithm Comment Optimal Not implementable, but useful as a benchmark FIFO Might throw

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 efficiently Working Set Clock Efficient working set algorithm Page Fault Frequency Fairness in working set allocation 25

Paging Issues q Paging and I/O interaction q Paging performance q Thrashing 26

Paging Issues q Paging and I/O interaction q Paging performance q Thrashing 26

Paging and I/O Interaction q Example: A thread performs a read system call, suspends

Paging and I/O Interaction q Example: A thread performs a read system call, suspends during I/O, then another thread runs, has a page fault If enough frames are not available, a frame needs to be evicted o The frame selected for eviction is the one that is involved in the read o This frame is allocated to another (unrelated) page o When I/O returns, OS will copy data from disk to new page! o q 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 27

Paging Performance q q q Paging works best if there are plenty of free

Paging Performance q q q 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 o Prefetching: swap in, in advance o 28

Paging Daemon q q OS can use a paging thread/daemon to maintain a pool

Paging Daemon q q 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 o Frees enough pages until pool reaches high watermark o q Frames in pool still hold previous contents of pages o q 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 (since then the page is written twice) 29

Prefetching q q Page faults can only be processed one page at a time,

Prefetching q q 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 o q For example, if a large array is being accessed sequentially, we can load the next page of the array What if we are wrong? 30

Thrashing q Thrashing is a situation where OS spends most time paging data from

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

Think Time q What the optimal page replacement policy? o q q q Is

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

Think Time q q q How does virtual memory interact with I/O? When should

Think Time q q q How does virtual memory interact with I/O? When should a paging daemon start operation and stop operation? Describe some situations when prefetching of pages will work well 33