4.4 page replacement algorithms. page replacement algorithms also seen in: cpu cache web server...

28
4.4 Page replacement algorithms

Upload: jazmyne-endicott

Post on 31-Mar-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

4.4 Page replacement algorithms

Page 2: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Page replacement algorithms

Also seen in:CPU cacheWeb server cache of web pagesBuffered I/O (file) caches

Page 3: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Optimal page replacement

1. Page fault occurs2. Scan all pages currently in memory3. Determine which page won’t be needed

(referenced) until furthest in the future4. Replace that page

Not really possible. (But useful as a benchmark.)

Depends on code as well as data.

Page 4: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Algorithms we will discuss:

1. Optimal2. NRU3. FIFO4. Second chance5. Clock6. LRU7. NFU8. Aging9. Working set10. WSClock

Page 5: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

NRU (not recently used)

Bits set by hardware after every memory reference

Cleared only by software (OS) R bits – set when page is referenced (read

or write) M bits – set when page is modified (written) Periodically (after k clock interrupts), R bits

are cleared

Page 6: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

NRU page categories

1. Not referenced, not modified

2. Not referenced, modified Occurs when #4’s R bit is cleared during

clock interrupt

3. Referenced, not modified

4. Referenced, modified

NRU algorithm: remove random page from lowest numbered non empty class

Page 7: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

NRU algorithm evaluation

Simple Efficient Not optimal but adequate

Page 8: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

FIFO page replacement

Queue pages as they are requested. Remove page at head (front) of

queue.Oldest page is removed first

+ simple/efficient

- might remove a heavily used page

Page 9: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Second chance page replacement Inspect R bit of oldest page

If R==0 then• page is old & unused• Replace it

Else• Clear R bit• Move page to from head to tail of FIFO

• Treating it as a newly loaded page

• Try another page

Page 10: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Second chance page replacement load time

page

Page 11: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Clock page replacement

Circular list instead of queue Clock hand points to oldest page If (R==0) then

Page is unusedReplace it

ElseClear RAdvance clock hand

Page 12: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Clock page replacement

Page 13: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

LRU (least recently used) page replacement Page recently used is likely to be used in

the near future; page not used in ages is not likely to be used in the near future.

Algorithm: “age” the pages

• Maintain a queue of pages in memory.• Recently used at front; oldest at rear.

• Every time a page is referenced, it is removed from the queue and placed at the front of the queue.

• This is slow!

Page 14: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

LRU in hardware

implementation #1:64 bit counter, C, incremented after

every instructionEach page also has a 64 bit counterWhen a page is referenced, C is

copied to its counter.Page with lowest counter is oldest.

Page 15: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

LRU in hardware

implementation #2:Given n page frames, let M be a nxn

matrix of bits initially all 0.Reference to page frame k occurs.Set all bits in row k of M to 1.Set all bits in column k of M to 0.Row with lowest binary value is least

recently used.

Page 16: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

LRU in hardware: implementation #2 example

oldest

Page 17: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

NFU (Not Frequently Used)

Hardware doesn’t often support LRU Software counter associated w/ each

page initially set to 0. At each clock interrupt:

Add R bit (either 0 or 1) to the counter for each page.

Page with lowest counter is NFU

Page 18: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

NFU problem

It never forgets! So pages that were frequently referenced

(during initialization for example) but are no longer needed appear to be FU.

Solution (called “aging”): Shift all counters to right 1 bit before R bit is

added in. Then R bit is added to MSb (leftmost bit)

instead of LSb (rightmost bit). Page w/ lowest value is chosen for removal.

Page 19: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

NFU w/ aging

1. Shift to right

2. MSb = R

00010000

Page 20: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Differences between LRU and NFU LRU updated after every instruction so it’s

resolution is very fine. NFU is coarse (updated after n instructions

execute between clock interrupts). A given page referenced by n-1 instruction is

given equal weight to a page referenced by only 1 instruction (between clock interrupts).

n/2 references to a given page at the beginning of the interval are given equal weight with n/2 references to another page at the end of the interval.

Page 21: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Working set page replacement algorithm Demand paging = start up processes with 0

pages and only load what’s needed. Locality of reference = during any phase of

execution, the process references only a relatively small fraction of its pages.

Working set = set of pages that a process is currently using.

Thrashing = causing a page fault every few instructions.

Page 22: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Working sets

Working set model = make sure a page is in memory before the process needs it.a.k.a. prepaging

w.s. = set of pages used in the k most recent memory references.

Page 23: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Working set algorithm

Uses current virtual time = amount of CPU time a process has actually used since it started.

T is a threshold on CVT R and M bits as before; clock interrupt

Page 24: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Working set algorithm

(greatest age/least virtual time) and choose that one if no better candidate exists.

age = current virtual time – time of last use

If no suitable candidate exists, pick one at random.

Page 25: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

WSClock page replacement

Previous WS algorithm requires entire page table be scanned at each page fault.

WSClock:Simple, efficient, widely used.Uses circular list of page frames.

Page 26: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

WSClock page replacementAt each page fault…

Loop once through page table:Examine PTE pointed to by clock hand.If r bit == 1 then

clear r bit; advance clock hand; goto loopelse

If age>tIf page is clean then use this page!Else write dirty page to disk; advance clock hand; goto loop

If write scheduled, wait for completion and used that page.Else pick a victim at random.

Page 27: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

WSClock page replacement

clear r bit and advance clock

hand.

Replace old and advance.

Page 28: 4.4 Page replacement algorithms. Page replacement algorithms Also seen in: CPU cache Web server cache of web pages Buffered I/O (file) caches

Summary of page replacement algorithms