Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Virtual Memory PowerPoint Presentation
Download Presentation
Virtual Memory

Virtual Memory

168 Vues Download Presentation
Télécharger la présentation

Virtual Memory

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. VirtualMemory Operating Systems: A Modern Perspective, Chapter 12

  2. VirtualMemory 12 Operating Systems: A Modern Perspective, Chapter 12

  3. Virtual Memory Organization Primary Memory Secondary Memory Swap in the part of program that is needed Swap out the part of program that is not needed Memory Image for pi Operating Systems: A Modern Perspective, Chapter 12

  4. Address Translation • Swapping: • Address space in absolute module same size as the space in primary memory • Virtual memory • Distinguish between name, virtual address, and physical address spaces • Provides map from symbolic names to virtual addresses • Also maps from virtual addresses to physical addresses • Two mapping techniques: paging & segmentation Operating Systems: A Modern Perspective, Chapter 12

  5. Address Translation • Source program contains name space • Identifiers, labels, variables • Absolute program contains virtual address space • Compiler and link editor translate symbolic names into addresses in the virtual address space. • Executable imagedynamically translates virtual addresses to physical addresses Operating Systems: A Modern Perspective, Chapter 12

  6. Executable Image Physical Address Space Bt: Virtual Address Space  Physical Address Space Address Translation Dynamically Source Program Absolute Module Name Space Pi’s Virtual Address Space Operating Systems: A Modern Perspective, Chapter 12

  7. <1% 15% 35% 20% <1% 30% Execution time Locality Address Space for pi • Address space is logically partitioned • Text, data, stack • Initialization, main, error handle • Different parts have different reference patterns: Initialization code (used once) Code for 1 Code for 2 Code for 3 Code for error 1 Code for error 2 Code for error 3 Data & stack Operating Systems: A Modern Perspective, Chapter 12

  8. Virtual Memory • Every process has code and data locality • Code tends to execute in a few fragments at one time • Tend to reference same set of data structures • Dynamically load/unload currently-used address space fragments as the process executes • Uses dynamic address relocation/binding • Generalization of base-limit registers • Physical address corresponding to a compile-time address is not bound until run time Operating Systems: A Modern Perspective, Chapter 12

  9. Virtual Memory (cont) • Since binding changes with time, use a dynamic virtual address map (“binding”), Bt Bt:virtual address space physical address space  (W) Virtual Address Space Bt Operating Systems: A Modern Perspective, Chapter 12

  10. Address Formation • Translation system creates an address space, but its address are virtual instead of physical • A virtual address, x: • Is mapped to physical address y = Bt(x) if x is loaded at physical address y • Is mapped to W if x is not loaded • The map, Bt, changes as the process executes -- it is “time varying” • Bt: Virtual Address  Physical Address  {W} Operating Systems: A Modern Perspective, Chapter 12

  11. Virtual Memory (cont) • If Bt(i) = W at time t when process tries to read/write virtual address i: • Mem man interrupts execution • Mem man retrieves info from secondary memory and loads to some primary memory location k • Mem man updates translation: Bt(i) = k • Mem man tells process man to put process into ready queue Operating Systems: A Modern Perspective, Chapter 12

  12. Physical Address Space 0 n-1 • Each address space is fragmented Primary Memory • Fragments of the virtual address space are dynamically loaded into primary memory at any given time Virtual Memory Secondary Memory Virtual Address Space for pi Virtual Address Space for pj Virtual Address Space for pk • Complete virtual address space is stored in secondary memory Operating Systems: A Modern Perspective, Chapter 12

  13. Virtual Memory (cont) • Problem: restarting instructions • Missing data discovered when CPU tries to execute an instruction. • CPU must “back out” of the execution until mem man reads in the necessary data • When data is available, CPU must re-execute the instruction from the beginning Operating Systems: A Modern Perspective, Chapter 12

  14. Size of Blocks of Memory • Virtual memory system transfers “blocks” of the address space to/from primary memory • Fixed size blocks: System-defined pages are moved back and forth between primary and secondary memory • Variable size blocks: Programmer-defined segments – corresponding to logical fragments – are the unit of movement • Paging is the commercially dominant form of virtual memory today Operating Systems: A Modern Perspective, Chapter 12

  15. Paging • A page is a fixed size, 2h, block of virtual addresses • A page frame is a fixed size, 2h, block of physical memory (the same size as a page) • When a virtual address, x, in page i is referenced by the CPU • If page i is loaded at page frame j, the virtual address is relocated to page frame j • If page is not loaded, the OS interrupts the process and loads the page into a page frame Operating Systems: A Modern Perspective, Chapter 12

  16. Addresses • Suppose there are G= 2g2h=2g+h virtual addresses and H=2j+h physical addresses assigned to a process • Each page/page frame is 2h addresses • There are 2g pages in the virtual address space • 2j page frames are allocated to the process • Rather than map individual addresses • Bt maps the 2g pages to the 2j page frames • That is, page_framej = Bt(pagei) • Address k in pagei corresponds to address k in page_framej Operating Systems: A Modern Perspective, Chapter 12

  17. Page-Based Address Translation • Let N = {d0, d1, … dn-1} be the pages • Let M = {b0, b1, …, bm-1} be page frames • Virtual address, i, satisfies 0i<G= 2g+h • Physical address, k = U2h+V (0V<G= 2h ) • U is page frame number, 2h is the page size • V is the line number within the page • Bt:[0:G-1]  <U, V>  {W} Operating Systems: A Modern Perspective, Chapter 12

  18. Page-Based Address Translation • Physical address, k = U2h+V (0V<G= 2h ) • U is page frame number, 2h is the page size • V is the line number within the page • Bt:[0:G-1]  <U, V>  {W} • Since every page is size c=2h • page number = U = i/c • line number = V = i mod c Operating Systems: A Modern Perspective, Chapter 12

  19. Page-Based Address Translation • To get page number from an address: • Shift the binary address right h bits • Mask out the g most insignificant bits • Example: 4 pages (g = 2), 8 bytes per page (h = 3) 00 000 3 bits offset 2 bits Page num Operating Systems: A Modern Perspective, Chapter 12

  20. Page-Based Address Translation • Example: 4 pages (g = 2), 8 bytes per page (h = 3) • Address 10111 = 23 23 / 8 = 2 = 10 23 mod 8 = 7 = 111 10 111 3 bits offset 2 bits Page num Operating Systems: A Modern Perspective, Chapter 12

  21. Page-Based Address Translation Page 0 Page 1 Page 2 Page 3 00 000 01 000 10 000 11 000 00 001 01 001 10 001 11 001 00 010 01 010 10 010 11 010 00 011 01 011 10 011 11 011 00 100 01 100 10 100 11 100 00 101 01 101 10 101 11 101 00 110 01 110 10 110 11 110 00 111 01 111 10 111 11 111 Operating Systems: A Modern Perspective, Chapter 12

  22. Page-Based Address Translation Page 0 Page 1 Page 2 Page 3 00 000 01 000 10 000 11 000 00 001 01 001 10 001 11 001 00 010 01 010 10 010 11 010 Page number indicated by first 2 bits 00 011 01 011 10 011 11 011 00 100 01 100 10 100 11 100 00 101 01 101 10 101 11 101 00 110 01 110 10 110 11 110 00 111 01 111 10 111 11 111 Operating Systems: A Modern Perspective, Chapter 12

  23. Page-Based Address Translation Page 0 Page 1 Page 2 Page 3 00 000 01 000 10 000 11 000 00 001 01 001 10 001 11 001 00 010 01 010 10 010 11 010 offset indicated by last 3 bits 00 011 01 011 10 011 11 011 00 100 01 100 10 100 11 100 00 101 01 101 10 101 11 101 00 110 01 110 10 110 11 110 00 111 01 111 10 111 11 111 Operating Systems: A Modern Perspective, Chapter 12

  24. Page-Based Address Translation • Example: 8 pages (g = 3), 32 bytes per page (h = 5) • Address 10111011 = 187 187 / 32 = 5 = 101 187 mod 32 = 27 = 11011 101 11011 5 bits offset 3 bits Page num Operating Systems: A Modern Perspective, Chapter 12

  25. Page-Based Address Translation • Example: 8 pages (g = 3), 32 bytes per page (h = 5) • Address 10111011 = 187. To get the page number: shift right 5 bits mask out last 3 bits 101 11011 111 11101 000 00101 Operating Systems: A Modern Perspective, Chapter 12

  26. Page-Based Address Translation • Once you have the page number, must determine the frame number. • Use the page translation table • The Bt map Operating Systems: A Modern Perspective, Chapter 12

  27. Address Translation with Paging g bits h bits Virtual Address Page # Line # “page table” Missing Page Bt j bits h bits Physical Address Frame # Line # CPU Memory MAR Operating Systems: A Modern Perspective, Chapter 12

  28. Address Translation with Paging • Mapping: Use a page table Operating Systems: A Modern Perspective, Chapter 12

  29. Address Translation with Paging • Translation done in hardware • Use a memory management unit (MMU) • Companion chip on the motherboard Operating Systems: A Modern Perspective, Chapter 12

  30. Address Translation with Paging • Contemporary Page Table Implementation • Page tables are sparse • MMU contains a memory of register banks • Called associative or content-addressable memory • Implements page map as an inverted page table • Each cell contains a key field and a data field • Entry is addressed by the key content rather than the cell address • Key search is implemented as a parallel pattern match in hardware (search all keys at once) • If key is not present, causes a page fault interrupt • Very fast Operating Systems: A Modern Perspective, Chapter 12

  31. Address Translation with Paging page table corresponding associative memory Operating Systems: A Modern Perspective, Chapter 12

  32. Address Translation with Paging • Associative Memory • Problem: what if process is swapped out? • Associative memory table will still have it’s pages in the table • to save the table in a context switch is expensive • Solutions • Associative memory could contain one entry per physical frame • could add PID field to the key Operating Systems: A Modern Perspective, Chapter 12

  33. Address Translation with Paging • Associative Memory • Problem: primary memory has grown faster that cost-effective associative memories • Can use special cache memory instead • Translation-lookaside buffer (TLB). • Used with the address translation hardware • full page table kept in primary memory. • when page is translated to a page frame, the mapping is read into the TLB • Acts like a special cache for the page table Operating Systems: A Modern Perspective, Chapter 12

  34. Paging • A paging policy defines how the page frames will be loaded and unloaded by the virtual memory system. Operating Systems: A Modern Perspective, Chapter 12

  35. Paging • paging policies: • Fetch policy. Decides when a page should be loaded into memory • Replacement policy. Determines which page should be removed from primary memory if all page frames are full. • Placement policy. Determines where the fetched page should be loaded in primary memory. Operating Systems: A Modern Perspective, Chapter 12

  36. The Fetch Policy • The fetch policy determines when a page should be brought into primary memory. • Prefetch paging. Pages are fetched into memory before they are needed. • difficult; how do we know which pages are going to be used? • Demand paging. Pages are fetched into memory only when process addresses it (demands it) Operating Systems: A Modern Perspective, Chapter 12

  37. Demand Paging Algorithm • Page fault occurs • Process with missing page is interrupted • Memory manager locates the missing page • Page frame is unloaded (replacement policy) • Page is loaded in the vacated page frame • Page table is updated • Process is restarted Operating Systems: A Modern Perspective, Chapter 12

  38. Modeling Page Behavior • Let R = r1, r2, r3, …, ri, … be a page reference stream • ri is the ith page # referenced by the process • The subscript is the virtual time for the process (time that a process references the next page) • Example: • Let N be the set of pages in the virtual address space: {0, 1, 2, 3, 4, 5} • An example reference stream is: R = 2, 0, 3, 4, 3, 2, 0, 2, 2, 3, … Operating Systems: A Modern Perspective, Chapter 12

  39. Modeling Page Behavior • Assume a frame allocation of m, • the memory state at time t, St(m), is the set of pages loaded • St(m) = St-1(m)  Xt - Yt • Xt is the set of fetched pages at time t • Yt is the set of replaced pages at time t • if no pages are initially loaded then • S0(m) = F (the empty set) Operating Systems: A Modern Perspective, Chapter 12

  40. More on Demand Paging • rt is the page addressed at time t • If rt was loaded at time t-1, St(m) = St-1(m) • If rt was not loaded at time t-1 and there were empty page frames • St(m) = St-1(m)  {rt} • If rt was not loaded at time t-1 and there were no empty page frames • St(m) = St-1(m)  {rt} - {y} where y is the page chosen to be swapped out. Operating Systems: A Modern Perspective, Chapter 12

  41. More on Demand Paging • example: R = 2, 0, 3, 4, 3, 2, 0, 2, 2, 3, … • assume m = 4 S0(m) = F S1(m) = {2} S2(m) = {0, 2} S3(m) = {0, 2, 3} S4(m) = {0, 2, 3, 4} S1(m) = ? must replace a page Operating Systems: A Modern Perspective, Chapter 12

  42. Paging algorithms • Two basic families of paging algorithms • static allocation • process uses a fixed number of allocated page frames • dynamic allocation • number of allocated page frames changes as the process executes. Operating Systems: A Modern Perspective, Chapter 12

  43. Static Allocation, Demand Paging • Number of page frames is static over the life of the process • Fetch policy: demand • Placement policy: must place the new page into the page just replaced. • Since St(m) = St-1(m)  {rt} - {y}, the replacement policy must choose y -- which uniquely identifies the paging policy Operating Systems: A Modern Perspective, Chapter 12

  44. 13 page faults • No knowledge of R not perform well • Easy to implement Random Replacement • Replaced page, y, is chosen from the m loaded page frames with probability 1/m Let page reference stream,R = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 1 2 2 2 0 2 0 3 2 1 3 2 1 3 2 1 0 3 1 0 3 1 0 3 1 2 0 1 2 0 3 2 0 3 2 0 6 2 4 6 2 4 5 2 7 5 2 Operating Systems: A Modern Perspective, Chapter 12

  45. Belady’s Optimal Algorithm • Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x) Let page reference stream,R = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 0 0 2 3 Operating Systems: A Modern Perspective, Chapter 12

  46. Belady’s Optimal Algorithm • Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x) Let page reference stream,R = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 1 0 0 2 3 FWD4(2) = 1 FWD4(0) = 2 FWD4(3) = 3 Operating Systems: A Modern Perspective, Chapter 12

  47. Belady’s Optimal Algorithm • Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x) Let page reference stream,R = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 1 0 0 0 2 31 FWD4(2) = 1 FWD4(0) = 2 FWD4(3) = 3 Operating Systems: A Modern Perspective, Chapter 12

  48. Belady’s Optimal Algorithm • Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x) Let page reference stream,R = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 1 0 0 0 0 0 2 31 1 1 Operating Systems: A Modern Perspective, Chapter 12

  49. Belady’s Optimal Algorithm • Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x) Let page reference stream,R = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 2 1 0 0 0 0 0 3 2 31 1 1 1 FWD7(2) = 2 FWD7(0) = 3 FWD7(1) = 1 Operating Systems: A Modern Perspective, Chapter 12

  50. Belady’s Optimal Algorithm • Replace page with maximal forward distance: yt = max xeS t-1(m)FWDt(x) Let page reference stream,R = 2031203120316457 Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7 0 2 2 2 2 2 2 2 2 2 0 1 0 0 0 0 0 3 3 3 3 2 31 1 1 1 1 1 1 FWD10(2) =  FWD10(3) = 2 FWD10(1) = 3 Operating Systems: A Modern Perspective, Chapter 12