page replacement algorithms - computer scienceporter/courses/cse306/s16/slides/16.page... · page...

34
1 Page Replacement Algorithms

Upload: others

Post on 19-Oct-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

1

Page Replacement Algorithms

Page 2: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

2

Virtual Memory Management Fundamental issues : A Recap

Key concept: Demand paging Ø  Load pages into memory only when a

page fault occurs

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

Ø  Load control strategies ❖  Determining how many jobs can be

in memory at one time

Operating System

User Program 1

User Program 2User Program 2

User Program n

...

Memory

Page 3: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

3

Page Replacement Algorithms Concept

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!

Which page should be replaced? Local replacement — Replace a page of the faulting process Global replacement — Possibly replace the page of another process

Page 4: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

4

Page Replacement Algorithms Evaluation methodology

Record a trace of the pages accessed by a process Ø  Example: (Virtual page, offset) 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 TLB Ø  Set a used bit in the page table entry Ø  Increment or shift a register

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

fewer faults better performance

Page 5: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

5

Optimal Page Replacement Clairvoyant 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

Faults

Page

Fram

es

0123

abcd

1 2 3 4 5 6 7 8 9 100Requests

Time

Time pageneeded next

Initial allocation

Page 6: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

6

Optimal Page Replacement Clairvoyant 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 c

Faults • •

Page

Fram

es

d d d d e e e e e e

0123

abcd

1 2 3 4 5 6 7 8 9 100Requests

Time

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

Time pageneeded next

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

Page 7: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

7

Local Page Replacement FIFO replacement

Simple to implement Ø  A single pointer suffices

Performance with 4 page frames:

c a d b e b a b c d

Faults

Page

Fram

es

0123

abcd

1 2 3 4 5 6 7 8 9 100RequestsTime

PhysicalMemory1

2

0

Frame List

Page 8: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

8

Local Page Replacement FIFO replacement

Simple to implement Ø  A single pointer suffices

Performance with 4 page frames:

c a d b e b a b c da 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

1 2 3 4 5 6 7 8 9 100RequestsTime

PhysicalMemory0

2

3

Frame List

Page 9: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

9

Least Recently Used Page Replacement Use 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

Faults

Page

Fram

es

0123

abcd

1 2 3 4 5 6 7 8 9 100RequestsTime

Time pagelast used

Page 10: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

10

Least Recently Used Page Replacement Use 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 d

Faults • • •

Page

Fram

es

d d d d d d d d c c

0123

abcd

1 2 3 4 5 6 7 8 9 100RequestsTime

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

Time pagelast used

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

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

Page 11: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

11

Least Recently Used Page Replacement Implementation

Maintain a “stack” of recently used pages

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

1 2 3 4 5 6 7 8 9 100RequestsTime

LRUpage stack

Page to replace

Page 12: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

12

Least Recently Used Page Replacement Implementation

Maintain a “stack” of recently used pages

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

1 2 3 4 5 6 7 8 9 100RequestsTime

cca

cad

cadb

adbe

adeb

deba

deab

eabc

abcd

LRUpage stack

Page to replace c d e

Page 13: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

13

What is the goal of a page replacement algorithm? Ø A. Make life easier for OS implementer Ø B. Reduce the number of page faults Ø C. Reduce the penalty for page faults when they occur Ø D. Minimize CPU time of algorithm

Page 14: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

14

Approximate LRU Page Replacement The Clock algorithm

Maintain a circular list of pages resident in memory Ø  Use 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 = 0 Ø  Replace pages that haven’t been referenced for one complete revolution

of the clock

func Clock_Replacementbegin while (victim page not found) do if(used bit for current page = 0) then replace current page else reset used bit end if advance clock pointer end while end Clock_Replacement

resident bitused bit frame number

01Page 7: 1

50Page 1: 1 30Page 4: 1

41Page 0: 111Page 3: 1

Page 15: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

15

dcba

c

Clock Page Replacement Example

Faults

Page

Fram

es

0123

abcd

0Requests

Time

Page table entriesfor resident pages:

1

dcba

a2

dcba

d3

dcba

b4

e5

b6

a7

b8

c9

d10

1111

abcd

Page 16: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

16

dcba

c

Clock Page Replacement Example

Faults

Page

Fram

es

0123

abcd

0Requests

Time

Page table entriesfor resident pages:

1

dcba

a2

dcba

d3

dcba

b4

dcbe

e5

•dcbe

b6

dabe

a7

•dabe

b8

cabe

c9

•cabd

d10

1000

ebcd

1100

ebcd

1010

ebad

1110

ebad

1111

ebac

1000

dbac

1111

abcd

Page 17: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

17

Optimizing Approximate LRU Replacement The Second Chance algorithm

There is a significant cost to replacing “dirty” pages Ø  Why?

❖ Must write back contents to disk before freeing! 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

resident bitused bit dirty bit

01Page 7: 1

50Page 1: 1 30Page 4: 1

41Page 0: 191Page 3: 1

0

0

1

0

1

Before clocksweep

After clocksweep

used dirty

0011

0101

used dirty

000

001

replace page

Second Chance Algorithm

Page 18: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

18

dcba

c

The Second Chance Algorithm Example

Faults

Page

Fram

es

0123

abcd

0Requests

Time

Page table entries

for resident pages:

1

dcba

aw

2

dcba

d3

dcba

bw

4b6

aw

7b8

10101010

abcd

e5

c9

d10

Page 19: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

19

dcba

c

The Second Chance Algorithm Example

Faults

Page

Fram

es

0123

abcd

0Requests

Time

Page table entries for

resident pages:

1

dcba

aw

2

dcba

d3

dcba

bw

4

deba

b6

deba

aw

7

deba

b8

00001000

a*

b*

ed

00101000

abed

11101000

abed

11101010

abec

00100000

a*

dec

10101010

abcd

11111010

abcd

deba

e5

•ceba

c9

•ceda

d10

Page 20: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

20

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

Faults

Page

Fram

es

0123

abc

a b c d a b c d a b c d

Faults

Page

Fram

es 012

abc

1 2 3 4 5 6 7 8 9 10 11 120RequestsTime

Page 21: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

21

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

Faults

Page

Fram

es

0123

abc

a b c d a b c d a b c d

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

1 2 3 4 5 6 7 8 9 10 11 120RequestsTime

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 22: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

22

Page Replacement Algorithms Performance

Local page replacement Ø  LRU — Ages pages based on when they were last used Ø  FIFO — Ages pages based on when they’re brought into memory

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

The principle of locality

Ø  90% of the execution of a program is sequential Ø  Most iterative constructs consist of a relatively small number of

instructions Ø  When processing large data structures, the dominant cost is sequential

processing on individual structure elements Ø  Temporal vs. physical locality

Page 23: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

23

Optimal Page Replacement For 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

Faults

Page

sin

Mem

ory Page a

Page bPage cPage d

•--•

1 2 3 4 5 6 7 8 9 100RequestsTime

Page e -

t = 0

t = -1

Page 24: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

24

Optimal Page Replacement For 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 -- - - F - - - - - -F • • • • • • • - -

Faults • • • • •

Page

sin

Mem

ory

• • • - - - - - - F

Page aPage bPage cPage d

•--•

1 2 3 4 5 6 7 8 9 100RequestsTime

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

t = 0

t = -1

Page 25: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

25

Explicitly Using Locality The 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 occurs Ø  The 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 Ø  The working set model performs implicit load control

Page 26: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

26

Working Set Page Replacement Implementation

Keep track of the last τ references Ø  The 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

Faults

Page

sin

Mem

ory Page a

Page bPage cPage d

•--•

1 2 3 4 5 6 7 8 9 100RequestsTime

Page e •

t = 0

t = -1

t = -2

Page 27: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

27

Working Set Page Replacement Implementation

Keep track of the last τ references Ø  The 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 •- - - F • • • • - -F • • • • • • • • •

Faults • • • • •

Page

sin

Mem

ory

• • • • • • - - - F

Page aPage bPage cPage d

•--•

1 2 3 4 5 6 7 8 9 100RequestsTime

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

t = 0

t = -1

t = -2

Page 28: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

28

Page-Fault-Frequency Page Replacement An alternate working set computation

Explicitly attempt to minimize page faults Ø  When page fault frequency is high — increase working set Ø  When page fault frequency is low — decrease working set

Algorithm: Keep track of the rate at which faults occur

When a fault occurs, compute the time since the last page fault Record the time, tlast, of the last page fault

If the time between page faults is “large” then reduce the working set

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 set If tcurrent – tlast ≤ τ, then add faulting page to the working set

Page 29: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

29

Page-Fault-Frequency Page Replacement Example, window size = 2

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

tcur – tlast

c c d b c e c e a d

Faults

Page

sin

Mem

ory Page a

Page bPage cPage d

•--•

1 2 3 4 5 6 7 8 9 100RequestsTime

Page e •

Page 30: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

30

Page-Fault-Frequency Page Replacement Example, window size = 2

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

3tcur – tlast 2 3 1

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

Faults • • • • •

Page

sin

Mem

ory

• • • • • • • • - F

Page aPage bPage cPage d

•--•

1 2 3 4 5 6 7 8 9 100RequestsTime

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

1

Page 31: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

31

Load Control Fundamental tradeoff

High multiprogramming level

Issues Ø  What 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?

Low paging overhead Ø  MPLmin = 1 process

minimum number of frames required for a process to executenumber of page framesØ  MPLmax =

Page 32: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

32

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

◆  Assume memory is nearly full ◆  A chain of page faults occur

Ø  A queue of processes forms at the paging device

◆  CPU utilization falls Operating system increases MPL Ø  New processes fault, taking memory away from existing processes

CPU utilization goes to 0, the OS increases the MPL further...

System is thrashing — spending all of its time paging

I/O Device

...

Paging Device

CPU

Page 33: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

33

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

(PFST) Ø  Σ WSi = size of memory

1.0

CPUUtilization

Multiprogramming Level

Thrashing can be ameliorated by local page replacement

Load Control Thrashing

Nmax NI/O-BALANCE

MTBFPFST

1.0

Page 34: Page Replacement Algorithms - Computer Scienceporter/courses/cse306/s16/slides/16.Page... · Page Replacement Algorithms Concept Typically Σ i VAS i >> Physical Memory With demand

34

Load Control Thrashing

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

Suspended

suspendedqueue

readyqueue

semaphore/condition queues

Waiting

Running Ready

?

Paging Disk

PhysicalMemory

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