Your SlideShare is downloading. ×
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Virtual Memory and Paging
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Virtual Memory and Paging

7,642

Published on

From the Operating Systems course (CMPSCI 377) at UMass Amherst, Fall 2007.

From the Operating Systems course (CMPSCI 377) at UMass Amherst, Fall 2007.

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,642
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
314
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Operating Systems CMPSCI 377 Virtual Memory & Paging Emery Berger University of Massachusetts Amherst UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
  • 2. Virtual vs. Physical Memory Programmers do not get direct access to  physical memory – they get access to virtual memory One level of indirection  Address you see is not “real” address  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 2
  • 3. Virtual Memory Processes use virtual addresses  Addresses start at 0  OS lays process down on pages  MMU (memory-management unit):  Translates virtual to physical addresses  Maintains page table (big hash table):  virtual -> physical TLB (translation lookaside buffer) = cache of  recently used page translations UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 3
  • 4. Mapping Virtual to Physical UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 4
  • 5. Why Virtual Memory? Why?  Simpler  Everyone gets illusion of  whole address space Isolation  Every process protected  from every other Optimization  Reduces space  requirements UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 5
  • 6. Sharing Paging allows sharing  of memory across processes Reduces memory  requirements Shared stuff includes  code, data Code always R/O  Data copy-on-write  Requires hardware support to be fast  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 6
  • 7. 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 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 7
  • 8. TLB Action UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 8
  • 9. Overview A Day in the Life of a Page  Allocation  Use  Eviction  Reuse  Replacement policies  Comparison  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 9
  • 10. A Day in the Life of a Page Allocate some memory  char * x = new char[16]; ultimately from sbrk or mmap  void * ptr = sbrk(n);  void * ptr = mmap(0, sz, …, -1, 0);  0x40001000 0x40001040 → 0x4000104F virtual memory layout UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 10
  • 11. A Day in the Life of a Page Update page tables  char * x = new char[16]; 0x40001000 0x40001040 → 0x4000104F virtual physical memory memory layout layout UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 11
  • 12. A Day in the Life of a Page Write contents – dirty page  strcpy(x, “hello”); 0x40001000 0x40001040 → 0x4000104F virtual physical memory memory layout layout UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 12
  • 13. A Day in the Life of a Page Other processes fill up memory…  virtual physical memory memory layout layout UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 13
  • 14. A Day in the Life of a Page Forcing our page to be evicted (paged out)  virtual physical swap memory memory space layout layout (disk) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 14
  • 15. A Day in the Life of a Page Now page unmapped & protected  virtual physical swap memory memory space layout layout (disk) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 15
  • 16. A Day in the Life of a Page Touch page – swap it in  y[0] = x[0]; 0x40001000 0x40001040 → 0x4000104F virtual physical swap memory memory space layout layout (disk) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 16
  • 17. A Day in the Life of a Page Touch page – swap it in  y[0] = x[0]; 0x40001000 0x40001040 → 0x4000104F virtual physical swap memory memory space layout layout (disk) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 17
  • 18. Overview A Day in the Life of a Page  Allocation  Use  Eviction  Reuse  Replacement policies  Comparison  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 18
  • 19. 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 A, B, C, D, E, F, G, H, I, J, A... size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 19
  • 20. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... A size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 20
  • 21. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... AB size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 21
  • 22. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... ABC size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 22
  • 23. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... ABCD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 23
  • 24. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... ABCDE size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 24
  • 25. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... FBCDE size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 25
  • 26. Cost of Paging Worst-case analysis – useless  Easy to construct adversary example:  every page requires page fault A, B, C, D, E, F, G, H, I, J, A... FGHI J size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 26
  • 27. Competitive Analysis Instead of worst-case:  compare algorithms to optimal 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  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 27
  • 28. 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 against other  algorithms How far from optimal  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 28
  • 29. MIN/OPT example sequence of page accesses contents of page frames Page faults: 5  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 29
  • 30. 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  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 30
  • 31. LRU example Page faults: ?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 31
  • 32. LRU example Page faults: 5  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 32
  • 33. LRU, example II Page faults: ?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 33
  • 34. LRU, example II Page faults: 12!  Loop: well-known worst-case for LRU  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 34
  • 35. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 35
  • 36. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... A size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 36
  • 37. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... AB size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 37
  • 38. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABC size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 38
  • 39. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 39
  • 40. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 40
  • 41. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 41
  • 42. Most-Recently Used (MRU) Evict most-recently used page  Shines for LRU’s worst-case: loop that  exceeds RAM size A, B, C, D, A, B, C, D, ... ABD size of available memory UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 42
  • 43. 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  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 43
  • 44. FIFO & Belady’s Anomaly UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 44
  • 45. LRU: No Belady’s Anomaly Why no anomaly for LRU?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 45
  • 46. The End Next time:  how real OS approximate LRU UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 46
  • 47. Virtual Memory in the Real World Implementing exact LRU  Approximating LRU  Hardware Support  Clock  Segmented queue  Multiprogramming  Global LRU  Working Set  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 47
  • 48. 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 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 48
  • 49. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A A, B, C, B, C, C, D 1 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 49
  • 50. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B A, B, C, B, C, C, D 1 2 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 50
  • 51. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B C A, B, C, B, C, C, D 1 2 3 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 51
  • 52. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B C A, B, C, B, C, C, D 1 4 3 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 52
  • 53. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B C A, B, C, B, C, C, D 1 4 5 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 53
  • 54. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used A B C A, B, C, B, C, C, D 1 4 6 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 54
  • 55. Implementing Exact LRU On each reference, time stamp page  When we need to evict: select oldest page  = least-recently used D A B C A, B, C, B, C, C, D 1 7 4 6 LRU page How should we implement this? UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 55
  • 56. 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) UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 56
  • 57. 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! UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 57
  • 58. 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?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 58
  • 59. Virtual Memory in the Real World Implementing exact LRU  Approximating LRU  Hardware Support  Clock  Segmented queue  Multiprogramming  Global LRU  Working Set  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 59
  • 60. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 1 1 1 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 60
  • 61. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 0 0 0 reset reference bits UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 61
  • 62. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 0 1 0 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 62
  • 63. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 0 1 1 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 63
  • 64. Hardware Support Maintain reference bits for every page  On each access, set reference bit to 1  Page replacement algorithm periodically  resets reference bits A B C A, B, C, B, C, C, D 0 1 1 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 64
  • 65. 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  D A B C A, B, C, B, C, C, D 0 1 1 1 Cost per miss = O(n)  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 65
  • 66. Virtual Memory in the Real World Implementing exact LRU  Approximating LRU  Hardware Support  Clock  Segmented queue  Multiprogramming  Global LRU  Working Set  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 66
  • 67. The Clock Algorithm Variant of FIFO & LRU B  1 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  1 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 67
  • 68. The Clock Algorithm Variant of FIFO & LRU B  1 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  1 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 68
  • 69. The Clock Algorithm Variant of FIFO & LRU B  1 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  1 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 69
  • 70. The Clock Algorithm Variant of FIFO & LRU B  1 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  0 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 70
  • 71. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  0 1 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 71
  • 72. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  0 0 next frame If reference bit = 0,  D replace page, set bit to 1 1 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 72
  • 73. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A C Checks reference bit of  0 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 73
  • 74. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  1 0 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 74
  • 75. The Clock Algorithm Variant of FIFO & LRU B  0 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 75
  • 76. The Clock Algorithm Variant of FIFO & LRU B F  0 1 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 76
  • 77. The Clock Algorithm Variant of FIFO & LRU B F  0 1 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 1 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 77
  • 78. The Clock Algorithm Variant of FIFO & LRU B F  0 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 1 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 78
  • 79. The Clock Algorithm Variant of FIFO & LRU B F  0 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 1 0 next frame If reference bit = 0,  D replace page, set bit to 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 79
  • 80. The Clock Algorithm Variant of FIFO & LRU B F  0 1 Keep frames in circle  On page fault, OS:  A E C Checks reference bit of  0 0 1 next frame If reference bit = 0,  D G replace page, set bit to 1 1 0 If reference bit = 1, set bit  to 0, advance pointer to A, B, C, D, B, C, E, F, C, G next frame UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 80
  • 81. 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  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 81
  • 82. Page 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) 1. all pages (0,1) - write out pages 2. all pages (0,0) – replace any page 3. Fast, but still coarse approximation of LRU  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 82
  • 83. 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 two segments?  clock exact LRU UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 83
  • 84. Virtual Memory in the Real World Implementing exact LRU  Approximating LRU  Hardware Support  Clock  Segmented queue  Multiprogramming  Global LRU  Working Set  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 84
  • 85. Multiprogramming & VM Multiple programs compete for main  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 to  avoid thrashing UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 85
  • 86. Global LRU Put all pages from all processes in one pool  Manage with LRU (Segmented Queue)  Used by Linux, BSD, etc.  Advantages:  Easy  Disadvantages:  Many  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 86
  • 87. 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  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 87
  • 88. Global LRU Disadvantages, Cont. “Sleepyhead” problem  Intermittent, important process  Every time it wakes up – no pages! – back to  sleep... Susceptible to denial of service  Non-paying “guest”, lowest priority, marches  over lots of pages – gets all available memory Alternatives?  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 88
  • 89. 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 UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 89
  • 90. 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  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 90
  • 91. 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%  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 91
  • 92. Calculating WSS w.r.t 5% Memory reference sequence mmmm g h h h k kmmm n kmmm n nnnnnggj j jknnkcknn bdddef i i i l l lccl l l abeef kkcf ccl ll LRU Queue mmmmm j h h g e d d b a nakkk bnnnni igf fec akccc kcllj cl l l Pages in Least Recently Used order 1 14 Hit Histogram 00004000000100 0 1 2 3 0 Associated with each LRU position 5 Fault Curve faults  faulti   hist i  1 1 i 1 4 11 14 pages UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 92
  • 93. 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  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 93
  • 94. Managing pages per process Pages protected by turning Pages evicted to disk. off permissions (minor fault) (major fault) Active (CLOCK) Inactive (LRU) Evicted (LRU) Major fault Header Refill & Adjustment Evicted Page Des Minor fault AVL node faults Pages Histogram UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 94
  • 95. Controlling overhead Pages protected by turning Pages evicted to disk. off permissions (minor fault) (major fault) Active (CLOCK) Inactive (LRU) Evicted (LRU) Header Page Des AVL node Buffer faults control the boundary: 1% of execution time Pages Histogram UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 95
  • 96. The End UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 96

×