paging - swarthmore college solution is paging. paging vocabulary • for each process, the virtual...

44
Paging 11/10/16

Upload: hakhuong

Post on 12-Apr-2018

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Paging11/10/16

Page 2: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

RecallfromTuesday

Oursolutiontofragmentationistosplitupaprocess’saddressspaceintosmallerchunks.

Process1

OS

Process2

Process1

Process3

Process2

PhysicalMemory

OS:Place

Process3

Process3

Process3

Process3

Page 3: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

RecallfromTuesday

Wesupportvirtualaddressingbytranslatingaddressesatruntime.

Wecan’tachievethisusingbaseandboundregistersunlesseachprocess’smemoryisallinoneblock. P2

0

N2-1

P2

P1

P3

0

N-1

Base +

<

Bound

y/n?

Page 4: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Howcanwedoboth?

• Wewanttosupporttranslationofvirtualaddressestophysicaladdresseson-the-fly.

• Wewanttosplitupeachprocess’saddressspacetousephysicalmemorymoreefficiently.

Thesolutionispaging.

Page 5: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

PagingVocabulary

• Foreachprocess,thevirtual addressspaceisdividedintofixed-sizepages.

• Forthesystem,thephysical memoryisdividedintofixed-sizeframes.

• Thesizeofapageisequaltothatofaframe.• Often4KBinpractice.

Page 6: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

MainIdea

• ANYvirtualpagecanbestoredinanyavailableframe.• Makesfindinganappropriately-sizedmemorygapveryeasy– they’reallthesamesize.

• Foreachprocess,OSkeepsatablemappingeachvirtualpagetophysicalframe.

Page 7: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

MainIdea

• ANYvirtualpagecanbestoredinanyavailableframe.• Makesfindinganappropriately-sizedmemorygapveryeasy– they’reallthesamesize.

PhysicalMemory

VirtualMemory

(OSMapping)Implicationsforfragmentation?

External:goesaway.Nomoreawkwardly-sized,unusablegaps.

Internal:Aboutthesame.Processcanalwaysrequestmemoryandnotuseit.

Page 8: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Addressing

• Likewedidwithcaching,we’regoingtochopupmemoryaddressesintopartitions.

• Virtualaddresses:• High-orderbits:page#• Low-orderbits:offsetwithinthepage

• Physicaladdresses:• High-orderbits:frame#• Low-orderbits:offsetwithintheframe

Page 9: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Example:32-bitvirtualaddresses

• Supposewehave8-KB(8192-byte)pages.• Weneedenoughbitstoindividuallyaddresseachbyteinthepage.• Howmanybitsdoweneedtoaddress8192items?

Page 10: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Example:32-bitvirtualaddresses

• Supposewehave8-KB(8192-byte)pages.• Weneedenoughbitstoindividuallyaddresseachbyteinthepage.• Howmanybitsdoweneedtoaddress8192items?• 213 =8192,soweneed13bits.• Lowest13bits:offsetwithinpage.

Page 11: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Example:32-bitvirtualaddresses

• Supposewehave8-KB(8192-byte)pages.• Weneedenoughbitstoindividuallyaddresseachbyteinthepage.• Howmanybitsdoweneedtoaddress8192items?• 213 =8192,soweneed13bits.• Lowest13bits:offsetwithinpage.

• Remaining19bits:pagenumber.

We’llcallthesebitsp. We’llcallthesebitsi.

Page 12: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

We’llcallthesebitsp. We’llcallthesebitsi.

OSPageTableForProcess

Virtualaddress:

Physicaladdress:

We’ll(still)callthesebitsi.We’llcallthesebitsf.

Whereisthispageinphysicalmemory?(Inwhichframe?)

Oncewe’vefoundtheframe,whichbyte(s)dowewanttoaccess?

AddressPartitioning

Page 13: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

PhysicalAddress

LogicalAddressPagep Offseti

FrameV Perm …R D

PhysicalMemory

PageTable

AddressTranslation

Page 14: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

PageTable• Onetableperprocess• Tableentryelements• V:validbit• R:referencedbit• D:dirtybit• Frame:locationinphy mem• Perm:accesspermissions

• Tableparametersinmemory• Pagetablebaseregister• Pagetablesizeregister

FrameV Perm …PTBRPTSR

R D

Page 15: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

• Virtualaddress=p+i• Physicaladdress=f+i• First,doaseriesofchecks

LogicalAddressPagep Offseti

PhysicalAddress

FrameV Perm …R D

AddressTranslation

Page 16: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

CheckifPagep isWithinRangeLogicalAddress

Pagep

PTBRPTSR

p <PTSR

Offseti

PhysicalAddress

FrameV Perm …R D

Page 17: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

CheckifPageTableEntryp isValidLogicalAddress

Pagep

PTBRPTSR

V==1

Offseti

PhysicalAddress

FrameV Perm …R D

Page 18: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

CheckifOperationisPermittedLogicalAddress

Pagep

PTBRPTSR

Perm(op)

Offseti

PhysicalAddress

FrameV Perm …R D

Page 19: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

TranslateAddressLogicalAddress

Pagep

PTBRPTSR

Offseti

PhysicalAddress

FrameV Perm …R D

concat

Page 20: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

PhysicalAddressbyConcatenation

LogicalAddressPagep

PTBRPTSR

Offseti

FrameV Perm …R D

PhysicalAddressFramef Offseti

Page 21: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

SizingthePageTableLogicalAddress

Pagep Offseti

Numberofbitsnspecifiesmaxsizeoftable,wherenumberofentries=2n

Numberofbitsneededtoaddressphysicalmemoryinunitsofframes

Numberofbitsspecifiespagesize

FrameV Perm …R D

Page 22: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

ExampleofSizingthePageTable

• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset

Pagep:20bits Offseti:12bits

FrameV Perm …R D

Page 23: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Howmanyentries(rows)willtherebeinthispagetable?A. 212,becausethat’showmanytheoffsetfieldcan

addressB. 220,becausethat’showmanythepagefieldcan

addressC. 230,becausethat’showmanyweneedtoaddress1

GBD. 232,becausethat’sthesizeoftheentireaddress

space

Page 24: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

ExampleofSizingthePageTable

Howbigisaframe?

Pagep:20bits Offseti:12bits

20bitstoaddress220=1Mentries

FrameV Perm …R D

• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset

Page 25: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Whatwillbetheframesize,inbytes?A. 212,becausethat’showmanybytestheoffset

fieldcanaddressB. 220,becausethat’showmanybytesthepagefield

canaddressC. 230,becausethat’showmanybytesweneedto

address1GBD. 232,becausethat’sthesizeoftheentireaddress

space

Page 26: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

ExampleofSizingthePageTablePagep:20bits Offseti:12bits

20bitstoaddress220=1Mentries

Pagesize=framesize=212 =4096bytes

FrameV Perm …R D

• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset

Page 27: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

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

Page 28: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

ExampleofSizingthePageTable

• 32bitvirtualaddresses,1GBphysicalmemory• Addresspartition:20bitpagenumber,12bitoffset

Pagep:20bits Offseti:12bits

20bitstoaddress220=1Mentries

18bitstoaddress230/212 frames

Pagesize=framesize=212 =4096bytes

Sizeofanentry?

FrameV Perm …R D

Page 29: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

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

Page 30: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

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?

Page 31: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

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

Page 32: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Concerns

• 4MBofbookkeepingforeveryprocess?• 200processes->800MBjusttostorepagetables…

• We’regoingtoneedatonofmemoryjustforpagetables…

• Weneedtodoalookupinourpagetable,whichisinmemory,everytimeaprocessaccessesmemory.• Isn’tthatslowingdownmemorybyafactorof2?

Page 33: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Multi-LevelPageTables(You’renotresponsibleforthis.TakeanOSclassforthedetails.)

LogicalAddress1st-levelPaged Offseti

FrameV …R D

2nd-levelPagep

FrameV …R DPointsto(base)framecontaining2nd-levelpagetable

concat

PhysicalAddressReducesmemoryusageSIGNIFICANTLY:onlyallocatepagetablespacewhenweneedit.Morememoryaccessesthough…

Page 34: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Cachingthepagetable

• Eachlookupcostsanothermemoryreference• Foreachreference,additionalreferencesrequired• Slowsmachinedownbyfactorof2ormore

• Takeadvantageoflocality• Mostreferencesaretoasmallnumberofpages• Keeptranslationsoftheseinhigh-speedmemory(acacheforpagetranslation)

Page 35: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

VMImplications

• Notallpiecesneedtobeinmemory• Needonlypiecebeingreferenced• Otherpiecescanbeondisk• Bringpiecesinonlywhenneeded

• Illusion:thereismuchmorememory• What’sneededtosupportthisidea?• Awaytoidentifywhetherapieceisinmemory• Awaytobringinpieces(fromwhere,towhere?)• Relocation(whichwehave)

Page 36: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

SampleContentsofPageTableEntry

• Valid:isentryvalid(pageinphysicalmemory)?• Ref:hasthispagebeenreferencedyet?• Dirty:hasthispagebeenmodified?• Frame:whatframeisthispagein?• Protection:whataretheallowableoperations?• read/write/execute

FramenumberValid Ref Dirty Prot:rwx

Page 37: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Pagefaults

Apagefaultoccurswhenwetrytoaccessavirtualaddressthathasnocorrespondingphysicaladdress.

mechanismforhandlingapagefault:1. readinthevirtualpagefromdisk

• Locationkeptinkerneldatastructure.2. storeitinaphysicalmemoryframe

• Mayneedtokicksomethingelseout.3. UpdatePTEwithframenum &validbit=14. Restartinstructionthatcausedthepagefault

Page 38: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

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 !

Page 39: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Handingfaultsfromdiskseemsveryexpensive.Howcanwegetawaywiththisinpractice?

A. Wehavelotsofmemory,anditisn’tusuallyfull.

B. Weusespecialhardwaretospeedthingsup.

C. Wetendtousethesamepagesoverandover.

D. Thisistoocommon&expensivetodoinpractice!

Page 40: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

PrincipleofLocality

• Notallpiecesreferenceduniformlyovertime• Makesuremostreferencedpiecesinmemory• Ifnot,thrashing:constantfetchingofpieces

• Referencesclusterintime/space• Willbetosameorneighboringareas• Allowspredictionbasedonpast

Page 41: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

PageReplacement

• Goal:removepage(s)notexhibitinglocality

• Pagereplacementisabout• whichpage(s)toremove• whentoremovethem

• Howtodoitinthecheapestwaypossible• Leastamountofadditionalhardware• Leastamountofsoftwareoverhead

Page 42: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

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)

Page 43: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

Summary• Wegiveeachprocessavirtualaddressspacetosimplifyprocessexecution.

• OSmaintainsmappingofvirtualaddresstophysicalmemorylocationsinapagetable.• Onepagetableforeveryprocess

• Providestheabstractionofverylargememory:notallpagesneedberesidentinmemory• Bringpagesinfromdiskondemand

Page 44: Paging - Swarthmore College solution is paging. Paging Vocabulary • For each process, the virtual address space is divided into fixed-size pages. • For the system, the physicalmemory

WorksheetexampleStepthroughthestreamofVirtualAddressesfromtheCPU:

• ShowhowthebitsofeachaddressareusedforeachVirtualAddress&itsPhysicalAddressmapping• TranslateeachVAtoitsPAusingtheappropriatePTE• UpdatePTEsappropriatelyasyougo• ShowthehistoryofthecontentsofRAMastheseaddressesareaccessed• Whichvirtualpageofwhichprocessdoesitstore

• ImplementaFIFOpagereplacementpolicyforRAM

44