Checkin with one Page Replacement condition variable Local vs Global replacement self.allCheckedIn = Condition(self.lock) What ’ s Local: victim chosen from frames of process wrong def checkin(): experiencing page fault with self.lock: fixed allocation per process with this? nArrived++ Global: victim chosen from frames allocated to if nArrived < nThreads: any process while nArrived < nThreads: variable allocation per process allCheckedIn.wait() Many replacement policies else: allCheckedIn.broadcast() Random, FIFO, LRU, Clock, Working set, etc. nArrived = 0 Goal: minimizing number of page faults � 80 � 81 Checkin: 2 condition variables How do we pick a victim? self.allCheckedIn = Condition(self.lock) We want: self.allLeaving = Condition(self.lock) def checkin(): nArrived++ low page fault-rate if nArrived < nThreads: / / not everyone has checked in while nArrived < nThreads: page faults as inexpensive as possible allCheckedIn.wait() / / wait for everyone to check in else: We need: nLeaving = 0 / / this thread is the last to arrive allCheckedIn.broadcast() / / tell everyone we’re all here! a way to compare the relative performance nLeaving++ of different page replacement algorithms if nLeaving < nThreads: / / not everyone has left yet while nLeaving < nThreads: some absolute notion of what a “good” page allLeaving.wait() / / wait for everyone to leave replacement algorithm should accomplish else: nArrived = 0 / / this thread is the last to leave allLeaving.broadcast() / / tell everyone we’re outta here! � 83
Comparing Page Optimal Page Replacement Replacement Algorithms Replace page needed furthest in future Record a trace of the pages accessed by a process Time 0 1 2 3 4 5 6 7 8 9 10 E.g. 3,1,4,2,5,2,1,2,3,4 (or c,a,d,b,e,b,a,b,c,b) Requests c a d b e b a b c d b d c b e 0 a a a a a a a a a a d Simulate behavior of page replacement Page Frames 1 b b b b b b b b b b b algorithm on trace 2 c c c c c c c c c c c Record number of page faults generated 3 d d d d d e e e e e e X X Faults a = 7 a = ∞ Time page b = 6 b = 11 c = 9 c = 13 needed next d = 10 e = 15 � 84 � 85 + Frames FIFO Replacement - Page Faults Replace pages in the order they come into memory Assume: Time 0 1 2 3 4 5 6 7 8 9 10 Number of page faults Requests c a d b e b a b c d a @ -3 b @ -2 0 a a a a a e e e e e d Page Frames c @ -1 d @ 0 1 b b a a a a b b b b b 2 c c c b b b c c c c c 3 d d d d d d e e e c c X X X X X Faults Number of frames � 86 � 87
For example... Belady’ s Anomaly Time 0 1 2 3 4 5 6 7 8 9 10 11 12 Time 0 1 2 3 4 5 6 7 8 9 10 11 12 Request a b c d a b e a b c d e Request a b c d a b e a b c d e s s 0 a a a a a a e e e e d d Page Frames Page Frames 0 a a a d d d e e e e e e FIFO FIFO 1 b b b b b a a a a a e 1 b b b a a a a a c c c c c 2 c c c b b b b b 2 c c c b b b b b d d 3 d d d d d d c c c Faults X X X X X X X X X X X X X X X X X X X Faults 3 frames - 9 page faults! 4 frames - 10 page faults! � 88 � 89 + Frames Locality of Reference - Page Faults? If a process access a memory location, then it is likely that Number of page faults the same memory location is going to be accessed again in the near future (temporal locality) nearby memory locations are going to be accessed in the future (spatial locality) 90% of the execution of a program is sequential Number of frames Most iterative constructs consist of a relatively small Yes, but only for stack page replacement policies number of instructions set of pages in memory with n frames is a subset of set of pages in memory with n+1 frames � 90 � 91
LRU: Least Recently Used Implementing LRU Replace page not referenced for the longest time Maintain a “stack” of recently used pages Time 0 1 2 3 4 5 6 7 8 9 10 Time 0 1 2 3 4 5 6 7 8 9 10 Requests c a d b e b a b c d Requests c a d b e b a b c d 0 a a a a a a a a a a a Page Frames 0 a a a a a a a a a a a Page Frames b b b 1 b b b b b b b b c e e 1 b c c c e e e d b b b b b b b b b b 2 c 3 d d d d d c c d d d d 2 c e e d c c c c e e e Faults X X X 3 d d d d d d d d d c c c a d b e b a b c d LRU Page Stack c a d b e b a b c X X X Faults c a d d e e a b c a a d d e a a = 2 a = 7 a = 7 Time page b = 4 b = 8 b = 8 c = 1 e = 5 e = 5 last used Page to replace c d e d = 3 d = 3 c = 9 � 92 � 93 Implementing LRU Implementing LRU Add a (64-bit) Add a (64-bit) Approximate LRU through aging timestamp to timestamp to keep a k-bit tag in each table entry each page table each page table at every “tick”: i) Shift tag right one bit ii) Copy Referenced (R) bit in tag entry entry iii) Reset Refereced bits to 0 HW counter HW counter If needed, evict page with lowest tag incremented on incremented on R bits at R bits at R bits at R bits at R bits at each instruction each instruction Tick 0 Tick 2 Tick 1 Tick 4 Tick 5 1 0 1 0 1 1 1 1 0 0 1 0 1 1 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 0 Page table entry Page table entry Page 0 10000000 11000000 11100000 11110000 01111000 timestamped timestamped Page 1 00000000 10000000 11000000 01100000 10110000 with counter with counter when referenced when referenced Page 2 10000000 01000000 00100000 00100000 10001000 Page 3 00000000 00000000 10000000 01000000 00100000 Replace page Replace page Page 4 10000000 11000000 01100000 10110000 01011000 with lowest with lowest timestamp timestamp Page 5 10000000 01000000 10100000 01010000 00101000 � 94 � 95
The Clock Algorithm Clock Page Replacement Page 3 Organize pages in 1 2 Time 0 1 2 3 4 5 6 7 8 9 10 memory as a circular list Page 2 Page 0 Requests c a d b e b a b c d 0 7 1 4 When page is e e e e e d 0 a a a a a Page Frames referenced, set its b b b b b b 1 b b b b b reference bit R to 1 2 c c c c a c a a c c c On page fault 3 d d d d d d c c d d d if R = 0: evict the X X X X Faults Page 4 Page 5 page 1 12 1 1 Page table entries 1 a 1 e 1 e 1 e 1 e 1 e 1 d if R = 1: clear R for resident pages 1 b 0 b 1 b 0 b 1 b 1 b 0 b R bit advance hand 1 c 0 c 0 c 1 a 1 a 1 a 0 a 0 5 Hand clock: frame # 1 d 0 d 0 d 0 d 0 d 1 c 0 c Page 1 � 96 � 97 Second Chance The Second Chance Algorithm Page Replacement Page 3 Dirty pages get “second 0 1 2 Time 0 1 2 3 4 5 6 7 8 9 10 chance” before eviction Requests c a w d b w e b a w b c d Page 2 Page 0 replacing dirty pages 0 0 7 1 1 4 a a a a a a a a 0 a a a Page Frames is expensive! 1 b b b b b b b b d b b 2 c c c e e e e e e c c Before After Clock sweep Clock sweep 3 d d d d d d d c c d d dirty R dirty R X X X Faults 0 0 replace page Page 4 Page 5 0 1 0 0 1 1 12 0 1 1 Page table entries 1 a 11 a 0 a 0 a 11 a 11 a 0 a 1 0 0 0 for resident pages 1 b 11 b 0 b 1 b 1 b 1 b 1 d 1 1 1 0 1 c 1 c 1 e 1 e 1 e 1 e 0 e R bit 1 0 5 1 d 1 d 0 d 0 d 0 d 1 c 0 c frame # Hand clock: dirty bit Page 1 � 98 � 99
Local vs. Global Brother, can you Page Replacement spare a frame? Local: Select victim only among allocated Time 0 1 2 3 4 5 6 7 8 9 10 11 12 frames Requests a b c d a b c d a b c d Equal or proportional frame allocation FIFO 0 a a a a d d d c c c b b b Global: Select any free frame, even if 1 b b b b b a a a d d d c c allocated to another process 2 c c c c c c b b b a a a d Processes have no control over their own page fault rate Faults X X X X X X X X X � 100 � 101 Brother, can you Memory as a Cache spare a frame? Demand paging enables frames to cache part of a process VA space Time 0 1 2 3 4 5 6 7 8 9 10 11 12 Requests a b c d a b c d a b c d If the cache is large enough, hit ratio is high 0 a a a a a a a a a a a a a few page faults FIFO 1 b b b b b b b b b b b b b What if there aren’ t enough frames to go 2 c c c c c c c c c c c c c around? 3 - - - - d d d d d d d d d should decrease degree of multiprogramming Faults X swapped out process can then release their So, what’ s wrong with global replacement? frames � 102 � 103
Instead… Instead… When not enough frames... When not enough frames... high page fault rate high page fault rate low CPU utilization low CPU utilization OS may increase degree of multiprogramming! OS may increase degree of multiprogramming! Thrashing CPU Utilization process spends all its time swapping pages in and out Degree of Multiprogramming � 104 � 105 Locality of Reference Tracking Locality If a process access a memory location, then When a process executes it moves from locality it is likely that (set of pages used together) to locality the same memory location is going to be accessed the size of the process’ locality (a.k.a. its again in the near future (temporal locality) working set) can change over time nearby memory locations are going to be accessed in the future (spatial locality) Goal: track the size of the process’ working set, dynamically acquiring and releasing frames as 90% of the execution of a program is sequential necessary Most iterative constructs consist of a relatively small number of instructions � 106 � 107
Recommend
More recommend