virtual memory
play

Virtual Memory ! Overview / Motivation ! Simple Approach: Overlays - PDF document

CPSC 410/611: Operating Systems Virtual Memory ! Overview / Motivation ! Simple Approach: Overlays ! Locality of Reference ! Demand Paging ! Policies ! Placement ! Replacement ! Allocation ! Case Studies: Unix


  1. CPSC 410/611: Operating Systems Virtual Memory ! • Overview / Motivation ! • Simple Approach: Overlays ! • Locality of Reference ! • Demand Paging ! • Policies ! – Placement ! – Replacement ! – Allocation ! • Case Studies: Unix SystemV ! • Reading: Silberschatz, Chapter 9 ! Demand Paging ! 0 Process 2 ! page 1 AA table 0 AA 2 0 1 v 1 BB 3 1 9 v 2 CC 2 i 4 A AA BB CC DD 3 i 3 DD Process 1 ! 5 page 4 i A B 4 EE table 0 A 6 5 i 0 4 v C D E F 5 FF 6 i 1 B 7 1 i 7 i 6 GG 2 C 8 2 10 v F 7 HH 3 i logical 3 D 9 BB memory 4 i 4 E 10 C 5 8 v 5 F 11 6 i backing store 7 i 6 G 12 7 H physical memory logical memory • “Lazy Swapper”: only swap in pages that are needed. ! • Whenever CPU tries to access a page that is not swapped in, a page e fault occurs. ! fa Virtual Memory 1

  2. CPSC 410/611: Operating Systems Mechanics of a Memory Reference ! OS reference 1 v CPU page table 3 2 complete access reference memory some frame physical memory Mechanics of a Page Fault ! page is on backing store 3 OS exception! 2 reference 1 v i CPU 6 restart instruction page table 4 frame free 5 frame used load page update page table physical memory Virtual Memory 2

  3. CPSC 410/611: Operating Systems Locality of Reference ! • Page faults are expensive! ! • Th Thrash ashing ing: Process spends most of the time paging in and out instead of executing code. ! • Most programs display a pattern of behavior called the principle e of f locality of f refer eferen ence. ! Locality of f Referen ence ! A program that references a location n at some point in time is likely to reference the same location n and locations in the immediate vicinity of n in the near future. Memory Access Trace ! Virtual Memory 3

  4. CPSC 410/611: Operating Systems Architectural Considerations ! • Must be able to restart any instruction after a page fault. ! • e.g. ! ADD A,B TO C • What about operations that modify several locations in memory? ! – e.g. block copy operations? ! • What about operations with side effects? ! – e.g. PDP-11, 80x86 auto-decrement, auto-increment operations? ! – Add mechanism for OS to “undo” instructions. ! Performance of Demand Paging ! • Effective Memory Access time ema ema: ! ema = (1-p) * ma ema ma + p * “p “page e fa fault time” me” ! • where ! – p = probability of a page fault ! – ma = memory access time ! • Operations during Page Fault: ! page is on backing store OS 1. service page fault trap interrupt reference 2. swap in page i CPU 3. restart process restart instruction page table free frame load page update page table Virtual Memory 4

  5. CPSC 410/611: Operating Systems OS Policies for Virtual Memory ! • Fet etch Po Policy ! – How/when to get pages into physical memory. ! – demand paging vs . prepaging. ! • Pl Placemen ement Po Policy ! – Where in physical memory to put pages. ! – Only relevant in NUMA machines. ! • Rep eplacemen ement Po Policy ! – Physical memory is full. Which frame to page out? ! • Res esiden ent Set et Managemen ement Po Policy ! – How many frames to allocate to process? ! – Replace someone elses frame? ! • Clea eaning Po Policy ! – When to write a modified page to disk. ! • Load C Load Contr ontrol ol ! Configuring the Win2k Memory Manager ! • Registry Values that Affect the Memory Manager: ! ClearPageFileAtShutdown DisablePagingExecutive IoPageLockLimit LargePageMinimum LargeSystemCache NonPagedPoolQuota NonPagedPoolSize PagedPoolQuota PagedPoolSize SystemPages Virtual Memory 5

  6. CPSC 410/611: Operating Systems Page Replacement ! • Virtual memory allows higher degrees of multiprogramming by over-allocating memory. ! 256kB 256kB 256kB 256kB 256kB 1024kB 0 K 0 2 v 1 L 1 4 v address ! 0 N 2 i 2 M space 1 ! 1 3 C 0 v 3 N M 2 K 3 B A 0 0 3 v A 1 i 4 L 1 B address ! 2 1 v 5 D 2 C space 2 ! 3 5 v 3 D frame table ! paging store ! page tables ! Mechanics of Page Replacement ! • Invoked whenever no free frame can be found. ! swap select out victim victim frame invalidate page vict.frame f v i d 3 2 entry for 1 victim page victim update nil f v i c 5 4 swap entry for in new new page page page table backing store physical memory • Problem: Need two page transfers Solution: Dirty bit . Virtual Memory 6

  7. CPSC 410/611: Operating Systems Page Replacement Algorithms ! • Objective: Minimize page fault rate. ! • Why bother? ! i a • Example ! for(int i=0; i<10; i++) { x a = x * a; } • Evaluation: Sequence of memory references: refer eferen ence e string. ! FIFO Page Replacement ! enter frame in FIFO queue FIFO queue select 1 victim 6 swap out invalidate victim f v/i 3 2 entry for page victim page victim update nil/f i/v 5 4 swap entry for in new new page page page table backing store physical memory Virtual Memory 7

  8. CPSC 410/611: Operating Systems FIFO Page Replacement (cont.) ! • Example: ! time 1 2 3 4 5 6 7 8 9 10 reference c a d b e b a b c d string frames a a a a a e e e e e d b b b b b b b a a a a c c c c c c c c b b b d d d d d d d d d c c ! ! ! ! ! • Advantage: simplicity • Disadvantage: Assumes that pages residing the longest in memory are the least likely to be referenced in the future (does not exploit principle of locality ). Optimal Replacement Algorithm ! • Algorithm with lowest page fault rate of all algorithms: ! Replace that page which will not be used ! for the longest period of time (in the future). ! • Example: ! time 1 2 3 4 5 6 7 8 9 10 reference c a d b e b a b c d string frames a a a a a a a a a a d b b b b b b b b b b b c c c c c c c c c c c d d d d d e e e e e e ! ! Virtual Memory 8

  9. CPSC 410/611: Operating Systems Approximation to Optimal: LRU ! • Lea east Rec ecen ently Used ed: replace the page that has not been accessed for longest period of time (in the past). ! • Example: ! time 1 2 3 4 5 6 7 8 9 10 reference c a d b e b a b c d string frames a a a a a a a a a a a b b b b b b b b b b b c c c c c e e e e e d d d d d d d d d d c c ! ! ! LRU: Implementation ! • Need to keep chronological history of page references; need to be reordered upon each reference. ! • St Stack ack: ! stack ? c a d b e b a b c d ? ? c a d b e b a b c ? ? ? c a d d e e a b ? ? ? ? c a a d d e a • Cap Capacit acitors rs: Associate a capacitor with each memory frame. Capacitor is charged with every reference to the frame. The subsequent exponential decay of the charge can be directly converted into a time interval . ! • Aging reg egister ers: Associate aging register of n bits (R n-1 , ..., R 0 ) with each frame in memory. Set R n-1 to 1 for each reference. Periodically shift registers to the right. ! Virtual Memory 9

  10. CPSC 410/611: Operating Systems Approximation to LRU: Clock Algorithm ! • Associate a use_bit with every frame in memory. ! – Upon each reference, set use_bit to 1. ! – Keep a pointer to first “victim candidate” page. ! – To select victim: If current frame’s use_bit is 0, select frame and increment pointer. Otherwise delete use_bit and increment pointer. ! time 1 2 3 4 5 6 7 8 9 10 reference c a d b e b a b c d string frames a/1 a/1 a/1 a/1 a/1 e/1 e/1 e/1 e/1 e/1 d/1 b/1 b/1 b/1 b/1 b/1 b/0 b/1 b/0 b/1 b/1 b/0 c/1 c/1 c/1 c/1 c/1 c/0 c/0 a/1 a/1 a/1 a/0 d/1 d/1 d/1 d/1 d/1 d/0 d/0 d/0 d/0 c/1 c/0 ! ! ! ! Improvement on Clock Algorithm ! (Second Chance Algorithm) ! • Consider read/write activity of page: dirty_bit (or modify_bit ) ! • Algorithm same as clock algorithm, except that we scan for frame with both use_bit and dirty_bit equal to 0. ! • Each time the pointer advances, the use_bit and dirty_bit are updated as follows: ! ud ud ! ud ud ! ud ud ! ud ! ud before ! be 11 ! 10 ! 01 ! 00 ! af after ! 01 ! 00 ! 00* ! (select) ! • Called Second Chance because a frame that has been written to is not removed until two full scans of the list later. ! • Note: Stallings describes a slightly different algorithm! ! Virtual Memory 10

  11. CPSC 410/611: Operating Systems Improved Clock (cont) ! • Example: ! time 1 2 3 4 5 6 7 8 9 10 reference c a w d b w e b a w b c d string frames a/10 a/10 a/11 a/11 a/11 a/00 * a/00 * a/11 a/11 a/11 b/10 b/10 b/10 b/10 b/11 b/00 * b/10 * b/10 * b/10 * b/10 * c/10 c/10 c/10 c/10 c/10 e/10 e/10 e/10 e/10 e/10 d/10 d/10 d/10 d/10 d/10 d/00 d/00 d/00 d/00 c/10 ! ! ! The Macintosh VM Scheme (see Stallings) ! • Uses use_ e_bit and mo modify fy_bit. ! • Step ep 1: Scan the frame buffer. Select first frame with use_ e_bit and mo modify fy_bit cleared. ! • Step ep 2: If Step 1 fails, scan frame buffer for frame with use_ e_bit cleared and mo modify fy_bit set. During scan, clear use_ e_bit on each bypassed frame. ! • Now all use_ e_bit’s are cleared. Repeat Step 1 and, if necessary, Step 2. ! Virtual Memory 11

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend