1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set
2 Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used A, B, C, B, C, C, D
3 Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used A1A1 A, B, C, B, C, C, D
4 Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used A1A1 B2B2 A, B, C, B, C, C, D
5 Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used A1A1 B2B2 C3C3 A, B, C, B, C, C, D
6 Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used A1A1 B4B4 C3C3 A, B, C, B, C, C, D
7 Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used A1A1 B4B4 C5C5 A, B, C, B, C, C, D
8 Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used A1A1 B4B4 C6C6 A, B, C, B, C, C, D
9 Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used A1A1 B4B4 C6C6 A, B, C, B, C, C, D D7D7 LRU page How should we implement this?
10 Implementing Exact LRU: Data Structures Hash table: optimize the common case (memory hit) Location of a page in memory: apply hash function to a page number update: O(1), eviction: O(n) (n: # of pages to process) Expensive: on every reference, compute hash of page address; update time stamp doubly-linked list Move items to front when referenced LRU items at end of list Still too expensive: Linear lookup time to find a page 4-6 pointer updates per reference
11 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Reference-bit algorithm Clock Algorithm Thrashing Cause Working Set
12 Reference-bit algorithm Hardware support: reference bit Maintain reference bit for every page On each access, set reference bit to 1 Periodically resets reference bits Evict page with reference bit = 0
13 Reference-bit algorithm : example Maintain reference bit for every page On each access, set reference bit to 1 Periodically resets reference bits Evict page with reference bit = 0 A1A1 B1B1 C1C1 A, B, C, B, C, C, D
14 Reference-bit algorithm: example Maintain reference bit for every page On each access, set reference bit to 1 Periodically resets reference bits Evict page with reference bit = 0 A0A0 B0B0 C0C0 A, B, C, B, C, C, D reset reference bits
15 Reference-bit algorithm: example Maintain reference bit for every page On each access, set reference bit to 1 Periodically resets reference bits Evict page with reference bit = 0 A0A0 B1B1 C0C0 A, B, C, B, C, C, D
16 Reference-bit algorithm: example Maintain reference bit for every page On each access, set reference bit to 1 Periodically resets reference bits Evict page with reference bit = 0 A0A0 B1B1 C1C1 A, B, C, B, C, C, D
17 Reference-bit algorithm: example Maintain reference bit for every page On each access, set reference bit to 1 Periodically resets reference bits Evict page with reference bit = 0 A0A0 B1B1 C1C1 A, B, C, B, C, C, D
18 Reference-bit algorithm: example Maintain reference bit for every page On each access, set reference bit to 1 Periodically resets reference bits Evict page with reference bit = 0 A0A0 B1B1 C1C1 A, B, C, B, C, C, D D1D1
19 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Reference-bit algorithm Clock Algorithm Thrashing Cause Working Set
20 The Clock Algorithm Set reference bit to 1 for an access Consider frames in circle Pointer: 0: initially Only advance pointer when page fault happens On page fault, OS: Repeat Checks reference bit of the current pointer If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
21 The Clock Algorithm Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame B1B1 C1C1 A1A1 D1D1 A, B, C, D, B, C, E, F, C, G
22 The Clock Algorithm B1B1 C1C1 A1A1 D1D1 A, B, C, D, B, C, E, F, C, G Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
23 The Clock Algorithm B1B1 C1C1 A1A1 D1D1 A, B, C, D, B, C, E, F, C, G Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
24 The Clock Algorithm B1B1 C1C1 A0A0 D1D1 A, B, C, D, B, C, E, F, C, G Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
25 The Clock Algorithm B0B0 C1C1 A0A0 D1D1 A, B, C, D, B, C, E, F, C, G Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
26 The Clock Algorithm B0B0 C0C0 A0A0 D1D1 A, B, C, D, B, C, E, F, C, G Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
27 The Clock Algorithm B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
28 The Clock Algorithm B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E1E1 Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
29 The Clock Algorithm B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
30 The Clock Algorithm B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F1F1 Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
31 The Clock Algorithm B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F1F1 C1C1 Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
32 The Clock Algorithm B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F0F0 C1C1 Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
33 The Clock Algorithm B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F0F0 C1C1 C0C0 Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
34 The Clock Algorithm B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F0F0 C1C1 C0C0 G1G1 Consider frames in circle On page fault, repeat: Checks reference bit If reference bit = 0, replace page, set bit to 1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance pointer to next frame
35 The Clock Algorithm: Summary Variant of FIFO & LRU LRU: ? FIFO: ? No need to reset referenece bit periodically
36 Enhancing Clock Recall: we don’t write back unmodified pages Idea: favor eviction of unmodified pages Extend hardware to keep another bit: modified bit Total order of tuples: (ref bit, mod bit) (0,0), (0,1), (1,0), (1,1) Evict page from lowest nonempty class
37 Page Replacement in Enhanced Clock OS scans at most three times Page (0,0) – replace that page Page (0,1) – write out page Page (1,0), (1,1) Fast, but still coarse approximation of LRU
38 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Reference-bit algorithm Clock Thrashing Cause Working Set
39 Thrashing A process is busy swapping pages in and out; no useful work is done low CPU utilization OS adds processes → even more page swapping & lower CPU utilization
40 Cause of Thrashing Locality A set of pages that are actively used together Process migrates from one locality to another Total locality of processes > total memory size Process cannot keep in memory all pages that it is currently using
41 Working Set Strategy Working set = pages referred in last references (approximate of locality) OS monitors working set of each process; allocate enough frames to process Another process can be started if there are enough extra frames Suspend process(es) if the sum of working-set sizes exceeds RAM
42 Working Set Problems Algorithm relies on key parameter, How do we set ? Is there one correct ? Different processes have different timescales over which they refer pages Not acceptable (or necessarily possible) to suspend processes altogether Not really used Very rough variant used in Windows