paging - swarthmore college solution is paging. paging vocabulary • for each process, the virtual...
TRANSCRIPT
Paging11/10/16
RecallfromTuesday
Oursolutiontofragmentationistosplitupaprocess’saddressspaceintosmallerchunks.
Process1
OS
Process2
Process1
Process3
Process2
PhysicalMemory
OS:Place
Process3
Process3
Process3
Process3
RecallfromTuesday
Wesupportvirtualaddressingbytranslatingaddressesatruntime.
Wecan’tachievethisusingbaseandboundregistersunlesseachprocess’smemoryisallinoneblock. P2
0
N2-1
P2
P1
P3
0
N-1
Base +
<
Bound
y/n?
Howcanwedoboth?
• Wewanttosupporttranslationofvirtualaddressestophysicaladdresseson-the-fly.
• Wewanttosplitupeachprocess’saddressspacetousephysicalmemorymoreefficiently.
Thesolutionispaging.
PagingVocabulary
• Foreachprocess,thevirtual addressspaceisdividedintofixed-sizepages.
• Forthesystem,thephysical memoryisdividedintofixed-sizeframes.
• Thesizeofapageisequaltothatofaframe.• Often4KBinpractice.
MainIdea
• ANYvirtualpagecanbestoredinanyavailableframe.• Makesfindinganappropriately-sizedmemorygapveryeasy– they’reallthesamesize.
• Foreachprocess,OSkeepsatablemappingeachvirtualpagetophysicalframe.
MainIdea
• ANYvirtualpagecanbestoredinanyavailableframe.• Makesfindinganappropriately-sizedmemorygapveryeasy– they’reallthesamesize.
PhysicalMemory
VirtualMemory
(OSMapping)Implicationsforfragmentation?
External:goesaway.Nomoreawkwardly-sized,unusablegaps.
Internal:Aboutthesame.Processcanalwaysrequestmemoryandnotuseit.
Addressing
• Likewedidwithcaching,we’regoingtochopupmemoryaddressesintopartitions.
• Virtualaddresses:• High-orderbits:page#• Low-orderbits:offsetwithinthepage
• Physicaladdresses:• High-orderbits:frame#• Low-orderbits:offsetwithintheframe
Example:32-bitvirtualaddresses
• Supposewehave8-KB(8192-byte)pages.• Weneedenoughbitstoindividuallyaddresseachbyteinthepage.• Howmanybitsdoweneedtoaddress8192items?
Example:32-bitvirtualaddresses
• Supposewehave8-KB(8192-byte)pages.• Weneedenoughbitstoindividuallyaddresseachbyteinthepage.• Howmanybitsdoweneedtoaddress8192items?• 213 =8192,soweneed13bits.• Lowest13bits:offsetwithinpage.
Example:32-bitvirtualaddresses
• Supposewehave8-KB(8192-byte)pages.• Weneedenoughbitstoindividuallyaddresseachbyteinthepage.• Howmanybitsdoweneedtoaddress8192items?• 213 =8192,soweneed13bits.• Lowest13bits:offsetwithinpage.
• Remaining19bits:pagenumber.
We’llcallthesebitsp. We’llcallthesebitsi.
We’llcallthesebitsp. We’llcallthesebitsi.
OSPageTableForProcess
Virtualaddress:
Physicaladdress:
We’ll(still)callthesebitsi.We’llcallthesebitsf.
Whereisthispageinphysicalmemory?(Inwhichframe?)
Oncewe’vefoundtheframe,whichbyte(s)dowewanttoaccess?
AddressPartitioning
PhysicalAddress
LogicalAddressPagep Offseti
FrameV Perm …R D
PhysicalMemory
PageTable
AddressTranslation
PageTable• Onetableperprocess• Tableentryelements• V:validbit• R:referencedbit• D:dirtybit• Frame:locationinphy mem• Perm:accesspermissions
• Tableparametersinmemory• Pagetablebaseregister• Pagetablesizeregister
FrameV Perm …PTBRPTSR
R D
• Virtualaddress=p+i• Physicaladdress=f+i• First,doaseriesofchecks
LogicalAddressPagep Offseti
PhysicalAddress
FrameV Perm …R D
AddressTranslation
CheckifPagep isWithinRangeLogicalAddress
Pagep
PTBRPTSR
p <PTSR
Offseti
PhysicalAddress
FrameV Perm …R D
CheckifPageTableEntryp isValidLogicalAddress
Pagep
PTBRPTSR
V==1
Offseti
PhysicalAddress
FrameV Perm …R D
CheckifOperationisPermittedLogicalAddress
Pagep
PTBRPTSR
Perm(op)
Offseti
PhysicalAddress
FrameV Perm …R D
TranslateAddressLogicalAddress
Pagep
PTBRPTSR
Offseti
PhysicalAddress
FrameV Perm …R D
concat
PhysicalAddressbyConcatenation
LogicalAddressPagep
PTBRPTSR
Offseti
FrameV Perm …R D
PhysicalAddressFramef Offseti
SizingthePageTableLogicalAddress
Pagep Offseti
Numberofbitsnspecifiesmaxsizeoftable,wherenumberofentries=2n
Numberofbitsneededtoaddressphysicalmemoryinunitsofframes
Numberofbitsspecifiespagesize
FrameV Perm …R D
ExampleofSizingthePageTable
• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset
Pagep:20bits Offseti:12bits
…
FrameV Perm …R D
Howmanyentries(rows)willtherebeinthispagetable?A. 212,becausethat’showmanytheoffsetfieldcan
addressB. 220,becausethat’showmanythepagefieldcan
addressC. 230,becausethat’showmanyweneedtoaddress1
GBD. 232,becausethat’sthesizeoftheentireaddress
space
ExampleofSizingthePageTable
Howbigisaframe?
Pagep:20bits Offseti:12bits
20bitstoaddress220=1Mentries
…
FrameV Perm …R D
• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset
Whatwillbetheframesize,inbytes?A. 212,becausethat’showmanybytestheoffset
fieldcanaddressB. 220,becausethat’showmanybytesthepagefield
canaddressC. 230,becausethat’showmanybytesweneedto
address1GBD. 232,becausethat’sthesizeoftheentireaddress
space
ExampleofSizingthePageTablePagep:20bits Offseti:12bits
20bitstoaddress220=1Mentries
Pagesize=framesize=212 =4096bytes
…
FrameV Perm …R D
• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset
Howmanybitsdoweneedtostoretheframenumber?
• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset
A:12 B:18C:20D:30E:32
Pagep:20bits Offseti:12bits
20bitstoaddress220=1Mentries
?
Pagesize=framesize=212 =4096bytes
…
FrameV Perm …R D
ExampleofSizingthePageTable
• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset
Pagep:20bits Offseti:12bits
20bitstoaddress220=1Mentries
18bitstoaddress230/212 frames
Pagesize=framesize=212 =4096bytes
…
Sizeofanentry?
FrameV Perm …R D
Howbigisanentry,inbytes?(Roundtoapoweroftwobytes.)
• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset
A:1B:2 C:4 D:8E:16
Pagep:20bits Offseti:12bits
20bitstoaddress220=1Mentries
18bitstoaddress230/212 frames
Pagesize=framesize=212 =4096bytes
…
Sizeofanentry?
FrameV Perm …R D
ExampleofSizingthePageTable
• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset
Pagep:20bits Offseti:12bits
20bitstoaddress220=1Mentries
18bitstoaddress230/212 frames
Pagesize=framesize=212 =4096bytes
…
4bytesneededtocontain24(1+1+1+18+3+…)bits
FrameV Perm …R D
Totaltablesize?
ExampleofSizingthePageTable
• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset
Pagep:20bits Offseti:12bits
20bitstoaddress220=1Mentries
18bitstoaddress230/212 frames
Pagesize=framesize=212 =4096bytes
…
4bytesneededtocontain24(1+1+1+18+3+…)bits Tablesize=
1Mx4=4MB
FrameV Perm …R D
Concerns
• 4MBofbookkeepingforeveryprocess?• 200processes->800MBjusttostorepagetables…
• We’regoingtoneedatonofmemoryjustforpagetables…
• Weneedtodoalookupinourpagetable,whichisinmemory,everytimeaprocessaccessesmemory.• Isn’tthatslowingdownmemorybyafactorof2?
Multi-LevelPageTables(You’renotresponsibleforthis.TakeanOSclassforthedetails.)
LogicalAddress1st-levelPaged Offseti
FrameV …R D
2nd-levelPagep
FrameV …R DPointsto(base)framecontaining2nd-levelpagetable
concat
PhysicalAddressReducesmemoryusageSIGNIFICANTLY:onlyallocatepagetablespacewhenweneedit.Morememoryaccessesthough…
Cachingthepagetable
• Eachlookupcostsanothermemoryreference• Foreachreference,additionalreferencesrequired• Slowsmachinedownbyfactorof2ormore
• Takeadvantageoflocality• Mostreferencesaretoasmallnumberofpages• Keeptranslationsoftheseinhigh-speedmemory(acacheforpagetranslation)
VMImplications
• Notallpiecesneedtobeinmemory• Needonlypiecebeingreferenced• Otherpiecescanbeondisk• Bringpiecesinonlywhenneeded
• Illusion:thereismuchmorememory• What’sneededtosupportthisidea?• Awaytoidentifywhetherapieceisinmemory• Awaytobringinpieces(fromwhere,towhere?)• Relocation(whichwehave)
SampleContentsofPageTableEntry
• Valid:isentryvalid(pageinphysicalmemory)?• Ref:hasthispagebeenreferencedyet?• Dirty:hasthispagebeenmodified?• Frame:whatframeisthispagein?• Protection:whataretheallowableoperations?• read/write/execute
FramenumberValid Ref Dirty Prot:rwx
Pagefaults
Apagefaultoccurswhenwetrytoaccessavirtualaddressthathasnocorrespondingphysicaladdress.
mechanismforhandlingapagefault:1. readinthevirtualpagefromdisk
• Locationkeptinkerneldatastructure.2. storeitinaphysicalmemoryframe
• Mayneedtokicksomethingelseout.3. UpdatePTEwithframenum &validbit=14. Restartinstructionthatcausedthepagefault
PageFaultsareExpensive
• Disk:5-6ordersmagnitudeslowerthanRAM• Veryexpensive;butifveryrare,tolerable
• Example• RAMaccesstime:100nsec• Diskaccesstime:10msec• p =pagefaultprobability• Effectiveaccesstime:100+p × 10,000,000nsec• Ifp =0.1%,effectiveaccesstime=10,100nsec !
Handingfaultsfromdiskseemsveryexpensive.Howcanwegetawaywiththisinpractice?
A. Wehavelotsofmemory,anditisn’tusuallyfull.
B. Weusespecialhardwaretospeedthingsup.
C. Wetendtousethesamepagesoverandover.
D. Thisistoocommon&expensivetodoinpractice!
PrincipleofLocality
• Notallpiecesreferenceduniformlyovertime• Makesuremostreferencedpiecesinmemory• Ifnot,thrashing:constantfetchingofpieces
• Referencesclusterintime/space• Willbetosameorneighboringareas• Allowspredictionbasedonpast
PageReplacement
• Goal:removepage(s)notexhibitinglocality
• Pagereplacementisabout• whichpage(s)toremove• whentoremovethem
• Howtodoitinthecheapestwaypossible• Leastamountofadditionalhardware• Leastamountofsoftwareoverhead
BasicPageReplacementAlgorithms
• FIFO:selectpagethatisoldest• Simple:useframeordering• Doesn’tperformverywell(oldestmaybepopular)
• OPT:selectpagetobeusedfurthestinfuture• Optimal,butrequiresfutureknowledge• Establishesbestcase,goodforcomparisons
• LRU:selectpagethatwasleastrecentlyused• Predictfuturebasedonpast;worksgivenlocality• Costly:time-stamppageseachaccess,findleast
• Goal:minimizereplacements(maximizelocality)
Summary• Wegiveeachprocessavirtualaddressspacetosimplifyprocessexecution.
• OSmaintainsmappingofvirtualaddresstophysicalmemorylocationsinapagetable.• Onepagetableforeveryprocess
• Providestheabstractionofverylargememory:notallpagesneedberesidentinmemory• Bringpagesinfromdiskondemand
WorksheetexampleStepthroughthestreamofVirtualAddressesfromtheCPU:
• ShowhowthebitsofeachaddressareusedforeachVirtualAddress&itsPhysicalAddressmapping• TranslateeachVAtoitsPAusingtheappropriatePTE• UpdatePTEsappropriatelyasyougo• ShowthehistoryofthecontentsofRAMastheseaddressesareaccessed• Whichvirtualpageofwhichprocessdoesitstore
• ImplementaFIFOpagereplacementpolicyforRAM
44