Computer Operating System - Chapter 10: Virtual Memory
Background
! Demand Paging
! Copy-on-Write
! Page Replacement
! Allocation of Frames
! Thrashing
! Memory-Mapped Files
! Allocating Kernel Memory
! Other Considerations
! Operating-System Examples
! Demand Paging
! Copy-on-Write
! Page Replacement
! Allocation of Frames
! Thrashing
! Memory-Mapped Files
! Allocating Kernel Memory
! Other Considerations
! Operating-System Examples
Bạn đang xem 20 trang mẫu của tài liệu "Computer Operating System - Chapter 10: Virtual Memory", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.
File đính kèm:
- computer_operating_system_chapter_10_virtual_memory.pdf
Nội dung text: Computer Operating System - Chapter 10: Virtual Memory
- Chapter 10: Outline ! Background ! Demand Paging ! Copy-on-Write ! Page Replacement ! Allocation of Frames ! Thrashing ! Memory-Mapped Files ! Allocating Kernel Memory ! Other Considerations ! Operating-System Examples Operating System Concepts 2 Silberschatz, Galvin and Gagne ©2018
- Background ! Observation " Code needs to be in memory to be executed, but entire program rarely used. E.g., Error code, unusual routines, large data structures " Entire program code are not needed at same time ! Motivation: consider ability to execute partially-loaded program " Program no longer constrained by limits of physical memory " Each program takes less memory while running ➤ more programs run at the same time 4 Increase CPU utilization and throughput 4 No increase in response time or turnaround time " Less I/O needed to load or swap programs into memory ➤ each user program runs faster Operating System Concepts 4 Silberschatz, Galvin and Gagne ©2018
- Virtual Address Space ! Virtual address space – logical view of how process is stored in memory " Usually start at address 0, contiguous addresses until end of space " Meanwhile, physical memory organized in page frames " MMU must map logical to physical addresses Operating System Concepts 6 Silberschatz, Galvin and Gagne ©2018
- Shared Library Using Virtual Memory Operating System Concepts 8 Silberschatz, Galvin and Gagne ©2018
- Basic Concepts ! Lazy swapper – never swaps a page into memory unless page will be needed (Swapper that deals with pages is a pager) ! With swapping, pager guesses which pages will be used before swapping out again " Instead, pager brings in only those pages into memory ! How to determine that set of pages? " Need new MMU functionality to implement demand paging ! If pages needed are already memory resident " No difference from non demand-paging ! If page needed are not memory resident " Need to detect and load the page into memory from storage 4 Without changing program behavior 4 Without programmer needing to change code Operating System Concepts 10 Silberschatz, Galvin and Gagne ©2018
- Page Table When Some Pages Are Not in Main Memory ⌧ ⌧ ⌧ Operating System Concepts 12 Silberschatz, Galvin and Gagne ©2018
- Steps in Handling a Page Fault (Cont.) Operating System Concepts 14 Silberschatz, Galvin and Gagne ©2018
- Instruction Restart ! Consider an instruction that could access several different locations, e.g., " Block move " Auto increment/decrement location ! Restart the whole operation? 4 What if source and destination overlap? Operating System Concepts 16 Silberschatz, Galvin and Gagne ©2018
- Stages in Demand Paging – Worse Case 1. Trap to the operating system 2. Save the registers and process state 3. Determine that the interrupt was a page fault 4. Check that the page reference was legal and determine the location of the page on the disk 5. Issue a read from the disk to a free frame: 1. Wait in a queue for this device until the read request is serviced 2. Wait for the device seek and/or latency time (disk) 3. Begin the transfer of the page to a free frame Operating System Concepts 18 Silberschatz, Galvin and Gagne ©2018
- Performance of Demand Paging ! Three major activities " Service the interrupt – careful coding means just several hundred instructions needed " Read the page – lots of time " Restart the process – again just a small amount of time ! Page fault rate p, 0 £ p £ 1 " if p = 0, no page faults " if p = 1, every reference is a fault ! Effective Access Time (EAT) EAT = (1 – p) x memory access + p x (page fault overhead + swap page out + swap page in ) Operating System Concepts 20 Silberschatz, Galvin and Gagne ©2018
- Demand Paging Optimizations ! Swap space I/O faster than file system I/O even if on the same device " Swap allocated in larger chunks, less management needed than file system ! Copy entire process image to swap space at process load time, then page in and out of swap space " Used in older BSD Unix ! Demand page in from program binary on disk, but discard rather than paging out when freeing frame " Used in Solaris and current BSD " Still need to write to swap space 4 Pages not associated with a file (like stack and heap) – anonymous memory 4 Pages modified in memory but not yet written back to the file system ! Mobile systems " Typically don’t support swapping " Instead, demand page from file system and reclaim read-only pages (such as code) Operating System Concepts 22 Silberschatz, Galvin and Gagne ©2018
- Before Process 1 Modifies Page C Operating System Concepts 24 Silberschatz, Galvin and Gagne ©2018
- What Happens if There is no Free Frame? ! Used up by process pages ! Also in demand from the kernel, I/O buffers, etc. ! How much to allocate to each? ! Page replacement – find some page in memory, but not really in use, page it out " Algorithm – terminate? swap out? replace the page? " Performance – want an algorithm which will result in minimum number of page faults ! Same page may be brought into memory several times Operating System Concepts 26 Silberschatz, Galvin and Gagne ©2018
- Need For Page Replacement Operating System Concepts 28 Silberschatz, Galvin and Gagne ©2018
- Page Replacement Operating System Concepts 30 Silberschatz, Galvin and Gagne ©2018
- Graph of Page Faults Versus The Number of Frames Operating System Concepts 32 Silberschatz, Galvin and Gagne ©2018
- FIFO Illustrating Belady’s Anomaly 16 14 12 10 8 6 number of page faults 4 2 1234567 number of frames Operating System Concepts 34 Silberschatz, Galvin and Gagne ©2018
- Least Recently Used (LRU) Algorithm ! Use past knowledge rather than future ! Replace page that has not been used in the most amount of time ! Associate time of last use with each page ! 12 faults – better than FIFO but worse than OPT ! Generally good algorithm and frequently used ! But how to implement? Operating System Concepts 36 Silberschatz, Galvin and Gagne ©2018
- Use Of A Stack to Record Most Recent Page References reference string 47071012127 1 2 2 7 ab 1 2 0 1 7 0 4 4 stack stack before after a b Operating System Concepts 38 Silberschatz, Galvin and Gagne ©2018
- Second-Chance (clock) Page-Replacement Algorithm reference pages reference pages bits bits 0 0 0 0 next 1 0 victim 1 0 0 0 1 1 1 1 circular queue of pages circular queue of pages (a) (b) Operating System Concepts 40 Silberschatz, Galvin and Gagne ©2018
- Counting Algorithms ! Keep a counter of the number of references that have been made to each page " Not common ! Least Frequently Used (LFU) Algorithm: replaces page with smallest count ! Most Frequently Used (MFU) Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used Operating System Concepts 42 Silberschatz, Galvin and Gagne ©2018
- Applications and Page Replacement ! All of these algorithms have OS guessing about future page access ! Some applications have better knowledge – i.e. databases ! Memory-intensive applications can cause double buffering " OS keeps copy of page in memory as I/O buffer " Application keeps page in memory for its own work ! Operating system can given direct access to the disk, getting out of the way of the applications " Raw disk mode ! Bypasses buffering, locking, etc. Operating System Concepts 44 Silberschatz, Galvin and Gagne ©2018
- Fixed Allocation ! Equal allocation – For example, if there are 100 frames (after allocating frames for the OS) and 5 processes, give each process 20 frames " Keep some as free frame buffer pool ! Proportional allocation – Allocate according to the size of process " Dynamic as degree of multiprogramming, process sizes change m = 64 s = size of process p i i s1 =10 S = åsi s2 =127 m = total number of frames 10 a1 = × 62 ≈ 4 si 137 ai = allocation for pi = ´ m S 127 a = × 62 ≈ 57 2 137 Operating System Concepts 46 Silberschatz, Galvin and Gagne ©2018
- Reclaiming Pages ! A strategy to implement global page-replacement policy ! Motivation: All memory requests are satisfied from the free-frame list, rather than waiting for the list to drop to zero before we begin selecting pages for replacement ! Page replacement is triggered when the list falls below a certain threshold ! This strategy attempts to ensure there is always sufficient free memory to satisfy new requests Operating System Concepts 48 Silberschatz, Galvin and Gagne ©2018
- Non-Uniform Memory Access (NUMA) ! So far all memory accessed equally ! Many systems are NUMA – speed of access to memory varies " E.g., Consider system boards containing CPUs and memory, interconnected over a system bus ! NUMA multiprocessing architecture Operating System Concepts 50 Silberschatz, Galvin and Gagne ©2018
- Thrashing ! If a process does not have “enough” frames, the page-fault rate is very high " Page fault to get frame " Replace existing frame " But, quickly need replaced frame back " More processes have page faults " This leads to: 4 Low CPU utilization 4 Operating system thinking that it needs to increase the degree of multiprogramming 4 Another process added to the system Operating System Concepts 52 Silberschatz, Galvin and Gagne ©2018
- Demand Paging and Thrashing ! Why does demand paging work? ! Locality model " Process migrates from one locality to another " Localities may overlap ! Why does thrashing occur? S size of locality > total memory size ! Limit effects by using local or priority page replacement Operating System Concepts 54 Silberschatz, Galvin and Gagne ©2018
- Working-Set Model ! D º working-set window º a fixed number of page references " E.g., 10,000 instructions ! WSSi (working set of Process Pi) = total number of pages referenced in the most recent D (varies in time) Operating System Concepts 56 Silberschatz, Galvin and Gagne ©2018
- Keeping Track of the Working Set ! Approximate with interval timer + a reference bit ! Example: D = 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 this is not completely accurate? ! Improvement = 10 bits and interrupt every 1000 time units Operating System Concepts 58 Silberschatz, Galvin and Gagne ©2018
- Working Sets and Page Fault Rates ! Direct relationship between working set of a process and its page- fault rate ! Working set changes over time ! Peaks and valleys over time Operating System Concepts 60 Silberschatz, Galvin and Gagne ©2018
- Buddy System (power-of-2 Allocator) ! Allocates memory from fixed-size segment consisting of physically- contiguous pages ! Memory allocated using power-of-2 allocator " Satisfies requests in units sized as power of 2 " Request rounded up to next highest power of 2 " When smaller allocation needed than is available, current chunk split into two buddies of next-lower power of 2 4 Continue until appropriate sized chunk available ! For example, assume 256KB chunk available, kernel requests 21KB " Split into AL and AR of 128KB each 4 One further divided into BL and BR of 64KB – One further into CL and CR of 32KB each – one used to satisfy request ! Advantage: quickly coalesce unused chunks into larger chunk ! Disadvantage: fragmentation Operating System Concepts 62 Silberschatz, Galvin and Gagne ©2018
- Slab Allocator ! Alternate strategy ! Slab is one or more physically contiguous pages ! Cache consists of one or more slabs ! Single cache for each unique kernel data structure " Each cache filled with objects – instantiations of the data structure ! When cache created, filled with objects marked as free ! When structures stored, objects marked as used ! If slab is full of used objects, next object allocated from empty slab " If no empty slabs, new slab allocated ! Benefits include no fragmentation, fast memory request satisfaction Operating System Concepts 64 Silberschatz, Galvin and Gagne ©2018
- Slab Allocator in Linux ! For example, process descriptor is of type struct task_struct ! Approx. 1.7KB of memory ! New task => allocate new struct from cache " Will use existing free struct task_struct ! Slab can be in three possible states 1. Full – all used 2. Empty – all free 3. Partial – mix of free and used ! Upon request, slab allocator 1. Uses free struct in partial slab 2. If none, takes one from empty slab 3. If no empty slab, create new empty Operating System Concepts 66 Silberschatz, Galvin and Gagne ©2018
- Other Considerations ! Pre-paging ! Page size ! TLB reach ! Inverted page table ! Program structure ! I/O interlock and page locking Operating System Concepts 68 Silberschatz, Galvin and Gagne ©2018
- Page Size ! Sometimes OS designers have a choice of page size " Especially if running on custom-built CPU ! Page size selection must take into consideration: " Fragmentation " Page table size " Resolution " I/O overhead " Number of page faults " Locality " TLB size and effectiveness ! Always power of 2, usually in the range 212 (4,096 bytes) to 222 (4,194,304 bytes) ! On average, growing over time Operating System Concepts 70 Silberschatz, Galvin and Gagne ©2018
- Example of a Program Structure ! Program structure " int[128,128] data; " Each row is stored in one page " Program 1 for (j = 0; j <128; j++) for (i = 0; i < 128; i++) data[i,j] = 0; 128 x 128 = 16,384 page faults " Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0; 128 page faults Operating System Concepts 72 Silberschatz, Galvin and Gagne ©2018
- Operating System Examples ! Windows ! Solaris Operating System Concepts 74 Silberschatz, Galvin and Gagne ©2018
- Solaris ! Maintains a list of free pages to assign faulting processes " Lotsfree – threshold parameter (amount of free memory) to begin paging " Desfree – threshold parameter to increasing paging " Minfree – threshold parameter to being swapping " Paging is performed by pageout process " Pageout scans pages using modified clock algorithm " Scanrate is the rate at which pages are scanned. This ranges from slowscan to fastscan " Pageout is called more frequently depending upon the amount of free memory available ! Priority paging gives priority to process code pages Operating System Concepts 76 Silberschatz, Galvin and Gagne ©2018
- Performance of Demand Paging ! Stages in Demand Paging (worse case) 1. Trap to the operating system 2. Save the user registers and process state 3. Determine that the interrupt was a page fault 4. Check that the page reference was legal and determine the location of the page on the disk 5. Issue a read from the disk to a free frame: 1. Wait in a queue for this device until the read request is serviced 2. Wait for the device seek and/or latency time 3. Begin the transfer of the page to a free frame 6. While waiting, allocate the CPU to some other user 7. Receive an interrupt from the disk I/O subsystem (I/O completed) 8. Save the registers and process state for the other user 9. Determine that the interrupt was from the disk 10. Correct the page table and other tables to show page is now in memory 11. Wait for the CPU to be allocated to this process again 12. Restore the user registers, process state, and new page table, and then resume the interrupted instruction Operating System Concepts 78 Silberschatz, Galvin and Gagne ©2018
- Priority Allocation ! Apply a proportional allocation scheme using priorities rather than size ! If process Pi generates a page fault, " select for replacement one of its frames " select for replacement a frame from a process with lower priority number Operating System Concepts 80 Silberschatz, Galvin and Gagne ©2018
- Memory Compression (Cont.) ! An alternative to paging is memory compression ! Rather than paging out modified frames to swap space, we compress several frames into a single frame, enabling the system to reduce memory usage without resorting to swapping pages Operating System Concepts 82 Silberschatz, Galvin and Gagne ©2018
- Summary (Cont.) ! Copy-on-write allows a child process to share the same address space as its parent. If either the child or the parent process writes (modifies) a page, a copy of the page is made. ! When available memory runs low, a page-replacement algorithm selects an existing page in memory to replace with a new page. Page-replacement algorithms include FIFO, optimal, and LRU. Pure LRU algorithms are impractical to implement, and most systems instead use LRU-approximation algorithms. ! Global page-replacement algorithms select a page from any process in the system for replacement, while local page-replacement algorithms select a page from the faulting process. ! Thrashing occurs when a system spends more time paging than executing. Operating System Concepts 84 Silberschatz, Galvin and Gagne ©2018
- Summary (Cont.) ! TLB reach refers to the amount of memory accessible from the TLB and is equal to the number of entries in the TLB multiplied by the page size. One technique for increasing TLB reach is to increase the size of pages. ! Linux, Windows, and Solaris manage virtual memory similarly, using demand paging and copy-on-write, among other features. Each system also uses a variation of LRU approximation known as the clock algorithm. Operating System Concepts 86 Silberschatz, Galvin and Gagne ©2018