Memory-Mapped Files & Unified VM SystemMechanicsAllocating MemoryManual Memory TuningWhat Is Main Memory?Consider Ages With PagesUnified VM SystemsWhy Mmap?Mmap DefinitionMmap DiagramMmap ImplicationsMmap Versus ReadCost ComparisonLazy Versus EagerDouble BufferingSharing MemoryReloading StateWhat Is a “Private” Mapping?Memory-Mapped Files & Unified VM SystemVivek Pai2MechanicsForgot to finish producer-consumerMemory-mapped files & unified VM3Allocating MemoryOld days: manual tuning of sizesBenefits?Drawbacks?VMVMVMVMVMVMVMVMFS CacheNetworkOSOSDesktopVMVMNetworkOSOSServerFS CacheFS CacheFS CacheFS CacheFS CacheFS CacheNetwork4Manual Memory TuningFixed-size allocations for VM, FS cacheDone right, protects programs from each otherBacking up filesystem trashes FS cacheLarge-memory programs don’t compete with disk-bound programsDone poorly? Memory underutilized5What Is Main Memory?At some level, a cache for the diskPermanent data written back to fsTemporary data in main memory or swapMain memory is much faster than diskConsider one program that accesses lots of files and uses lots of memoryHow do you optimize this program?Could you view all accesses as page faults?6Consider Ages With PagesWhat happens if 5 FS pages are really active?What happens if relative demands change over time?VM, 1VM, 1VM, 3VM, 5VM, 10VM, 20VM, 50VM, 100FS, 5FS, 3FS, 1FS, 17Unified VM SystemsNow what happens when a page is needed?What happens on disk backup?Did we have the same problem before?VM, 1VM, 1FS, 1FS, 1FS, 3VM, 3VM, 5FS, 5VM, 10VM, 20VM, 50VM, 1008Why Mmap?File pages are a lot like VM pagesWe don’t load all of a process at once – why load all of a file at once?Why copy a file to access it?There’s one good reason9Mmap Definition void *mmap( void *addr, size_t len, int prot, int flags, int fildes, off_t off);Addr: where we want to map itLen: how much we want mappedProt: allow reading, writing, execFlags: is mapped shared/private/anonymous, fixed/variable location, swap space reserved?Fildes: what file is being mappedOff: start offset in file10Mmap DiagramCodeCodeDataHeapStackStackFile A Process File B11Mmap Implications# of VM regions increasesWas never really just code/text/heap/stackAccess/protection info on all regionsFilesystem no longer sole way to access filePreviously, access info via read( ) and write( )Same file via filesystem and mmap?12Mmap Versus ReadWhen read( ) completesAll pages in range were loaded at some pointA copy of the data in user’s buffersIf underlying file changes, no change to data in user’s bufferWhen mmap( ) completesMapping of the file is completeVirtual address space modifiedNo guarantee file is in memory13Cost ComparisonRead:All work done (incl disk) before call returnsNo extra VM trickery neededContrast with write( )Mmap:Inode in memory from open( )Mapping is relatively cheapPages needed only on access14Lazy Versus EagerEager:Do it right nowBenefit: low latency if you need itDrawback: wasted work if you don’tLazy:Do it at the last minuteBenefit: “pay as you go”Drawback: extra work if you need it all15Double BufferingCodeCodeDataFS CopyHeapStackStackFSFile Read Process File Mmap16Sharing MemoryTwo processes map same file sharedBoth map it with “shared” flagSame physical page accessed by two processes at two virtual addressesWhat happens when that page victimized (PTE mechanics)?Have we seen this somewhere else?17Reloading StateMap a file at a fixed locationBuild data structures inside itRe-map at program startupBenefits versus other approaches?18What Is a “Private” Mapping?Process specifies changes not to be visible to other processesModified pages look like VM pagesWritten to swap if pressureDisposed when process
View Full Document