Virtual Memory! Demand Paging!

Virtual Memory! Demand Paging!

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 1 9 v 3 2 CC 2 i 4 A AA BB CC DD 3 i Process 1! 3 DD 5 page 4 i A B table4 EE 0 A 5 i 6 0 4 5 v C D E F 1 B FF 6 i 7 1 i 6 GG 7 i 2 C 2 10 v 8 F 3 7 HH 3 D i logical 9 BB 4 i memory 4 E 10 5 8 v C 5 F 6 i 11 backing store 6 G 7 i 12 7 H logical physical memory memory • “Lazy Swapper”: only swap in pages that are needed.! • Whenever CPU tries to access a page that is not swapped in, a page fault occurs.! Virtual Memory 1 CPSC 410/611: Operating Systems Mechanics of a Memory Reference! OS reference 1 CPU v 3 page table 2 complete access reference memory some frame physical memory Mechanics of a Page Fault! page is on backing store 3 OS 2 exception! reference 1 CPU vi 6 restart instruction page table 4 framefree 5 frameused update page table load page physical memory Virtual Memory 2 CPSC 410/611: Operating Systems Locality of Reference! • Page faults are expensive!! • Thrashing: Process spends most of the time paging in and out instead of executing code.! • Most programs display a pattern of behavior called the principle of locality of reference.! Locality of Reference! 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 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 = (1-p) * ma + p * “page fault time”! • where! – p = probability of a page fault! – ma = memory access time! • Operations during Page Fault:! page is on backing store OS trap 1. service page fault interrupt reference 2. swap in page CPU i restart 3. restart process instructionpage table free frame update page table load page Virtual Memory 4 CPSC 410/611: Operating Systems OS Policies for Virtual Memory! • Fetch Policy! – How/when to get pages into physical memory.! – demand paging vs. prepaging.! • Placement Policy! – Where in physical memory to put pages.! – Only relevant in NUMA machines.! • Replacement Policy! – Physical memory is full. Which frame to page out?! • Resident Set Management Policy! – How many frames to allocate to process?! – Replace someone elses frame?! • Cleaning Policy! – When to write a modified page to disk.! • Load Control! 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 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 address! 1 L 1 4 v 2 0 N space 1! 2 M i 3 0 v 1 C 3 N 2 K M 0 A 0 3 v 3 A B 1 i 1 B 4 L address! 2 1 v 5 D space 2! 2 C 3 5 v 3 D page tables! frame table! paging store! Mechanics of Page Replacement! • Invoked whenever no free frame can be found.! swap select out victim victim frame invalidate page vict.frame f vi d 3 entry for 1 2 victim page victim update nilf vi 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 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++) { a = x * a; x } • Evaluation: Sequence of memory references: reference string.! FIFO Page Replacement! enter frame in FIFO queue FIFO queue 1 select victim 6 swap out invalidate victim f v/i 3 2 entry for page victim page victim nil/f i/v 5 update 4 swap entry for in new new page page page table backing store physical memory Virtual Memory 7 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 CPSC 410/611: Operating Systems Approximation to Optimal: LRU! • Least Recently Used: 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.! • Stack:! 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 • Capacitors: 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 registers: Associate aging register of n bits (Rn-1, ..., R0) with each frame in memory. Set Rn-1 to 1 for each reference. Periodically shift registers to the right.! Virtual Memory 9 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! before! 11! 10! 01! 00! 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 CPSC 410/611: Operating Systems Improved Clock (cont)! • Example:! time 1 2 3 4 5 6 7 8 9 10 reference c aw d bw e b aw 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_bit and modify_bit.! • Step 1: Scan the frame buffer. Select first frame with use_bit and modify_bit cleared.! • Step 2: If Step 1 fails, scan frame buffer for frame with use_bit cleared and modify_bit set.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us