virtual memory questions
play

Virtual Memory Questions? ! What is virtual memory and when is it - PDF document

Virtual Memory Questions? ! What is virtual memory and when is it useful? CSCI [4|6] 730 ! What is demand paging? Operating Systems ! What pages should be resident in memory, and which should be replaced? Virtual Memory ! What is the


  1. Virtual Memory Questions? ! What is virtual memory and when is it useful? CSCI [4|6] 730 ! What is demand paging? Operating Systems ! What pages should be » resident in memory, and » which should be replaced? Virtual Memory ! What is the working set model? Maria Hybinette, UGA Maria Hybinette, UGA Operating System ’ s Goals The Illusion: “ Virtual ” Memory ! OS provides an illusion of more ! Support processes when there is not enough memory than is physically physical memory available: » Single process with very large address space » Large logical space but really » Multiple processes with combined address spaces » small physical memory ! User code should be independent of amount ! Why does this work? of physical memory » Only part of the program needs to be in memory (at a particular » Correctness, if not performance time) for execution » Relies on key properties of user processes – workload and – machine architecture (hardware) Maria Hybinette, UGA Maria Hybinette, UGA The Million Dollar Question? ! How do the OS decide what is in “ main ” memory and what is on disk? ! How can we decide? » Memory Access Patterns Maria Hybinette, UGA

  2. Observations: Memory Access Patterns Approach: Demand Paging ! Sequential memory accesses of a process are ! Bring in pages into predictable and tend to have locality of reference : memory only when needed » Spatial: reference memory addresses near previously » Less memory referenced addresses (in memory) » Less I/O » Temporal: reference memory addresses that have » Faster response time? referenced in the past ! Process viewed as a ! Processes spend majority of time in small portion sequence of page of code accesses rather than » Estimate: 90% of time in 10% of code contiguous address space ! Implication: » Process only uses small amount of address space at any moment » Only small amount of address space must be resident in physical memory Maria Hybinette, UGA Maria Hybinette, UGA Virtual Memory Approach: Virtual Address Space Intuition Mechanisms ! Each page in virtual address space maps to one of three ! Idea : OS keeps unreferenced pages on disk locations: » Slower, cheaper backing store than memory » Physical main memory: Small, fast, expensive ! Process can run when not all pages are loaded into » Disk (backing store): Large, slow, cheap main memory » Nothing (error): Free ! OS and hardware cooperate to provide illusion of large disk as fast as main memory Smaller, faster » Same behavior as if all of address space in main memory and more Leverage memory expensive » Hopefully have similar performance registers hierarchy of machine ! Requirements: architecture cache » OS must have mechanism to identify location of each Each layer acts as “ backing store ” for the page in address space in memory or on disk Bigger, slower main memory layer above and cheaper » OS must have policy for determining which pages live in memory and which (remain) on disk Disk Storage Maria Hybinette, UGA Maria Hybinette, UGA Virtual Address Space Mechanisms Virtual Memory Mechanisms (cont) Frame # � valid-invalid bit � Extend page tables with an extra bit The TLB factor: Hardware and OS cooperate to translate addresses 1 � to indicate whether it is in memory 1 � ! First, hardware checks TLB for virtual address 1 � or on disk (a resident bit): 1 � » TLB hit: Address translation is done; page in physical memory 0 � ! valid (or invalid) � ! � » TLB miss: ! Page in memory: valid bit set in page 0 � table entry (PTE) – Hardware or OS walk page tables 0 � page table � ! Page out to disk: valid bit cleared – If PTE designates page is valid, then page in physical memory ( invalid ) ! Main Memory Miss: Not in main memory: Page fault (i.e., – PTE points to block on disk invalid ) – Causes trap into OS when page is » Trap into OS (not handled by hardware) referenced – Trap: page fault » [if memory is full)] OS selects victim page in memory to replace – Write victim page out to disk if modified (add dirty bit to PTE) » OS reads referenced page from disk into memory » Page table is updated, valid bit is set » Process continues execution Maria Hybinette, UGA Maria Hybinette, UGA

  3. Flow of “ Paging ” Operations Virtual Memory Policies PTE in TLB? CPU checks TLB ! OS needs to decide on policies on page faults concerning: Yes PTE in TLB? » Page selection (When to bring in) No Access page table – When should a page (or pages) on disk be brought into memory? Page fault routine – Two cases OS Instructs CPU No to read the page Page in MM? ! When process starts, code pages begin on disk from disk Yes ! As process runs, code and data pages may be moved to disk CPU activates Update TLB I/O hardware » Page replacement (What to replace) Page transferred – Which resident page (or pages) in memory should be thrown out from disk to CPU generates main memory physical address to disk? ! Goal: Minimize number of page faults Yes Memory Full? » Page faults require milliseconds to handle (reading from disk) No Page replacement » Implication: Plenty of time for OS to make good decision Page tables updated Maria Hybinette, UGA The When : Page Selection Page Selection Continued ! When should a page be brought from disk into memory? ! Prepaging (anticipatory, prefetching): OS loads page into memory before page is referenced ! Request paging: User specifies which pages are needed for process » OS predicts future accesses (oracle) and brings pages into memory ahead of time » Problems: – How? – Manage memory by hand – Works well for some access patterns (e.g., sequential) – Users do not always know future references » Advantages: May avoid page faults – Users are not impartial (and infact they may be wrong) » Problems? : ! Demand paging: Load page only when page fault occurs ! Hints: Combine demand or prepaging with user- » Intuition: Wait until page must absolutely be in memory supplied hints about page references » When process starts: No pages are loaded in memory » User specifies: may need page in future, don ’ t need this » Advantage: Less work for user page anymore, or sequential access pattern, ... » Disadvantage: Pay cost of page fault for every newly » Example: madvise() in Unix (1994 4.4 BSD UNIX) accessed page Maria Hybinette, UGA Maria Hybinette, UGA What happens if there is no free Virtual Page Optimizations frame? ! Page replacement ! Copy-on-Write: on process creation allow » find some page in memory, that is not really in use, parent and child to share the same page in and swap it out. memory until one modifies the page. ! Observation: Same page may be brought into memory several times (so try to keep that one in memory) copy page C Maria Hybinette, UGA Maria Hybinette, UGA

  4. Page Replacement Strategies Page Replacement Continued ! Which page in main memory should selected as victim? ! FIFO: Replace page that has been in memory the longest » Write out victim page to disk if modified (dirty bit set) » Intuition: First referenced long time ago, done with it now » If victim page is not modified (clean), just discard (cheaper to replace) » Advantages: 3 Frames – Fair: All pages receive equal residency A B D A B B A B A C D – Easy to implement (circular buffer) C » Disadvantage: Some pages may always be needed Future ! OPT: Replace page not used for longest time in future ! LRU: Replace page not used for longest time in past » Advantage: Guaranteed to minimize number of page faults » Intuition: Use the past to predict the future » Disadvantage: Requires that OS predict the future MRU » Advantages: – Not practical, but is good to use comparison (best you can do) – With locality, LRU approximates OPT (but look backwards) ! Random: Replace any page at random » Disadvantages: » Advantage: Easy to implement – Harder to implement, must track which pages have been accessed » Surprise?: Works okay when memory is not severely over- – Does not handle all workloads well committed (recall lottery scheduling, random is not too shabby, in many areas) LRU Maria Hybinette, UGA Maria Hybinette, UGA MFR, LFU Example: Counting Faults of How to Evaluate Page Replacement FIFO Page Replacement Algorithm Algorithms? ! Want: lowest page-fault rate (least #misses) ! Idea: Keep track of memory references – test with particular string of memory references and count page faults (based on real data or generated) ! Algorithm: Convert address to page location » Example: Assume 100 bytes per page and – Step 1: Assume the address sequence: First IN is the one that is first out ! 0100, 0210, 0250, 0300, 0350, 0380, 0400, 0160, 0250, 0505, 0100, 0110, 0230, 0350, 0450, 0450, 0500, 0500 ! 3 Frames are available – Step 2: Convert address to a page reference string: ! FIFO: Replace page that has been in memory the ! 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. longest – Step 3: Count page faults. ! Count page faults ? Maria Hybinette, UGA Maria Hybinette, UGA Page Replacement Example Page Replacement Example Page reference string: A B C A B D A D B C B Page reference string: A B C A B D A D B C B Three pages of physical memory Three pages of physical memory 5 7 5 OPT FIFO LRU OPT FIFO LRU ABC ABC A B C A B C A B C A A B B D D A B D D B C A B D A A D A C D D B B D A B C C C A B B C B D B B Maria Hybinette, UGA Maria Hybinette, UGA

Recommend


More recommend