Download presentation
Presentation is loading. Please wait.
Published byGervais Fox Modified over 9 years ago
1
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Virtual Memory & Paging Emery Berger and Mark Corner University of Massachusetts Amherst
2
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 2 Virtual vs. Physical Memory Apps don’t access physical memory –Well, not directly Apps use virtual memory –Addresses start at 0 –One level of indirection –Address you see is not “real” address Any ideas why?
3
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Memory Pages Programs use memory as individual bytes OS manages groups of bytes: pages –typically 4kB, 8kB –Why? (think Tetris with all squares) –Applies this to virtual and physical memory Physical pages usually called frames A
4
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 4 Mapping Virtual to Physical Note this is simplified and the data here includes the heap, not the typical data segment…
5
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 5 Why Virtual Memory? Why? –Simpler Everyone gets illusion of whole address space –Isolation Every process protected from every other –Optimization Reduces space requirements
6
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Typical Virtual Memory Layout Some things grow –Must leave room! Mmap and heap spaces –Mmap increases mmap –Brk increases heap Other layouts possible
7
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Quick Quiz! Are arrays contiguous in memory? –Physical memory? –Virtual memory? Where does the code for a program live?
8
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Memory Management Unit Programs issue loads and stores What kind of addresses are these? MMU Translates virtual to physical addresses –Maintains page table (big hash table): –Almost always in HW… Why? MMU Physical Address Virtual Address ProgramMemory Page Table
9
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Page Tables Table of translations –virtual pages -> physical pages One page table per process One page table entry per virtual page How? –Programs issue virtual address –Find virtual page (how?) –Lookup physical page, add offset
10
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Page Table Entries Do all virtual pages -> physical page? – Valid and Invalid bits PTEs have lots of other information –For instance some pages can only be read
11
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 11 Address Translation Powers of 2: –Virtual address space: size 2^m –Page size 2^n Page#: High m-n bits of virtual address Lower n bits select offset in page
12
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Quick Activity How much mem does a page table need? –4kB pages, 32 bit address space –page table entry (PTE) uses 4 bytes 2^32/2^12*4=2^22 bytes=4MB –Is this a problem? –Isn’t this per process? –What about a 64 bit address space? Any ideas how to fix this?
13
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Multi-Level Page Tables Use a multi-level page table AA A Level 0 Table Level 1 Table
14
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Quick Activity How much mem does a page table need? –4kB pages, 32 bit address space –Two level page table –20bits = 10 bits each level –page table entry (PTE) uses 4 bytes –Only first page of program is valid 2^10*4+2^10*4=2^13 bytes=8kB Isn’t this slow?
15
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 15 Translation Lookaside Buffer (TLB) TLB: fast, fully associative memory –Caches page table entries –Stores page numbers (key) and frame (value) in which they are stored Assumption: locality of reference –Locality in memory accesses = locality in address translation TLB sizes: 8 to 2048 entries –Powers of 2 simplifies translation of virtual to physical addresses
16
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Virtual Memory is an Illusion! How much memory does a process have? –Do all processes have this? Key idea: use RAM as cache for disk –OS transparently moves pages Requires locality: –Working set must fit in RAM memory referenced recently –If not: thrashing (nothing but disk traffic)
17
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 17 Paging
18
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 18 A B A B Paging + Locality Most programs obey 90/10 “rule” –90% of time spent accessing 10% of memory Exploit this rule: –Only keep “live” parts of process in memory
19
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 19 Key Policy Decisions Two key questions: (for any cache): –When do we read page from disk? –When do we write page to disk?
20
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 20 Reading Pages Read on-demand: –OS loads page on its first reference –May force an eviction of page in RAM –Pause while loading page = page fault Can also perform pre-paging: –OS guesses which page will next be needed, and begins loading it Most systems just do demand paging What about writes?
21
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 21 Demand Paging On every reference, check if page is in memory (resident bit in page table) –Who is doing this? If not: trap to OS –How does this work in HW? OS: –Selects victim page to be replaced –Writes victim page if necessary, marks non-resident –Begins loading new page from disk –OS can switch to another process more on this later
22
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 22 Swap Space Swap space = where victim pages go –Partition or special file reserved on disk Size of reserved swap space limits what?
23
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Tricks with Page Tables Do all pages of memory end up in swap? Parts of address space mapped into files –see man pages for mmap
24
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 24 Overview A Day in the Life of a Page –Allocation –Use –Eviction –Reuse Terms: Resident and Non-resident
25
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science char * x = new char[16]; 25 virtual memory layout Allocate some memory 0x40001000 0x40001040 → 0x4000104F A Day in the Life of a Page
26
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science char * x = new char[16]; 26 virtual memory layout Update page tables 0x40001000 0x40001040 → 0x4000104F physical memory layout A Day in the Life of a Page
27
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science strcpy(x, “hello”); 27 virtual memory layout Write contents – dirty page 0x40001000 0x40001040 → 0x4000104F physical memory layout A Day in the Life of a Page
28
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 28 virtual memory layout Other processes fill up memory… physical memory layout A Day in the Life of a Page
29
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 29 virtual memory layout Forcing our page to be evicted (paged out) physical memory layout swap space (disk) A Day in the Life of a Page
30
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 30 virtual memory layout Now page nonresident & protected physical memory layout swap space (disk) A Day in the Life of a Page
31
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science y[0] = x[0]; 31 virtual memory layout Touch page – swap it in 0x40001000 0x40001040 → 0x4000104F physical memory layout swap space (disk) A Day in the Life of a Page
32
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science y[0] = x[0]; 32 virtual memory layout Touch page – swap it in 0x40001000 0x40001040 → 0x4000104F physical memory layout swap space (disk) A Day in the Life of a Page
33
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 33 Tricks with Page Tables: Sharing Paging allows sharing of memory across processes –Reduces memory requirements Shared stuff includes code, data –Code typically R/O
34
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Tricks with Page Tables: COW Copy on write (COW) –Just copy page tables –Make all pages read-only What if process changes mem? All processes are created this way!
35
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Allocating Pages ultimately from sbrk or mmap Sbrk increases # of valid pages –Increases the heap Mmap maps address space to file –Increases the mmap space Oddity: –Allocators can use either mmap or brk to get pages –You will use mmap What does mmap /dev/zero mean? –Think about COW
36
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 36 Overview Replacement policies –Comparison
37
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 37 A, B, C, D, E, F, G, H, I, J, A... size of available memory Cost of Paging Usually in algorithms, we pick algorithm with best asymptotic worst-case –Paging: worst-case analysis useless! –Easy to construct adversary: every page requires page fault
38
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 38 A A, B, C, D, E, F, G, H, I, J, A... size of available memory Cost of Paging Worst-case analysis – useless –Easy to construct adversary example: every page requires page fault
39
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 39 AB A, B, C, D, E, F, G, H, I, J, A... size of available memory Cost of Paging Worst-case analysis – useless –Easy to construct adversary example: every page requires page fault
40
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 40 ABC A, B, C, D, E, F, G, H, I, J, A... size of available memory Cost of Paging Worst-case analysis – useless –Easy to construct adversary example: every page requires page fault
41
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 41 ABCD A, B, C, D, E, F, G, H, I, J, A... size of available memory Cost of Paging Worst-case analysis – useless –Easy to construct adversary example: every page requires page fault
42
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 42 ABCDE A, B, C, D, E, F, G, H, I, J, A... size of available memory Cost of Paging Worst-case analysis – useless –Easy to construct adversary example: every page requires page fault
43
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 43 FBCDE A, B, C, D, E, F, G, H, I, J, A... size of available memory Cost of Paging Worst-case analysis – useless –Easy to construct adversary example: every page requires page fault
44
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 44 FGHIJ A, B, C, D, E, F, G, H, I, J, A... size of available memory Cost of Paging Worst-case analysis – useless –Easy to construct adversary example: every page requires page fault
45
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 45 Optimal Replacement (MIN/OPT) Evict page accessed furthest in future –Optimal page replacement algorithm Invented by Belady (“MIN”), a.k.a. “OPT” Provably optimal policy –Just one small problem... Requires predicting the future –Useful point of comparison How far from optimal
46
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 46 sequence of page accesses contents of page frames Quick Activity: OPT Page faults: 5
47
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 47 Least-Recently Used (LRU) Evict page not used in longest time (least-recently used) –Approximates OPT If recent past ≈ predictor of future –Variant used in all real operating systems
48
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 48 Quick Activity: LRU example Page faults: ?
49
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 49 LRU example Page faults: 5
50
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 50 LRU, example II Page faults: ?
51
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 51 LRU, example II Page faults: 12! –Loop: well-known worst-case for LRU
52
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 52 A, B, C, D, A, B, C, D,... size of available memory Most-Recently Used (MRU) Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size
53
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 53 A A, B, C, D, A, B, C, D,... size of available memory Most-Recently Used (MRU) Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size
54
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 54 AB A, B, C, D, A, B, C, D,... size of available memory Most-Recently Used (MRU) Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size
55
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 55 ABC A, B, C, D, A, B, C, D,... size of available memory Most-Recently Used (MRU) Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size
56
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 56 ABD A, B, C, D, A, B, C, D,... size of available memory Most-Recently Used (MRU) Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size
57
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 57 ABD A, B, C, D, A, B, C, D,... size of available memory Most-Recently Used (MRU) Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size
58
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 58 ABD A, B, C, D, A, B, C, D,... size of available memory Most-Recently Used (MRU) Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size
59
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 59 ABD A, B, C, D, A, B, C, D,... size of available memory Most-Recently Used (MRU) Evict most-recently used page Shines for LRU’s worst-case: loop that exceeds RAM size
60
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 60 FIFO First-in, first-out: evict oldest page As competitive as LRU, but performs miserably in practice! –Ignores locality –Suffers from Belady’s anomaly: More memory can mean more paging! –LRU & similar algs. do not Stack algorithms – more memory means ≥ hits
61
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 61 Virtual Memory in Reality Implementing exact LRU Approximating LRU –Hardware Support –Clock –Segmented queue Multiprogramming –Global LRU –Working Set
62
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 62 A, B, C, B, C, C, D Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used
63
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 63 A1A1 A, B, C, B, C, C, D Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used
64
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 64 A1A1 B2B2 A, B, C, B, C, C, D Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used
65
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 65 A1A1 B2B2 C3C3 A, B, C, B, C, C, D Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used
66
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 66 A1A1 B4B4 C3C3 A, B, C, B, C, C, D Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used
67
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 67 A1A1 B4B4 C5C5 A, B, C, B, C, C, D Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used
68
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 68 A1A1 B4B4 C6C6 A, B, C, B, C, C, D Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used
69
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 69 A1A1 B4B4 C6C6 A, B, C, B, C, C, D D7D7 LRU page How should we implement this? Implementing Exact LRU On each reference, time stamp page When we need to evict: select oldest page = least-recently used
70
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 70 Implementing Exact LRU Could keep pages in order – optimizes eviction –Priority queue: update = O(log n), eviction = O(log n) Optimize for common case! –Common case: hits, not misses –Hash table: update = O(1), eviction = O(n)
71
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 71 Cost of Maintaining Exact LRU Hash tables: too expensive –On every reference: Compute hash of page address Update time stamp –Unfortunately: 10x – 100x more expensive!
72
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 72 Cost of Maintaining Exact LRU Alternative: doubly-linked list –Move items to front when referenced –LRU items at end of list –Still too expensive 4-6 pointer updates per reference Can we do better?
73
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 73 Virtual Memory in Reality Implementing exact LRU Approximating LRU –Hardware Support –Clock –Segmented queue Multiprogramming –Global LRU –Working Set
74
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 74 A1A1 B1B1 C1C1 A, B, C, B, C, C, D Hardware Support Maintain reference bits for every page –On each access, set reference bit to 1 –Page replacement algorithm periodically resets reference bits
75
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 75 A0A0 B0B0 C0C0 A, B, C, B, C, C, D reset reference bits Hardware Support Maintain reference bits for every page –On each access, set reference bit to 1 –Page replacement algorithm periodically resets reference bits
76
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 76 A0A0 B1B1 C0C0 A, B, C, B, C, C, D Hardware Support Maintain reference bits for every page –On each access, set reference bit to 1 –Page replacement algorithm periodically resets reference bits
77
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 77 A0A0 B1B1 C1C1 A, B, C, B, C, C, D Hardware Support Maintain reference bits for every page –On each access, set reference bit to 1 –Page replacement algorithm periodically resets reference bits
78
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 78 A0A0 B1B1 C1C1 A, B, C, B, C, C, D Hardware Support Maintain reference bits for every page –On each access, set reference bit to 1 –Page replacement algorithm periodically resets reference bits
79
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 79 A0A0 B1B1 C1C1 A, B, C, B, C, C, D D1D1 Hardware Support Maintain reference bits for every page –On each access, set reference bit to 1 –Page replacement algorithm periodically resets reference bits –Evict page with reference bit = 0 Cost per miss = O(n)
80
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 80 Virtual Memory in Reality Implementing exact LRU Approximating LRU –Hardware Support –Clock –Segmented queue Multiprogramming –Global LRU –Working Set
81
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 81 B1B1 C1C1 A1A1 D1D1 A, B, C, D, B, C, E, F, C, G The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
82
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 82 B1B1 C1C1 A1A1 D1D1 A, B, C, D, B, C, E, F, C, G The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
83
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 83 B1B1 C1C1 A1A1 D1D1 A, B, C, D, B, C, E, F, C, G The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
84
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 84 B1B1 C1C1 A0A0 D1D1 A, B, C, D, B, C, E, F, C, G The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
85
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 85 B0B0 C1C1 A0A0 D1D1 A, B, C, D, B, C, E, F, C, G The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
86
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 86 B0B0 C0C0 A0A0 D1D1 A, B, C, D, B, C, E, F, C, G The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
87
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 87 B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
88
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 88 B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E1E1 The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
89
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 89 B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
90
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 90 B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F1F1 The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
91
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 91 B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F1F1 C1C1 The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
92
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 92 B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F0F0 C1C1 The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
93
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 93 B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F0F0 C1C1 C0C0 The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
94
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 94 B0B0 C0C0 A0A0 D0D0 A, B, C, D, B, C, E, F, C, G E0E0 F0F0 C1C1 C0C0 G1G1 The Clock Algorithm Variant of FIFO & LRU Keep frames in circle On page fault, OS: –Checks reference bit of next frame –If reference bit = 0, replace page, set bit to 1 –If reference bit = 1, set bit to 0, advance pointer to next frame
95
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 95 clockexact LRU Segmented Queue Real systems: segment queue into two –approximate for frequently-referenced pages e.g., first 1/3 page frames – fast –exact LRU for infrequently-referenced pages last 2/3 page frames; doubly-linked list – precise How do we move between the two?
96
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 96 Enhancing Clock Recall: 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
97
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 97 Replacement, Enhanced Clock OS scans at most three times –Page (0,0) – replace that page –Page (0,1) – write out page, clear mod bit –Page (1,0), (1,1) – clear reference bit Passes: –all pages (0,0) or (0,1) –all pages (0,1) - write out pages –all pages (0,0) – replace any page Fast, but still coarse approximation of LRU
98
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 98 Multiprogramming & VM Multiple programs compete for memory –Processes move memory from and to disk –Pages needed by one process may get squeezed out by another process –thrashing - effective cost of memory access = cost of disk access = really really bad Must balance memory across processes –avoid thrashing
99
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 99 Global LRU Put all pages from all procs in one pool –Manage with LRU (Segmented Queue) –Used by Linux, BSD, etc. Advantages: –Easy Disadvantages: –Many
100
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 100 Global LRU Disadvantages No isolation between processes –One process touching many pages can force another process’ pages to be evicted Priority ignored, or inverted –All processes treated equally Greedy (or wasteful) processes rewarded –Programs with poor locality squeeze out those with good locality –Result: more page faults
101
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 101 Global LRU Disadvantages “Sleepyhead” problem –Intermittent, important process –Every time it wakes up – no pages! – back to sleep... –Think ntpd Susceptible to denial of service –Non-paying “guest”, lowest priority, marches over lots of pages – gets all available memory Alternatives? –Pinning?
102
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 102 Working Set Denning: Only run processes whose working set fits in RAM –Other processes: deactivate (suspend) Classical definition: working set = pages touched in last references Provides isolation –Process’s reference behavior only affects itself
103
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 103 Working Set Problems Algorithm relies on key parameter, –How do we set ? –Is there one correct ? Different processes have different timescales over which they touch pages Not acceptable (or necessarily possible) to suspend processes altogether Not really used –Very rough variant used in Windows
104
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 104
105
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 105 Solution: CRAMM New VM management alg: Cooperative Robust Automatic Memory Management [OSDI 2006, Yang et al.] Redefine working set size = pages required to spend < n% time paging –CRAMM default = 5%
106
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 106 defghijklmncklmncbcdefghijklmncklmnabaabcdefghijklmnabcdefghijklmnabdefghijcklnmabcdefghijkmnlabcdefghijlmnkabdefghijklmnc4n3211 Memory reference sequence LRU Queue Pages in Least Recently Used order Hit Histogram Fault Curve 00000000000000mn 114 lmnklmncklmnabcdefghijklmncklmn 5 1 1 114 Associated with each LRU position pages faults Calculating WSS w.r.t 5%
107
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 107 Computing hit histogram Not possible in standard VM: –Global LRU queues –No per process/file information or control Difficult to estimate app’s WSS / available memory CRAMM VM: –Per process/file page management: Page list: Active, Inactive, Evicted Add & maintain histogram
108
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 108 Active (CLOCK) Inactive (LRU)Evicted (LRU) Major fault Evicted Refill & Adjustment Minor fault Pages protected by turning off permissions (minor fault) Pages evicted to disk. (major fault) Header Page Des AVL node Histogram Pages faults Managing pages per process
109
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 109 Buffer Active (CLOCK) Inactive (LRU)Evicted (LRU) Pages protected by turning off permissions (minor fault) Pages evicted to disk. (major fault) Header Page Des AVL node Histogram Pages faults control the boundary: 1% of execution time Controlling overhead
110
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 110 Competitive Analysis I removed this slide because I don’t get it. This is the worst case analysis… Instead of worst-case, Compare replacement policy (OPT) –How much worse is algorithm than optimal? Result: LRU & FIFO both “k-competitive” –k = size of queue –Can incur k times more misses than OPT
111
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 111 FIFO & Belady’s Anomaly
112
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science 112 LRU: No Belady’s Anomaly Why no anomaly for LRU?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.