
On demand paging On-demand Paging Physical page field flags Number of physical page if P=1; address on R W U M P disk of the page if P=0 Page descriptor The page table contains a page descriptor for each page Beyond the information for translation of the address, the descriptor alco contains a number of flags: • R, W: read/write access rights • M, U: modified/use bits (for the page replacement algorithms) • P: presence bit P = 1: page in main memory P = 0: page not in main memory page-fault Page fault management b) page-fault management Page table of PE Swap-area CPU x = pg ,of 1 I pg pg pf 1 6 4 Page table of PE 5 Core map pg I 0 2 page-fault pf pagina pg di PE pf Physical pages table (Core Map) 7 pf libera CPU x = pg, of 8 3 Main (physical) memory c) After page-fault a) At page-fault On-demand Paging 1. TLB miss 8. Disk interrupt when 2. Page table walk DMA complete 3. Page fault (page invalid 9. Mark page as valid in page table) 4. Trap to kernel 10. Resume process at 5. Convert address to file + faulting instruction offset 11. TLB miss 6. Allocate page frame 12. Page table walk to – Evict page if needed fetch translation 7. Initiate disk block read into page frame 13. Execute instruction Page replacement 3 Page table of PE’ Swap-area Page table of PE’ Disk Pg’ I’ pg’ pf 1 Pg’ I’ 0 4 2 Main memory Core map Core map pf pagina pg’ di PE’ pf pagina pg’ di PE’ pf libera 5 1 a) Page replacement algorithm selects b) After page replacement (PE’, pg’) Note: save of pg’ on disk not necessary if pg’ not modified Allocating a Page Frame • Select old page to evict • Find all page table entries that refer to old page – If page frame is shared • Set each page table entry to invalid • Remove any TLB entries – Copies of now invalid page table entry • Write changes to page to disk, if necessary – i.e. if the page had been modified How do we know if page has been modified? • Every page table entry has some bookkeeping – Has page been modified? • Set by hardware on store instruction to page • In both TLB and page table entry – Has page been used? • Set by hardware on load or store instruction to page • In page table entry on a TLB miss • Can be reset by the OS kernel – When changes to page are flushed to disk – To track whether page is recently used Emulating a Modified Bit • Some processor architectures do not keep a modified bit in the page table entry – Extra bookkeeping and complexity • OS can emulate a modified bit: – Set all clean pages as read-only – On first write, take page fault to kernel – Kernel sets modified bit, marks page as read-write Emulating a Use Bit • Some processor architectures do not keep a use bit in the page table entry – Extra bookkeeping and complexity • OS can emulate a use bit: – Set all unused pages as invalid – On first read/write, take page fault to kernel – Kernel sets use bit, marks page as read or read/write Caching and Virtual Memory Main Points • Cache concept • Cache Replacement Policies – FIFO, MIN, LRU, LFU, Clock • Memory-mapped files • Demand-paged virtual memory Definitions • Cache – Copy of data that is faster to access than the original – Hit: if cache has copy – Miss: if cache does not have copy • Cache block – Unit of cache storage (multiple memory locations) • Temporal locality – Programs tend to reference the same memory locations multiple times in a given period of time – Example: instructions in a loop • Spatial locality – Programs tend to reference nearby locations – Example: data in a loop Working Set Model • Working Set: set of memory locations that need to be cached for reasonable cache hit rate • Thrashing: when system has too small a cache Phase Change Behavior • Programs can change their working set • Context switches also change working set Cache Replacement Policy • On a cache miss, how do we choose which entry to replace? – Assuming the new entry is more likely to be used in the near future • Policy goal: reduce cache misses – Improve expected case performance – Also: reduce likelihood of very poor performance Page replacement policies A Simple Policy • Random? – Replace a random page • FIFO? – Replace the page that has been in the cache (main memory) the longest time – What could go wrong? FIFO in Action Worst case for FIFO is if program strides through memory that is larger than the main memory MIN, LRU, LFU • MIN (ideal, optimal) – Replace the page that will not be used for the longest time into the future – Optimality proof based on exchange: if evict a page used sooner, that will trigger an earlier page fault (=cache miss) • Least Recently Used (LRU) – Replace the page that has not been used for the longest time in the past – Approximation of MIN • Not Recently used (NRU) – Replace one of the pages that have not been used recently – Relax the requirements of LRU – Approximation of LRU, easier to implement – Examples: second chance, working set algorithms LRU/MIN for Sequential Scan Clock Algorithm: estimating LRU • Periodically, sweep through all pages • If page is unused, reclaim • If page is used, mark as unused Page replacement “second-chance” (clock algorithm) pagina 0 pagina 0 pagina 7 0 pagina 1 pagina 7 0 pagina 1 1 1 1 10 pagina 6 pagina 2 pagina 6 pagina 2 0 1 0 10 1 1 1 10 pagina 5 0 pagina 3 pagina 5 0 pagina 3 pagina 4 pagina 4 vittima a) all’inizio dell’algoritmo b) Come procede l’algoritmo Nth Chance: generalization of 2nd chance • Periodically, sweep through all page frames • If page hasn’t been used in any of the past N sweeps, reclaim • If page is used, mark as unused and set as active in current sweep Local and global page replacement • Global algorithms: – The page selected for removal is selected among all pages in main memory – Irrespective of the owner – “past distance” of a pages defined based on a global time (absolute clock) – May result in trashing of slow processes • Local algorithms – The page selected for removal belongs to the process that caused the page fault – Fair with “slow” processes about trashing – Past distance of a page based on relative time • The time a process has spent in running state Local vs global page replacement a) T b) T c) T A0 10 A0 10 A0 10 T: time of last A1 7 A1 7 A1 7 reference A2 5 A2 5 A2 5 B0 9 B0 9 B0 9 B1 6 B1 6 B1 6 C0 12 C0 12 C0 12 C1 4 C1 4 C1 4 C2 3 C2 3 C2 3 a) Initial configuration b) Page replacement with a local polity (WS, LRU, sec. chance) c) Page replacement with a global policy (LRU, sec. Chance) Working set algorithm • Keep in memory the working set of a process: – the pages that the process is currently using • Working set defined as: – The set of pages referred in the last k memory accesses • difficult to implement – The set of pages referred in the last period T • usually implemented in this way, using the «use» bit Working set w(t) t • working set: The set of pages referred in the last k memory accesses • w(t) is the size of the working set as function of time Working set algorithm • Each process has a number of physical pages reserved to upload its working set – WS replacement policy is inherently local • Resident set: – is the actual set of virtual pages in main memory – some of them may be out of the working set – working set Working set algorithm • WS defined as the set of pages referred in the last period P – P is a parameter of the algorithm • For each page: – R bit (called “referred” or “use” bit) indicates whether the page had been referred in the last time tick – Keep an approximation of the time of last reference to the page • At the end of each time tick resets bit R for each page and updates the approximation of time of last reference – Age of a page defined as the difference between current time and time of last reference • At page fault: – For each page checks bit R and time of last reference • If R=1: set last reference time to current time and resets R – The pages referred in the last period P are in the working set and (if possible) are not removed Working set algorithm Current virtual time: 2204 Page table Time of last reference Bit R … For each page: { 2084 1 if (R==1) 2003 0 time of last reference = current virtual time; R=0 1980 1 else if (R==0) && (age>P) 1213 0 removes the page 2014 1 } ------------------------------------ 2020 1 1604 0 if (age<=P for each page) removes the page with smaller time of last reference Age: current virtual time – time of last reference WSClock (working set clock) Current virtual time : 2204 • Considers only the pages in main memory 1620 0 – More efficient than scanning the page table 2084 1 2032 1 • Pages in a circular list 2003 1 2020 1 • At page fault looks for a page out of the WS 1980 1 2014 1 – Better if not “dirty” – If selects a dirty page, the page is saved before its 1213 0 actual removal WSClock (working set clock) Current virtual time : 2204 • Considers only the pages in main memory 1620 0 – More efficient than scanning the page table 2084 1 2032 1 • Pages in a circular list 2003 1 2020 1 • At page fault looks for a page out of the WS 1980 1 2014 1 – Better if not “dirty” – If selects a dirty page, the page is saved before its 1213 0 actual removal Removes this page WSClock (working set + clock) Current virtual time : 2204 • Considers only the pages in main memory 1620 0 – More efficient than scanning the page table 2084 1 2032 1 • Pages in a circular list 2003 1 2020 1 • At page fault looks for a page out of the WS 1980 1 2204 0 – Better if not “dirty” – If selects a dirty page, the page is saved before its 2204 1 actual removal New page Working set algorithm • In practice, WS and all page replacement algorithms are executed
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages66 Page
-
File Size-