Virtual Memory Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution - some can be on disk Disk address can be stored in place of frame number A valid–invalid bit is associated with each page table entry (1 in-memory, 0 not-in-memory) During address translation, if valid–invalid bit in page table entry is 0 page fault bring in to memory Operating System Concepts 10.1 Silberschatz, Galvin and Gagne 2002 Page Table When Some Pages Are Not in Main Memory Sum logical address space can therefore be much larger than physical address space. More processes Operating System Concepts 10.2 Silberschatz, Galvin and Gagne 2002 Page Faults Bringing a Page into Memory Get empty frame. Swap page into frame. Reset tables, validation bit = 1. Restart instruction Operating System Concepts 10.3 Silberschatz, Galvin and Gagne 2002 Need For Page Replacement Operating System Concepts 10.4 Silberschatz, Galvin and Gagne 2002 What happens if there is no free frame? Page replacement – find some page in memory, but not really in use, swap it out. Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk. Operating System Concepts 10.5 Silberschatz, Galvin and Gagne 2002 Basic Page Replacement 1. Find the location of the desired page on disk. 2. Find a free frame: If there is a free frame, use it. 2. If there is no free frame, use a page replacement algorithm to select a victim frame. 3. If victim is dirty, write it out to disk 3. Read the desired page into the (newly) free frame. Update the page and frame tables. 4. Restart the process 1. Operating System Concepts 10.6 Silberschatz, Galvin and Gagne 2002 Page Replacement Policies Global vs. Local Allocation Global replacement – process selects a replacement frame from the set of all frames; one process can take a frame from another - only for crucial OS processes Local replacement – each process selects from only its own set of allocated frames - the normal case I/O interlock Pages that are used for copying a file from a device must be locked Operating System Concepts 10.7 Silberschatz, Galvin and Gagne 2002 Thrashing VM works because of locality Process migrates from one locality to another. Localities may overlap What happens if size of localities > total memory size Operating System Concepts 10.8 Silberschatz, Galvin and Gagne 2002 Thrashing If a process does not have “enough” pages for its locality, the page-fault rate is very high. Thrashing a process is busy swapping pages in and out. Thrashing may lead to: low CPU utilization. operating system thinks that it needs to increase the degree of multiprogramming. another process added to the system It is important to allocate enough frames to each process If that’s not possible, the process must be swapped out Operating System Concepts 10.9 Silberschatz, Galvin and Gagne 2002 Allocation of Frames Each process needs minimum number of frames Example: IBM 370 – 6 pages to handle SS MOVE instruction: instruction is 6 bytes, might span 2 pages. 2 pages to handle from. 2 pages to handle to. Two major allocation schemes. fixed allocation priority allocation Operating System Concepts 10.10 Silberschatz, Galvin and Gagne 2002 Fixed Allocation Equal allocation – e.g., if 100 frames and 5 processes, give each 20 frames. Proportional allocation – Allocate according to the size of process. si size of process pi S si m total number of frames s ai allocation for pi i m S m 64 si 10 s2 127 10 64 5 137 127 a2 64 59 137 a1 Operating System Concepts 10.11 Silberschatz, Galvin and Gagne 2002 Priority Allocation Use a proportional allocation scheme using priorities rather than size. If process Pi generates a page fault, select for replacement one of its frames. select for replacement a frame from a process with lower priority number. Operating System Concepts 10.12 Silberschatz, Galvin and Gagne 2002 Page-Fault Frequency Scheme Establish “acceptable” page-fault rate. If actual rate too low, process loses frame. If actual rate too high, process gains frame. Operating System Concepts 10.13 Silberschatz, Galvin and Gagne 2002 Initial Load Load pages predicted to be needed (compiler flags) or Load no pages Allows for more efficient process creation Operating System Concepts 10.14 Silberschatz, Galvin and Gagne 2002 Performance of Demand Paging Page Fault Rate 0 p 1.0 (hopefully low, e.g. 5%) if p = 0 no page faults if p = 1, every reference is a fault Effective Access Time (EAT) EAT = (1 – p) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead) Values Assume memory access roughly 1 (with TLB hits) Page fault overhead is small Swap is about 10000 Restart is small Operating System Concepts 10.15 Silberschatz, Galvin and Gagne 2002 Page Replacement Algorithms Want lowest page-fault rate (disk is slow) Evaluate algorithm by running it on a particular string of page references (reference string) and computing the number of page faults on that string. A page fault means a disk access (or two), and that’s where the time goes. Assume no initial load, local replacement, no I/O (no interlock issues) Operating System Concepts 10.16 Silberschatz, Galvin and Gagne 2002 Optimal Algorithm Replace page that will not be used for longest period of time in future. 3 frames example: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 (9) 4 frames example: 1 2 3 4 1 2 5 1 2 3 4 5 (6) But the future is unknown (like SJF scheduling) Operating System Concepts 10.17 Silberschatz, Galvin and Gagne 2002 First-In-First-Out (FIFO) Algorithm 3 frames example: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 (15) Operating System Concepts 10.18 Silberschatz, Galvin and Gagne 2002 FIFO Illustrating Belady’s Anamoly 4 frames example: 1 2 3 4 1 2 5 1 2 3 4 5 (10) 3 frames example: 1 2 3 4 1 2 5 1 2 3 4 5 (9) Belady’s Anomaly - more frames does not imply less page faults Operating System Concepts 10.19 Silberschatz, Galvin and Gagne 2002 Counting Algorithms Keep a counter of the number of references that have been made to each page. Count is reset each time a page is brought in LFU Algorithm: replaces page with smallest count. May delay start of counting to avoid bias from an initial rush MFU Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used. See example in Geoff’s notes Operating System Concepts 10.20 Silberschatz, Galvin and Gagne 2002 LRU Page Replacement 3 frames example: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 (11) 4 frames example: 1 2 3 4 1 2 5 1 2 3 4 5 (?) Operating System Concepts 10.21 Silberschatz, Galvin and Gagne 2002 Least Recently Used (LRU) Algorithm Counter implementation Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. When a page needs to be changed, look at the counters to determine which are to change. Stack implementation – keep a stack of page numbers in a double link form: Page referenced: move it to the top requires 6 pointers to be changed No search for replacement Both need HW support, as they are done very very often Operating System Concepts 10.22 Silberschatz, Galvin and Gagne 2002 Use Of A Stack to Record The Most Recent Page References Operating System Concepts 10.23 Silberschatz, Galvin and Gagne 2002 LRU Approximation Algorithms Usually not enough HW support for full LRU, so … Reference bit in the page table With each page associate a bit, in HW initially = 0 When page is referenced bit set to 1. Replace the one which is 0 (if one exists). We do not know the order, however. Clear when all 1 Reference bytes in the page table Make top bit the reference bit Shift right reference byte at regular intervals Victim is page with smallest value byte Second chance bit associated with each frame Looks through frames in order, with wrap-around, starting at frame after last loaded If frame has bit = 1 then: set bit 0. leave page in memory. replace next page, subject to same rules. Operating System Concepts 10.24 Silberschatz, Galvin and Gagne 2002 Second-Chance (clock) Page-Replacement Algorithm Operating System Concepts 10.25 Silberschatz, Galvin and Gagne 2002 Enhanced Second Chance Keep a reference bit and a modified bit with each frame 0,0 => not recently used or modified => best to replace 0,1 => not recently used but modified => must be written out 1,0 => recently used but not modified => may be used again soon 1,1 => Recently used and modified => best keep this one in RAM Victim selection After each load, the current position is set to the frame after the loaded one. Scan from current position round for a 0,0 frame If not found, scan round for a 0,1 frame, reseting the use bit (ala 2nd chance) If not found, start again (a victim must be found) Used by Mac OS Operating System Concepts 10.26 Silberschatz, Galvin and Gagne 2002 Page Buffering Keep some frames free -a frame pool Immediately swap in on page fault Write out while first process uses CPU Remember what pages are in the frame pool, in case they are requested again - used by VMS with FIFO to recover from preemptive removal Write out modified pages whenever the swap disk is idle Operating System Concepts 10.27 Silberschatz, Galvin and Gagne 2002 Other Techniques Pre-paging - predicting page needs Windows NT clustering Copy-on-Write Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory. If either process modifies a shared page, only then is the page copied. COW allows more efficient process creation as only modified pages are copied. Operating System Concepts 10.28 Silberschatz, Galvin and Gagne 2002 Other Considerations Program structure int A[ ][ ] = new int[1024][1024]; Each row is stored in one page Program 1 for (j = 0; j < A.length; j++) for (i = 0; i < A.length; i++) A[i,j] = 0; 1024 x 1024 page faults Program 2 for (i = 0; i < A.length; i++) for (j = 0; j < A.length; j++) A[i,j] = 0; 1024 page faults Operating System Concepts 10.29 Silberschatz, Galvin and Gagne 2002 Windows NT Uses demand paging with clustering. Clustering brings in pages surrounding the faulting page. Processes are assigned working set minimum and working set maximum. Working set minimum is the minimum number of pages the process is guaranteed to have in memory. A process may be assigned as many pages up to its working set maximum. When the amount of free memory in the system falls below a threshold, automatic working set trimming is performed to restore the amount of free memory. Working set trimming removes pages from processes that have pages in excess of their working set minimum. On single x86 CPUS, uses a second chance style algorithm to select victims On Alpha and SMP, uses a modified FIFO Operating System Concepts 10.30 Silberschatz, Galvin and Gagne 2002 Solaris 2 Maintains a list of free pages to assign faulting processes. Lotsfree – threshold parameter to begin pageout. Pageout is called more frequently depending upon the amount of free memory available. Pageout scans pages using variation on the second chance algorithm (2 handed clock algorithm) Scanrate is the rate at which pages are scanned. This ranged from slowscan to fastscan. This increases as free memory decreases Handspread affects time process has to reuse a page before the big-hand send the page out Pages from shared libraries are kept in memory (recent variation) Operating System Concepts 10.31 Silberschatz, Galvin and Gagne 2002