Upload
vokien
View
221
Download
1
Embed Size (px)
Citation preview
Operating Systems:Operating Systems:Memory managementMemory management
Mon 14.9.2009Mon 14.9.2009
Tiina NiklanderTiina Niklander
2
Memory ManagementMemory ManagementProgrammer wants memory to beProgrammer wants memory to be
Indefinitely largeIndefinitely largeIndefinitely fastIndefinitely fastNon volatileNon volatile
Memory hierarchyMemory hierarchysmall amount of fast, expensive memorysmall amount of fast, expensive memory –– cachecachesome mediumsome medium--speed, medium price main memoryspeed, medium price main memorygigabytes of slow, cheap disk storagegigabytes of slow, cheap disk storage
Memory manager handles the memory hierarchyMemory manager handles the memory hierarchyRequirements for memory managementRequirements for memory management
Logical and physical organizationLogical and physical organizationProtection and sharingProtection and sharing
Magnetic tape
Magnetic disk
Main memory
Cache
Registers
Memory managementMemory management
Programs use logical addresses of their own addressPrograms use logical addresses of their own addressspace (0..MAX)space (0..MAX)OS kernel usually in a fixed location using physicalOS kernel usually in a fixed location using physicalmemory addresses directlymemory addresses directlyRest of the physical memory for user processes andRest of the physical memory for user processes andother OS partsother OS partsOS task: memory allocation and process relocationOS task: memory allocation and process relocationHardware task: address translation (to protect memory)Hardware task: address translation (to protect memory)
MMUMMU –– memory management unitmemory management unit
3
4
Basic Memory Management: One programBasic Memory Management: One programMonoprogrammingMonoprogramming without Swapping or Pagingwithout Swapping or Paging
No memory abstraction, no address space,No memory abstraction, no address space,just an operating system with one user processjust an operating system with one user process
Multiprogramming with Fixed PartitionsMultiprogramming with Fixed Partitions
5
OS places one process in one partitionOS places one process in one partitionInternal fragmentation (whole partition allocated to aInternal fragmentation (whole partition allocated to asmaller process)smaller process)
Fixed partitionsFixed partitions
Process queue for partition on diskProcess queue for partition on diskIn shared queue for all partitions orIn shared queue for all partitions orIn multiple queues for different sizes,In multiple queues for different sizes,
No free partition, OS can swapNo free partition, OS can swapMove one process to diskMove one process to diskPCB always in memoryPCB always in memory
Program too large for any partition; programmer has toProgram too large for any partition; programmer has todesign solutiondesign solution
Overlaying: keep just part of the program in memoryOverlaying: keep just part of the program in memoryWrite the program to control the part swapping betweenWrite the program to control the part swapping betweenmemory and diskmemory and disk
6
Relocation and ProtectionRelocation and Protection
Cannot be sure whereCannot be sure whereprogram will be loaded inprogram will be loaded inmemorymemory
address locations of variables,address locations of variables,code routines cannot becode routines cannot beabsoluteabsolutemust keep a program out ofmust keep a program out ofother processes’ partitionsother processes’ partitions
UseUse base and limitbase and limit valuesvaluesaddress locations added to baseaddress locations added to basevalue to map to physicalvalue to map to physical addraddraddress locations larger thanaddress locations larger thanlimit value is an errorlimit value is an error
Address translation by MMUAddress translation by MMU7
SharingSharing
Access to shared code / dataAccess to shared code / dataNo violation of the protections!No violation of the protections!
Shared codeShared codemust be reentrant, not to change during executionmust be reentrant, not to change during executionJust one copy of the shared code (e.g. library)Just one copy of the shared code (e.g. library)
Shared dataShared dataProcesses coProcesses co--operate and share data structuresoperate and share data structuresE.g. shared buffer of producer and consumerE.g. shared buffer of producer and consumer
Solution:Solution: system calls between processes, threadssystem calls between processes, threadswithin a process (in virtual memory alt. solution)within a process (in virtual memory alt. solution) 8
Swapping (1)Swapping (1)
9
Memory allocation changes asMemory allocation changes asprocesses come into memoryprocesses come into memoryleave memoryleave memory
Shaded regions are unused memoryShaded regions are unused memory
Swapping (2)Swapping (2)
10
(a) Allocating space for growing data segment(a) Allocating space for growing data segment(b) Allocating space for growing stack & data segment(b) Allocating space for growing stack & data segment
FragmentationFragmentation
11
No fixed predetermined partition sizesNo fixed predetermined partition sizesExternal fragmentsExternal fragments: 6M + 6M + 4M = 14M: 6M + 6M + 4M = 14MOS could occasionally reorganize the memoryOS could occasionally reorganize the memory (compaction)(compaction)
Memory Management: bookkeepingMemory Management: bookkeepingallocations and free areasallocations and free areas
Part of memory with 5 processes, 3 holesPart of memory with 5 processes, 3 holestick marks show allocation unitstick marks show allocation unitsshaded regions are freeshaded regions are free
(b) Corresponding bit map(b) Corresponding bit map(c) Same information as a list(c) Same information as a list
12
Combining freed areasCombining freed areas
13
Four neighbor combinations for the terminating process XFour neighbor combinations for the terminating process X
14
AllocationAllocation
Where to place the newWhere to place the newprocess?process?Goal: avoid externalGoal: avoid externalfragmentation andfragmentation andcompactioncompactionSome alternatives:Some alternatives:BestBest--fitfitFirstFirst--fitfitNextNext--fitfitWorstWorst--fitfitQuickQuick--fitfit
Sta Fig 7.5
15
Virtual memoryVirtual memory(using paging)(using paging)
PagingPagingOS: the programOS: the programsplit to pagessplit to pages
Page location storedPage location storedin page tablein page table
Process locationProcess locationLogical addressLogical addressalways the samealways the sameMMU translatesMMU translateslogical address tological address tophysical addressphysical addressusing page tableusing page tableEach page relocatedEach page relocatedseparatelyseparately
16
00000011111111110000010000000000
Sta Fig 7.11
PagingPaging
Each process has ownEach process has ownpage tablepage table
Contains the locationsContains the locations(frame numbers) of(frame numbers) ofallocated framesallocated framesPage table location storedPage table location storedin PCB, copied to PTRin PCB, copied to PTRfor executionfor execution
OS maintains a table (orOS maintains a table (orlist) of page frames, tolist) of page frames, toknow which areknow which areunallocatedunallocated
17
Paging: Address TranslationPaging: Address Translation
18
MMU has one special register, Page Table RegisterMMU has one special register, Page Table Register(PTR), for address translation(PTR), for address translation
Paging: Address TranslationPaging: Address Translation
19Sta 8.3
20
Page tablePage table
Page tablePage table
Each process has its own page tableEach process has its own page tableEach entry has a present bit, since not all pages need to be in theEach entry has a present bit, since not all pages need to be in thememory all the timememory all the time --> page faults> page faultsRemember the locality principleRemember the locality principleLogical address space can be much larger than the physicalLogical address space can be much larger than the physical
21
Typical Page Table Entry, Fig. 3.11
22
Page TablesPage Tables
InternalInternaloperation ofoperation ofMMU with 16MMU with 164 KB pages4 KB pages
23
TranslationTranslation LookasideLookasideBufferBuffer ––
TLBTLB((osoitteenmuunnososoitteenmuunnos--
puskuripuskuri))
TLBTLB –– TranslationTranslation LookasideLookaside BufferBuffer
Goal is to speed up pagingGoal is to speed up pagingTLB is a cache in MMU for page table entriesTLB is a cache in MMU for page table entries
24
TLBTLB –– translation lookasidetranslation lookasidebufferbuffer
Part of memory management unit (MMU)Part of memory management unit (MMU)Cache for used page table entries to avoid extra memoryCache for used page table entries to avoid extra memoryaccess during address translationaccess during address translation
Associative searchAssociative searchCompare with all elements at the same time (fast)Compare with all elements at the same time (fast)
Each TLB element contains: page number, page tableEach TLB element contains: page number, page tableentry, validity bitentry, validity bitEach process uses the same page numbers 0, 1, 2, …,Each process uses the same page numbers 0, 1, 2, …,stored on different page framesstored on different page frames
TLB must be cleared during process switchTLB must be cleared during process switchAt least clear the validity bits (this is fast)At least clear the validity bits (this is fast)
25
Sta FigSta Fig8.8.8.8.
OperationOperationof Pagingof Pagingand TLBand TLB
27
Fig. 5.47 fromHennessy-Patterson,Computer Architecture
DEC Alpha AXP 21064memory hierarchy
Fully assoc,32 entrydata TLB
8 KB,directmapped,256 line(each 32B)data cache
Fully assoc, 12 entryinstruction TLB
8 KB, direct mapped,256 line (each 32B)instruction cache
2 MB, 64K line (each 32B)direct mapped, unified,write-back L2 cache
main memory
paging disk (dma)
More? -> course Computer Organisation II
TLB and cacheTLB and cache
28Sta Fig. 8.10
Multilevel andMultilevel andInvertedInverted
Page TablesPage Tables
Multilevel page tableMultilevel page table
Large virtual address spaceLarge virtual address spaceLogical address could be 32Logical address could be 32-- or 64or 64--bitsbits
Each process has a large page tableEach process has a large page tableUsing 32Using 32--bit address and frame size 4KB (12 bit offset),bit address and frame size 4KB (12 bit offset),means 2means 22020 = 1M of page tables entries for a single process= 1M of page tables entries for a single processEach entry requires several bytes (lets say 4 bytes), so theEach entry requires several bytes (lets say 4 bytes), so thefinal size of page table could be for example 4 MBfinal size of page table could be for example 4 MB
Thus the page table is divided to several pages also andThus the page table is divided to several pages also andpart of it can be on the diskpart of it can be on the disk
Only the part of the page table that covers the pages usedOnly the part of the page table that covers the pages usedcurrently in the execution of the process is in memorycurrently in the execution of the process is in memory
30
31
TwoTwo--level hierarchical page tablelevel hierarchical page tableTopTop--mostmost level in one page and always in the memorylevel in one page and always in the memory
1 K entries (= 1024 = 210)
1K * 1K = 1M entries
AddressAddresstranslationtranslationwith twowith two
levelslevels
32(Fig 4-12 [Tane01])
FA
VirtualAddress:
PCB
Address translation with two levelsAddress translation with two levels
33Sta Fig 8.5
Inverted Page TablesInverted Page Tables
Comparison of a traditional page table with an inverted page tableComparison of a traditional page table with an inverted page table
34
Inverted page tableInverted page table
Physical memory often smaller than the virtual addressPhysical memory often smaller than the virtual addressspace of processesspace of processesInvert booking: Store for each page frame what pageInvert booking: Store for each page frame what page(of which process) is stored there(of which process) is stored there
Only one global table (inverted page table), one entry forOnly one global table (inverted page table), one entry foreach page frame.each page frame.
Search for the page based on the content of the tableSearch for the page based on the content of the tableInefficient, if done sequentially,Inefficient, if done sequentially,Use hash to calculate the location, start search from thereUse hash to calculate the location, start search from there
If page not found, page faultIf page not found, page faultUseful, only if TLB is largeUseful, only if TLB is large 35
Inverted page tableInverted page table
36
j
Frame numberFrame numberIndex of the tableIndex of the tableNot stored in theNot stored in theentryentry
Sta Fig 8.6
37
Virtual memoryVirtual memoryOperations and policiesOperations and policies
Chapters 3.4.Chapters 3.4. –– 3.73.7
Policies and methodsPolicies and methods
Fetch policyFetch policy (Noutopolitiikka)(Noutopolitiikka)When to load page to memory?When to load page to memory?
Placement policyPlacement policy(Sijoituspolitiikka )(Sijoituspolitiikka )
Where to place the new page?Where to place the new page?
Replacement policyReplacement policy(Korvaus/poistopolitiikka)(Korvaus/poistopolitiikka)
Which page to be evicted andWhich page to be evicted andreplacedreplaced
cleaning policycleaning policy (Levylle(Levyllekirjoituskirjoitus ––politiikka)politiikka)
When to evict a modifiedWhen to evict a modifiedpage?page?
Multiprogramming, loadMultiprogramming, loadcontrolcontrol (Moniajoaste)(Moniajoaste)
How many processes in theHow many processes in thesystem at the same time?system at the same time?How much memory, howHow much memory, howmany pages per process?many pages per process?
Working setWorking set (Käyttöjoukko)(Käyttöjoukko)How many page framesHow many page framesallocated for one process?allocated for one process?(resident set)(resident set)Which areas have beenWhich areas have beenreferenced recently?referenced recently?
38
(Fig 8.8 [Stal05])
Perform pagereplacement
Page faultPage fault ((SivunSivun puutospuutos))
If the requested page is not in memory, MMU causesIf the requested page is not in memory, MMU causesinterruptinterruptOS process the interrupt and notice a page faultOS process the interrupt and notice a page fault
Move the process to blocked stateMove the process to blocked stateAllocate page frame for the new pageAllocate page frame for the new pageStart page load from disk using driver to command controllerStart page load from disk using driver to command controllerSwitch another process for executionSwitch another process for execution
After the device interruptAfter the device interruptOS notices the termination of page transferOS notices the termination of page transferMove process to ready stateMove process to ready stateContinue the process now or later (scheduling decision)Continue the process now or later (scheduling decision)
40
Locking pagesLocking pages
To avoid page fault, page can be locked to memoryTo avoid page fault, page can be locked to memoryKernel pagesKernel pagesKey data structures of OSKey data structures of OSI/O buffers (at least during the transfer)I/O buffers (at least during the transfer)Process pages (at least in real time systems)Process pages (at least in real time systems)
How?How?Page frame table has a lock bit on the framePage frame table has a lock bit on the frame
Per framePer frame
Or page table entry has the lock bitOr page table entry has the lock bitPer processPer process
41
Page replacementPage replacement
When?When?Page faultPage faultMemory too full, no free frames (usually some kept free!)Memory too full, no free frames (usually some kept free!)Not enough free unallocated frames (limit value)Not enough free unallocated frames (limit value)
Which page to replace?Which page to replace?One, that is not needed in the (near) futureOne, that is not needed in the (near) future
Predict based on the past behaviourPredict based on the past behaviourLocality principle helps: no more references to the page, processLocality principle helps: no more references to the page, processpassed that phasepassed that phaseSome time mistakes happenSome time mistakes happen
42
See Fig 8.1 [Stal05]