1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock...

42
1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    0

Transcript of 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock...

Page 1: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

1

Virtual Memory in the Real World

Implementing exact LRU Approximating LRU

Hardware Support Clock Algorithm

Thrashing Cause Working Set

Page 2: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

2

Implementing Exact LRU

On each reference, time stamp page

When we need to evict: select oldest page= least-recently usedA, B, C, B, C, C, D

Page 3: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

3

Implementing Exact LRU

On each reference, time stamp page

When we need to evict: select oldest page= least-recently usedA1

A, B, C, B, C, C, D

Page 4: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

4

Implementing Exact LRU

On each reference, time stamp page

When we need to evict: select oldest page= least-recently usedA1

B2

A, B, C, B, C, C, D

Page 5: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

5

Implementing Exact LRU

On each reference, time stamp page

When we need to evict: select oldest page= least-recently usedA1

B2

C3

A, B, C, B, C, C, D

Page 6: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

6

Implementing Exact LRU

On each reference, time stamp page

When we need to evict: select oldest page= least-recently usedA1

B4

C3

A, B, C, B, C, C, D

Page 7: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

7

Implementing Exact LRU

On each reference, time stamp page

When we need to evict: select oldest page= least-recently usedA1

B4

C5

A, B, C, B, C, C, D

Page 8: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

8

Implementing Exact LRU

On each reference, time stamp page

When we need to evict: select oldest page= least-recently usedA1

B4

C6

A, B, C, B, C, C, D

Page 9: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

9

Implementing Exact LRU

On each reference, time stamp page When we need to evict: select oldest page

= least-recently used

A1

B4

C6

A, B, C, B, C, C, DD7

LRU page

How should we implement this?

Page 10: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

10

Implementing Exact LRU:Data Structures Hash table: optimize the common case

(memory hit) Location of a page in memory: apply hash

function to a page number update: O(1), eviction: O(n) (n: # of pages to

process) Expensive: on every reference, compute

hash of page address; update time stamp doubly-linked list

Move items to front when referenced LRU items at end of list Still too expensive:

Linear lookup time to find a page 4-6 pointer updates per reference

Page 11: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

11

Virtual Memory in the Real World

Implementing exact LRU Approximating LRU

Reference-bit algorithm Clock Algorithm

Thrashing Cause Working Set

Page 12: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

12

Reference-bit algorithm

Hardware support: reference bit Maintain reference bit for every

page On each access, set reference bit to

1 Periodically resets reference bits Evict page with reference bit = 0

Page 13: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

13

Reference-bit algorithm : example

Maintain reference bit for every page On each access, set reference bit to

1 Periodically resets reference bits Evict page with reference bit = 0A

1B1

C1

A, B, C, B, C, C, D

Page 14: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

14

Reference-bit algorithm: example

Maintain reference bit for every page On each access, set reference bit to

1 Periodically resets reference bits Evict page with reference bit = 0A

0B0

C0

A, B, C, B, C, C, D

reset reference bits

Page 15: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

15

Reference-bit algorithm: example

Maintain reference bit for every page On each access, set reference bit to

1 Periodically resets reference bits Evict page with reference bit = 0A

0B1

C0

A, B, C, B, C, C, D

Page 16: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

16

Reference-bit algorithm: example

Maintain reference bit for every page On each access, set reference bit to

1 Periodically resets reference bits Evict page with reference bit = 0A

0B1

C1

A, B, C, B, C, C, D

Page 17: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

17

Reference-bit algorithm: example

Maintain reference bit for every page On each access, set reference bit to

1 Periodically resets reference bits Evict page with reference bit = 0A

0B1

C1

A, B, C, B, C, C, D

Page 18: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

18

Reference-bit algorithm: example

Maintain reference bit for every page On each access, set reference bit to 1 Periodically resets reference bits Evict page with reference bit = 0

A0

B1

C1

A, B, C, B, C, C, DD1

Page 19: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

19

Virtual Memory in the Real World

Implementing exact LRU Approximating LRU

Reference-bit algorithm Clock Algorithm

Thrashing Cause Working Set

Page 20: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

20

The Clock Algorithm Set reference bit to 1 for an access Consider frames in circle Pointer:

0: initially Only advance pointer when page fault

happens On page fault, OS:

Repeat Checks reference bit of the current pointer If reference bit = 0, replace page, set bit to

1; advance pointer to next frame; break; If reference bit = 1, set bit to 0, advance

pointer to next frame

Page 21: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

21

The Clock Algorithm

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

B1

C1

A1

D1

A, B, C, D, B, C, E, F, C, G

Page 22: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

22

The Clock Algorithm

B1

C1

A1

D1

A, B, C, D, B, C, E, F, C, G

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 23: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

23

The Clock Algorithm

B1

C1

A1

D1

A, B, C, D, B, C, E, F, C, G

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 24: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

24

The Clock Algorithm

B1

C1

A0

D1

A, B, C, D, B, C, E, F, C, G

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 25: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

25

The Clock Algorithm

B0

C1

A0

D1

A, B, C, D, B, C, E, F, C, G

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 26: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

26

The Clock Algorithm

B0

C0

A0

D1

A, B, C, D, B, C, E, F, C, G

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 27: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

27

The Clock Algorithm

B0

C0

A0

D0

A, B, C, D, B, C, E, F, C, G

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 28: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

28

The Clock Algorithm

B0

C0

A0

D0

A, B, C, D, B, C, E, F, C, G

E1

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 29: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

29

The Clock Algorithm

B0

C0

A0

D0

A, B, C, D, B, C, E, F, C, G

E0

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 30: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

30

The Clock Algorithm

B0

C0

A0

D0

A, B, C, D, B, C, E, F, C, G

E0

F1

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 31: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

31

The Clock Algorithm

B0

C0

A0

D0

A, B, C, D, B, C, E, F, C, G

E0

F1

C1

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 32: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

32

The Clock Algorithm

B0

C0

A0

D0

A, B, C, D, B, C, E, F, C, G

E0

F0

C1

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 33: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

33

The Clock Algorithm

B0

C0

A0

D0

A, B, C, D, B, C, E, F, C, G

E0

F0

C1C0

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 34: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

34

The Clock Algorithm

B0

C0

A0

D0

A, B, C, D, B, C, E, F, C, G

E0

F0

C1C0

G1

Consider frames in circle

On page fault, repeat: Checks reference bit If reference bit = 0,

replace page, set bit to 1; advance pointer to next frame; break;

If reference bit = 1, set bit to 0, advance pointer to next frame

Page 35: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

35

The Clock Algorithm: Summary

Variant of FIFO & LRU LRU: ? FIFO: ?

No need to reset referenece bit periodically

Page 36: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

36

Enhancing Clock

Recall: we don’t write back unmodified pages Idea: favor eviction of unmodified pages Extend hardware to keep another bit:

modified bit Total order of tuples: (ref bit, mod

bit) (0,0), (0,1), (1,0), (1,1) Evict page from lowest nonempty class

Page 37: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

37

Page Replacementin Enhanced Clock

OS scans at most three times Page (0,0) – replace that page Page (0,1) – write out page Page (1,0), (1,1)

Fast, but still coarse approximation of LRU

Page 38: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

38

Virtual Memory in the Real World

Implementing exact LRU Approximating LRU

Reference-bit algorithm Clock

Thrashing Cause Working Set

Page 39: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

39

Thrashing

A process is busy swapping pages in and out; no useful work is done low CPU utilization

OS adds processes →

even more page swapping & lower CPU utilization

Page 40: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

40

Cause of Thrashing

Locality A set of pages that are actively used

together Process migrates from one locality to

another Total locality of processes > total

memory size Process cannot keep in memory all

pages that it is currently using

Page 41: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

41

Working Set Strategy

Working set = pages referred in last references (approximate of locality)

OS monitors working set of each process; allocate enough frames to process Another process can be started if

there are enough extra frames Suspend process(es) if the sum of

working-set sizes exceeds RAM

Page 42: 1 Virtual Memory in the Real World Implementing exact LRU Approximating LRU Hardware Support Clock Algorithm Thrashing Cause Working Set.

42

Working Set Problems

Algorithm relies on key parameter, How do we set ? Is there one correct ?

Different processes have different timescales over which they refer pages

Not acceptable (or necessarily possible) to suspend processes altogether

Not really used Very rough variant used in Windows