
Operang Systems CMPSCI 377 Virtual Memory & Paging Emery Berger and Mark Corner University of Massachuse9s Amherst Virtual vs. Physical Memory . Apps don’t access physical memory – Well, not directly . Apps use virtual memory – Addresses start at 0 – One level of indirec?on – Address you see is not “real” address . Any ideas why? 2 Memory Pages . Programs use memory as individual bytes . OS manages groups oF bytes: pages – typically 4kB, 8kB – Why? (think Tetris with all squares) – Applies this to virtual and physical memory • Physical pages usually called frames A Mapping Virtual to Physical . Note this is simplified and the data here includes the heap, not the typical data segment… 4 Why Virtual Memory? . Why? – Simpler • Everyone gets illusion oF whole address space – Isolaon • Every process protected From every other – Op&mizaon • Reduces space requirements 5 Typical Virtual Memory Layout . Some things grow – Must leave room! . Mmap and heap spaces – Mmap increases mmap – Brk increases heap . Other layouts possible Quick Quiz! . Are arrays con&nuous in memory? – Physical memory? – Virtual memory? . Where does the code For a program live? Memory Management Unit . Programs issue loads and stores . What kind oF addresses are these? . MMU Translates virtual to physical addresses – Maintains page table (big hash table): – Almost always in HW… Why? Virtual Physical Program Address MMU Address Memory Page Table Page Tables . Table oF translaons – virtual pages -> physical pages . One page table per process . One page table entry per virtual page . How? – Programs issue virtual address – Find virtual page (how?) – Lookup physical page, add offset Page Table Entries . Do all virtual pages -> physical page? – Valid and Invalid bits . PTEs have lots oF other inFormaon – For instance some pages can only be read Address Transla?on . Powers oF 2: – Virtual address space: size 2^m – Page size 2^n . Page#: High m-n bits oF virtual address . Lower n bits select offset in page 11 Quick Ac?vity . How much mem does a page table need? – 4kB pages, 32 bit address space – page table entry (PTE) uses 4 bytes . 2^32/2^12*4=2^22 bytes=4MB – Is this a problem? – Isn’t this per process? – What about a 64 bit address space? . Any ideas how to fix this? Mul?-Level Page Tables . Use a mul&-level page table A A Level 0 Table Level 1 Table A Level 1 Table Quick Ac?vity . How much mem does a page table need? – 4kB pages, 32 bit address space – Two level page table – 20bits = 10 bits each level – page table entry (PTE) uses 4 bytes – Only first page oF program is valid . 2^10*4+2^10*4=2^13 bytes=8kB . Isn’t this slow? Transla?on Lookaside Buffer (TLB) . TLB: Fast, Fully associave memory – Caches page table entries – Stores page numbers (key) and Frame (value) in which they are stored . Assump&on: locality oF reFerence – Locality in memory accesses = locality in address translaon . TLB sizes: 8 to 2048 entries – Powers oF 2 simplifies translaon oF virtual to physical addresses 15 Virtual Memory is an Illusion! . How much memory does a process have? – Do all processes have this? . Key idea: use RAM as cache For disk – OS transparently moves pages . Requires locality: – Working set must fit in RAM • memory reFerenced recently – If not: thrashing (nothing but disk traffic) Paging 17 Paging + Locality . Most programs obey 90/10 “rule” – 90% oF &me spent accessing 10% oF A memory A . Exploit this rule: B – Only keep “live” parts B of process in memory 18 Key Policy Decisions . Two key ques&ons: (For any cache): – When do we read page From disk? – When do we write page to disk? 19 Reading Pages . Read on-demand: – OS loads page on its first reFerence – May Force an evic&on oF page in RAM – Pause while loading page = page Fault . Can also perForm pre-paging: – OS guesses which page will next be needed, and begins loading it . Most systems just do demand paging . What about writes? 20 Demand Paging . On every reFerence, check iF page is in memory (resident bit in page table) – Who is doing this? . If not: trap to OS – How does this work in HW? . OS: – Selects vic&m page to be replaced – Writes vic&m page iF necessary, marks non-resident – Begins loading new page From disk – OS can switch to another process • more on this later 21 Swap Space . Swap space = where vic&m pages go – Par&&on or special file reserved on disk . Size oF reserved swap space limits what? 22 Tricks with Page Tables . Do all pages oF memory end up in swap? . Parts oF address space mapped into files – see man pages For mmap Overview . A Day in the LiFe oF a Page – Allocaon – Use – Evicon – Reuse . Terms: Resident and Non-resident 24 A Day in the Life of a Page Allocate some memory char * x = new char[16]; 0x40001000 0x40001040 → 0x4000104F virtual memory layout 25 A Day in the Life of a Page Update page tables char * x = new char[16]; 0x40001000 0x40001040 → 0x4000104F virtual physical memory memory layout layout 26 A Day in the Life of a Page Write contents – dirty page strcpy(x, “hello”); 0x40001000 0x40001040 → 0x4000104F virtual physical memory memory layout layout 27 A Day in the Life of a Page Other processes fill up memory… virtual physical memory memory layout layout 28 A Day in the Life of a Page Forcing our page to be evicted (paged out) virtual physical swap memory memory space layout layout (disk) 29 A Day in the Life of a Page Now page nonresident & protected virtual physical swap memory memory space layout layout (disk) 30 A Day in the Life of a Page Touch page – swap it in y[0] = x[0]; 0x40001000 0x40001040 → 0x4000104F virtual physical swap memory memory space layout layout (disk) 31 A Day in the Life of a Page Touch page – swap it in y[0] = x[0]; 0x40001000 0x40001040 → 0x4000104F virtual physical swap memory memory space layout layout (disk) 32 Tricks with Page Tables: Sharing . Paging allows sharing oF memory across processes – Reduces memory requirements . Shared stuff includes code, data – Code typically R/O 33 Tricks with Page Tables: COW . Copy on write (COW) – Just copy page tables – Make all pages read-only . What iF process changes mem? . All processes are created this way! Alloca?ng Pages . ul&mately From sbrk or mmap . Sbrk increases # oF valid pages – Increases the heap . Mmap maps address space to file – Increases the mmap space . Oddity: – Allocators can use either mmap or brk to get pages – You will use mmap . What does mmap /dev/zero mean? – Think about COW Overview . Replacement policies – Comparison 36 Cost of Paging . Usually in algorithms, we pick algorithm with best asympto&c worst-case – Paging: worst-case analysis useless! – Easy to construct adversary: every page requires page Fault A, B, C, D, E, F, G, H, I, J, A... size of available memory 37 Cost of Paging . Worst-case analysis – useless – Easy to construct adversary example: every page requires page Fault A A, B, C, D, E, F, G, H, I, J, A... size of available memory 38 Cost of Paging . Worst-case analysis – useless – Easy to construct adversary example: every page requires page Fault A B A, B, C, D, E, F, G, H, I, J, A... size of available memory 39 Cost of Paging . Worst-case analysis – useless – Easy to construct adversary example: every page requires page Fault A B C A, B, C, D, E, F, G, H, I, J, A... size of available memory 40 Cost of Paging . Worst-case analysis – useless – Easy to construct adversary example: every page requires page Fault A B C D A, B, C, D, E, F, G, H, I, J, A... size of available memory 41 Cost of Paging . Worst-case analysis – useless – Easy to construct adversary example: every page requires page Fault A B C D E A, B, C, D, E, F, G, H, I, J, A... size of available memory 42 Cost of Paging . Worst-case analysis – useless – Easy to construct adversary example: every page requires page Fault F B C D E A, B, C, D, E, F, G, H, I, J, A... size of available memory 43 Cost of Paging . Worst-case analysis – useless – Easy to construct adversary example: every page requires page Fault F G H I J A, B, C, D, E, F, G, H, I, J, A... size of available memory 44 Op?mal Replacement (MIN/OPT) . Evict page accessed Furthest in Future – Op&mal page replacement algorithm • Invented by Belady (“MIN”), a.k.a. “OPT” . Provably op&mal policy – Just one small problem... Requires predic&ng the Future – UseFul point oF comparison • How Far From op&mal 45 Quick Ac?vity: OPT sequence oF page accesses contents oF page Frames . Page Faults: 5 46 Least-Recently Used (LRU) . Evict page not used in longest &me (least-recently used) – Approximates OPT • If recent past ≈ predictor oF Future – Variant used in all real operang systems 47 Quick Ac?vity: LRU eXample . Page Faults: ? 48 LRU eXample . Page Faults: 5 49 LRU, eXample II . Page Faults: ? 50 LRU, eXample II . Page Faults: 12! – Loop: well-known worst-case For LRU 51 Most-Recently Used (MRU) . Evict most-recently used page . Shines For LRU’s worst-case: loop that exceeds RAM size A, B, C, D, A, B, C, D, ... size of available memory 52 Most-Recently Used (MRU) . Evict most-recently used page . Shines For LRU’s worst-case: loop that exceeds RAM size A A, B, C, D, A, B, C, D, ... size of available memory 53 Most-Recently Used (MRU) .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages113 Page
-
File Size-