50
Part III Part III Storage Management Storage Management Chapter 9: Virtual Memory Chapter 9: Virtual Memory 1 Fall 2010

Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Part IIIPart IIIStorage ManagementStorage ManagementChapter 9: Virtual MemoryChapter 9: Virtual Memory

1Fall 2010

Page 2: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Observations

A complete program does not have to be in memory becausememory, because

error handling codes are not frequently usedt bl l d t t t llarrays, tables, large data structures usually

allocate memory more than necessary and many parts are not used at the same timemany parts are not used at the same timesome options and cases may be used rarely

If th t d d h t th b iIf they are not needed, why must they be in memory?

2

Page 3: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

BenefitsBenefitsP l th i t t i t d t lProgram length is not restricted to real memory size. That is, virtual address size can b l th h i l ibe larger than physical memory size.Can run more programs because space originally allocated for the un-loaded parts can be used by other programs.Save load/swap I/O time because we do not have to load/swap a complete program.p p p g

3

Page 4: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Virtual MemoryVirtual MemoryVirtual memory is the separation of user logicalVirtual memory is the separation of user logical memory from physical memory.This permits to have extremely large virtualThis permits to have extremely large virtual memory, which makes programming large systems easiersystems easier.Because memory segments can be shared, this further improves performance and save timefurther improves performance and save time.Virtual memory is commonly implemented with demand paging demand segmentation orwith demand paging, demand segmentation or demand paging+segmentation.

4

Page 5: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Demand PagingDemand Paging

vi

70 01 0 0

1

virtual memory page table physical memory page frame table

proc A pg 4

v

ii

i1

123

2345

123

123

proc A, pg 4

ii

345

56

345

4565

6567

789

proc A, pg 1

89

process A

5valid/invalid orpresent/absent bit

Page 6: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Address TranslationAddress TranslationAddress translation from a virtual address to aAddress translation from a virtual address to a physical address is the same as a paging system.However there is an additional check If theHowever, there is an additional check. If the needed page is not in physical memory (i.e., its valid bit is not set) a page fault (i e a trap) occursbit is not set), a page fault (i.e., a trap) occurs.If a page fault occurs, we need to do the following:

Find an unused page frame If no such pageFind an unused page frame. If no such page frame exists, a victim must be found and evicted.Write the old page out and load the new page inWrite the old page out and load the new page in.Update both page tables.R h i d i i

6Resume the interrupted instruction.

Page 7: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Details of Handling a Page Fault

Trap to the OS // a context switch occurspMake sure it is a page fault;If the address is not a legal one then

address error, returnFind a page frame // page replacement algorithmWrite the victim page back to disk // page outWrite the victim page back to disk // page outLoad the new page from disk // page inUpdate both page tables // two pages are involved!p p g p gResume the execution of the interrupted instruction

7

Page 8: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Hardware SupportHardware Support

Page Table Base Register, Page Table Length Register, and a Page Table. Each entry of a page table must have a valid/invalid bit. Valid means that page is in p gphysical memory. The address translation hardware must recognize this bit and generate a page fault if the valid bit is not set.Secondary Memory: use a disk.Secondary Memory: use a disk.Other hardware components may be needed and will be discussed later

8

and will be discussed later.

Page 9: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Too Many Memory Accesses?!Too Many Memory Accesses?!Each address reference may use at least two

f bl l kmemory accesses, one for page table look up and the other for accessing the page. It may be worse! See below:worse! See below:

A

C

ADD A, B, C

How many memory accesses are there?May be more than eight!

9B

Page 10: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Performance Issue: 1/2Performance Issue: 1/2Let p be the probability of a page fault, the page p p y p g p gfault rate, 0 ≤ p ≤ 1.The effective access time is(1-p)*memory access time + p*page fault time

The page fault rate p should be small, andThe page fault rate p should be small, and memory access time is usually between 10 and 200 nanoseconds.To complete a page fault, three components are important:

Serve the page-fault trapPage-in and page-out, a bottleneck

10

g p g ,Resume the interrupted process

Page 11: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Performance Issue: 2/2Performance Issue: 2/2Suppose memory access time is 100 nanosecondsSuppose memory access time is 100 nanoseconds, paging requires 25 milliseconds (software and hardware). Then, effective access time is) ,

(1-p)*100 + p*(25 milliseconds)= (1-p)*100 + p*25 000 000 nanoseconds (1-p) 100 + p 25,000,000 nanoseconds= 100 + 24,999,900*p nanoseconds

If page fault rate is 1/1000 the effective access timeIf page fault rate is 1/1000, the effective access time is 25,099 nanoseconds = 25 microseconds. It is 250 times slower!times slower! If we wish only 10% slower, effective access time is no more than 110 and p=0 0000004

11

no more than 110 and p 0.0000004.

Page 12: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Three Important Issues in V MThree Important Issues in V.M.

P t bl b l If dd hPage tables can be very large. If an address has 32 bits and page size is 4K, then there are 232/212 220 (210)2 1M t i i t bl232/212=220=(210)2= 1M entries in a page table per process!Virtual to physical address translation must be fast. This is done with TLB.Page replacement. When a page fault occurs and there is no free page frame, a victim page must p g p gbe found. If the victim is not selected properly, system degradation may be high.

12

y g y g

Page 13: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Page Table SizePage Table Sizevirtual address

page tablebase register

index 1 index 2 index 3 offset

8 6 6 12memorybase register memory

Only level 1level 1

page table level 2page table level 3

page tables are inphysical memory. page table

page tableOther page tables may bepaged-in when they are May be in virtual memory

13referred to.

Page 14: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Page Replacement: 1/2g pThe following is a basic scheme

Find the desired page on diskFind a free page frame in physical memoryp g p y y

If there is a free page frame, use itIf there is no free page frame, use a page-If there is no free page frame, use a pagereplacement algorithm to find a victim pageWrite this victim page back to disk andWrite this victim page back to disk and update the page table and page frame table

Read the desired page into the selected frameRead the desired page into the selected frame and update page tables and page frame tableRestart the interrupted instruction

14

Restart the interrupted instruction

Page 15: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Page Replacement: 2/2Page Replacement: 2/2If there is no free page frame, two page transfers (i e page in and page out) may be required(i.e., page-in and page-out) may be required.A modify bit may be added to a page table entry. The modify bit is set if that page has been modifiedThe modify bit is set if that page has been modified (i.e., storing info into it). It is initialized to 0 when a page is loaded into memorypage is loaded into memory.Thus, if a page is not modified (i.e., modify bit = 0), it does not have to be written back to diskit does not have to be written back to disk.Some systems may also have a reference bit. When a page is referenced (i e reading or writing) itsa page is referenced (i.e., reading or writing), its reference bit is set. It is initialized to 0 when a page is brought in.

15

gBoth bits are set by hardware automatically.

Page 16: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Page Replacement Algorithmsg p gWe shall discuss the following page replacement

l i halgorithms:First-In-First-Out - FIFOThe Least Recently Used – LRUThe Optimal Algorithmp gThe Second Chance AlgorithmThe Clock AlgorithmThe Clock Algorithm

The fewer number of page faults an algorithm generates, the better the algorithm performs.generates, the better the algorithm performs.Page replacement algorithms work on page numbers. A string of page numbers is referred to

16

numbers. A string of page numbers is referred to as a page reference string.

Page 17: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The FIFO AlgorithmThe FIFO algorithm always selects the “oldest” page to be the victim.p g

0 1 2 3 0 1 4 0 1 2 3 40 0 0 3 3 3 4 4 4 4 4 43 frames

1 1 1 0 0 0 0 0 2 2 22 2 2 1 1 1 1 1 3 3

page fault=9 miss ratio=9/12=75% hit ratio = 25%0 1 2 3 0 1 4 0 1 2 3 4

0 0 0 0 0 0 4 4 4 4 3 3

1 1 1 1 1 1 0 0 0 0 44 frames

1 1 1 1 1 1 0 0 0 0 4

2 2 2 2 2 2 1 1 1 1

3 3 3 3 3 3 2 2 217

3 3 3 3 3 3 2 2 2

page fault=10 miss ratio=10/12=83.3% hit ratio = 16.7%

Page 18: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Belady AnomalyBelady AnomalyIntuitively increasing the number of page framesIntuitively, increasing the number of page frames should reduce the number of page faults.H l t l ith d tHowever, some page replacement algorithms do not satisfy this “intuition.” The FIFO algorithm is an

lexample.Belady Anomaly: Page faults may increase as the number of page frames increases.FIFO was used in DEC VAX-78xx series and NT because it is easy to implement: append the new page to the tail and select the head to be a victim!

18

p g

Page 19: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The LRU Algorithm: 1/2gThe LRU algorithm always selects the page that has not been used for the longest period of time.has not been used for the longest period of time.

0 1 2 3 0 1 4 0 1 2 3 40 0 0 3 3 3 4 4 4 2 2 23 frames

1 1 1 0 0 0 0 0 0 3 32 2 2 1 1 1 1 1 1 4

page fault=10 miss ratio=10/12=83.3% hit ratio = 16.7%0 1 2 3 0 1 4 0 1 2 3 4

0 0 0 0 0 0 0 0 0 0 0 4

1 1 1 1 1 1 1 1 1 1 14 frames

1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 4 4 4 4 3 3

3 3 3 3 3 3 2 2 219

3 3 3 3 3 3 2 2 2

page fault=8 miss ratio=8/12=66.7% hit ratio = 33.3%

Page 20: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The LRU Algorithm: 2/2gThe memory content of 3-frames is a subset of the memory content of 4-frames. This is the inclusion property. With p p ythis property, Belady anomaly never occurs. Why?

0 1 2 3 0 1 4 0 1 2 3 40 1 2 3 0 1 4 0 1 2 3 40 0 0 3 3 3 4 4 4 2 2 2

1 1 1 0 0 0 0 0 0 3 31 1 1 0 0 0 0 0 0 3 32 2 2 1 1 1 1 1 1 4

0 0 0 0 0 0 0 0 0 0 0 4

0 1 2 3 0 1 4 0 1 2 3 4extra 0 0 0 0 0 0 0 0 0 0 0 4

1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 4 4 4 4 3 3

ext a

20

2 2 2 2 4 4 4 4 3 3

3 3 3 3 3 3 2 2 2

Page 21: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Optimal Algorithm: 1/2gThe optimal algorithm always selects the page that will not be used for the longest period of timewill not be used for the longest period of time.

0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 0 0 2 2 23 frames

1 1 1 1 1 1 1 1 1 3 32 3 3 3 4 4 4 4 4 4

page fault=7 miss ratio=7/12=58.3% hit ratio = 41.7%0 1 2 3 0 1 4 0 1 2 3 4

0 0 0 0 0 0 0 0 0 0 3 3

1 1 1 1 1 1 1 1 1 1 14 frames

1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2

3 3 3 4 4 4 4 4 421

3 3 3 4 4 4 4 4 4

page fault=6 miss ratio=6/12=50% hit ratio = 50%

Page 22: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Optimal Algorithm: 2/2The optimal algorithm always delivers the fewest page faults, if it can be implemented. It also satisfiespage faults, if it can be implemented. It also satisfies the inclusion property (i.e., no Belady anomaly).

0 1 2 3 0 1 4 0 1 2 3 40 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 0 0 2 2 2

1 1 1 1 1 1 1 1 1 3 31 1 1 1 1 1 1 1 1 3 32 3 3 3 4 4 4 4 4 4

0 0 0 0 0 0 0 0 0 0 3 3

0 1 2 3 0 1 4 0 1 2 3 4extra 0 0 0 0 0 0 0 0 0 0 3 3

1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2

ext a

22

2 2 2 2 2 2 2 2 2 2

3 3 3 4 4 4 4 4 4

Page 23: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Inclusion PropertyThe Inclusion Property

Define the following notations:Define the following notations:P = <p1, p2, …, pn> : a page trace

f fm : the number of page framesMt(P, α, m) : the memory content after page pt is

f d i h lreferenced with respect to a page replacement algorithm α.

A l l i h i fi hA page replacement algorithm satisfies the inclusion property if Mt(P,α,m) ⊆ Mt(P,α,m+1)holds for every tholds for every t.Homework: Inclusion property means no Belady anomaly

23

anomaly.

Page 24: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

LRU Approximation AlgorithmsLRU Approximation Algorithms

FIFO h B l d l th O ti l l ithFIFO has Belady anomaly, the Optimal algorithm requires the knowledge in the future, and the LRU

l ith i t i f f th talgorithm requires accurate info of the past.The optimal and LRU algorithms are difficult to implement, especially the optimal algorithm. Thus, LRU approximation algorithms are needed. We will discuss three:

The Second-Chance AlgorithmgThe Clock AlgorithmThe Enhanced Second-Chance Algorithm

24

The Enhanced Second-Chance Algorithm

Page 25: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Second-Chance Algorithm: 1/3gThe second chance algorithm is a FIFO algorithm. It uses the reference bit of each pageIt uses the reference bit of each page.The page frames are in page-in order (linked-list).If a page frame is needed check the oldest (head):If a page frame is needed, check the oldest (head):

If its reference bit is 0, take this oneOtherwise, clear the reference bit, move it to the tail, and (perhaps) set the current time. This gives this page frame a second chancegives this page frame a second chance.

Repeat this procedure until a 0 reference bit page is found Do page out and page in if necessary andfound. Do page-out and page-in if necessary, and move it to the tail.Problem: Page frames are moved too frequently

25

Problem: Page frames are moved too frequently.

Page 26: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Second-Chance Algorithm: 2/3The Second Chance Algorithm: 2/3new page = X

26

Page 27: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Second Chance Algorithm: 3/3The Second-Chance Algorithm: 3/3new page = X

27

Page 28: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Clock Algorithm: 1/2The Clock Algorithm: 1/2

If th d h l ith i i l t dIf the second chance algorithm is implemented with a circular list, we have the clock algorithm.A “next” pointer is needed.When a page frame is needed, we examine the p gpage under the “next” pointer:

If its reference bit is 0, take itIf its reference bit is 0, take itOtherwise, clear the reference bit and advance the “next” pointerthe next pointer.

Repeat this until a 0 reference bit frame is found.

28Do page-in and page-out, if necessary

Page 29: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Th Cl k Al ith 2/2The Clock Algorithm: 2/2new page = X

29

Page 30: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Enhanced Second-Chance Algorithm: 1/5

Four page lists based on their reference-modify bits (r,c) are used:

Q00 - pages were not recently referenced and not modified, the best candidates!Q01 - pages were changed but not recently Q p g g yreferenced. Need a page-out. Q10 - pages were recently used but cleanQ10 pages were recently used but clean.Q11 - pages were recently used and modified. Need a page outNeed a page-out.

30

Page 31: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Enhanced Second-Chance Algorithm: 2/5

We still need a “next” pointer.When a page frame is needed:

Does the “next” frame has 00 combination? If yes, victim is found. Otherwise, reset the reference bit and move this page to the

di li (i Q10 Q11)corresponding list (i.e., Q10 or Q11).If Q00 becomes empty, check Q01. If there is a f i h 01 bi i i i h i iframe with 01 combination, it is the victim. Otherwise, reset the reference bit and move the frame to the corresponding list (i e Q10 or Q11)frame to the corresponding list (i.e., Q10 or Q11).If Q01 becomes empty, move Q10 to Q00 and Q11 to Q01 Restart the scanning process

31

Q11 to Q01. Restart the scanning process.

Page 32: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Enhanced Second-Chance Algorithm: 3/5

1 11 5 10 8 11 10 11Q00 Q01 Q10 Q11

123

111011

567

101110

89

1111

101112

111111

4 11

56

1011

89

1111

1011

1111

Q00 Q01 Q10 Q11

267

1110

9 11 1112

11112 00

1 01134

010101

32

Page 33: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Enhanced Second-Chance Algorithm: 4/5

56

1011

89

1111

1011

1111

Q00 Q01 Q10 Q11

267

1110

9 11 1112

11112 00

1 01

34

0101

10 118 11Q00 Q01 Q10 Q11

1112

1111

1 01

29 11

2 00

5 00 134

010101

57

0000

33

4 01

6 01

Page 34: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

1011

1111

89

1111

Q00 Q01 Q10 Q11

1112

1111

1 01

29 11

2 00

5 00

This algorithm was usedin IBM DOS/VS and

134

010101

57

0000 MacOS!

6 01

Q00 Q01 Q10 Q11101112

111111

22 00

5 00

Q Q Q Q89

0101

12 11

13

0101

7 00

34

0101

6 01

34

6

Page 35: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Other Important IssuesOther Important Issues

Gl b l L l All tiGlobal vs. Local AllocationLocality of ReferenceThrashingThe Working Set ModelThe Working Set ModelThe Working Set Clock AlgorithmP F lt F R l t Al ithPage-Fault Frequency Replacement Algorithm

35

Page 36: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Global vs Local ReplacementGlobal vs. Local Replacement

Global replacement allows a process to select aGlobal replacement allows a process to select a victim from the set of all page frames, even if the page frame is currently allocated to another processpage frame is currently allocated to another process.Local replacement requires that each process selects

i ti f it t f ll t d fa victim from its own set of allocated frames.With a global replacement, the number of frames allocated to a process may change over time, and, as a result, paging behavior of a process is affected by other processes and may be unpredictable.

36

Page 37: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Global vs Local: A ComparisonGlobal vs. Local: A Comparison

With a global replacement algorithm a process cannotWith a global replacement algorithm, a process cannot control its own page fault rate, because the behavior of a process depends on the behavior of other processesa process depends on the behavior of other processes. The same process running on a different system may have a totally different behavior.yWith a local replacement algorithm, the set of pages of a process in memory is affected by the paging behavior p y y p g gof that process only. A process does not have the opportunity of using other less used frames. Performance may be lower.With a global strategy, throughput is usually higher,

37and is commonly used.

Page 38: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Locality of Reference

During any phaseDuring any phase of execution, the process referencesprocess references only a relatively small fraction ofsmall fraction of pages.

38

Page 39: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

ThrashingThrashingThrashing means a process spends more timeThrashing means a process spends more time paging than executing (i.e., low CPU utilization and high paging rate)and high paging rate).If CPU utilization is too low, the medium-term

h d l i i k d t ischeduler is invoked to swap in one or more swapped-out processes or bring in one or more

j b Th b f inew jobs. The number of processes in memory is referred to as the degree of

lti imultiprogramming.

39

Page 40: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Degree of Multiprogramming: 1/3g g gWe cannot increase the degree of multiprogramming arbitrarily as throughput will drop at certain point and thrashing occurs.Therefore, the medium-term scheduler must maintain the optimal degree of multiprogramming.

40

Page 41: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Degree of Multiprogramming: 2/3g p g g1. Suppose we use a global strategy and the CPU

ili i i l Th di h d lutilization is low. The medium-term scheduler will add a new process.

2 S hi i I2. Suppose this new process requires more pages. It starts to have more page faults, and page frames of other processes will be taken by this processof other processes will be taken by this process.

3. Other processes also need these page frames. Thus they start to have more page faultsThus, they start to have more page faults.

4. Because pages must be paged- in and out, these processes must wait and the number of processesprocesses must wait, and the number of processes in the ready queue drops. CPU utilization is lower.

41

lower.

Page 42: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Degree of Multiprogramming: 3/3Degree of Multiprogramming: 3/3

5 Consequently the medium term scheduler brings5. Consequently, the medium-term scheduler brings in more processes into memory. These new processes also need page frames to run causingprocesses also need page frames to run, causing more page faults.

6 Thus CPU utilization drops further causing the6. Thus, CPU utilization drops further, causing the medium-term scheduler to bring in even more processes.p

7. If this continues, the page fault rate increases dramatically, and the effective memory access y, ytime increases. Eventually, the system is paralyzed because the processes are spending

42almost all time to do paging!

Page 43: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Working Set Model: 1/4The working set of a process at virtual time t, written as W(t,θ), is the set of pages that were ( ), p greferenced in the interval (t- θ, t], where θ is the window size.θ = 3. The result is identical to that of LRU:

0 1 2 3 0 1 4 0 1 2 3 40 0 0 3 3 3 4 4 4 2 2 2

1 1 1 0 0 0 0 0 0 3 32 2 2 1 1 1 1 1 1 4

page fault=10 miss ratio=10/12=83.3% hit ratio = 16.7%43

p g f

Page 44: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Working Set Model: 2/4The Working Set Model: 2/4However, the result of θ = 4 is different from thatHowever, the result of θ 4 is different from that of LRU.

0 0 0 0 0 0 0 0 0 0 0 4

0 1 2 3 0 1 4 0 1 2 3 4

1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 4 4 4 4 3 3

3 3 3 3 2 2 2

page fault=8 miss ratio=8/12=66 7% hit ratio = 33 3%page fault=8 miss ratio=8/12=66.7% hit ratio = 33.3%

44only three pages here

Page 45: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Working Set Model: 3/4The Working Set Model: 3/4The Working Set Policy: Find a good θ, and keepThe Working Set Policy: Find a good θ, and keep W(t,θ) in memory for every t. What is the best value of θ? This is a system tuningWhat is the best value of θ? This is a system tuning issue. This value can change as needed from time to time.

45

Page 46: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Working Set Model: 4/4The Working Set Model: 4/4

U f t t l lik LRU th ki t liUnfortunately, like LRU, the working set policy cannot be implemented directly, and an

i ti iapproximation is necessary.But, the working set model does satisfy the inclusion property.A commonly used algorithm is the Working Set y g gClock algorithm, WSClock. This is a good and efficient approximation.pp

46

Page 47: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The WSClock Algorithm47

The WSClock Algorithm

Page 48: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Page-Fault Frequency Algorithm: 1/2g q y g

Since thrashing is due to high page-fault rate, we g g p g ,can control thrashing by controlling page-fault rate.If the page-fault rate of a process is too high, this process needs more page frames. On the otherprocess needs more page frames. On the other hand, if the page-fault rate is too low, this process may causes too many page frames.may causes too many page frames.Therefore, if we can always maintain the page-fault rate of a process to certain level we controlfault rate of a process to certain level, we control the number of page frames that process can have.

48

Page 49: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

The Page-Fault Frequency Algorithm: 2/2We establish an upper bound and a lower bound, and monitor the page-fault rate periodically.If the rate is higher (resp., lower) than the upper (resp., lower) bound, a new (resp., existing) page is allocated to (resp removed from) this processallocated to (resp., removed from) this process.This algorithm may not satisfy the inclusion property.property.

49

Page 50: Part IIIPart III Storage ManagementStorage Management · Performance Issue:Performance Issue: 1/2 Let p be the ppypgrobabilit y of a page fault, the ppgage fault rate , 0 ≤p ≤

Th E dThe End

50