16.pagereplacementalgos

17
Page Replacement Algorithms 1 Virtual Memory Management Fundamental issues : A Recap Key concept: Demand paging ¾ Load pages into memory only when a page fault occurs User Program n . Issues: ¾ Placement strategies Place pages anywhere – no placement policy required ¾ Replacement strategies What to do when there exist more jobs than can fit in memory User Program 1 User Program 2 ... 2 ¾ Load control strategies Determining how many jobs can be in memory at one time Operating System Operating System Memory

Upload: saichandrasekhard

Post on 30-Dec-2015

4 views

Category:

Documents


0 download

DESCRIPTION

dfasdssd

TRANSCRIPT

Page 1: 16.PageReplacementAlgos

Page Replacement Algorithms

1

Virtual Memory ManagementFundamental issues : A Recap

Key concept: Demand paging Load pages into memory only when a page fault occurs User Program nUser Program n

.

Issues:Placement strategies

Place pages anywhere – no placement policy required

Replacement strategiesWhat to do when there exist more jobs than can fit in memory

User Program 1User Program 1

User Program 2User Program 2User Program 2User Program 2

...

2

a ca e o y

Load control strategiesDetermining how many jobs can be in memory at one time

Operating SystemOperating System

Memory

Page 2: 16.PageReplacementAlgos

Page Replacement AlgorithmsConcept

Typically Σi VASi >> Physical Memory

With demand paging, physical memory fills quickly

When a process faults & memory is full, some page must be swapped out

Handling a page fault now requires 2 disk accesses not 1!Though writes are more efficient than reads (why?)

Which page should be replaced?Local replacement — Replace a page of the faulting processGl b l l P ibl l h f h

3

p p p g g pGlobal replacement — Possibly replace the page of another process

Page Replacement AlgorithmsEvaluation methodology

Record a trace of the pages accessed by a processExample: (Virtual) address trace...

(3,0), (1,9), (4,1), (2,1), (5,3), (2,0), (1,9), (2,4), (3,1), (4,8)generates page trace

3, 1, 4, 2, 5, 2, 1, 2, 3, 4 (represented as c, a, d, b, e, b, a, b, c, d)

Hardware can tell OS when a new page is loaded into the TLBSet a used bit in the page table entryIncrement or shift a register

Simulate the behavior of a page replacement algorithm on the trace and record the number of page faults generated

4

trace and record the number of page faults generatedfewer faults better performance

Page 3: 16.PageReplacementAlgos

Optimal Page ReplacementClairvoyant replacement

Replace the page that won’t be needed for the longest time in the future

c a d b e b a b c d

Page

Fram

es

012

abc

1 2 3 4 5 6 7 8 9 100Requests

Time

5

Faults

F 23

cd

Time pageneeded next

Optimal Page ReplacementClairvoyant replacement

Replace the page that won’t be needed for the longest time in the future

c a d b e b a b c d

a a a a a a a a a db b b b b b b b b bc c c c c c c c c cPa

geFr

ames

012

abc

1 2 3 4 5 6 7 8 9 100Requests

Time

6

c c c c c c c c c c

Faults • •

F

d d d d e e e e e e23

cd

a = 7b = 6c = 9d = 10

Time pageneeded next

a = 15b = 11c = 13d = 14

Page 4: 16.PageReplacementAlgos

Local Page ReplacementFIFO replacement

Simple to implementA single pointer suffices

PhysicalMemory0

2

3

Performance with 4 page frames:

c a d b e b a b c d1 2 3 4 5 6 7 8 9 100

RequestsTime

Frame List

7

Faults

Page

Fram

es

0123

abcd

Local Page ReplacementFIFO replacement

Simple to implementA single pointer suffices

PhysicalMemory0

2

3

Performance with 4 page frames:

c a d b e b a b c d1 2 3 4 5 6 7 8 9 100

RequestsTime

Frame List

8

a a a a e e e e e db b b b b b a a a ac c c c c c c b b b

Faults • • • • •

Page

Fram

es

d d d d d d d d c c

0123

abcd

Page 5: 16.PageReplacementAlgos

Least Recently Used Page ReplacementUse the recent past as a predictor of the near future

Replace the page that hasn’t been referenced for the longest time

c a d b e b a b c d

Page

Fram

es

012

abc

1 2 3 4 5 6 7 8 9 100RequestsTime

9

Faults

F

3 d

Time pagelast used

Least Recently Used Page ReplacementUse the recent past as a predictor of the near future

Replace the page that hasn’t been referenced for the longest time

c a d b e b a b c da a a a a a a a a ab b b b b b b b b bc c c c e e e e e dPa

geFr

ames

012

abc

1 2 3 4 5 6 7 8 9 100RequestsTime

10

Faults • • •

F

d d d d d d d d c c3 d

a = 2b = 4c = 1d = 3

Time pagelast used

a = 7b = 8e = 5d = 3

a = 7b = 8e = 5c = 9

Page 6: 16.PageReplacementAlgos

Least Recently Used Page ReplacementImplementation

Maintain a “stack” of recently used pages

c a d b e b a b c d1 2 3 4 5 6 7 8 9 100

R tTime

c a d b e b a b c d

a a a a a a a a a ab b b b b b b b b bc c c c e e e e e d

Faults • • •

Page

Fram

es

d d d d d d d d c c

0123

abcd

Requests

11

cca

cad

cadb

adbe

adeb

deba

deab

eabc

abcd

LRUpage stack

Page to replace c d e

Least Recently Used Page ReplacementImplementation

Maintain a “stack” of recently used pages

c a d b e b a b c d1 2 3 4 5 6 7 8 9 100

R tTime

c a d b e b a b c d

a a a a a a a a a ab b b b b b b b b bc c c c e e e e e d

Faults • • •

Page

Fram

es

d d d d d d d d c c

0123

abcd

Requests

12

cca

cad

cadb

adbe

adeb

deba

deab

eabc

abcd

LRUpage stack

Page to replace c d e

Page 7: 16.PageReplacementAlgos

What is the goal of a page replacement algorithm?A. Make life easier for OS implementerB R d th b f f ltB. Reduce the number of page faultsC. Reduce the penalty for page faults when they occurD. Minimize CPU time of algorithm

13

Approximate LRU Page ReplacementThe Clock algorithm

Maintain a circular list of pages resident in memoryUse a clock (or used/referenced) bit to track how often a page is accessed The bit is set whenever a page is referenced

Clock hand sweeps over pages looking for one with used bit = 0p p g gReplace pages that haven’t been referenced for one complete revolution of the clock

func Clock_Replacementbeginwhile (victim page not found) do

if(used bit for current page = 0) then

01Page 7: 1

50Page 1: 1 30Page 4: 1

14

replace current pageelse

reset used bitend ifadvance clock pointer

end whileend Clock_Replacement

resident bitused bit frame number

50Page 1: 1 30Page 4: 1

41Page 0: 111Page 3: 1

Page 8: 16.PageReplacementAlgos

c

Clock Page ReplacementExample

0Requests

Time 1a2

d3

b4

e5

b6

a7

b8

c9

d10

dcba

Faults

Page

Fram

es

0123

abcd

q

dcba

dcba

dcba

15

Faults

Page table entriesfor resident pages:

1000

ebcd

1100

ebcd

1110

ebad

1110

ebad

1111

ebac

1000

dbac

1111

abcd

c

Clock Page ReplacementExample

0Requests

Time 1a2

d3

b4

e5

b6

a7

b8

c9

d10

dcba

Faults

Page

Fram

es

0123

abcd

q

dcba

dcba

dcba

dcbe

•dcbe

dabe

•dabe

cabe

•cabd

16

Faults

Page table entriesfor resident pages:

1000

ebcd

1100

ebcd

1010

ebad

1110

ebad

1111

ebac

1000

dbac

1111

abcd

Page 9: 16.PageReplacementAlgos

Optimizing Approximate LRU ReplacementThe Second Chance algorithm

There is a significant cost to replacing “dirty” pages

Modify the Clock algorithm to allow dirty pages to always survive one sweep of the clock hand

Use both the dirty bit and the used bit to drive replacement

01Page 7: 1

50Page 1: 1 30Page 4: 1

0

00

Before clocksweep

After clocksweep

Second Chance Algorithm

17

resident bitused bit dirty bit

50Page 1: 1 30Page 4: 1

41Page 0: 191Page 3: 1

0

1

0

1

used dirty

0011

0101

used dirty

000

001

replace page

c

The Second Chance AlgorithmExample

0Requests

Time 1aw

2d3

bw

4b6

aw

7b8

e5

c9

d10

dcba

Faults

Page

Fram

es

0123

abcd

q

dcba

dcba

dcba

18

Faults

Page table entries

for resident pages:

00001000

a*

b*

ed

00101000

a*

b*

ed

11101000

ab*

ed

11101010

ab*

ec

00100000

a*

dec

10101010

abcd

11111010

abcd

Page 10: 16.PageReplacementAlgos

c

The Second Chance AlgorithmExample

0Requests

Time 1aw

2d3

bw

4b6

aw

7b8

e5

c9

d10

dcba

Faults

Page

Fram

es

0123

abcd

q

dcba

dcba

dcba

deba

deba

deba

deba

•ceba

•ceda

19

Faults

Page table entries for

resident pages:

00001000

a*

b*

ed

00101000

abed

11101000

abed

11101010

abec

00100000

a*

dec

10101010

abcd

11111010

abcd

The Problem With Local Page ReplacementHow much memory do we allocate to a process?

a b c d a b c d a b c d1 2 3 4 5 6 7 8 9 10 11 120

RequestsTime

Faults

Page

Fram

es 012

abc

20Faults

Page

Fram

es

0123

abc–

Page 11: 16.PageReplacementAlgos

The Problem With Local Page ReplacementHow much memory do we allocate to a process?

a b c d a b c d a b c d1 2 3 4 5 6 7 8 9 10 11 120

RequestsTime

a a a d d d c c c b b bb b b b a a a d d d c cc c c c c b b b a a a d

Faults • • • • • • • • •

Page

Fram

es 012

abc

21Faults

Page

Fram

es

0123

abc–

a a a a a a a a a a a ab b b b b b b b b b b bc c c c c c c c c c c c

d d d d d d d d d

Page Replacement AlgorithmsPerformance

Local page replacementLRU — Ages pages based on when they were last usedFIFO — Ages pages based on when they’re brought into memory

Towards global page replacement with variable number ofTowards global page replacement ... with variable number of page frames allocated to processes

The principle of locality

90% of the execution of a program is sequentialMost iterative constructs consist of a relatively small number of

22

Most iterative constructs consist of a relatively small number of instructionsWhen processing large data structures, the dominant cost is sequential processing on individual structure elementsTemporal vs. physical locality

Page 12: 16.PageReplacementAlgos

Optimal Page ReplacementFor processes with a variable number of frames

VMIN — Replace a page that is not referenced in the next τaccesses

Example: τ = 4

c c d b c e c e a d

s ory Page a

P b•

1 2 3 4 5 6 7 8 9 100RequestsTime

t = 0

23

Faults

Page

sin

Mem

o Page bPage cPage d

--•

Page e -t = -1

Optimal Page ReplacementFor processes with a variable number of frames

VMIN — Replace a page that is not referenced in the next τaccesses

Example: τ = 4

c c d b c e c e a d- - - - - - - - F -

Fs ory Page a

P b•

1 2 3 4 5 6 7 8 9 100RequestsTime

t = 0

24

- - - F - - - - - -F • • • • • • • - -

Faults • • • • •

Page

sin

Mem

o

• • • - - - - - - F

Page bPage cPage d

--•

- - - - - F • • - -Page e -t = -1

Page 13: 16.PageReplacementAlgos

Explicitly Using LocalityThe working set model of page replacement

Assume recently referenced pages are likely to be referenced again soon…

... and only keep those pages recently referenced in memory (called the working set)

Thus pages may be removed even when no page fault occursThe number of frames allocated to a process will vary over time

A process is allowed to execute only if its working set fits into memory

Th ki t d l f i li it l d t l

25

The working set model performs implicit load control

Working Set Page ReplacementImplementation

Keep track of the last τ referencesThe pages referenced during the last τ memory accesses are the working setτ is called the window size

Example: Working set computation, τ = 4 references:

c c d b c e c e a d

y Page a •

1 2 3 4 5 6 7 8 9 100RequestsTime

0

26

Faults

Page

sin

Mem

ory Page aPage bPage cPage d

--•

Page e •

t = 0

t = -1

t = -2

Page 14: 16.PageReplacementAlgos

Working Set Page ReplacementImplementation

Keep track of the last τ referencesThe pages referenced during the last τ memory accesses are the working setτ is called the window size

Example: Working set computation, τ = 4 references:What if τ is too small? too large?

c c d b c e c e a d• • • - - - - - F •y Page a •

1 2 3 4 5 6 7 8 9 100RequestsTime

0

27

F- - - F • • • • - -F • • • • • • • • •

Faults • • • • •

Page

sin

Mem

ory

• • • • • - - - - F

Page aPage bPage cPage d

--•

• - - - - F • • • •Page e •

t = 0

t = -1

t = -2

Page-Fault-Frequency Page ReplacementAn alternate working set computation

Explicitly attempt to minimize page faultsWhen page fault frequency is high — increase working setWhen page fault frequency is low — decrease working set

Algorithm: Keep track of the rate at which faults occurWhen a fault occurs, compute the time since the last page fault

Record the time, tlast, of the last page faultIf the time between page faults is “large” then reduce the working set

If t nt – tl st > τ then remove from memory all pages not

28

If tcurrent tlast > τ, then remove from memory all pages not referenced in [tlast, tcurrent ]

If the time between page faults is “small” then increase working setIf tcurrent – tlast ≤ τ, then add faulting page to the working set

Page 15: 16.PageReplacementAlgos

Page-Fault-Frequency Page ReplacementExample, window size = 2

If tcurrent – tlast > 2, remove pages not referenced in [tlast, tcurrent ] from the working setIf tcurrent – tlast ≤ 2, just add faulting page to the working set

c c d b c e c e a d

ages

Mem

ory Page a

Page bPage c

•--

1 2 3 4 5 6 7 8 9 100RequestsTime

29

tcur – tlast

Faults

Pain

M

gPage d •Page e •

Page-Fault-Frequency Page ReplacementExample, window size = 2

If tcurrent – tlast > 2, remove pages not referenced in [tlast, tcurrent ] from the working setIf tcurrent – tlast ≤ 2, just add faulting page to the working set

c c d b c e c e a d• • • - - - - - F •- - - F • • • • - -F • • • • • • • • •ag

esM

emor

y Page aPage bPage c

•--

1 2 3 4 5 6 7 8 9 100RequestsTime

30

3tcur – tlast 2 3 1

Faults • • • • •

Pain

M • • • • • • • • - Fg

Page d •• • • - - F • • • •Page e •

1

Page 16: 16.PageReplacementAlgos

Load Control Fundamental tradeoff

High multiprogramming level

number of page framesMPL =

Issues

Low paging overheadMPLmin = 1 process

minimum number of frames required for a process to execute

f p g fMPLmax =

31

IssuesWhat criterion should be used to determine when to increase or decrease the MPL?Which task should be swapped out if the MPL must be reduced?

Load Control How not to do it: Base load control on CPU utilization

Assume memory is nearly full

I/ODevice

...CPU

A chain of page faults occurA queue of processes forms at the paging device

CPU utilization fallsOperating system increases MPL

New processes fault, taking memory away from existing processesCPU utilization goes to 0, the OS increases the MPL further...

PagingPagingDevice

32

System is thrashing — spending all of its time paging

Page 17: 16.PageReplacementAlgos

Better criteria for load control: Adjust MPL so that:mean time between page faults (MTBF) = page fault service time (PFST)

Thrashing can be ameliorated by local page replacement

Load Control Thrashing

(PFST)Σ WSi = size of memory

1.0

CPUUtilization

MTBFPFST

1.0

33

Multiprogramming LevelNmax NI/O-BALANCE

Load Control Thrashing

ready Waiting

RunningReady

?

PhysicalMemory

When the multiprogramming level should be decreased, which process should be swapped out?

Suspended

suspendedqueue

readyqueue

semaphore/condition queues

Waiting?

34

out?

Paging Disk

Lowest priority process?Smallest process?Largest process?Oldest process?Faulting process?