67
Lecture 9: Demand Paging CSE 120: Principles of Opera>ng Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Lecture9:DemandPagingCSE120:PrinciplesofOpera>ngSystems

UCSanDiego:SummerSessionI,2009FrankUyeda

Page 2: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Announcements

•  Project2isdueonTonight•  Homework3isdueMonday.

•  FinalExam:3p‐6ponSaturday,August1

2

Page 3: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PeerWise

•  Processesareloadedandthenremovedfrommemory,crea>ngfreememoryspacebrokenintoliTlepieces.Whattypeoffragmenta>onisthis?–  Internal– External

3

Page 4: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PeerWise

•  Whatisthedifferencebetween"Segmen>on"and"Paging"?(Choosetheonewiththehighesttruthvalue)–  A.Lehastheanswer.–  B.Segmenta>onisnon‐con>nousalloca>onofpagesof

memory,wherepagingiscon>nousalloca>onofpagesofmemory.

–  C.Segmenta>onistheuseofvariablepagesizes,andpagingistheuseofdiscretepagesizes.

–  D.C–  E.D&A

•  Really?!–  Noneoftheanswersarecorrect…except,maybeA.

4

Page 5: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:Paging

55

Page1

Page2

Page3

PageP

PhysicalMemoryPage1

Page2

Process1’sVAS

…..

0x00000000

0x00040000

0x00000400

...

PageN

..

PageM

Page3

…..

Page1

Page2

Process2’sVAS

PageQ

Page3

MMU

TLB

PageTable

V PageFrame

1 0x04

1 0x01

1 0x05

1 0x07

V PageFrame

1 0x04

1 0x01

1 0x05

1 0x07

Page 6: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:VirtualMemory

•  MemoryManagementUnit(MMU)–  Hardwareunitthattranslatesavirtualaddresstoaphysicaladdress

•  Transla>onTable–  Storedinmainmemory–  EachPageTableEntrystorespermissionandbookkeepingbits.

•  Transla>onLookasideBuffer(TLB)–  HardwarecachefortheMMU’svirtual‐to‐physicaltransla>onstable

6

CPUTransla>on

TableMMU

Memory

VirtualAddress

PhysicalAddress

TLB

PageFrameNumberProtVRM

Page 7: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:PageTables

7

Page1

Page2

Page3

PageN

PhysicalMemory…..

Pageframe Offset

PhysicalAddress

Pagenumber Offset

VirtualAddress

Pagetableentry

PageTable

0xFFFFFFFF

0x00000000

Problem:PageTablescanbelarge!

Page 8: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:2LevelPageTable

8

Page1

Page2

Page3

PageN

PhysicalMemory…..

Pageframe Offset

PhysicalAddress

Secondary Offset

VirtualAddress

SecondaryPageTable(s)

0xFFFFFFFF

0x00000000

Master

Pagetableentry

MasterPageTable

Pageframe(PTE)Pageframe(PTE)

Page 9: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

InvertedPageTable

9

Page1

Page2

Page3

PageN

PhysicalMemory…..

index Offset

PhysicalAddress

VPN Offset

VirtualAddress

0xFFFFFFFF

0x00000000

PID

<PID,VPN>

InvertedPageTable

index

Page 10: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

InvertedPageTable

•  Oneglobalpagetable–  Onepagetableentryperphysicalpage.–  EntrieskeyedbyPIDandvirtualpagenumber.–  Physicalframenumber=indexinpagetable.

•  Advantages–  Boundedamountofmemoryforpagetable(s).

•  32‐bitaddressspace,4Kpages,4GBRAM,4BperPTE•  1‐levelpagetable:_____x#processes.•  InvertedTable:______MB

•  Disadvantages–  Costlytransla>on

•  Usinghashingcanhelp

10

Page 11: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:Segmenta>on

11

Heap

Text

Data

Stack

Page 12: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:Segmenta>on

12

PhysicalMemorySegmentTable

limit baseVirtualAddress

Segment# Offset

< +Yes?

No?

Protec>onFault

Page 13: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

GoalsforToday

•  PagingTricks– DemandPaging

– CopyonWrite

•  PageReplacementPolicies– Whichpagesshouldwekeepinmemory?– Howmanypagesshouldeachprocessget?

13

Page 14: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PagedVirtualMemory•  Whatifthereisn’tenoughphysicalmemoryavailableto

loadaprogram?–  Alargepor>onofprogram’scodemaybeunused–  Alargepor>onofprogram’scodemaybeusedinfrequently

•  Idea:loadapageonlyifyouneeditProcess’sVirtualAddressSpace Machine’sPhysicalAddressSpace

(availableRAM)

14

Page 15: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

DemandPaging

•  We’vemen>onedbeforethatpagescanbemovedbetweenmemoryanddisk–  Thisprocessiscalleddemandpaging

•  OSusesmainmemoryasapagecacheofallthedataallocatedbyprocessesinthesystem–  Ini>ally,pagesareallocatedfrommemory– Whenmemoryfillsup,alloca>ngapageinmemoryrequiressomeotherpagetoevictedfrommemory

•  Whyphysicalmemorypagesarecalled“frames”–  Evictedpagesgotodisk(where?Theswapfilepar>>on)–  ThemovementofpagesbetweenmemoryanddiskisdonebytheOS,andistransparenttotheapplica>on

15

Page 16: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PagedVirtualMemory•  Aprogram’spagesloadedondemand•  Wherearepagesotherwise?

–  Ondisk(executable)–  InOSswapfile(anactualfileondisk)–  Shared(morelater)

Process’sVirtualAddressSpace

Machine’sPhysicalAddressSpace(availableRAM)

/swap~/program.coff

Disk

16

Page 17: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

DemandPagingExampleProcess’sVirtualAddressSpace PhysicalMemory

/swap~/program.coff

Disk

PC

PC

0x00 1 0x000x01 0 ‐‐‐0x02 0 ‐‐‐0x03 0 ‐‐‐

0x11 0 ‐‐‐0x10 0 ‐‐‐

VPN V PPNPageTable

0x01 1 0x01

17

Page 18: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

DemandPagingExampleProcess’sVirtualAddressSpace PhysicalMemory

/swap~/program.coff

Disk

PC

0x00 1 0x000x01 0 ‐‐‐0x02 1 0x020x03 1 0x03

0x11 0 ‐‐‐0x10 1 0x09

VPN V PPNPageTable

0x01 1 0x01

0x10 0 0x00??

0x04 1 0x09

0x00

0x090x0A

18

Page 19: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageFaults•  Whathappenswhenaprocessaccessesapagethathasbeen

evicted(orisotherwisenotinmemory)?–  1.Whenitevictsapage,theOSsetsthePTEasinvalidand

storestheloca>onofthepageintheswapfileinthePTE–  2.Whenaprocessaccessesthepage,theinvalidPTEwill

causeatrap(pagefault)–  3.ThetrapwillruntheOSpagefaulthandler–  4.HandlerusestheinvalidPTEtolocatepageinswapfile–  5.Readspageintoaphysicalframe,updatesPTEtopointtoit–  6.Restartsprocess

•  Butwheredoesitputit?Hastoevictsomethingelse–  OSusuallykeepsapooloffreepagesaroundsothatalloca>onsdonot

alwayscauseevic>ons–  Replaceanotherpage

•  Evictedpagemaygoinswapfile.WhichpagesneedtobewriTenouttoswap?

19

Page 20: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:PageLookups

20

CPU

PageTable

Memory

VirtualAddress

PhysicalAddress

TLB

Disk

Page 21: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:PageLookups

21

CPU

PageTable

Memory

VirtualAddress

PhysicalAddress

TLB

Disk

SecondaryPageTable

.....

Page 22: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:PageLookups

22

CPU

PageTable

Memory

VirtualAddress

PhysicalAddress

TLB

Disk

SecondaryPageTable

Case1:VirtualAddress–to‐PhysicalAddressMappingisinTLB

VPN V R M RO PFN

0x000 1 1 0 1 0x100

0x121 1 0 0 0 0x111

0x071 1 1 0 0 0x212

Howmanymemoryreferences?TLB_hit:1memoryreference

(1)

Page 23: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:PageLookups

23

CPU

PageTable

Memory

VirtualAddress

PhysicalAddress

TLB

Disk

SecondaryPageTable

Case2:VirtualAddress–to‐PhysicalAddressMappingisnotinTLBbutisinMemory

VPN V R M RO PFN

0x000 1 1 0 1 0x100

0x121 1 0 0 0 0x111

0x071 1 1 0 0 0x212

Howmanymemoryreferences?Mem_hit:1pagetablelookup +1memoryreferenceTwo‐levelPagetable?

(1)

(2)

Page 24: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Recap:PageLookups

24

CPU

PageTable

Memory

VirtualAddress

PhysicalAddress

TLB

Disk

SecondaryPageTable

Case3:VirtualAddress–to‐PhysicalAddressMappingisnotinTLBandnotinMemory

VPN V R M RO PFN

0x000 1 1 0 1 0x100

0x121 0 0 0 0 0x111

0x071 1 1 0 0 0x212

Howmanymemoryreferences?Mem_miss:1pagetablelookup +1pagetableupdate(s)* +1memoryreference

Howmuch6me?diskretrievalinvolvedtoo

(1)

(2)

(3?)

(4?)

Page 25: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

MemoryAccesses

•  TLBHit:fast–  1TLBaccess+1memoryaccess

•  MemoryHit:medium–  1TLBaccess+2+memoryaccess

•  MemoryMiss:slow–  1TLBaccess+3+memoryaccess+1diskaccess

•  Wantthingsfastaspossible;minimize>mespentresolvingphysicaladdress.Howdoyoudothis?–  Minimizenumberofmemoryreferences(TLBpolicy)–  Minimizenumberofpagefaults(Pagereplacementpolicy)

25

Page 26: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Observa>on

•  OSesspendalotof>mecopyingdata– Systemcallargumentsbetweenuser/kernelspace

– En>readdressspacestoimplementfork()

•  Howcanwereducethiscost?

26

Page 27: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageSharingEmacsbuffer#1

PhysicalMemory

0x00 1 0x03

0x01 0 ‐‐‐0x10 1 0x120x11 1 0x15

VPN V PPN

0x03 1 0x11

0x00 1 0x03

0x01 0 ‐‐‐0x10 1 0x140x11 1 0x16

VPN V PPN

0x03 1 0x13

Emacsbuffer#2

..

Whatifsharedpageismodified?

0x04

27

Page 28: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Copy‐On‐Write

•  OSesspendalotof>mecopyingdata–  Systemcallargumentsbetweenuser/kernelspace–  En>readdressspacestoimplementfork()

•  UseCopy‐on‐Write(CoW)todeferlargecopiesaslongaspossible,hopingtoavoidthemaltogether–  Insteadofcopyingpages,createsharedmappingsofparentpagesinchildvirtualaddressspace

–  Sharedpagesareprotectedasread‐onlyinchild•  Readshappenasusual•  Writesgenerateaprotec>onfault,traptoOS,copypage,changepagemappinginclientpagetable,restartwriteinstruc>on

•  Howdoesthishelpfork()?

28

Page 29: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageSharing:CoWEmacsbuffer#1

PhysicalMemory

0x00 1 0x03

0x01 0 ‐‐‐0x10 1 0x120x11 1 0x15

VPN V PPN

0x03 1 0x11

Emacsbuffer#2

..

0M

000

0x00 1 0x03

0x01 0 ‐‐‐0x10 1 0x120x11 1 0x16

VPN V PPN

0x03 1 0x13

0M

000

BuffersshareVPN0x12Processwritestopage

1

29

Page 30: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageSharing:CoWEmacsbuffer#1

PhysicalMemory

0x00 1 0x03

0x01 0 ‐‐‐0x10 1 0x120x11 1 0x15

VPN V PPN

0x03 1 0x11

Emacsbuffer#2

..

0M

000

0x00 1 0x03

0x01 0 ‐‐‐0x10 1 0x140x11 1 0x16

VPN V PPN

0x03 1 0x13

0M

000

BuffersshareVPN0x12ProcesswritestopageNewpageisallocated

30

Page 31: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PagingTricksSummary

•  DemandPaging– Observa>on:LimitedavailableRAM,notallpagesused

–  Idea:Loadpagesonlywhenneeded•  PageSharing

– Observa>on:Ozen>mes,processeshavesimilarvirtualpages

–  Idea:Copy‐on‐writeforefficientpagesharing

31

Page 32: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

ActualMemorySta>s>cs3.7GBused,4.1GBavailable=>90+%memoryused2.1GBswapallocated

32

Page 33: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

ActualMemorySta>s>cs~10%‐50%ofmemoryissharedperprocess=>~370MB–1.85GBmemsaved

33

Page 34: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

ActualMemorySta>s>csWhichpagestoevict?Howmuchphysicalmemoryshouldeachprocessget?

34

Page 35: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageReplacement

•  Problem:Memoryisscarce–  Idea:let’suseonlypagesthatweneed

•  Pagereplacementpolicy:– Howdowedecidewhichpagestokickoutwhenmemoryisfullornearfull?

– Howmuchmemorydoweallocatetoeachprocess?

35

Page 36: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageReplacementPolicy

•  Wanttokeepmostac>vepagesinmemory– Conversely,wanttokickoutinac6vepages

•  Whatcanwedo?– Swapping:suspendanen>reprocessandputsallofitspagestodisk/swap

– Paging:removeunusedorinfrequentlyusedpagesforanyprocesstodisk/swap

36

Page 37: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Goal:Evic>ngtheBestPage

•  Thegoalofthereplacementalgorithmistoreducethepagefaultrate–  Selectthebestvic>mpagetoremove

•  Thebestpagetoevictistheonenevertouchedagain–  Processwillneveraskforit,soitwillneverfaultonit

•  Howdoweknowthatthereabestpage?•  Neverisalong>me,sopickingthepageclosestto

“never”isthenextbestthing–  Evic>ngthepagethatwon’tbeusedforthelongestperiodof

>meminimizesthenumberofpagefaults–  ProvedbyBelady

37

Page 38: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Locality

•  Allpagingschemesdependonlocality–  ProcessesreferencepagesinlocalizedpaTerns

•  Temporallocality–  Loca>onsreferencedrecentlylikelytobereferencedagain

•  Spa>allocality–  Loca>onsnearrecentlyreferencedloca>onsarelikelytobereferencedsoon

•  Althoughthecostofpagingishigh,ifitisinfrequentenoughitisacceptable–  Processesusuallyexhibitbothkindsoflocalityduringtheirexecu>on,makingpagingprac>cal

38

Page 39: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Goal:Evic>ngtheBestPage

•  Thegoalofthereplacementalgorithmistoreducethepagefaultrate–  Selectthebestvic>mpagetoremove

•  Thebestpagetoevictistheonenevertouchedagain–  Processwillneveraskforit,soitwillneverfaultonit

•  Howdoweknowthatthereabestpage?Locality•  Neverisalong>me,sopickingthepageclosestto

“never”isthenextbestthing–  Evic>ngthepagethatwon’tbeusedforthelongestperiodof

>meminimizesthenumberofpagefaults–  ProvedbyBelady

39

Page 40: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Belady’sAlgorithm

•  Belady’salgorithmisknownastheop>malpagereplacementalgorithm–  Lowestfaultrateforanypagereferencestream–  Idea:replacethepagethatwillnotbeusedforthelongest>me

inthefuture–  Problem:havetopredictthefuture

•  WhyisBelady’susefulthen?Useitasayards>ck–  Compareimplementa>onsofpagereplacementalgorithmswith

theop>maltogaugeroomforimprovement–  Ifop>malisnotmuchbeTer,thenalgorithmispreTygood–  Ifop>malismuchbeTer,thenalgorithmcouldusesomework

•  Randomreplacementisozenthelowerbound•  Butrandomisnotnecessarilytheworst…..whatwouldbe?

Pagereferences:

1 2 3 4 1 2 5 1 2 3 4 5

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

2 3 4 4 2 5 5 5 5 5 52PagesRAM

9pagefaults

40

Page 41: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Belady’sAlgorithm

•  Belady’salgorithmisknownastheop>malpagereplacementalgorithm–  Lowestfaultrateforanypagereferencestream–  Idea:replacethepagethatwillnotbeusedforthelongest>me

inthefuture–  Problem:havetopredictthefuture

•  WhyisBelady’susefulthen?Useitasayards>ck–  Compareimplementa>onsofpagereplacementalgorithmswith

theop>maltogaugeroomforimprovement–  Ifop>malisnotmuchbeTer,thenalgorithmispreTygood–  Ifop>malismuchbeTer,thenalgorithmcouldusesomework

•  Randomreplacementisozenthelowerbound•  Butrandomisnotnecessarilytheworst…..whatwouldbe?

41

Page 42: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

First‐InFirst‐Out(FIFO)

•  FIFOisanobviousalgorithmandsimpletoimplement– Maintainalistofpagesinorderinwhichtheywerepagedin

–  Onreplacement,evicttheonebroughtinlongest>meago

•  Whymightthisbegood?– Maybetheonebroughtinthelongestagoisnotbeingused

•  Whymightthisbebad?–  Thenagain,maybeit’snot

•  Wedon’thaveanyinforma>ontosayonewayoranother

42

Page 43: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

FIFOPageReplacementExamplePagereferences:

1 2 3 4 1 2 5 1 2 3 4 5

1 1 3 3 1 1 5 5 2 2 4 4

2 2 4 4 2 2 1 1 3 3 52PagesRAM

3PagesRAM

4PagesRAM

12pagefaults

43

Page 44: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

FIFOPageReplacementExamplePagereferences:

1 2 3 4 1 2 5 1 2 3 4 5

1 1 3 3 1 1 5 5 2 2 4 4

2 2 4 4 2 2 1 1 3 3 5

1 1 1 4 4 4 5 5 5 5 5 5

2 2 2 1 1 1 1 1 3 3 3

3 3 3 2 2 2 2 2 4 4

2PagesRAM

3PagesRAM

4PagesRAM

12pagefaults

9pagefaults

44

Page 45: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

FIFOPageReplacementExamplePagereferences:

1 2 3 4 1 2 5 1 2 3 4 5

1 1 3 3 1 1 5 5 2 2 4 4

2 2 4 4 2 2 1 1 3 3 5

1 1 1 4 4 4 5 5 5 5 5 5

2 2 2 1 1 1 1 1 3 3 3

3 3 3 2 2 2 2 2 4 4

1 1 1 1 1 1 5 5 5 5 4 4

2 2 2 2 2 2 1 1 1 1 5

3 3 3 3 3 3 2 2 2 2

4 4 4 4 4 4 3 3 3

2PagesRAM

3PagesRAM

4PagesRAM

12pagefaults

9pagefaults

10pagefaults!

45

Page 46: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

First‐InFirst‐Out(FIFO)

•  FIFOsuffersfrom“Belady’sAnomaly”– Thefaultratemightactuallyincreasewhenthealgorithmisgivenmorememory(verybad)

46

Page 47: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

LeastRecentlyUsed(LRU)

•  LRUusesreferenceinforma>ontomakeamoreinformedreplacementdecision–  Idea:Wecan’tpredictthefuture,butwecanmakeaguessbaseduponpastexperience

– Onreplacement,evictthepagethathasnotbeenusedforthelongest>meinthepast(Belady’s:future)

– WhendoesLRUdowell?WhendoesLRUdopoorly?•  Implementa>on

–  Tobeperfect,needto>mestampeveryreference(ormaintainastack)–muchtoocostly

–  Soweneedtoapproximateit

47

Page 48: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

LRUPageReplacementExamplePagereferences:

1 2 3 4 1 2 5 1 2 3 4 5

1 1 3 3 1 1 5 5 2 2 4 4

2 2 4 4 2 2 1 1 3 3 52PagesRAM

3PagesRAM

4PagesRAM

12pagefaults

48

Page 49: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

LRUPageReplacementExamplePagereferences:

1 2 3 4 1 2 5 1 2 3 4 5

1 1 3 3 1 1 5 5 2 2 4 4

2 2 4 4 2 2 1 1 3 3 5

1 1 1 4 4 4 5 5 5 3 3 3

2 2 2 1 1 1 1 1 1 4 4

3 3 3 2 2 2 2 2 2 5

1 1 1 1 1 1 1 1 1 1 1 5

2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 5 5 5 5 4 4

4 4 4 4 4 4 3 3 3

2PagesRAM

3PagesRAM

4PagesRAM

12pagefaults

10pagefaults

8pagefaults

49

Page 50: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

LeastRecentlyUsed(LRU)

•  LRUusesreferenceinforma>ontomakeamoreinformedreplacementdecision–  Idea:Wecan’tpredictthefuture,butwecanmakeaguessbaseduponpastexperience

– Onreplacement,evictthepagethathasnotbeenusedforthelongest>meinthepast(Belady’s:future)

– WhendoesLRUdowell?WhendoesLRUdopoorly?•  Implementa>on

–  Tobeperfect,needto>mestampeveryreference(ormaintainastack)–muchtoocostly

–  Soweneedtoapproximateit

50

Page 51: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Approxima>ngLRU

•  LRUapproxima>onsusethePTEreferencebit–  Keepacounterforeachpage–  Atregularintervals,foreverypagedo:

•  Ifrefbit=0,incrementcounter

•  Ifrefbit=1,zerothecounterandthereferencebit–  Thecounterwillcontainthenumberofintervalssincethelastreferencetothepage

–  Thepagewiththelargestcounteristheleastrecentlyused•  Somearchitecturesdon’thaveareferencebit

–  Cansimulatereferencebitusingthevalidbittoinducefaults–  Whathappenswhenwemakeapageinvalid?

51

Page 52: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Approxima>ngLRU

VPN PFN CNTRR

0x00

0x01

0x02

0x03

0x04

0x05

0xA1

0xA2

0xA0

0x10

0x03

0x04

0

0

0

0

0

0

0000

0000

0000

0000

0000

0000

PageTable

CNTR

0001

0001

0001

0001

0001

0001

Interval1

CNTR

0002

0000

0000

0002

0002

0000

Interval2

CNTR

0003

0001

0001

0003

0003

0001

Interval3

CNTR

0004

0000

0000

0004

0004

0002

Interval4

0

1

1

0

0

1

0

0

0

0

0

0

0

1

1

0

0

0

Whichpage(s)wouldbeevictednextbasedonApproximateLRU?Whatdoesagoodapproxima>ondependupon?

52

Page 53: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Approxima>ngLRU

•  LRUapproxima>onsusethePTEreferencebit–  Keepacounterforeachpage–  Atregularintervals,foreverypagedo:

•  Ifrefbit=0,incrementcounter

•  Ifrefbit=1,zerothecounterandthereferencebit–  Thecounterwillcontainthenumberofintervalssincethelastreferencetothepage

–  Thepagewiththelargestcounteristheleastrecentlyused•  Somearchitecturesdon’thaveareferencebit

–  Cansimulatereferencebitusingthevalidbittoinducefaults–  Whathappenswhenwemakeapageinvalid?

53

Page 54: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Approxima>ngLRU:Clock

•  NotRecentlyUsed(NRU)–UsedbyUnix–  Replacepagethatis“oldenough”–  Arrangeallofphysicalpageframesinabigcircle(clock)–  AclockhandisusedtoselectagoodLRUcandidate

•  Sweepthroughthepagesincircularorderlikeaclock•  Iftherefbitisoff,ithasn’tbeenusedrecently

–  Whatistheminimum“age”ifreferencebitisoff?

•  Ifthereferencebitison,turnitoffandgotonextpage–  Armmovesquicklywhenpagesareneeded–  Lowoverheadwhenplentyofmemory–  Ifmemoryislarge,“accuracy”ofinforma>ondegrades

•  Useaddi>onalhands

54

Page 55: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Approxima>ngLRU:Clock

VPN PFN R

0x00

0x01

0x02

0x03

0xA1

0xA2

0xA0

0x10

1

1

1

1

PageTable

ClockHand

(1)

VPN PFN R

0x00

0x01

0x02

0x03

0xA1

0xA2

0xA0

0x10

0

1

1

1

(2)

VPN PFN R

0x00

0x01

0x02

0x03

0xA1

0xA2

0xA0

0x10

0

0

1

1

(3)

VPN PFN R

0x00

0x01

0x02

0x03

0xA1

0xA2

0xA0

0x10

0

0

0

1

(4)

VPN PFN R

0x00

0x01

0x02

0x03

0xA1

0xA2

0xA0

0x10

0

0

0

0

(5)

Evictpage!

JustlikeFIFO,dependingonclockhand?55

Page 56: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Approxima>ngLRU:Clock

VPN PFN R

0x00

0x01

0x02

0x03

0xA1

0xA2

0xA0

0x10

1

0

0

1

PageTable

ClockHand

(1)

VPN PFN R

0x00

0x01

0x02

0x03

0xA1

0xA2

0xA0

0x10

0

0

1

1

(2)

Evictpage!

Whatdoyouneedforthisalgorithmtoworkwell?Whatifmemoryislarge?

56

Page 57: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Approxima>ngLRU:Clock

•  NotRecentlyUsed(NRU)–UsedbyUnix–  Replacepagethatis“oldenough”–  Arrangeallofphysicalpageframesinabigcircle(clock)–  AclockhandisusedtoselectagoodLRUcandidate

•  Sweepthroughthepagesincircularorderlikeaclock•  Iftherefbitisoff,ithasn’tbeenusedrecently

–  Whatistheminimum“age”ifreferencebitisoff?

•  Ifthereferencebitison,turnitoffandgotonextpage–  Armmovesquicklywhenpagesareneeded–  Lowoverheadwhenplentyofmemory–  Ifmemoryislarge,“accuracy”ofinforma>ondegrades

•  Useaddi>onalhands

57

Page 58: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Today’slecture:PageReplacement(2)

•  Problem:Memoryisscarce–  Idea:let’suseonlypagesthatweneed

•  Pagereplacementpolicy:– Howdowedecidewhichpagestokickoutwhenmemoryisfullornearfull?

– Howmuchmemorydoweallocatetoeachprocess?

58

Page 59: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageReplacementPolicies

•  Inamul>programmingsystem,weneedawaytoallocatememorytocompe>ngprocesses

•  Problem:Howtodeterminehowmuchmemorytogivetoeachprocess?–  Fixedspacealgorithms

•  Eachprocessisgivenalimitofpagesitcanuse•  Whenitreachesthelimit,itreplacesfromitsownpages•  Localreplacement

–  Someprocessesmaydowellwhileotherssuffer

–  Variablespacealgorithms•  Process’setofpagesgrowsandshrinksdynamically•  Globalreplacement

–  Oneprocesscanruinitfortherest

59

Page 60: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

WorkingSetModel

•  Aworkingsetofaprocessisusedtomodelthedynamiclocalityofitsmemoryusage–  DefinedbyPeterDenninginthe60s

•  Defini>on–  WS(t,w) = { pages P s.t. P was referenced in the time

interval (t,t+w) } –  t = time, w = working-set window (measuredinpagereferences)

•  ApageisintheworkingsetWSonlyifitwasreferencedinthelastwreferences

60

Page 61: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

WorkingSetSize

•  Workingsetsizeisthenumberofpagesintheworkingset–  Thenumberofpagesreferencedintheinterval(t,t+w)

•  Theworkingsetsizechangeswithprogramlocality–  Duringperiodsofpoorlocality,youreferencemorepages–  Withinthatperiodof>me,theworkingsetsizeislarger

•  Intui>vely,wanttheworkingsettobethesetofpagesaprocessneedsinmemorytopreventheavyfaul>ng–  Eachprocesshasaparameterwthatdeterminesaworkingset

withfewfaults–  Denning:Don’trunaprocessunlessworkingsetisinmemory

61

Page 62: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

WorkingSetProblems

•  Problems– Howdowedeterminew?– Howdoweknowwhentheworkingsetchanges?

•  Toohardtoanswer–  So,workingsetisnotusedinprac>ceasapagereplacementalgorithm

•  However,itiss>llusedasanabstrac>on–  Theintui>oniss>llvalid– Whenpeopleask,“HowmuchmemorydoesiTunesneed?”,theyareineffectaskingforthesizeofitsworkingset.

62

Page 63: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageFaultFrequency(PFF)

•  PageFaultFrequency(PFF)isavariablespacealgorithmthatusesamoread‐hocapproach–  1.Monitorthefaultrateforeachprocess–  2.Ifthefaultrateisaboveahighthreshold:giveitmorememory

•  Hopethatitfaultsless•  Notalwaystrue(FIFO+Belady’sAnomaly)

–  3.Elseifthefaultrateisbelowalowthreshold:takeawaymemory

•  Hopethatits>lldoesn’tfaultmuch•  Mightnotfaultmoreatall

•  HardtousePFFtodis>nguishbetweenchangesinlocalityandchangesinsizeofworkingset

63

Page 64: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PFFandMul>programming•  Whatdoesthepagefaultfrequencyofprocessestellyou?

Degreeofmul>programming(#concurrentprocesses)

Canthishappen?

System“goodput”(#ofprograms’instruc>onsexecuted)

64

Page 65: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

Thrashing

•  Pagereplacementalgorithmsavoidthrashing– Whenmostofthe>meisspendbytheOSinpagingdatabackandforthfromthedisk

–  No>mespentdoingusefulwork(makingprogress)–  Inhissitua>on,thesystemisovercommiTed

•  Noideawhichpagesshouldbeinmemorytoreducefaults•  Couldjustbethatthereisn’tenoughphysicalmemoryforalloftheprocessesinthesystem

•  Ex:RunningWindowsXPwith64MBmemory…(orVistawith1GB)

–  Possiblesolu>ons•  Swapping–writeoutallpagesofaprocess•  Buymorememory

65

Page 66: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

PageReplacementSummary•  Whichpage(s)tokickoutwhenmemoryisunderconstraint?

–  Pagereplacementalgorithms•  Belady’s–op>malreplacement(min#faults)•  FIFO–replacepageloadedfurthestinpast•  LRU–replacepagereferencedfurthestinpast•  LRUClock–replacepagethatis“oldenough”•  PageFaultFrequency–grow/shrinkpagesetasafunc>onoffaultrate

•  Howmanypagesperprocess?–  Globalvslocalreplacementpolicies

•  Shouldaprocessreplaceitsownpage,orthatofanother?–  WorkingSet–keepthesetofpagesinmemorythathasminimalfault

rate(the“workingset”)•  PageFaultFrequency–grow/shrinkpagesetasafunc>onoffaultratetoavoid

thrashing

66

Page 67: Lecture 9: Demand Paging - University of California, San Diego · Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda

NextTime

•  ReadChapter10&11.1‐11.8•  HomeworkdueonMonday.

•  CheckWebsiteforcourseannouncements–  hTp://www.cs.ucsd.edu/classes/su09/cse120

67