Lecture9:DemandPagingCSE120:PrinciplesofOpera>ngSystems
UCSanDiego:SummerSessionI,2009FrankUyeda
Announcements
• Project2isdueonTonight• Homework3isdueMonday.
• FinalExam:3p‐6ponSaturday,August1
2
PeerWise
• Processesareloadedandthenremovedfrommemory,crea>ngfreememoryspacebrokenintoliTlepieces.Whattypeoffragmenta>onisthis?– Internal– External
3
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
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
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
Recap:PageTables
7
Page1
Page2
Page3
PageN
PhysicalMemory…..
Pageframe Offset
PhysicalAddress
Pagenumber Offset
VirtualAddress
Pagetableentry
PageTable
0xFFFFFFFF
0x00000000
Problem:PageTablescanbelarge!
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)
InvertedPageTable
9
Page1
Page2
Page3
PageN
PhysicalMemory…..
index Offset
PhysicalAddress
VPN Offset
VirtualAddress
0xFFFFFFFF
0x00000000
PID
<PID,VPN>
InvertedPageTable
index
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
Recap:Segmenta>on
11
Heap
Text
Data
Stack
Recap:Segmenta>on
12
PhysicalMemorySegmentTable
limit baseVirtualAddress
Segment# Offset
< +Yes?
No?
Protec>onFault
GoalsforToday
• PagingTricks– DemandPaging
– CopyonWrite
• PageReplacementPolicies– Whichpagesshouldwekeepinmemory?– Howmanypagesshouldeachprocessget?
13
PagedVirtualMemory• Whatifthereisn’tenoughphysicalmemoryavailableto
loadaprogram?– Alargepor>onofprogram’scodemaybeunused– Alargepor>onofprogram’scodemaybeusedinfrequently
• Idea:loadapageonlyifyouneeditProcess’sVirtualAddressSpace Machine’sPhysicalAddressSpace
(availableRAM)
14
DemandPaging
• We’vemen>onedbeforethatpagescanbemovedbetweenmemoryanddisk– Thisprocessiscalleddemandpaging
• OSusesmainmemoryasapagecacheofallthedataallocatedbyprocessesinthesystem– Ini>ally,pagesareallocatedfrommemory– Whenmemoryfillsup,alloca>ngapageinmemoryrequiressomeotherpagetoevictedfrommemory
• Whyphysicalmemorypagesarecalled“frames”– Evictedpagesgotodisk(where?Theswapfilepar>>on)– ThemovementofpagesbetweenmemoryanddiskisdonebytheOS,andistransparenttotheapplica>on
15
PagedVirtualMemory• Aprogram’spagesloadedondemand• Wherearepagesotherwise?
– Ondisk(executable)– InOSswapfile(anactualfileondisk)– Shared(morelater)
Process’sVirtualAddressSpace
Machine’sPhysicalAddressSpace(availableRAM)
/swap~/program.coff
Disk
16
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
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
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
Recap:PageLookups
20
CPU
PageTable
Memory
VirtualAddress
PhysicalAddress
TLB
Disk
Recap:PageLookups
21
CPU
PageTable
Memory
VirtualAddress
PhysicalAddress
TLB
Disk
SecondaryPageTable
.....
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)
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)
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?)
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
Observa>on
• OSesspendalotof>mecopyingdata– Systemcallargumentsbetweenuser/kernelspace
– En>readdressspacestoimplementfork()
• Howcanwereducethiscost?
26
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
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
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
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
PagingTricksSummary
• DemandPaging– Observa>on:LimitedavailableRAM,notallpagesused
– Idea:Loadpagesonlywhenneeded• PageSharing
– Observa>on:Ozen>mes,processeshavesimilarvirtualpages
– Idea:Copy‐on‐writeforefficientpagesharing
31
ActualMemorySta>s>cs3.7GBused,4.1GBavailable=>90+%memoryused2.1GBswapallocated
32
ActualMemorySta>s>cs~10%‐50%ofmemoryissharedperprocess=>~370MB–1.85GBmemsaved
33
ActualMemorySta>s>csWhichpagestoevict?Howmuchphysicalmemoryshouldeachprocessget?
34
PageReplacement
• Problem:Memoryisscarce– Idea:let’suseonlypagesthatweneed
• Pagereplacementpolicy:– Howdowedecidewhichpagestokickoutwhenmemoryisfullornearfull?
– Howmuchmemorydoweallocatetoeachprocess?
35
PageReplacementPolicy
• Wanttokeepmostac>vepagesinmemory– Conversely,wanttokickoutinac6vepages
• Whatcanwedo?– Swapping:suspendanen>reprocessandputsallofitspagestodisk/swap
– Paging:removeunusedorinfrequentlyusedpagesforanyprocesstodisk/swap
36
Goal:Evic>ngtheBestPage
• Thegoalofthereplacementalgorithmistoreducethepagefaultrate– Selectthebestvic>mpagetoremove
• Thebestpagetoevictistheonenevertouchedagain– Processwillneveraskforit,soitwillneverfaultonit
• Howdoweknowthatthereabestpage?• Neverisalong>me,sopickingthepageclosestto
“never”isthenextbestthing– Evic>ngthepagethatwon’tbeusedforthelongestperiodof
>meminimizesthenumberofpagefaults– ProvedbyBelady
37
Locality
• Allpagingschemesdependonlocality– ProcessesreferencepagesinlocalizedpaTerns
• Temporallocality– Loca>onsreferencedrecentlylikelytobereferencedagain
• Spa>allocality– Loca>onsnearrecentlyreferencedloca>onsarelikelytobereferencedsoon
• Althoughthecostofpagingishigh,ifitisinfrequentenoughitisacceptable– Processesusuallyexhibitbothkindsoflocalityduringtheirexecu>on,makingpagingprac>cal
38
Goal:Evic>ngtheBestPage
• Thegoalofthereplacementalgorithmistoreducethepagefaultrate– Selectthebestvic>mpagetoremove
• Thebestpagetoevictistheonenevertouchedagain– Processwillneveraskforit,soitwillneverfaultonit
• Howdoweknowthatthereabestpage?Locality• Neverisalong>me,sopickingthepageclosestto
“never”isthenextbestthing– Evic>ngthepagethatwon’tbeusedforthelongestperiodof
>meminimizesthenumberofpagefaults– ProvedbyBelady
39
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
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
First‐InFirst‐Out(FIFO)
• FIFOisanobviousalgorithmandsimpletoimplement– Maintainalistofpagesinorderinwhichtheywerepagedin
– Onreplacement,evicttheonebroughtinlongest>meago
• Whymightthisbegood?– Maybetheonebroughtinthelongestagoisnotbeingused
• Whymightthisbebad?– Thenagain,maybeit’snot
• Wedon’thaveanyinforma>ontosayonewayoranother
42
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
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
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
First‐InFirst‐Out(FIFO)
• FIFOsuffersfrom“Belady’sAnomaly”– Thefaultratemightactuallyincreasewhenthealgorithmisgivenmorememory(verybad)
46
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
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
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
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
Approxima>ngLRU
• LRUapproxima>onsusethePTEreferencebit– Keepacounterforeachpage– Atregularintervals,foreverypagedo:
• Ifrefbit=0,incrementcounter
• Ifrefbit=1,zerothecounterandthereferencebit– Thecounterwillcontainthenumberofintervalssincethelastreferencetothepage
– Thepagewiththelargestcounteristheleastrecentlyused• Somearchitecturesdon’thaveareferencebit
– Cansimulatereferencebitusingthevalidbittoinducefaults– Whathappenswhenwemakeapageinvalid?
51
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
Approxima>ngLRU
• LRUapproxima>onsusethePTEreferencebit– Keepacounterforeachpage– Atregularintervals,foreverypagedo:
• Ifrefbit=0,incrementcounter
• Ifrefbit=1,zerothecounterandthereferencebit– Thecounterwillcontainthenumberofintervalssincethelastreferencetothepage
– Thepagewiththelargestcounteristheleastrecentlyused• Somearchitecturesdon’thaveareferencebit
– Cansimulatereferencebitusingthevalidbittoinducefaults– Whathappenswhenwemakeapageinvalid?
53
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
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
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
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
Today’slecture:PageReplacement(2)
• Problem:Memoryisscarce– Idea:let’suseonlypagesthatweneed
• Pagereplacementpolicy:– Howdowedecidewhichpagestokickoutwhenmemoryisfullornearfull?
– Howmuchmemorydoweallocatetoeachprocess?
58
PageReplacementPolicies
• Inamul>programmingsystem,weneedawaytoallocatememorytocompe>ngprocesses
• Problem:Howtodeterminehowmuchmemorytogivetoeachprocess?– Fixedspacealgorithms
• Eachprocessisgivenalimitofpagesitcanuse• Whenitreachesthelimit,itreplacesfromitsownpages• Localreplacement
– Someprocessesmaydowellwhileotherssuffer
– Variablespacealgorithms• Process’setofpagesgrowsandshrinksdynamically• Globalreplacement
– Oneprocesscanruinitfortherest
59
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
WorkingSetSize
• Workingsetsizeisthenumberofpagesintheworkingset– Thenumberofpagesreferencedintheinterval(t,t+w)
• Theworkingsetsizechangeswithprogramlocality– Duringperiodsofpoorlocality,youreferencemorepages– Withinthatperiodof>me,theworkingsetsizeislarger
• Intui>vely,wanttheworkingsettobethesetofpagesaprocessneedsinmemorytopreventheavyfaul>ng– Eachprocesshasaparameterwthatdeterminesaworkingset
withfewfaults– Denning:Don’trunaprocessunlessworkingsetisinmemory
61
WorkingSetProblems
• Problems– Howdowedeterminew?– Howdoweknowwhentheworkingsetchanges?
• Toohardtoanswer– So,workingsetisnotusedinprac>ceasapagereplacementalgorithm
• However,itiss>llusedasanabstrac>on– Theintui>oniss>llvalid– Whenpeopleask,“HowmuchmemorydoesiTunesneed?”,theyareineffectaskingforthesizeofitsworkingset.
62
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
PFFandMul>programming• Whatdoesthepagefaultfrequencyofprocessestellyou?
Degreeofmul>programming(#concurrentprocesses)
Canthishappen?
System“goodput”(#ofprograms’instruc>onsexecuted)
64
Thrashing
• Pagereplacementalgorithmsavoidthrashing– Whenmostofthe>meisspendbytheOSinpagingdatabackandforthfromthedisk
– No>mespentdoingusefulwork(makingprogress)– Inhissitua>on,thesystemisovercommiTed
• Noideawhichpagesshouldbeinmemorytoreducefaults• Couldjustbethatthereisn’tenoughphysicalmemoryforalloftheprocessesinthesystem
• Ex:RunningWindowsXPwith64MBmemory…(orVistawith1GB)
– Possiblesolu>ons• Swapping–writeoutallpagesofaprocess• Buymorememory
65
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
NextTime
• ReadChapter10&11.1‐11.8• HomeworkdueonMonday.
• CheckWebsiteforcourseannouncements– hTp://www.cs.ucsd.edu/classes/su09/cse120
67