CS 444CS 544 Operating Systems Virtual Memory 4062007

  • Slides: 13
Download presentation
CS 444/CS 544 Operating Systems Virtual Memory 4/06/2007 Prof. Searleman jets@clarkson. edu

CS 444/CS 544 Operating Systems Virtual Memory 4/06/2007 Prof. Searleman jets@clarkson. edu

Outline l l Returned & discussed HW#9 Replacement Strategies l l l optimal FIFO

Outline l l Returned & discussed HW#9 Replacement Strategies l l l optimal FIFO LRU NOTE: l Read: Chapters 9 & 10 l HW#10, due Wednesday, 4/11 l Exam#3: Tuesday, 4/17, 7: 00 pm, SC 160

recap: Page Faults Problem: must minimize the # of page faults. How? Prepaging? l

recap: Page Faults Problem: must minimize the # of page faults. How? Prepaging? l Anticipate a page fault before it happens and prefetch the data l Overlap fetch with computation l Can be hard to predict and if the prediction is wrong, you can evict something useful l Programmers can give hints l vm_advise

Avoiding Paging l l Given the cost of paging, we want to make it

Avoiding Paging l l Given the cost of paging, we want to make it as infrequent as we can Function of: l l Degree of locality in the application (size of the working set over time) Amount of physical memory Page replacement policy The OS can only control the replacement policy

Goals of Replacement Policy l Performance l l Best to evict a page that

Goals of Replacement Policy l Performance l l Best to evict a page that will never be accessed again if possible If not possible, evict page that won’t be used for the longest time How can we best predict this? Fairness l When OS divides up the available memory among processes, what is a fair way to do that? l Same amount to everyone? Well some processes may not need that amount for their working set while others are paging to disk constantly with that amount of memory Give each process its working set? As long as enough memory for each process to have its working set resident then everyone is happy l If not how do we resolve the conflict? l l

Page replacement algorithms l l l Remember all the different CPU scheduling algorithms the

Page replacement algorithms l l l Remember all the different CPU scheduling algorithms the OS could use to choose the next job to run Similarly, there are many different algorithms for picking which page to kick out when you have to bring in a new page and there is no free DRAM left Goal? l l l Reduce the overall system page fault rate? Balance page fault rates among processes? Minimize page faults for high priority jobs?

Belady’s Algorithm (Optimal) l l Evict the page that won’t be used again for

Belady’s Algorithm (Optimal) l l Evict the page that won’t be used again for the longest time Much like Shortest Job First! Has provably optimal lowest page fault rate Difficult to predict which page won’t be used for a while l Even if not practical can use it to compare other algorithms too

First-In-First-Out (FIFO) l Evict the page that was inserted the longest time ago l

First-In-First-Out (FIFO) l Evict the page that was inserted the longest time ago l l l When page moved into memory, put it on tail of list Evict head of list Is it always (usually) the case that the thing accessed the longest time ago will not be accessed for a long time? What about things accessed all the time! FIFO suffers an interesting anomaly (Belady’s Anomaly) l It is possible to increase the page fault rate by increasing the amount of available memory

Least-Recently Used (LRU) l Idea: the past is a good predictor of the future

Least-Recently Used (LRU) l Idea: the past is a good predictor of the future l l l Page that we haven’t used for the longest time likely not to be used again for longest time Is past a good predictor? l Generally yes l Can be exactly the wrong thing! Consider streaming access To do this requires keeping a history of past accesses l l To be exact LRU would need to save a timestamp on each access (I. e. write the PTE on each access!) Too expensive!

Optimal replacement look ahead to see what pages will be needed 1 * 2

Optimal replacement look ahead to see what pages will be needed 1 * 2 1 5 6 2 1 2 1 2 4 4 1 2 5 1 2 6 1 2 3 1 2 4 * * 3 1 * 4 1 * 2 3 7 6 6 page reference string: 1, 2, 3, 4, 2, 1, 5, 6, … three page frames * means a page fault occurred victim page is circled 3

FIFO replacement look at when the page was loaded 1 * 2 1 5

FIFO replacement look at when the page was loaded 1 * 2 1 5 6 2 1 2 2 4 1 3 3 4 1 5 6 2 3 4 2 3 * * * 3 1 * 4 1 * 2 3 7 6 5 page reference string: 1, 2, 3, 4, 2, 1, 5, 6, … three page frames * means a page fault occurred victim page is circled 3

LRU replacement look at when the page was last referenced 1 * 2 1

LRU replacement look at when the page was last referenced 1 * 2 1 5 6 2 1 2 2 4 2 3 1 5 2 1 5 6 3 4 2 3 * * * 3 1 * 4 1 * 2 3 7 6 2 page reference string: 1, 2, 3, 4, 2, 1, 5, 6, … three page frames * means a page fault occurred victim page is circled 3

Approximating LRU l Remember the reference bit in the PTE l l At some

Approximating LRU l Remember the reference bit in the PTE l l At some regular interval (much less often than for each access) clear all the reference bits l l Set if read or written Only PTE without the ref bit clear are eligible for eviction More than 1 bit of state? l l Associate some number of counter bits At regular interval, if ref bit is 0 increment counter and if ref bit is 1 then zero counter Counter tells you # intervals since the last reference More bits you give to counter = more accurate approximation