310 likes | 499 Views
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
E N D
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
Sum logical address space can therefore be much larger than physical address space. More processes Page Table When Some Pages Are Not in Main Memory Operating System Concepts
Page Faults • Bringing a Page into Memory • Get empty frame. • Swap page into frame. • Reset tables, validation bit = 1. • Restart instruction Operating System Concepts
Need For Page Replacement Operating System Concepts
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
Basic Page Replacement • Find the location of the desired page on disk. • Find a free frame: • If there is a free frame, use it. • If there is no free frame, use a page replacement algorithm to select a victim frame. • If victim is dirty, write it out to disk • Read the desired page into the (newly) free frame. Update the page and frame tables. • Restart the process Operating System Concepts
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
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
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
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
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. Operating System Concepts
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
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
Initial Load • Load pages predicted to be needed (compiler flags) or • Load no pages • Allows for more efficient process creation Operating System Concepts
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
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
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
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
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 FIFO Illustrating Belady’s Anamoly Operating System Concepts
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
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 (?) LRU Page Replacement Operating System Concepts
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
Use Of A Stack to Record The Most Recent Page References Operating System Concepts
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
Second-Chance (clock) Page-Replacement Algorithm Operating System Concepts
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
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
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
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
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
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