Virtual Memory Prof. Van Renesse & Sirer. Segments Note: overloaded term… Chunks of virtual address space Access Protection – User/Supervisor – Read/Write/Execute.

Slides:



Advertisements
Similar presentations
Memory.
Advertisements

Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Chapter 9: Virtual Memory
Chapter 4 Memory Management Page Replacement 补充:什么叫页面抖动?
Virtual Memory. 2 What is virtual memory? Each process has illusion of large address space –2 32 for 32-bit addressing However, physical memory is much.
Virtual Memory.
OS Fall’02 Virtual Memory Operating Systems Fall 2002.
Paging Hardware With TLB
Caching and Virtual Memory. Main Points Cache concept – Hardware vs. software caches When caches work and when they don’t – Spatial/temporal locality.
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Module 9: Virtual Memory
Module 10: Virtual Memory Background Demand Paging Performance of Demand Paging Page Replacement Page-Replacement Algorithms Allocation of Frames Thrashing.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
Paging and Virtual Memory. Memory management: Review  Fixed partitioning, dynamic partitioning  Problems Internal/external fragmentation A process can.
Memory Management.
Chapter 7: Main Memory CS 170, Fall Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation.
Virtual Memory Management B.Ramamurthy. Paging (2) The relation between virtual addresses and physical memory addres- ses given by page table.
03/22/2004CSCI 315 Operating Systems Design1 Virtual Memory Notice: The slides for this lecture have been largely based on those accompanying the textbook.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
Chapter 4 Memory Management 4.1 Basic memory management 4.2 Swapping
Main Memory. Background Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only.
03/17/2008CSCI 315 Operating Systems Design1 Virtual Memory Notice: The slides for this lecture have been largely based on those accompanying the textbook.
1 Tuesday, July 04, 2006 "Programs expand to fill the memory available to hold them." - Modified Parkinson’s Law.
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
Chap 8 Memory Management. Background Program must be brought into memory and placed within a process for it to be run Input queue – collection of processes.
Virtual Memory.
Caching and Virtual Memory. Main Points Cache concept – Hardware vs. software caches When caches work and when they don’t – Spatial/temporal locality.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Chapter 8: Main Memory. 8.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 8: Memory Management Background Swapping Contiguous.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 346, Royden, Operating System Concepts Operating Systems Lecture 24 Paging.
Page 19/11/2015 CSE 542: Graduate Operating Systems Review  Chapter 9: Memory management  Swapping  Contiguous Allocation  Paging  Segmentation 
Part 8: Virtual Memory. Silberschatz, Galvin and Gagne ©2005 Virtual vs. Physical Address Space Each process has its own virtual address space, which.
Operating Systems Chapter 8
Chapter 8 Memory Management Dr. Yingwu Zhu. Outline Background Basic Concepts Memory Allocation.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 34 Paging Implementation.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 9: Memory Management Background Swapping Contiguous Allocation Paging Segmentation.
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-8 Memory Management (2) Department of Computer Science and Software.
8.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Paging Physical address space of a process can be noncontiguous Avoids.
8.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Implementation of Page Table Page table is kept in main memory Page-table base.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Paging.
1 Memory Management (b). 2 Paging  Logical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
9.1 Operating System Concepts Paging Example. 9.2 Operating System Concepts.
Demand Paging Reference Reference on UNIX memory management
Page Table Implementation. Readings r Silbershatz et al:
1 Lecture 8: Virtual Memory Operating System Fall 2006.
Chapter 8: Main Memory. 8.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Memory and Addressing It all starts.
8.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Fragmentation External Fragmentation – total memory space exists to satisfy.
Silberschatz, Galvin and Gagne  Operating System Concepts Paging Logical address space of a process can be noncontiguous; process is allocated.
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Virtual Memory. 2 Last Week Memory Management Increase degree of multiprogramming –Entire process needs to fit into memory Dynamic Linking and Loading.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
W4118 Operating Systems Instructor: Junfeng Yang.
Virtual Memory What if program is bigger than available memory?
Module 9: Virtual Memory
Paging and Segmentation
Chapter 9: Virtual Memory
Operating System Concepts
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Operating Systems CMPSC 473
CSE 542: Operating Systems
Module 9: Virtual Memory
Virtual Memory.
CSE 542: Operating Systems
Presentation transcript:

Virtual Memory Prof. Van Renesse & Sirer

Segments Note: overloaded term… Chunks of virtual address space Access Protection – User/Supervisor – Read/Write/Execute Sharing – Code, libraries – Shared memory for IPC Virtualization – Illusion of more memory than there really is Code Non-zero Init’d Data Zero Init’d Data + Heap Stack Code Non-zero Init’d Data Zero Init’d Data + Heap Stack Device Registers Kernel User Virtual Address Space 0

Segment examples Code – Execute-only, shared among all processes that execute the same code Private Data – R/W, private to a single process Heap – R/W, Explicit allocation, zero-initialized, private Stack – R/W, Implicit allocation, zero-initialized, private Shared Memory – explicit allocation, shared among processes, some read- only, others R/W

MMU Translates virtual (or “logical”) addresses to physical addresses Enforces R/W/X protection Throws exceptions on illegal accesses Unit: “page” – Typically 1, 2, 4, 8, or 16 Kbytes Often also tracks read and write accesses Physical page often called “page frame”

Address Translation Scheme Address generated by CPU is divided into: – Page number (p) – used as an index into a page table which contains base address of each page frame in physical memory – Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit – For given virtual address space 2 m and page size 2 n page number page offset p d m - n n

Paging Hardware PTBR

Implementation of Page Table Page table can be kept in main memory Page-table base register (PTBR) points to the page table Page-table length register (PRLR) indicates size of the page table In this scheme every data/instruction access requires two memory accesses. One for the page table and one for the data/instruction.

Structure of the Page Table Hierarchical Paging Hashed Page Tables Inverted Page Tables Software vs. Hardware maintained… For portability, most kernels maintain their own page tables These have to be translated into MMU tables

Hierarchical Page Tables Break up the logical address space into multiple page tables A simple technique is a two-level page table

Two-Level Page-Table Scheme PTBR

Two-Level Paging Example A logical address (on 32-bit machine with 1K page size) is divided into: – a page offset of 10 bits (1024 = 2^10) – a page number of 22 bits (32-10) Since the page table is paged, the page number is further divided into: – a 12-bit page number – a 10-bit page offset Thus, a logical address is as follows: page number page offset pipi p2p2 d 12 10

Address-Translation Scheme PTBR

Hashed Page Tables Common in address spaces > 32 bits The virtual page number is hashed into a page table. This page table contains a chain of elements hashing to the same location. Virtual page numbers are compared in this chain searching for a match. If a match is found, the corresponding physical frame is extracted.

Hashed Page Table

Inverted Page Table One entry for each real page of memory Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs Use hash table to limit the search to one — or at most a few — page-table entries

Inverted Page Table Architecture

Translation Look-aside Buffers (TLBs) The multiple memory access problem can be solved by the use of a special fast-lookup hardware cache (an associative memory) Allows parallel search of all entries. Address translation (p, d) – If p is in TLB get frame # out (quick!) – Otherwise get frame # from page table in memory – And replace an existing entry – But which? (stay tuned)

Paging Hardware With TLB

Software-Loaded TLB Some older architectures support only a TLB No hardware page tables Upon a page fault, software updates the TLB Simplifies operating system, but relatively expensive in terms of overhead

Updated Context Switch Save registers of current process in PCB Set up PTBR (base of top-level paging table) – This info is kept in the PCB Flush TLB Restore registers of next process to run “Return from Interrupt”

“Demand Paging” (simplified) Upon page fault: Identify page in which fault occurred and reason If access inconsistent with segment access rights, terminate process If access within code segment: – Check to see if a frame with the code already exists – If not, allocate a frame and read code from executable file If disk access required, another process can run in the mean time – Map page for execution only – Return from interrupt If access within non-zero initialized data segment: – Allocate a frame and read data from executable file – Map page for R/W access – Return from interrupt If access within zero-initialized data (BSS) or stack – Allocate a frame and fill page with zero bytes – Map page for R/W access – Return from interrupt

Copy-on-Write Segments Useful for “fork()” and for initialized data Initially map page read-only Upon page fault: – Allocate a new frame – Copy frame – Map new page R/W – If fork(), map “other” page R/W as well

Pre-fetching Disk/network overhead of fetching pages can be high If a process accesses page X in a segment, the process is likely to to access page X+1 as well Pre-fetch: start fetch even before page fault has occurred

(Virtual) Null Page Often made inaccessible to all – Why?

25 Page Replacement Pages are released upon termination of a process But what happens if there is no free frame to allocate? – Select a frame and deallocate it The frame to eject is selected using the Page Replacement/Eviction Algorithm – Unmap any pages that map to this frame – If the frame is “dirty” (modified), save it on disk so it can be restored later if needed Upon subsequent page fault, load the frame from where it was stored Goal: Select frame that minimizes future page faults Note: strong resemblance to caching algorithms

26 Modified/Dirty Bits Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk, non-modified pages can always be brought back from the original source – Process text segments are rarely modified, can bring pages back from the program image stored on disk If MMU does not support dirty bit, can simulate it by mapping a page “read-only” and mark it dirty upon first page fault

“Swapping” Originally, a way to free frames by copying the memory of an entire process to “swap space” – Swap out, swap in a process… This technique is not so widely used any more “Swapping” now sometimes used as synonymous with “paging”

28 Page Replacement Algorithms Random: Pick any page to eject at random – Used mainly for comparison FIFO: The page brought in earliest is evicted – Ignores usage OPT: Belady’s algorithm – Select page not used for longest time LRU: Evict page that hasn’t been used the longest – Past could be a good predictor of the future MRU: Evict the most recently used page LFU: Evict least frequently used page

29 First-In-First-Out (FIFO) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in memory at a time per process): 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 4 frames: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Belady’s Anomaly: more frames  more page faults page faults page faults 4 43

30 FIFO Illustrating Belady’s Anomaly

31 Optimal Algorithm (Belady) Replace page that will not be used for longest period of time 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 How do you know this? Used for measuring how well your algorithm performs page faults 4 5

OPT Approximation In real life, we do not have access to the future page request stream of a program – No crystal ball, no way to know definitively which pages a program will access So we need to make a best guess at which pages will not be used for the longest time 32

33 Least Recently Used (LRU) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 8 page faults…

Implementing Perfect LRU On reference: Timestamp each page On eviction: Scan for oldest frame Problems: – Large page lists – Timestamps are costly Approximate LRU – LRU is already an approximation!

35 LRU: Clock Algorithm Each page has a reference bit – Set on use Allocation algorithm: – FIFO + reference bit (keep pages in circular list) – Scan: if ref bit is 1, set to 0, and try next. – If ref bit is 0, stop and evict. Problem: – Low accuracy for large memory R=1 R=0 R=1 R=0 R=1 R=0 R=1

36 LRU with large memory Solution: Add another hand – Leading edge clears ref bits – Trailing edge evicts pages with ref bit 0 What if angle small? What if angle big? Sensitive to sweeping interval and angle – Fast: lose usage information – Slow: all pages look used R=1 R=0 R=1 R=0 R=1 R=0 R=1

Other Algorithms MRU: Remove the most recently touched page – Works well for data accessed only once, e.g. a movie file – Not a good fit for most other data, e.g. frequently accessed items LFU: Remove page with lowest count – No track of when the page was referenced – Use multiple bits. Shift right by 1 at regular intervals. MFU: remove the most frequently used page LFU and MFU do not approximate OPT well 37

38 Allocating Pages to Processes Global replacement – Single memory pool for entire system – On page fault, evict oldest page in the system – Problem: lack of performance isolation Local (per-process) replacement – Have a separate pool of pages for each process – Page fault in one process can only replace pages from its own process – Problem: might have idle resources

39 Thrashing Def: Excessive rate of paging – May stem from lack of resources – More likely, caused by bad choices of the eviction algorithm – Keep throwing out page that will be referenced soon – So, they keep accessing memory that is not there Why does it occur? – Poor locality, past != future – There is reuse, but process does not fit model – Too many processes in the system

40 Page Fault Frequency Thrashing viewed as poor ratio of fetch to work PFF = page faults / instructions executed if PFF rises above threshold, process needs more memory – not enough memory on the system? Swap out. if PFF sinks below threshold, memory can be taken away

41 Working Set Peter Denning, 1968 – He uses this term to denote memory locality of a program Def: pages referenced by process in last  time-units comprise its working set

42 Working Sets The working set size is num pages in the working set – the number of pages touched in the interval [t-Δ+1..t]. The working set size changes with program locality. – during periods of poor locality, you reference more pages. – Within that period of time, you will have a larger working set size. Goal: keep WS for each process in memory. – E.g. If  WS i for all i runnable processes > physical memory, then suspend a process

43 Working Set Approximation Approximate with interval timer + a reference bit Example:  = 10,000 – Timer interrupts after every 5000 time units – Keep in memory 2 bits for each page – Whenever a timer interrupts copy and sets the values of all reference bits to 0 – If one of the bits in memory = 1  page in working set Why is this not completely accurate? – Cannot tell (within interval of 5000) where reference occurred Improvement = 10 bits and interrupt every 1000 time units