Checkin with one Page Replacement condition variable Local vs - - PowerPoint PPT Presentation

checkin with one page replacement condition variable
SMART_READER_LITE
LIVE PREVIEW

Checkin with one Page Replacement condition variable Local vs - - PowerPoint PPT Presentation

Checkin with one Page Replacement condition variable Local vs Global replacement self.allCheckedIn = Condition(self.lock) What s Local: victim chosen from frames of process wrong def checkin(): experiencing page fault with self.lock: fixed


slide-1
SLIDE 1

Page Replacement

Local vs Global replacement

Local: victim chosen from frames of process experiencing page fault

fixed allocation per process

Global: victim chosen from frames allocated to any process

variable allocation per process

Many replacement policies

Random, FIFO, LRU, Clock, Working set, etc.

Goal: minimizing number of page faults

80

Checkin with one condition variable

81

self.allCheckedIn = Condition(self.lock) def checkin(): with self.lock: nArrived++ if nArrived < nThreads: while nArrived < nThreads: allCheckedIn.wait() else: allCheckedIn.broadcast() nArrived = 0

What’s wrong with this?

Checkin: 2 condition variables

def checkin(): nArrived++ if nArrived < nThreads: / / not everyone has checked in while nArrived < nThreads: allCheckedIn.wait() / / wait for everyone to check in else: nLeaving = 0 / / this thread is the last to arrive allCheckedIn.broadcast() / / tell everyone we’re all here! nLeaving++ if nLeaving < nThreads: / / not everyone has left yet while nLeaving < nThreads: allLeaving.wait() / / wait for everyone to leave else: nArrived = 0 / / this thread is the last to leave allLeaving.broadcast() / / tell everyone we’re outta here!

self.allCheckedIn = Condition(self.lock) self.allLeaving = Condition(self.lock)

How do we pick a victim?

We want: low page fault-rate page faults as inexpensive as possible We need: a way to compare the relative performance

  • f different page replacement algorithms

some absolute notion of what a “good” page replacement algorithm should accomplish

83

slide-2
SLIDE 2

Comparing Page Replacement Algorithms

Record a trace of the pages accessed by a process

E.g. 3,1,4,2,5,2,1,2,3,4 (or c,a,d,b,e,b,a,b,c,b)

Simulate behavior of page replacement algorithm on trace Record number of page faults generated

84

Optimal Page Replacement

Replace page needed furthest in future

Time 1 2 3 4 5 6 7 8 9 10 Requests c a d b e b a b c d a 1 b 2 c 3 d Faults

Time page needed next

Page Frames a b c d a b c d a b c d a b c d a b c e X a b c e a b c e a b c e a b c e d b c e X

a = 7 b = 6 c = 9 d = 10 a = ∞ b = 11 c = 13 e = 15

b d c b e

85

FIFO Replacement

Replace pages in the order they come into memory

Time 1 2 3 4 5 6 7 8 9 10 Requests c a d b e b a b c d a 1 b 2 c 3 d Faults Page Frames a b c d a b c d a b c d a b c d e b c d X e b c e e a c e X e a b e X e a b c X d a b c X

Assume: a @ -3 b @ -2 c @ -1 d @ 0

86

+ Frames

  • Page Faults

Number of frames Number of page faults

87

slide-3
SLIDE 3

For example...

3 frames - 9 page faults!

Time 1 2 3 4 5 6 7 8 9 10 11 12 Request s a b c d a b e a b c d e 1 2 Faults

Page Frames

a X a b X a b c X d b c X d a c X d a b X e a b X e a b e a b e c b X e c d X e c d

FIFO

88

Belady’ s Anomaly

4 frames - 10 page faults!

Time 1 2 3 4 5 6 7 8 9 10 11 12 Request s a b c d a b e a b c d e 1 2 3 Faults Page Frames a X a b X a b c X a b c d X a b c d a b c d e a c d X e a b d X e a b d X e a b c X d a b c X d e b c X

FIFO

89

+ Frames

  • Page Faults?

Yes, but only for stack page replacement policies set of pages in memory with n frames is a subset of set of pages in memory with n+1 frames

Number of frames Number of page faults

90

Locality of Reference

If a process access a memory location, then it is likely that

the same memory location is going to be accessed again in the near future (temporal locality) nearby memory locations are going to be accessed in the future (spatial locality)

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

91

slide-4
SLIDE 4

LRU: Least Recently Used

Replace page not referenced for the longest time

Time 1 2 3 4 5 6 7 8 9 10 Requests c a d b e b a b c d a 1 b 2 c 3 d Faults

Time page last used

Page Frames a b c d a b c d a b c d a b c d a b e d X a b e d a b e d a b e d a b e c X

a = 2 b = 4 c = 1 d = 3 a = 7 b = 8 e = 5 d = 3 a = 7 b = 8 e = 5 c = 9

a b d c X

92

Implementing LRU

Maintain a “stack” of recently used pages

Time 1 2 3 4 5 6 7 8 9 10 Requests c a d b e b a b c d a 1 b 2 c 3 d Faults Page Frames a b c d a b c d a b c d a b e d X a b e d a b e d a b e d a b e c X a b c d a b d c X c a c d a c b d a c e b d a b e d a a b e d b a e d c b a e d c b a c d e Page to replace LRU Page Stack 93

Implementing LRU

Add a (64-bit) timestamp to each page table entry

HW counter incremented on each instruction Page table entry timestamped with counter when referenced Replace page with lowest timestamp

94

Implementing LRU

Add a (64-bit) timestamp to each page table entry

HW counter incremented on each instruction Page table entry timestamped with counter when referenced Replace page with lowest timestamp

Approximate LRU through aging

keep a k-bit tag in each table entry at every “tick”: If needed, evict page with lowest tag

11000000 10000000 01000000 00000000 11000000 01000000 11100000 11000000 00100000 10000000 01100000 11110000 01111000 01100000 00100000 01000000 10110000 10110000 10001000 00100000 01011000 10100000 01010000 00101000 1 0 1 0 1 1 R bits at Tick 0 1 1 0 0 1 0 R bits at Tick 1 0 1 1 0 0 0 1 1 0 1 0 1 R bits at Tick 2 1 0 0 0 1 0 R bits at Tick 4 R bits at Tick 5 10000000 00000000 10000000 00000000 10000000 10000000 Page 0 Page 1 Page 2 Page 3 Page 4 Page 5

i) Shift tag right one bit ii) Copy Referenced (R) bit in tag iii) Reset Refereced bits to 0

95

slide-5
SLIDE 5

The Clock Algorithm

Organize pages in memory as a circular list When page is referenced, set its reference bit R to 1 On page fault if R = 0: evict the page if R = 1: clear R advance hand

1 4

Page 0

1 1 1 12 7 1 2 5

Page 4 Page 1 Page 5 Page 2 Page 3 R bit frame # 96

Clock Page Replacement

Time 1 2 3 4 5 6 7 8 9 10 Requests c a d b e b a b c d a 1 b 2 c 3 d Faults Page Frames a b c d a b c d a b c d a b c d

1 a 1 b 1 c 1 d Page table entries for resident pages Hand clock:

e b c d X

1 e 0 b 0 c 0 d

e b c d

1 e 1 b 0 c 0 d

e b a d X

1 e 0 b 1 a 0 d 1 e 1 b 1 a 0 d

e b c d e b a c X

1 e 1 b 1 a 1 c

d b a c X

1 d 0 b 0 a 0 c

97

The Second Chance Algorithm

Dirty pages get “second chance” before eviction replacing dirty pages is expensive!

1 1 4

Page 0

0 1 1 1 1 12 0 0 7 0 1 2 1 0 5

Page 4 Page 1 Page 5 Page 2 Page 3 R bit frame #

dirty R 1 1 1 1 dirty R replace page 1

Before Clock sweep After Clock sweep dirty bit 98

Second Chance Page Replacement

Time 1 2 3 4 5 6 7 8 9 10 Requests c aw d bw e b aw b c d a 1 b 2 c 3 d Faults Page Frames a b c d a b c d a b c d a b c d

1 a 1 b 1 c 1 d Page table entries for resident pages Hand clock:

a b e d X

11 a 11 b 1 c 1 d 0 a 0 b 1 e 0 d 0 a 1 b 1 e 0 d

a b e d a b e d a b e d a b e c X a d e c X

11 a 1 b 1 e 0 d 11 a 1 b 1 e 1 c 0 a 1 d 0 e 0 c

99

slide-6
SLIDE 6

Local vs. Global Page Replacement

Local: Select victim only among allocated frames Equal or proportional frame allocation Global: Select any free frame, even if allocated to another process Processes have no control over their own page fault rate

100

Brother, can you spare a frame?

Time 1 2 3 4 5 6 7 8 9 10 11 12 Requests a b c d a b c d a b c d a a a a d d d c c c b b b 1 b b b b b a a a d d d c c 2 c c c c c c b b b a a a d Faults X X X X X X X X X

FIFO

101

Brother, can you spare a frame?

Time 1 2 3 4 5 6 7 8 9 10 11 12 Requests a b c d a b c d a b c d a a a a a a a a a a a a a 1 b b b b b b b b b b b b b 2 c c c c c c c c c c c c c 3

  • d

d d d d d d d d Faults X So, what’ s wrong with global replacement?

FIFO

102

Memory as a Cache

Demand paging enables frames to cache part

  • f a process VA space

If the cache is large enough, hit ratio is high

few page faults

What if there aren’ t enough frames to go around?

should decrease degree of multiprogramming

swapped out process can then release their frames

103

slide-7
SLIDE 7

Instead…

When not enough frames... high page fault rate low CPU utilization OS may increase degree of multiprogramming!

104

Instead…

When not enough frames... high page fault rate low CPU utilization OS may increase degree of multiprogramming! Thrashing process spends all its time swapping pages in and out

Degree of Multiprogramming CPU Utilization 105

Locality of Reference

If a process access a memory location, then it is likely that

the same memory location is going to be accessed again in the near future (temporal locality) nearby memory locations are going to be accessed in the future (spatial locality)

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

106

Tracking Locality

When a process executes it moves from locality (set of pages used together) to locality the size of the process’ locality (a.k.a. its working set) can change over time Goal: track the size of the process’ working set, dynamically acquiring and releasing frames as necessary

107

slide-8
SLIDE 8

The Working Set Model

Choose page references as WS window WSSi = # of distinct pages referenced by in latest

too small does not cover locality too large covers many localities

Thrashing if WSSi > # frames

if so, suspend one of the processes

If enough free frames, increase degree of multiprogramming Σi pi

<latexit sha1_base64="Z HG+RkPq35frN/qTOCNhAC+vQ8=">A B3 icdVDLSgMxFL1TX7W+qi7dBIvgapix4gMUC25cVnRsoS0lk6ZtaGYmJHeEUrp3o7hS3Pk5/oK/4ReYtrqojwMXDuecS+5JqKQw6HnvTmZmdm5+IbuYW1peWV3Lr2/cmCTVjAcskYmuhtRwKWIeoEDJq0pzGoWSV8Le+civ3HJtRBJfY1/xRkQ7sWgLRtFKV6opmvmC73pjkP9J4ezjVL0CQLmZf6u3EpZGPEYmqTE131PYGFCNgk +zNVTwxVlPdrhg/GBQ7JjpRZpJ9pOjGSsTuVoZEw/Cm0yotg1P72R+JdXS7F91BiIWKXIYzZ5qJ1Kg kZtSUtoTlD2beEMi3shYR1qaYM7Z/kbHXPLR7s+0WP/Cbf1YM9 j1L/1C6Q myMIWbM u+HAIJbiAMgTAoAP38ATPTujcOQ/O4ySacb52NmEKzs nDGaL9w= </latexit> <latexit sha1_base64="1/VW9mlyb6fVGRlOAzoBG5h9tNU=">A B3 icdVDLSgMxFL1TX3V8V 26CRbB1TBjxQdYL hxWdGxhbaUTJp QzMzIckIpXTvRnGluPZn/APxN/wCM60u6uPAhcM5 J7EgjOlHbd ys3Mzs3v5BftJeWV1bXCusb1ypJ aE+SXgi6wFWlLOY+p TutCUhwFnNaC/lnm126oVCyJr/RA0FaEuzELGcHaSJeizdqFoue4Y6D/SfH0oyxe3uxytV14bXYSk Y01oRjpRqeK3RriKVmhNOR3UwVFZj0cZcOxweO0I6ROihMpJlYo7E6lcORUoMoM kI6576 WXiX14j1eFRa8hikWoak8lDYcqRTlDWFnWYpETzgSGYSGYuRKSHJSba/IltqrtO6WDfK7noN/mu7u85x4534RUrJzB HrZgG3bBg0OowDlUwQcCXbiDR3iyAuvWurceJtGc9bWzCVOwnj8BgsCNGA= </latexit> <latexit sha1_base64="1/VW9mlyb6fVGRlOAzoBG5h9tNU=">A B3 icdVDLSgMxFL1TX3V8V 26CRbB1TBjxQdYL hxWdGxhbaUTJp QzMzIckIpXTvRnGluPZn/APxN/wCM60u6uPAhcM5 J7EgjOlHbd ys3Mzs3v5BftJeWV1bXCusb1ypJ aE+SXgi6wFWlLOY+p TutCUhwFnNaC/lnm126oVCyJr/RA0FaEuzELGcHaSJeizdqFoue4Y6D/SfH0oyxe3uxytV14bXYSk Y01oRjpRqeK3RriKVmhNOR3UwVFZj0cZcOxweO0I6ROihMpJlYo7E6lcORUoMoM kI6576 WXiX14j1eFRa8hikWoak8lDYcqRTlDWFnWYpETzgSGYSGYuRKSHJSba/IltqrtO6WDfK7noN/mu7u85x4534RUrJzB HrZgG3bBg0OowDlUwQcCXbiDR3iyAuvWurceJtGc9bWzCVOwnj8BgsCNGA= </latexit> <latexit sha1_base64="1/VW9mlyb6fVGRlOAzoBG5h9tNU=">A B3 icdVDLSgMxFL1TX3V8V 26CRbB1TBjxQdYL hxWdGxhbaUTJp QzMzIckIpXTvRnGluPZn/APxN/wCM60u6uPAhcM5 J7EgjOlHbd ys3Mzs3v5BftJeWV1bXCusb1ypJ aE+SXgi6wFWlLOY+p TutCUhwFnNaC/lnm126oVCyJr/RA0FaEuzELGcHaSJeizdqFoue4Y6D/SfH0oyxe3uxytV14bXYSk Y01oRjpRqeK3RriKVmhNOR3UwVFZj0cZcOxweO0I6ROihMpJlYo7E6lcORUoMoM kI6576 WXiX14j1eFRa8hikWoak8lDYcqRTlDWFnWYpETzgSGYSGYuRKSHJSba/IltqrtO6WDfK7noN/mu7u85x4534RUrJzB HrZgG3bBg0OowDlUwQcCXbiDR3iyAuvWurceJtGc9bWzCVOwnj8BgsCNGA= </latexit> <latexit sha1_base64="1/VW9mlyb6fVGRlOAzoBG5h9tNU=">A B3 icdVDLSgMxFL1TX3V8V 26CRbB1TBjxQdYL hxWdGxhbaUTJp QzMzIckIpXTvRnGluPZn/APxN/wCM60u6uPAhcM5 J7EgjOlHbd ys3Mzs3v5BftJeWV1bXCusb1ypJ aE+SXgi6wFWlLOY+p TutCUhwFnNaC/lnm126oVCyJr/RA0FaEuzELGcHaSJeizdqFoue4Y6D/SfH0oyxe3uxytV14bXYSk Y01oRjpRqeK3RriKVmhNOR3UwVFZj0cZcOxweO0I6ROihMpJlYo7E6lcORUoMoM kI6576 WXiX14j1eFRa8hikWoak8lDYcqRTlDWFnWYpETzgSGYSGYuRKSHJSba/IltqrtO6WDfK7noN/mu7u85x4534RUrJzB HrZgG3bBg0OowDlUwQcCXbiDR3iyAuvWurceJtGc9bWzCVOwnj8BgsCNGA= </latexit> <latexit sha1_base64="r/Ss4if5ogUMD6/+QWEvGwrnzac=">A B3 icdVDLSgMxFL1TX7W+qi7dBIvgapix Qe4KLhxWdGxhbaUTJp QzOZIbkjlNK9G8WV4h/5C/6N6cNFfRwIHM454d5zw1QKg5736eSWl dW1/LrhY3Nre2d4u7evUkyzXjAEpnoRkgNl0LxA VK3kg1p3EoeT0cXE38+gPXRiTqDocpb8e0p0QkGEUr3aYd0SmWfNebgvxPSjBHrVP8aHUTlsVcIZPUmKbvpdgeUY2CST4utDLDU8oGtMdH0wXH5MhKXRIl2j6FZKou5GhszDAObTKm2Dc/vYn4l9fM Dpvj4RKM+SKzQZFmS YkElb0hWaM5RDSyjTwm5IWJ9qytDepGCre275tOKXPfKbfFcPTtwL17/xS9XL+Q3ycACHcAw+nE VrqEGATDowRO8wpsTOo/Os/Myi+ac+Z9 WIDz/gXJiIl5</latexit>

108

WS Page Replacement = 4

Time 1 2 3 4 5 6 7 8 9 10 Requests c c d b c e c e a d Page a

  • Page b

Page c Page d

  • Page e
  • Faults

Pages in Memory

t=0 t=−1 t=−2 109

WS Page Replacement = 4

Time 1 2 3 4 5 6 7 8 9 10 Requests c c d b c e c e a d Page a

  • Page b

Page c Page d

  • Page e
  • Faults

Pages in Memory

t=0 t=−1 t=−2

  • X
  • 110
  • X
  • X
  • X
  • X

Computing the WS

Use interval timer , the R bit, and extra bits per page When elapses, shift bits right, copy R bit in MSB and reset R bit If one of the bits is 1, the corresponding page is in WS τ ∆=τ ×k k k τ k

111

slide-9
SLIDE 9

When too high, increase WS; decrease when too low.

Keep time of last page fault On page fault: if , then unmap all pages not referenced in [ ] else add faulting page to the working set

Tracking Page Fault Frequency

tlast tcurrent − tlast > τ ∗ tlast − tcurrent

112

threshold

PFF Page Replacement

Time 1 2 3 4 5 6 7 8 9 10 Requests c c d b c e c e a d Page a

  • Page b

Page c Page d

  • Page e
  • Faults

Pages in Memory

τ ∗ = 2

tcurrent − tlast

113

PFF Page Replacement

Time 1 2 3 4 5 6 7 8 9 10 Requests c c d b c e c e a d Page a

  • Page b
  • Page c
  • Page d
  • Page e
  • Faults

X X X X X 1 3 2 3 1 Pages in Memory

τ ∗ = 2

tcurrent − tlast

114