CSE 3320 Operating Systems Memory...
Transcript of CSE 3320 Operating Systems Memory...
CSE3320OperatingSystems
MemoryManagement
Jia RaoDepartmentofComputerScience and Engineering
http://ranger.uta.edu/~jrao
RecapofPreviousClasses
• Multiprogrammingo Requiresmultipleprogramstorunatthe“same”time
o Programsmustbebroughtintomemoryandplacedwithinaprocessforittoberun
o Howtomanagethememoryoftheseprocesses?
o Whatifthememoryfootprintoftheprocessesislargerthanthephysicalmemory?
MemoryManagement• Ideallyprogrammerswantmemorythatis
o largeo fasto nonvolatileo andcheap
• Memoryhierarchyo smallamountoffast,expensivememory– cacheo somemedium-speed,mediumpricemainmemoryo gigabytesofslow,cheapdiskstorage
• Memorymanagementtaskso Allocateandde-allocatememoryforprocesseso Keeptrackofusedmemoryandbywhomo Addresstranslationandprotection
Memoryischeapandlargeintoday’sdesktop,whymemory
managementisstillimportant?
NoMemoryAbstraction
Threesimplewaysoforganizingmemory.(a)earlymainframes.(b)Handheldandembeddedsystems.(c)earlyPC.
• Mono-programming:Oneprogramatatime,sharingmemorywithOS
• Needformulti-programming:1.UsemultipleCPUs.2.OverlappingI/OandCPU
MultiprogrammingwithFixedPartitions
• Fixed-sizememorypartitions,withoutswappingorpagingo Separateinputqueuesforeachpartitiono Singleinputqueueo Variousjobschedulers
Whatarethedisadvantages?
Multiprogrammingwandw/oSwapping
• Swappingo Oneprogramatonetime
o Savetheentirememoryofthepreviousprogramtodisk
o Noaddresstranslationisrequiredandnoprotectionisneeded
• w/oswappingo Memoryisdividedintoblocks
o Eachblockisassignedaprotectionkey
o Programsworkonabsolutememory,noaddresstranslation
o Protectionisenforcedbytrappingunauthorizedaccesses
RunningaProgram*Sourceprogram
Objectprogram
Loadableprogram
compiler
linker
In-memoryexecution
Staticlibrary
dynamiclibrary
• Compileandlinktimeo Alinkerperformsrelocationifthememoryaddressisknown
• Loadtimeo Mustgeneraterelocatable codeifmemorylocationisnotknownatcompiletime
RelocationandProtection• Relocation:whataddresstheprogramwillbeginininmemory
o Staticrelocation:OSperformsone-timechangeofaddressesinprogram
o Dynamicrelocation:OSisabletorelocateaprogramatruntime
• Protection:mustkeepaprogramoutofotherprocesses’ partitions
Illustration of the relocation problem.
Staticrelocation– OScannotmoveitoncea programisassignedaplaceinmemory
AMemoryAbstraction:AddressSpaces
• Exposingtheentirephysicalmemorytoprocesseso Dangerous,maytrashtheOS
o Inflexible,hardtorunmultipleprogramssimultaneously
• Programshouldhavetheirownviewsofmemoryo Theaddressspace– logicaladdress
o Non-overlappingaddressspaces– protection
o Moveaprogrambymappingitsaddressestoadifferentplace-relocation
DynamicRelocation
• OSdynamicallyrelocatesprogramsinmemoryo Twohardwareregisters:baseandlimit
o Hardwareaddsrelocationregister(base)tovirtualaddresstogetaphysicaladdress
o Hardwarecomparesaddresswithlimitregister addressmustbelessthanlimit
Disadvantage:twooperationsoneverymemoryaccessandtheadditionisslow
DealingwithMemoryOverload- Swapping
Memoryallocationchangesaso processescomeintomemoryo leavememory
Shadedregionsareunusedmemory(memoryholes)
° Swapping:bringineachprocessinitsentirety,M-D-M- …
° Keyissues:allocatingandde-allocatingmemory,keeptrackofit
Whynotmemorycompaction?
Anotherwayistousevirtualmemory
Swapping– MemoryGrowing
(a)Allocatingspaceforgrowingsingle(data)segment(b)Allocatingspaceforgrowingstack&datasegment
Whystackgrowsdownward?
MemoryManagementwithBitMaps
• Partofmemorywith5processes,3holeso tickmarksshowallocationunits(whatisitsdesirablesize?)o shadedregionsarefree
• Correspondingbitmap(searchingabitmapforarunofn0s?)
• Sameinformationasalist (betterusingadoubly-linkedlist)
° Keeptrackofdynamicmemoryusage:bitmapandfreelists
MemoryManagementwithLinkedLists
Four neighborcombinationsfortheterminatingprocessX
° De-allocatingmemoryistoupdatethelist
MemoryManagementwithLinkedLists(2)
Ref.MOS3E,OS@Austin,Columbia,RochesterUC.ColoradoSpringsCS4500/5500
° Howtoallocatememoryforanewlycreatedprocess(orswapping)?• Firstfit:allocatethefirstholethatisbigenough
• Bestfit:allocatethesmallestholethatisbig
• Worstfit:allocatethelargesthole
• HowaboutseparatePandHlistsforsearchingspeedup?Butwithwhatcost?
° Example:ablockofsize2isneededformemoryallocation
Whichstrategyisthebest?
Quickfit:searchfast,mergeslow
VirtualMemory° Virtualmemory:thecombinedsizeoftheprogram,data,andstackmay
exceedtheamountofphysicalmemoryavailable.
• Swappingwithoverlays;buthardandtime-consumingtosplitaprogramintooverlaysbytheprogrammer
• Whattodomoreefficiently?
MappingofVirtualaddressestoPhysicaladdresses
Logicalprogramworksinitscontiguousvirtualaddressspace
ActuallocationsofthedatainphysicalmemoryAddresstranslation
donebyMMU
PagingandItsTerminology
Pagetablegivestherelationbetweenvirtualaddressesandphysicalmemoryaddresses
° Terms• Pages• Pageframes• Pagehit• Pagefault• Pagereplacement
° Examples:• MOVREG,0• MOVREG,8192• MOVREG,20500• MOVREG,32780
20K– 24K:20480– 24575
24K– 28K:24576– 28671
PageTables
Twoissues:
1.Mappingmustbefast
2.Pagetablecanbelarge
Whohandlespagefaults?
InternaloperationofMMUwith164KBpages
StructureofaPageTableEntryVirtualAddress
Virtualpagenumber Pageoffset
Whosetsallthosebits?
/dirty
TranslationLook-asideBuffers(TLB)TakingadvantageofTemporalLocality:
Awaytospeedupaddresstranslationistouseaspecialcache ofrecentlyusedpagetableentries-- thishasmanynames,butthemostfrequentlyusedisTranslationLookaside Buffer orTLB
Virtualpagenumber Cache Ref/use Dirty Protection PhysicalAddress(virtual page #) (physical page #)
TLBaccesstimecomparabletocacheaccesstime;muchlessthanPageTable(usuallyinmainmemory)accesstime
Traditionally,TLBmanagementandhandlingweredonebyMMUHardware,today,someinsoftware/OS(manyRISCmachines)
WhohandlesTLBmanagementandhandling,suchasaTLBmiss?
ATLBExample
ATLBtospeeduppaging(usuallyinsideofMMUtraditionally)
PageTableSizeGivena32-bitvirtualaddress,
4KBpages,4bytesperpagetableentry(memoryaddr.ordiskaddr.)
Whatisthesizeofthepagetable?
Thenumberofpagetableentries:
2^32/2^12=2^20
Thetotalsizeofpagetable:
2^20*2^2=2^22(4MB)
WhenwecalculatePageTablesize,theindexitself(virtualpagenumber)isoftenNOTincluded!
Whatifthevirtualmemoryaddressis64-bit? 2^64/2^12*2^2=2^24GB
Whatif2KBpages?
2^32/2^11*2^2=2^23(8MB)
Multi-levelPageTablesSecond-level page tables
(a)32bitaddresswith2pagetablefields.(b)Two-levelpagetables
Ifonly4tablesareneeded,thetotalsizewillbe2^10*4=4KB
Example-1:PT1=1,PT2=3,Offset=4Virtualaddress:1*2^22+3*2^12+4=4206596
4M4K
Example-2:givenlogicaladdress4,206,596Whatwillbethevirtualaddressandwhereareitspositionsinthepagetables4206596/4096=1027,remainder41027/1024=1,remainder3 0-4095
4206592-4210687
InvertedPageTables° Invertedpagetable:oneentryperpageframeinphysicalmemory,insteadof
oneentryperpageofvirtualaddressspace.Givena64-bitvirtualaddress,
4KBpages,256MBphysicalmemory
HowmanyentriesinthePageTable?Homemanypageframesinstead?HowlargeisthePageTableifoneentry8B?
Comparisonofatraditionalpagetablewithaninvertedpagetable
InvertedPageTables(2)° Invertedpagetable:howtoexecutevirtual-to-physicaltranslation?
• TLBhelps!ButwhatifaTLBmisses?
IntegratingTLB,Cache,andVMJustlikeanyothercache,theTLBcanbeorganizedasfullyassociative,
setassociative,ordirectmapped
TLBsareusuallysmall,typicallynotmorethan128- 256entriesevenonhighendmachines.Thispermitsfullyassociativelookuponthesemachines.Mostmid-rangemachinesusesmalln-waysetassociativeorganizations.
CPU TLBLookup Cache Main
Memory
VA PA miss
hit
data
Trans-lation
hit
missTranslationwith a TLB
TLBmissesorPagefault,gotoPageTabletranslationfordiskpageaddresses
Putitalltogether:LinuxandX86
Logicaladdress
cr3
PageGlobalDirectory
PageUpperDirectory
PageMiddleDirectory
PageTable
Page
Switchthecr3valuewhencontextswitchingthreadsandflushtheTLB
Acommonmodelfor32-bit(two-level,4Bpte)and64-bit(four-level,8Bpte)SRC/include/linux/sched.h->task_struct->mm->pgd
SRC/kernel/sched.c->context_switch->switch_mm
Summary• Twotasksofmemorymanagement
• Whymemoryabstraction?
• Managefreememory
• Twowaystodealwithmemoryoverload
o Swappingandvirtualmemory
• Virtualmemory
• PagingandPagetable