CPSC 410/611: Operating Systems Virtual Memory • Overview / Motivation • Locality of Reference • Demand Paging • Policies – Placement – Replacement – Allocation 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 fault occurs. Virtual Memory 1
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
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: Example Virtual Memory 3
CPSC 410/611: Operating Systems Performance of Demand Paging Operations during Page Fault: page is on backing store OS trap reference i CPU restart 1. service page fault instruction page table interrupt free frame 2. swap in page load page update page table 3. restart process Effective Memory Access Time ema : ema = (1-p) * ma + p * “ page fault time ” where – p = probability of a page fault – ma = memory access time Interlude: 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. 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 . pre-paging. • 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 else ’ s frame? • Cleaning Policy – When to write a modified page to disk. • Load Control Configuring the Windows 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 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 space 2 C 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
� � � 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: reference string . FIFO Page Replacement enter frame in FIFO queue FIFO queue select 1 victim 6 swap out invalidate victim f/nil 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
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 provably lowest page fault rate of all algorithms: Replace that page which will not be used for the longest period of time (in the future). 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). 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 Problem: We 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 (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
� � � � 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: Other authors (e.g., Stallings) describe 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 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_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. During scan, clear use_bit on each bypassed frame. • Now all use_bit ’ s are cleared. Repeat Step 1 and, if necessary, Step 2. Virtual Memory 11
Recommend
More recommend