101
Virtual Memory

Virtual Memory. 2 Virtual Memory Manager Provides abstraction of physical memory Creates virtual address space in secondary memory and then “automatically”

Embed Size (px)

Citation preview

Virtual Memory

2

Virtual Memory Manager

• Provides abstraction of physical memory

• Creates virtual address space in secondary memory and then “automatically” determines which part of the address space to load into primary memory at any given time

• Allows application programmers to think that they have a very large address space in which to write programs

3

Virtual Memory Organization

Memory Image for pi

Secondary Memory

Primary Memory

4

Locality

• Programs do not access their address space uniformly– they access the same location over and over

• Spatial locality: processes tend to access location near to location they just accessed– because of sequential program execution– because data for a function is grouped together

• Temporal locality: processes tend to access data over and over again– because of program loops– because data is processed over and over again

5

Spatial Reference Locality

• Address space is logically partitioned– Text, data, stack– Initialization, main, error handle

• Different parts have different reference patterns:

Address Space for pi

Initialization code (used once)Code for 1Code for 2Code for 3Code for error 1Code for error 2Code for error 3Data & stack

30%

20%

35%

15%

<1%

<1%

Execution time

6

Virtual Memory

• Every process has code and data locality

• Dynamically load/unload currently-used address space fragments as the process executes

• Uses dynamic address relocation/binding– Generalization of base-limit registers– Physical address corresponding to a compile-

time address is not bound until run time

7

Virtual Memory – cont

• Since binding changes with time, use a dynamic virtual address map, t

VirtualAddressSpace

t

8

Virtual Memory – cont

Virtual Address Space for pi

Virtual Address Space for pj

Virtual Address Space for pk

Secondary Memory

• Complete virtual address space is stored in secondary memory

Primary Memory

0

n-1

Physical Address Space

• Fragments of the virtual address space are dynamically loaded into primary memory at any given time

• Each address space is fragmented

9

Address Translation

• Virtual memory systems distinguish among symbolic names, virtual address, and physical address spaces

• Need to map symbolic names to virtual addresses, and then to physical addresses

• Compiler/assembler and link editor handle mapping from symbolic names in name space to virtual address

• When program is executed, the virtual addresses are mapped to physical addresses

10

Names, Virtual Addresses & Physical Addresses

SourceProgram

AbsoluteModule

Name SpaceName Space

Pi’s VirtualAddress Space

Pi’s VirtualAddress Space

ExecutableImage

PhysicalAddress Space

PhysicalAddress Space

t: Virtual Address Space Physical Address Space

11

Address Formation

• Translation system creates an address space, but its address are virtual instead of physical

• A virtual address, x:– Is mapped to physical address y = t(x) if x is

loaded at physical address y– Is mapped to (the null address) if x is not

loaded

• The map, t, changes as the process executes -- it is “time varying”

• t: Virtual Address Physical Address {}

12

Translation Process

• If t(k) = at time t and the process references location k, then– The virtual manager will stop the process

– The referenced location is loaded at some location (say m)

– The manager changes t(k) = m

– The manager lets the process continue execution

• Note that the referenced element was determined missing after an instruction started execution – CPU needs to be able to “back out” of an instruction and reexecute instruction after translation mapping

13

Size of Blocks of Memory

• Virtual memory system transfers “blocks” of the address space to/from primary memory

• Fixed size blocks: System-defined pages are moved back and forth between primary and secondary memory

• Variable size blocks: Programmer-defined segments – corresponding to logical fragments – are the unit of movement

• Paging is the commercially dominant form of virtual memory today

14

Paging

• A page is a fixed size, 2h, block of virtual addresses

• A page frame is a fixed size, 2h, block of physical memory (the same size as a page)

• When a virtual address, x, in page i is referenced by the CPU– If page i is loaded at page frame j, the virtual

address is relocated to page frame j– If page is not loaded, the OS interrupts the

process and loads the page into a page frame

15

Practicality of paging

• Paging only works because of locality– at any one point in time programs don’t need

most of their pages

• Page fault rates must be very, very low for paging to be practical– like one page fault per 100,000 or more memory

references

16

Addresses

• Suppose there are G= 2g2h=2g+h virtual addresses and H=2j+h physical addresses assigned to a process– Each page/page frame is 2h addresses– There are 2g pages in the virtual address space– 2j page frames are allocated to the process– Rather than map individual addresses

• t maps the 2g pages to the 2j page frames

• That is, page_framej = t(pagei)

• Address k in pagei corresponds to address k in page_framej

17

Page-Based Address Translation

• Let N = {d0, d1, … dn-1} be the pages

• Let M = {b0, b1, …, bm-1} be page frames

• Virtual address, i, satisfies 0i<G= 2g+h

• Physical address, k = U2h+V (0V<G= 2h )– U is page frame number– V is the line number within the page

– t:[0:G-1] <U, V> {}

– Since every page is size c=2h

• page number = U = i/c• line number = V = i mod c

18

Address Translation (cont)

Page # Line #Virtual Address

g bits h bits

tMissing Page

Frame # Line #j bits h bits

Physical Address

MAR

CPU Memory

“page table”

19

Paging Algorithms

• Two basic types of paging algorithms– Static allocation– Dynamic allocation

• Three basic policies in defining any paging algorithm– Fetch policy – when a page should be loaded– Replacement policy –which page is unloaded– Placement policy – where page should be loaded

20

Fetch Policy

• Determines when a page should be brought into primary memory

• Usually don’t have prior knowledge about what pages will be needed

• Majority of paging mechanisms use a demand fetch policy– Page is loaded only when process references it

21

Demand Paging Algorithm

1. Page fault occurs

2. Process with missing page is interrupted

3. Memory manager locates the missing page

4. Page frame is unloaded (replacement policy)

5. Page is loaded in the vacated page frame

6. Page table is updated

7. Process is restarted

22

Page references

• Processes continually reference memory– and so generate a stream of page references

• The page reference stream tells us everything about how a process uses memory– For a given size, we only need to consider the page number

– If we have a reference to a page, then immediately following references to the page will never generate a page fault

0100, 0432, 0101, 0612, 0102, 0103, 0104, 0101, 0611, 0102, 0103

0104, 0101, 0610, 0103, 0104, 0101, 0609, 0102, 0105

– Suppose the page size is 100 bytes, what is the page reference stream?

• We use page reference streams to evaluate paging algorithms

23

Modeling Page Behavior

• Let = r1, r2, r3, …, ri, … be a page reference stream– ri is the ith page # referenced by the process

– The subscript is the virtual time for the process

• Given a page frame allocation of m, the memory state at time t, St(m), is set of pages loaded– St(m) = St-1(m) Xt - Yt

• Xt is the set of fetched pages at time t

• Yt is the set of replaced pages at time t

24

More on Demand Paging

• If rt was loaded at time t-1, St(m) = St-1(m)

• If rt was not loaded at time t-1 and there were empty page frames– St(m) = St-1(m) {rt}

• If rt was not loaded at time t-1 and there were no empty page frames– St(m) = St-1(m) {rt} - {y}

where y is the page frame unloaded

25

Replacement Policy

• When there is no empty page frame in memory, we need to find one to replace – Write it out to the swap area if it has been

changed since it was read in from the swap area• Dirty bit or modified bit

– pages that have been changed are referred to as “dirty”– these pages must be written out to disk because the disk

version is out of date» this is called “cleaning” the page

• Which page to remove from memory to make room for a new page– We need a page replacement algorithm

26

Page replacement algorithms

• The goal of a page replacement algorithm is to produce the fewest page faults

• We can compare two algorithms– on a range of page reference streams

• Or we can compare an algorithm to the best possible algorithm

• We will start by considering static page replacement algorithms

27

Static Paging Algorithms

• A fixed number of page frames is allocated to each process when it is created

• Paging policy defines how these page frames will be loaded and unloaded

• Placement policy is fixed– The page frame holding the new page is always

the one vacated by the page selected for replacement

28

Static Allocation, Demand Paging

• Number of page frames is static over the life of the process

• Fetch policy is demand

• Since St(m) = St-1(m) {rt} - {y}, the replacement policy must choose y -- which uniquely identifies the paging policy

29

Random page replacement

• Algorithm: replace a page randomly

• Theory: we cannot predict the future at all

• Implementation: easy

• Performance: poor– but it is easy to implement– but the best case, worse case and average case are

all the same

30

Random Replacement

• Replaced page, y, is chosen from the m loaded page frames with probability 1/m

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7012

213

213

210

310

310

312

012

032

032

062

462

452

752

203

20

2

• No knowledge of doesn’t perform well

13 page faults

31

Belady’s Optimal algorithm

• The one that produces the fewest possible page faults on all page reference sequences

• Algorithm: replace the page that will not be used for the longest time in the future

• Problem: it requires knowledge of the future

• Not realizable in practice– but it is used to measure the effectiveness of

realizable algorithms

32

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

33

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

FWD4(2) = 1 FWD4(0) = 2FWD4(3) = 3

34

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 21 0 0 02 3 1

FWD4(2) = 1 FWD4(0) = 2FWD4(3) = 3

35

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 21 0 0 0 0 02 3 1 1 1

36

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 21 0 0 0 0 0 32 3 1 1 1 1

FWD7(2) = 2 FWD7(0) = 3FWD7(1) = 1

37

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 2 2 01 0 0 0 0 0 3 3 3 32 3 1 1 1 1 1 1 1

FWD10(2) = FWD10(3) = 2FWD10(1) = 3

38

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 2 2 0 0 01 0 0 0 0 0 3 3 3 3 3 32 3 1 1 1 1 1 1 1 1 1

FWD13(0) = FWD13(3) = FWD13(1) =

39

Belady’s Optimal Algorithm

• Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x)

Let page reference stream, = 2031203120316457

• Perfect knowledge of perfect performance• Impossible to implement

10 page faults

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 2 2 0 0 0 0 4 4 41 0 0 0 0 0 3 3 3 3 3 3 6 6 6 72 3 1 1 1 1 1 1 1 1 1 1 1 5 5

40

Theories of program behavior

• All replacement algorithms try to predict the future and act like Belady’s optimal algorithm

• All replacement algorithms have a theory of how program behave– they use it to predict the future, that is, when

pages will be referenced– then the replace the page that they think won’t be

referenced for the longest time.

41

LRU page replacement

• Least-recently used (LRU)• Algorithm: remove the page that hasn’t been

referenced for the longest time• Theory: the future will be like the past, page

accesses tend to be clustered in time• Implementation: hard, requires hardware

assistance (and then still not easy)• Performance: very good, within 30%-40% of

optimal

42

LRU model of the future

43

Least Recently Used (LRU)

• Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

BKWD4(2) = 3BKWD4(0) = 2BKWD4(3) = 1

44

Least Recently Used (LRU)

• Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 11 0 0 02 3 3

BKWD4(2) = 3BKWD4(0) = 2BKWD4(3) = 1

45

Least Recently Used (LRU)

• Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 1 11 0 0 0 22 3 3 3

BKWD5(1) = 1BKWD5(0) = 3BKWD5(3) = 2

46

Least Recently Used (LRU)

• Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 1 1 11 0 0 0 2 22 3 3 3 0

BKWD6(1) = 2BKWD6(2) = 1BKWD6(3) = 3

47

Least Recently Used (LRU)

• Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 1 1 1 3 3 3 0 0 0 6 6 6 71 0 0 0 2 2 2 1 1 1 3 3 3 4 4 42 3 3 3 0 0 0 2 2 2 1 1 1 5 5

48

Least Recently Used (LRU)

• Replace page with maximal forward distance: yt = max xeS t-1(m)BKWDt(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 3 2 2 2 2 6 6 6 61 0 0 0 0 0 0 0 0 0 0 0 0 4 4 42 3 3 3 3 3 3 3 3 3 3 3 3 5 53 1 1 1 1 1 1 1 1 1 1 1 1 7

• Backward distance is a good predictor of forward distance -- locality

49

LFU page replacement

• Least-frequently used (LFU)

• Algorithm: remove the page that hasn’t been used often in the past

• Theory: an actively used page should have a large reference count

• Implementation: hard, also requires hardware assistance (and then still not easy)

• Performance: not very good

50

Least Frequently Used (LFU)

• Replace page with minimum use: yt = min xeS t-1(m)FREQ(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

FREQ4(2) = 1FREQ4(0) = 1FREQ4(3) = 1

51

Least Frequently Used (LFU)

• Replace page with minimum use: yt = min xeS t-1(m)FREQ(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 21 0 0 12 3 3

FREQ4(2) = 1FREQ4(0) = 1FREQ4(3) = 1

52

Least Frequently Used (LFU)

• Replace page with minimum use: yt = min xeS t-1(m)FREQ(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 21 0 0 1 1 12 3 3 3 0

FREQ6(2) = 2FREQ6(1) = 1FREQ6(3) = 1

53

Least Frequently Used (LFU)

• Replace page with minimum use: yt = min xeS t-1(m)FREQ(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 21 0 0 1 1 12 3 3 3 0

FREQ7(2) = ?FREQ7(1) = ?FREQ7(0) = ?

54

FIFO page replacement

• Algorithm: replace the oldest page

• Theory: pages are used for a while and then stop being used

• Implementation: easy

• Performance: poor– because old pages are often accessed, that is, the

theory if FIFO is not correct

55

First In First Out (FIFO)

• Replace page that has been in memory the longest: yt = max xeS t-1(m)AGE(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 1 0 0 2 3

56

First In First Out (FIFO)

• Replace page that has been in memory the longest: yt = max xeS t-1(m)AGE(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3

AGE4(2) = 3AGE4(0) = 2AGE4(3) = 1

57

First In First Out (FIFO)

• Replace page that has been in memory the longest: yt = max xeS t-1(m)AGE(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 11 0 0 02 3 3

AGE4(2) = 3AGE4(0) = 2AGE4(3) = 1

58

First In First Out (FIFO)

• Replace page that has been in memory the longest: yt = max xeS t-1(m)AGE(x)

Let page reference stream, = 2031203120316457

Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 11 0 0 02 3 3

AGE5(1) = ?AGE5(0) = ?AGE5(3) = ?

59

Belady’s Anomaly

• FIFO with m = 3 has 9 faults

• FIFO with m = 4 has 10 faults

Let page reference stream, = 012301401234

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 4 4 4 4 4 41 1 1 1 0 0 0 0 0 2 2 22 2 2 2 1 1 1 1 1 3 3

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 4 4 4 4 3 31 1 1 1 1 1 1 0 0 0 0 42 2 2 2 2 2 2 1 1 1 13 3 3 3 3 3 3 2 2 2

60

Belady’s Anomaly

• The paging algorithm has worse performance when the amount of primary memory allocated to the process increases

• Problem arises because the set of pages loaded with the smaller memory allocation is not necessarily also loaded with the larger memory allocation

61

Avoiding Belady’s Anomaly

• Inclusion Property– Set of pages loaded with an allocation of m

frames is always a subset of the set of pages that has a page frame allocation of m+1

• FIFO does not satisfy the inclusion property

• LRU and LFU do

• Algorithms that satisfy the inclusion property are called stack algorithms

62

Stack Algorithms• Some algorithms are well-behaved

• Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 31 1 1 12 2 2

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 01 1 1 12 2 23 3

LRU

63

Stack Algorithms• Some algorithms are well-behaved

• Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 31 1 1 1 12 2 2 0

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 01 1 1 1 12 2 2 23 3 3

LRU

64

Stack Algorithms• Some algorithms are well-behaved

• Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 31 1 1 1 0 02 2 2 2 1

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 01 1 1 1 1 12 2 2 2 23 3 3 3

LRU

65

Stack Algorithms• Some algorithms are well-behaved

• Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 41 1 1 1 0 0 02 2 2 2 1 1

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 01 1 1 1 1 1 12 2 2 2 2 43 3 3 3 3

LRU

66

Stack Algorithms• Some algorithms are well-behaved

• Inclusion Property: Pages loaded at time t with m is also loaded at time t with m+1

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 4 4 4 2 2 21 1 1 1 0 0 0 0 0 0 3 32 2 2 2 1 1 1 1 1 1 4

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 0 0 0 0 0 41 1 1 1 1 1 1 1 1 1 1 12 2 2 2 2 4 4 4 4 3 33 3 3 3 3 3 3 2 2 2

LRU

67

Stack Algorithms• Some algorithms are not well-behaved

• Inclusion Property: Pages loaded at time t with m aren’t loaded at time t with m+1

FIFOFrame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 4 4 4 4 4 41 1 1 1 0 0 0 0 0 2 2 22 2 2 2 1 1 1 1 1 3 3

Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 4 4 4 4 3 31 1 1 1 1 1 1 0 0 0 0 42 2 2 2 2 2 2 1 1 1 13 3 3 3 3 3 3 2 2 2

68

Implementation

• LRU has become preferred algorithm• Difficult to implement

– Must record when each page was referenced– Difficult to do in hardware

• Approximate LRU with a reference bit– Periodically reset– Set for a page when it is referenced

• Dirty bit– pages that have been changed are referred to as “dirty”– these pages must be written out to disk because the disk

version is out of date• this is called “cleaning” the page

69

First LRU approximation

• When you get a page fault– replace any page whose referenced bit is off– then turn off all the referenced bits

• Two classes of pages– Pages referenced since the last page fault– Pages not referenced since the last page fault

• the least recently used page is in this class but you don’t know which one it is

• A crude approximation of LRU

70

Second LRU approximation

• Algorithm:– Keep a counter for each page– Have a daemon wake up every 500 ms and

• add one to the counter of each page that has not been referenced

• zero the counter of pages that have been referenced• turn off all referenced bits

– When you get a page fault• replace the page whose counter is largest

• Divides pages into 256 classes

71

Dynamic Paging Algorithms

• Static page replacement algorithms assume that a process is allocated to a fixed amount of primary memory

• But, the amount of physical memory – the number of page frames – varies as the process executes

• How much memory should be allocated?– Fault rate must be “tolerable”

– Will change according to the phase of process

• Need to define a placement & replacement policy• Contemporary models based on working set

72

Working Set

• Intuitively, the working set is the set of pages in the process’s locality– Somewhat imprecise– Time varying

– Given k processes in memory, let mi(t) be # of pages frames allocated to pi at time t

• mi(0) = 0

i=1k mi(t) |primary memory|

• Also have St(mi(t)) = St(mi(t-1)) Xt - Yt

• Or, more simply S(mi(t)) = S(mi(t-1)) Xt - Yt

73

Placed/Replaced Pages

• S(mi(t)) = S(mi(t-1)) Xt - Yt

• For the missing page– Allocate a new page frame

– Xt = {rt} in the new page frame

• How should Yt be defined?

• Consider a parameter, , called the window size– Determine BKWDt(y) for every yS(mi(t-1))

– if BKWDt(y) , unload y and deallocate frame

– if BKWDt(y) < do not disturb y

74

Working Set Principle

• Process pi should only be loaded and active if it can be allocated enough page frames to hold its entire working set

• The size of the working set is estimated using – Unfortunately, a “good” value of depends on

the size of the locality– Empirically this works with a fixed

75

Working set algorithm

• Algorithm– Keep track of the working set of each running

process– Only run a process if its entire working set fits

in memory – called working set principle

76

Working set algorithm example

• With =3, there are 16 page faults• With =4, there are 8 – the minimum size since there are 8 distinct pages

77

Working set algorithm example – cont.

• Letting =8 does not reduce the number of page faults• In fact, not all the page frames are used

78

Working set algorithm example – cont.

• Here the page frame allocation changes dynamicallyincreasing and decreasing

79

Implementing the Working Set• Global LRU will behave similarly to a

working set algorithm– Page fault

• Add a page frame to one process• Take away a page frame from another process

– Use LRU implementation idea• Reference bit for every page frame• Cleared periodically, set with each reference• Change allocation of some page frame with a clear

reference bit

– Clock algorithms use this technique by searching for cleared ref bits in a circular fashion

80

Performance of Demand Paging

• Page Fault Rate probability: 0 p 1.0– if p = 0 no page faults – if p = 1, every reference is a fault

• Effective Access Time (EAT) EAT = (1 – p) x memory access

+ p (page fault overhead+ [swap page out ]+ swap page in+ restart overhead)

81

Demand Paging Performance Example

• Assume memory access time = 100 nanosecond• Assume fault service time = 25 ms

= 25,000,000 ns• Then EAT = (1 – p) x 100 + p (25,000,000)

= 100 + 24,999,900 p (in ns)• So, if one out of 1000 accesses causes a page

fault, then

EAT = 100+24,999,900x0.001=25,099.9 ns

≈ 25 microseconds

82

Demand Paging Performance Example

• So, if one access out of 1000 causes a page fault, the computer would be slowed down by a factor of 250 because of demand paging!

• Can calculate that if we want less than 10% degradation, need to allow only one access out of 2,500,000 to page fault

83

Evaluating paging algorithms

• Mathematical modeling– powerful where it works– but most real algorithms cannot be analyzed

• Measurement– implement it on a real system and measure it– extremely expensive

• Simulation– Test on page reference traces– reasonably efficient– effective

84

Performance of paging algorithms

85

Thrashing

• VM allows more processes in memory, so several processes are more likely to be ready to run at the same time

• If CPU usage is low, it is logical to bring more processes into memory

• But, low CPU use may to due to too many pages faults because there are too many processes competing for memory

• Bringing in processes makes it worse, and leads to thrashing

86

Thrashing Diagram

• There are too many processes in memory and no process has enough memory to run. As a result, the page fault is very high and the system spends all of its time handling page fault interrupts.

87

Load control

• Load control: deciding how many processes should be competing for page frames– too many leads to thrashing– too few means that memory is underused

• Load control determines which processes are running at a point in time– the others have no page frames and cannot run

• CPU load is a bad load control measure• Page fault rate is a good load control measure

88

Load control and page replacement

89

Two levels of scheduling

90

Load control algorithms

• A load control algorithm measures memory load and swaps processes in and out depending on the current load

• Load control measures– rotational speed of the clock hand– average time spent in the standby list– page fault rate

91

Page fault frequency load control

• L = mean time between page faults

• S = mean time to service a page fault

• Try to keep L = S– if L < S, then swap a process out– if L > S, then swap a process in

• If L = S, then the paging system can just keep up with the page faults

92

Windows NT Paging System

PrimaryMemory

Virtual AddressSpace

Supv spaceUser space

Paging Disk(Secondary Memory)

2. Lookup (Page i, Addr k)

3. Translate (Page i, Addr k) to (Page Frame j, Addr k)

4. Reference (Page Frame j, Addr k)

1. Reference to Address k in Page i (User space)

93

Windows Address Translation

Page Directory Page Table Byte Index

Virtual page number Line number

Page Directory

Page Tables

TargetPage

Target Byte

c

b

a

A

C

B

94

Linux Virtual Address Translation

j.pgdj.pgd j.pmdj.pmdVirtual Address

PageDirectory

PageDirectoryBase

Page

PageTable

Page MiddleDirectory

j.ptej.pte j.offsetj.offset

95

Segmentation

• Unit of memory movement is:– Variably sized– Defined by the programmer

• Two component addresses, <Seg#, offset>– Seg # is reference to a base location– Offset is offset of target within segment

• Address translation is more complex than paging

96

Segment Address Translation

• t: segments x offsets physical address {}• t(i, j) = k; i = segment, j = offset, k = physical address

• Segment names typically symbolic; bound at runtime• : segments segment addresses• t((segName), j) = k

• Offset may also not be bound until runtime• : offset names offset addresses

• So, address map could be as complex as• t((segName), (offsetName)) = k

• Address translation is more complex than paging

97

Segment Address Translation

• Task of designing segmentation system to handle such general address translation is very challenging

• Each memory reference is theoretically a pair of symbols to be translated when the reference occurs

• In addition, the mappings are time-varying– The segment could be anywhere in primary

and/or secondary memory

98

Address Translation

<segmentName, offsetName>

segment # offset

t

Missing segment

Limit Base P

Limit

Relocation

+

?

To Memory Address Register

99

Address Translation – cont.

• System maintains segment table for each process (which is a segment itself)

• Table contains a set of entries – called segment descriptors

• Descriptors contain fields to support relocation; also indicates if not loaded– Base: relocation register for segment– Limit: length of segment– Protection: allowable forms of access

100

Implementation

• Most implementations do not fully implement the address translation model

• Segmentation requires special hardware– Segment descriptor support– Segment base registers (segment, code, stack)– Translation hardware

• Some of translation can be static– No dynamic offset name binding– Limited protection

101

Multics

• Designed in late 60’s

• Old, but still state-of-the-art segmentation

• Uses linkage segments to support sharing

• Uses dynamic offset name binding

• Required sophisticated memory management unit

• See pp 500-502