Demand Paging CSE 120: Principles of Opera�Ng Systems

Demand Paging CSE 120: Principles of Opera�Ng Systems

Lecture 9: Demand Paging CSE 120: Principles of Operang Systems UC San Diego: Summer Session I, 2009 Frank Uyeda Announcements • Project 2 is due on Tonight • Homework 3 is due Monday. • Final Exam: 3p‐6p on Saturday, August 1 2 PeerWise • Processes are loaded and then removed from memory, creang free memory space broken into lile pieces. What type of fragmentaon is this? – Internal – External 3 PeerWise • What is the difference between "Segmenon" and "Paging"? (Choose the one with the highest truth value) – A. Le has the answer. – B. Segmentaon is non‐connous allocaon of pages of memory, where paging is connous allocaon of pages of memory. – C. Segmentaon is the use of variable page sizes, and paging is the use of discrete page sizes. – D. C – E. D & A • Really?! – None of the answers are correct… except, maybe A. 4 V Page Frame Recap: Paging 1 0x04 Process 1’s VAS 1 0x01 Physical Memory 1 0x05 Page 1 0x00000000 1 0x07 Page 1 Page 2 0x00000400 Process 2’s VAS Page 2 Page 3 MMU ….. Page 1 TLB Page 3 Page ... Page M Page 2 Table Page P Page 3 V Page Frame .. ….. 1 0x04 1 0x01 Page N Page Q 0x00040000 5 1 0x05 1 0x07 Recap: Virtual Memory • Memory Management Unit (MMU) – Hardware unit that translates a virtual address to a physical address • Translaon Table – Stored in main memory – Each Page Table Entry stores permission and bookkeeping bits. • Translaon Lookaside Buffer (TLB) – Hardware cache for the MMU’s virtual‐to‐physical translaons table Virtual Address M R V Prot Page Frame Number Translaon Physical CPU MMU Table Address Memory TLB 6 Recap: Page Tables Physical Memory 0x00000000 Virtual Address Page number Offset Page 1 Physical Address Page 2 Page frame Offset Page Table Page 3 ….. Page table entry Page N 0xFFFFFFFF Problem: Page Tables can be large! 7 Recap: 2 Level Page Table Physical Memory 0x00000000 Virtual Address Master Secondary Offset Page 1 Physical Address Page 2 Page frame Offset Page 3 ….. Page frame (PTE) Page frame (PTE) Page table entry Page N Master Secondary 0xFFFFFFFF Page Table Page Table(s) 8 Inverted Page Table Physical Memory 0x00000000 Virtual Address PID VPN Offset Page 1 Physical Address Page 2 index Offset Page 3 index ….. <PID, VPN> Page N Inverted Page Table 0xFFFFFFFF 9 Inverted Page Table • One global page table – One page table entry per physical page. – Entries keyed by PID and virtual page number. – Physical frame number = index in page table. • Advantages – Bounded amount of memory for page table(s). • 32‐bit address space, 4K pages, 4GB RAM, 4B per PTE • 1‐level page table: _____ x # processes. • Inverted Table: ______ MB • Disadvantages – Costly translaon • Using hashing can help 10 Recap: Segmentaon Heap Stack Text Data 11 Recap: Segmentaon Physical Memory Segment Table Virtual Address limit base Segment # Offset Yes? < + No? Protecon Fault 12 Goals for Today • Paging Tricks – Demand Paging – Copy on Write • Page Replacement Policies – Which pages should we keep in memory? – How many pages should each process get? 13 Paged Virtual Memory • What if there isn’t enough physical memory available to load a program? – A large poron of program’s code may be unused – A large poron of program’s code may be used infrequently • Idea: load a page only if you need it Process’s Virtual Address Space Machine’s Physical Address Space (available RAM) 14 Demand Paging • We’ve menoned before that pages can be moved between memory and disk – This process is called demand paging • OS uses main memory as a page cache of all the data allocated by processes in the system – Inially, pages are allocated from memory – When memory fills up, allocang a page in memory requires some other page to evicted from memory • Why physical memory pages are called “frames” – Evicted pages go to disk (where? The swap file paron) – The movement of pages between memory and disk is done by the OS, and is transparent to the applicaon 15 Paged Virtual Memory • A program’s pages loaded on demand Machine’s Physical Address Space • Where are pages otherwise? (available RAM) – On disk (executable) – In OS swap file (an actual file on disk) – Shared (more later) Process’s Virtual Address Space Disk ~/program.coff /swap 16 Demand Paging Example Process’s Virtual Address Space Physical Memory PC PC Page Table VPN V PPN 0x00 1 0x00 0x01 0 1 0x01 ‐‐‐ 0x02 0 ‐‐‐ 0x03 0 ‐‐‐ … Disk 0x10 0 ‐‐‐ 0x11 0 ‐‐‐ ~/program.coff /swap … … 17 Demand Paging Example Process’s Virtual Address Space Physical Memory 0x00 PC Page Table VPN V PPN 0x00 1 0x00 0x01 0 1 0x01 ‐‐‐ 0x09 0x02 1 0x02 0x0A 0x03 1 0x03 0x04 1 … 0x09 Disk 0x10 1 0 0x00 ?? 0x09 0x11 0 ‐‐‐ ~/program.coff /swap … … 18 Page Faults • What happens when a process accesses a page that has been evicted (or is otherwise not in memory)? – 1. When it evicts a page, the OS sets the PTE as invalid and stores the locaon of the page in the swap file in the PTE – 2. When a process accesses the page, the invalid PTE will cause a trap (page fault) – 3. The trap will run the OS page fault handler – 4. Handler uses the invalid PTE to locate page in swap file – 5. Reads page into a physical frame, updates PTE to point to it – 6. Restarts process • But where does it put it? Has to evict something else – OS usually keeps a pool of free pages around so that allocaons do not always cause evicons – Replace another page • Evicted page may go in swap file. Which pages need to be wrien out to swap ? 19 Recap: Page Lookups Virtual Address CPU TLB Page Table Disk Physical Memory Address 20 Recap: Page Lookups Virtual Address CPU TLB ..... Secondary Page Table Page Table Disk Physical Memory Address 21 Recap: Page Lookups Virtual Address Case 1: Virtual Address –to‐ Physical Address Mapping is in TLB CPU VPN V R M RO PFN 0x000 1 1 0 1 0x100 TLB 0x121 1 0 0 0 0x111 Secondary Page Table 0x071 Page Table 1 1 0 0 0x212 (1) How many memory references? Disk TLB_hit: 1 memory reference Physical Memory Address 22 Recap: Page Lookups Virtual Address Case 2: Virtual Address –to‐ Physical Address Mapping is not in TLB but is in Memory CPU VPN V R M RO PFN 0x000 1 1 0 1 0x100 TLB 0x121 1 0 0 0 0x111 Secondary (1) Page Table Page Table 0x071 1 1 0 0 0x212 (2) Disk How many memory references? Physical Mem_hit: 1 page table lookup Memory Address + 1 memory reference Two‐level Page table? 23 Recap: Page Lookups Virtual Address Case 3: Virtual Address –to‐ Physical Address Mapping is not in TLB and not in Memory CPU VPN V R M RO PFN 0x000 1 1 0 1 0x100 TLB 0x121 0 0 0 0 0x111 Secondary (1) Page Table (3?) Page Table 0x071 1 1 0 0 0x212 (2) (4?) How many memory references? Mem_miss: 1 page table lookup + 1 page table update(s)* Disk Physical + 1 memory reference Memory Address How much me? disk retrieval involved too 24 Memory Accesses • TLB Hit: fast – 1 TLB access + 1 memory access • Memory Hit: medium – 1 TLB access + 2+ memory access • Memory Miss: slow – 1 TLB access + 3+ memory access + 1 disk access • Want things fast as possible; minimize me spent resolving physical address. How do you do this? – Minimize number of memory references (TLB policy) – Minimize number of page faults (Page replacement policy) 25 Observaon • OSes spend a lot of me copying data – System call arguments between user/kernel space – Enre address spaces to implement fork() • How can we reduce this cost? 26 Page Sharing Emacs buffer #1 Emacs buffer #2 VPN V PPN VPN V PPN 0x00 1 0x03 0x00 1 0x03 0x04 0x010x03 0 1 0x11 ‐‐‐ 0x010x03 0 1 0x13 ‐‐‐ 0x10 1 0x12 0x10 1 0x14 0x11 1 0x15 0x11 1 0x16 Physical Memory .. What if shared page is modified? 27 Copy‐On‐Write • OSes spend a lot of me copying data – System call arguments between user/kernel space – Enre address spaces to implement fork() • Use Copy‐on‐Write (CoW) to defer large copies as long as possible, hoping to avoid them altogether – Instead of copying pages, create shared mappings of parent pages in child virtual address space – Shared pages are protected as read‐only in child • Reads happen as usual • Writes generate a protecon fault, trap to OS, copy page, change page mapping in client page table, restart write instrucon • How does this help fork()? 28 Page Sharing: CoW Emacs buffer #1 VPN V M PPN Emacs buffer #2 0x00 1 0 0x03 0x010x03 0 1 0 ‐‐‐ 0x11 0x10 1 0 1 0x12 0x11 1 0 0x15 VPN V M PPN 0x00 1 0 0x03 0x010x03 0 1 0 ‐‐‐ 0x13 0x10 1 0 0x12 0x11 1 0 0x16 Physical Memory Buffers share VPN 0x12 .. Process writes to page 29 Page Sharing: CoW Emacs buffer #1 VPN V M PPN Emacs buffer #2 0x00 1 0 0x03 0x010x03 0 1 0 ‐‐‐ 0x11 0x10 1 0 0x12 0x11 1 0 0x15 VPN V M PPN 0x00 1 0 0x03 0x010x03 0 1 0 ‐‐‐ 0x13 0x10 1 0 0x14 0x11 1 0 0x16 Physical Memory Buffers share VPN 0x12 .. Process writes to page New page is allocated 30 Paging Tricks Summary • Demand Paging – Observaon: Limited available RAM, not all pages used – Idea: Load pages only when needed • Page Sharing – Observaon: Oen mes, processes have similar virtual pages – Idea: Copy‐on‐write for efficient page sharing 31 Actual Memory Stascs 3.7 GB used, 4.1 GB available => 90+% memory used 2.1 GB swap allocated 32 Actual Memory Stascs ~10% ‐ 50% of memory is shared per process => ~370 MB – 1.85 GB mem saved 33 Actual Memory Stascs Which pages to evict? How much physical memory should each process get? 34 Page Replacement • Problem: Memory is scarce – Idea: let’s use only pages that we need • Page replacement policy: – How do we decide which pages to kick out when memory is full or near full? – How much memory do we allocate to each process? 35 Page Replacement Policy • Want to keep most acve pages in memory – Conversely, want to kick out inacve pages • What can we do? – Swapping:

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    67 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us