cs5460 operating systems lecture 16 page replacement ch 9
play

CS5460: Operating Systems Lecture 16: Page Replacement (Ch. 9) CS - PowerPoint PPT Presentation

CS5460: Operating Systems Lecture 16: Page Replacement (Ch. 9) CS 5460: Operating Systems Last Time: Demand Paging Key idea: RAM is used as a cache for disk Dont give a process a page of RAM until it is needed When running short


  1. CS5460: Operating Systems Lecture 16: Page Replacement (Ch. 9) CS 5460: Operating Systems

  2. Last Time: Demand Paging  Key idea: RAM is used as a cache for disk – Don’t give a process a page of RAM until it is needed – When running short on RAM, take pages away from processes – This only works if accesses to memory pages have high temporal locality » Why don’t we care about spatial locality?  Three basic kinds of page table entries – Valid mapping – the OS is not involved – translation performed entirely by the CPU – Invalid mapping – trap, then kernel does something special, such as kill the process – Valid but not present – trap and do demand paging  Demand paging makes the exec() system call fast CS 5460: Operating Systems

  3. Timeline of a Page Fault 1. Trap to operating system 7. (Optional) Schedule another 2. Save state in PCB process while waiting 3. Vector to page fault handler 8. Take disk interrupt 4. If invalid, send SIGSEGV 9. Update page table 5. If valid, find or create a free 10. Add process to run queue page 11. Wait for process to be a. Possibly involves disk write scheduled next 6. Issue disk read for page 12. Restore state from PCB a. Wait until request queued at disk controller 13. Return from OS b. Wait for seek/latency 14. Re-execute faulting c. Wait for data transfer (DMA) instruction d. Wait for completion interrupt CS 5460: Operating Systems

  4. Effective Access Times  What is average access latency? – L1 cache: 2 cycles – L2 cache: 10 cycles – Main memory: 150 cycles – Disk: 10 ms à à 30,000,000 cycles on 3.0 GHz processor – Assume access have following characteristics: » 98% handled by L1 cache » 1% handled by L2 cache » 0.99% handled by DRAM » 0.01% cause page fault – Average access latency: » (0.98 * 2) + (0.01 * 10) + (0.0099 * 150) + (0.0001 * 30,000,000) = 1.96 + 0.1 + 1.485 + 3000 = about 3000 cycles / access  Moral: Need LOW fault rates to sustain performance! CS 5460: Operating Systems

  5. Issues in Demand Paging  Page selection policy – When do we load a page?  Page replacement policy – What page(s) do we swap to disk to make room for new pages? – When do we swap pages out to disk?  How do we handle thrashing? CS 5460: Operating Systems

  6. Page Selection Policy  Demand paging: – Load page in response to access (page fault) – Predominant selection policy  Pre-paging (prefetching) – Predict what pages will be accessed in near future – Prefetch pages in advance of access – Problems: » Hard to predict accurately (trace cache) » Mispredictions can cause useful pages to be replaced  Overlays – Application controls when pages loaded/replaced – Only really relevant now for embedded/real-time systems CS 5460: Operating Systems

  7. Page Replacement Policies  Optimal – Throw out page used farthest in the future  Random – Works surprisingly well  FIFO (first in, first out) – Throw out oldest pages  LRU (least recently used) – Throw out page not used in longest time  NRU (not recently used) – Approximation to LRU à à do not throw out recently used pages  How should we evaluate page replacement policies? CS 5460: Operating Systems

  8. FIFO Page Replacement  FIFO: replace oldest page (first loaded)  Example: – Memory system with three pages à à all initially free – Reference string: A B C A B D A D B C B A B C A B D A D B C B • A • D Frame1 • C • √ Frame2 • B • A • √ • B Frame3 • C • √ • √ Result: 7 page faults CS 5460: Operating Systems

  9. Optimal Page Replacement  Optimal: replace page used farthest in the future  Example: – Memory system with three pages à à all initially free – Reference string: A B C A B D A D B C B A B C A B D A D B C B • A Frame1 • C • √ • √ Frame2 • B • √ • √ • √ • D Frame3 • C • C • √ Result: 5 page faults CS 5460: Operating Systems

  10. LRU Page Replacement  LRU: replace least recently used page  Example: – Memory system with three pages à à all initially free – Reference string: A B C A B D A D B C B A B C A B D A D B C B • A Frame1 • C • √ • √ Frame2 • B • √ • √ • √ • D Frame3 • C • √ Result: 5 page faults CS 5460: Operating Systems

  11.  How would you implement … – Random – FIFO – Optimal – LRU – NRU  Which ones are efficient? CS 5460: Operating Systems

  12. NRU Page Replacement  Observations – LRU is pretty good approximation of OPT » Past performance is often reasonable predictor of future performance » Captures “ phase ” behavior in many (but not all) applications – Implementing true LRU requires far too much overhead » Logically, need to update “ sort order ” on every memory access  How can we approximate LRU efficiently? – Exploit “ referenced ” bit in modern page tables – Only replace pages that have not been recently referenced (NRU) – Periodically clear referenced bits à à enforces “ recently ” » Optionally: Maintain recent history of referenced bits per-page » Example: 10010101 à à denotes times page referenced last 8 sweeps CS 5460: Operating Systems

  13. NRU Page Replacement  This is a modified version of FIFO  Checks if the page at the head of the FIFO queue has its referenced bit set – Yes? Then clear the bit and put it at the back of the queue and look at the next page – No? Then select this page  Is this fast? What is the worst case?  This is called the “second chance” algorithm CS 5460: Operating Systems

  14. Clock Algorithm  This is basically an 0 optimized version of second chance 1  Maintains “ next ” pointer 0 – Starts sweep there until done – Persists across invocations Next 0 1  While (need more pages) – Check referenced bit Next 0 Free! – If 0 à à add to free pool Next 0 1 – If 1 à à reset bit  Between sweeps Next 1 0 – If a process accesses page, referenced bit gets set Next 0 – TLB helps here! Free! Referenced CS 5460: Operating Systems

  15. BSD Page Replacement (NRU)  Goal: maintain pool of free pages at all times – Avoid waiting for replacement algorithm/write during page fault – Typical goal: ~5% of main memory in free page pool  Sweeper process – Privileged (kernel) process – Scheduled whenever free page pool drops below threshold » Low watermark (sweep) –vs- high watermark (goal) – Sweeps through list of allocated pages doing 2 nd chance CS 5460: Operating Systems

  16. Nth Chance  Like second chance but … – If page is referenced, clear its counter and move on – If page is not referenced, increment its counter » If new counter == N, select this page » Otherwise move on – If N is big, we have a really good LRU approximation » But we spend a lot of time looking for pages – If N == 1 we have second chance – If N == 0 we have FIFO  Lots more work exists on page replacement … CS 5460: Operating Systems

  17. Belady’s Anomaly  For some replacement algorithms – MORE pages in main memory can lead to … – MORE page faults!  This phenomenon is known as “ Belady’s Anomaly ”  Example: – FIFO replacement policy – Reference string: A B C D A B E A B C D E – Three pages à à 9 faults – Four pages à à 10 faults!  Interesting since we would expect that adding more memory always helps CS 5460: Operating Systems

  18. Thrashing  Working set: collection of memory currently being used by a process  If all working sets do not fit in memory à à thrashing – One “ hot ” page replaces another – Percentage of accesses that generate page faults skyrockets  Typical solution: “ swap out ” entire processes – Scheduler needs to get involved – Two-level scheduling policy à à runnable vs memory-available – Need to be fair – Invoked when page fault rate exceeds some bound  When swap devices are full, Linux invokes the “ OOM killer ” CS 5460: Operating Systems

  19.  Who should we compete against for memory?  Global replacement: – All pages for all processes come from single shared pool – Advantage: very flexible à à can globally “ optimize ” memory usage – Disadvantages: Thrashing more likely, can often do just the wrong thing (e.g., replace the pages of a process about to be scheduled) – Many OSes, including Linux, do this  Per-process replacement: – Each process has private pool of pages à à competes with itself – Alleviates inter-process problems, but not every process equal – Need to know working set size for each process – Windows kernel does this » There are Win32 API calls to set a process’s minimum and maximum working set sizes CS 5460: Operating Systems

  20. Important From Today  Demand paging – What is it? What is the “ effective access time ” ?  Page replacement policies – Random, FIFO, Optimal, LRU, NRU, … – Belady ’ s anomaly  Thrashing  Global vs local allocation – Concept of a process’s “working set” CS 5460: Operating Systems

Recommend


More recommend