Virtual Memory Management CS-3013 A-term 20091 Virtual Memory Management CS-3013, Operating Systems A-term 2009 (Slides include materials from Modern Operating.

Slides:



Advertisements
Similar presentations
Virtual Memory (Chapter 4.3)
Advertisements

Module 10: Virtual Memory
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 补充:什么叫页面抖动?
1 Virtual Memory Management B.Ramamurthy. 2 Demand Paging Main memory LAS 0 LAS 1 LAS 2 (Physical Address Space -PAS) LAS - Logical Address.
9.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Virtual Memory OSC: Chapter 9. Demand Paging Copy-on-Write Page Replacement.
Virtual MemoryCS-502 Fall Virtual Memory CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System Concepts, 7 th ed.,
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.
Chapter 9: Virtual-Memory Management. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 9: Virtual Memory Background Demand Paging.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Virtual-Memory Management
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Virtual MemoryCS-3013 C-term Virtual Memory CS-3013, Operating Systems C-term 2008 (Slides include materials from Operating System Concepts, 7 th.
Virtual Memory Chapter 8. Hardware and Control Structures Memory references are dynamically translated into physical addresses at run time –A process.
CS-3013 & CS-502, Summer 2006 Virtual Memory1 CS-3013 & CS-502 Operating Systems Summer 2006.
Virtual MemoryCS-502 Fall Virtual Memory CS-502 Operating Systems Fall 2006 (Slides include materials from Operating System Concepts, 7 th ed., by.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 9: Virtual Memory Background.
Chapter 10: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 10: Virtual Memory.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 9: Virtual Memory.
Virtual Memory 1 CS502 Spring 2006 Virtual Memory CS-502 Operating Systems Spring 2006.
1 Virtual Memory Management B.Ramamurthy Chapter 10.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
Operating Systems ECE344 Ding Yuan Page Replacement Lecture 9: Page Replacement.
CSS430 Virtual Memory Textbook Ch9
Virtual Memory.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Page 19/17/2015 CSE 30341: Operating Systems Principles Optimal Algorithm  Replace page that will not be used for longest period of time  Used for measuring.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Background Virtual memory –
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples Operating.
Operating Systems CMPSC 473 Virtual Memory Management (3) November – Lecture 20 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
9.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts Chapter 9: Virtual-Memory Management Background Demand Paging Page Replacement Allocation.
Caching Principles & Paging Performance CS-502 (EMC) Fall Caching Principles and Paging Performance CS-502, Operating Systems Fall 2009 (EMC) (Slides.
Paging (continued) & Caching CS-3013 A-term Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 9: Virtual-Memory Management.
Demand Paging Reference Reference on UNIX memory management
Chapter 9: Virtual-Memory Management. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 9: Virtual-Memory Management 9.1 Background.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
Virtual Memory. 2 Last Week Memory Management Increase degree of multiprogramming –Entire process needs to fit into memory Dynamic Linking and Loading.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
COS 318: Operating Systems Virtual Memory Paging.
Chapter 9: Virtual Memory
OPERATING SYSTEM CONCEPTS AND PRACTISE
Chapter 9: Virtual Memory – Part I
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Caching Principles and Paging Performance
Caching Principles and Paging Performance
Operating Systems CMPSC 473
Module 9: Virtual Memory
Chapter 9: Virtual Memory CSS503 Systems Programming
Virtual Memory.
Presentation transcript:

Virtual Memory Management CS-3013 A-term Virtual Memory Management CS-3013, Operating Systems A-term 2009 (Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, & Gagne)

Virtual Memory Management CS-3013 A-term Review Virtual Memory — the address space in which a process “thinks” As distinguished from Physical Memory, the address space of the hardware memory system Multiple forms Base and Limit registers Segmentation Paging Memory Management Unit (MMU) Present in most modern processors Converts all virtual addresses to physical addresses Transparent to execution of (almost all) programs

Virtual Memory Management CS-3013 A-term Review (continued) Translation Lookaside Buffer (TLB) Hardware associative memory for very fast page table lookup of small set of active pages Can be managed in OS or in hardware (or both) Must be flushed on context switch Page table organizations Direct:– maps virtual  physical address Hashed Inverted:– maps physical address  virtual address

Virtual Memory Management CS-3013 A-term Reading Assignment Tanenbaum –§§ 3.1–3.3 (previous topics) Memory Management Paging –§§ 3.4–3.6 (this topic) Page Replacement Algorithms Design Issues for Paging Systems Implementation Issues for Paging Systems –§ 3.7 More on Segmentation

Virtual Memory Management CS-3013 A-term Useful terms Thrashing Too many page faults per unit time Results from insufficient physical memory to support the working set System spends most of its time swapping pages in and out, rather than executing process Working set The set of pages needed to keep a process from thrashing Caching The art and science of keeping the most active elements in fast storage for better execution speed Depends upon locality of references

Virtual Memory Management CS-3013 A-term Outline for this topic Performance metrics Swap-in strategies Page replacement strategies Miscellaneous topics –More on segmentation –Kernel memory allocation –VM summary: Linux & Windows A quantitative study of the fundamental principle of caching

Virtual Memory Management CS-3013 A-term Demand Paging Performance Page Fault Rate (p) 0 < p < 1.0 (measured in average number of faults / reference) Page Fault Overhead = fault service time + read page time + restart process time Fault service time ~ 0.1–10  sec Restart process time ~ 0.1–10–100  sec Read page time ~ 8-20 milliseconds! Dominated by time to read page in from disk!

Virtual Memory Management CS-3013 A-term Demand Paging Performance (continued) Effective Access Time (EAT) = (1-p) * (memory access time) + p * (page fault overhead) Want EAT to degrade no more than, say, 10% from true memory access time –i.e., EAT < (1 + 10%) * memory access time Definition

Virtual Memory Management CS-3013 A-term Performance Example Memory access time = 100 nanosec = Page fault overhead = 25 millisec = Page fault rate = 1/1000 = EAT = (1-p) * p * (0.025) = (0.999) * *  25 microseconds per reference! I.e.,  250 * memory access time!

Virtual Memory Management CS-3013 A-term Performance Example (continued) Goal: achieve less than 10% degradation (1-p) * p * (0.025) < 1.1 * i.e., p < (0.1 * 10 -7) / ( )  I.e., 1 fault in 2,500,000 accesses!

Virtual Memory Management CS-3013 A-term Working Set Size Assume average swap time of 25 millisec. For memory access time = 100 nanoseconds Require < 1 page fault per 2,500,000 accesses For memory access time = 1 microsecond Require < 1 page fault per 250,000 accesses For memory access time = 10 microseconds Require < 1 page fault per 25,000 accesses

Virtual Memory Management CS-3013 A-term Object Lesson Working sets must get larger in proportion to memory speed! Disk speed ~ constant (nearly) I.e., faster computers need larger physical memories to exploit the speed!

Virtual Memory Management CS-3013 A-term Class Discussion 1.What is first thing to do when the PC you bought last year becomes too slow? 2.What else might help? 3.Can we do the same analysis on TLB performance?

Virtual Memory Management CS-3013 A-term TLB fault performance Assumptions –m = memory access time = 100 nsec –t = TLB load time from memory = 300 nsec = 3 * m Goal is < 5% penalty for TLB misses –I.e., EAT < 1.05 * m How low does TLB fault rate need to be?

Virtual Memory Management CS-3013 A-term TLB fault performance Assumptions –m = memory access time = 100 nsec –t = TLB load time from memory = 300 nsec = 3 * m Goal is < 5% penalty for TLB misses –I.e., EAT < 1.05 * m EAT = (1-p) * m + p * t < 1.05 *m  < (0.05 * m) / (t – m) = 0.05 * m / 2 * m = I.e., TLB fault rate should be < 1 per 40 accesses!

Virtual Memory Management CS-3013 A-term TLB fault performance (continued) Q: How large should TLB be so that TLB faults are not onerous, in these circumstances? A: Somewhat less than 40 entries Assuming a reasonable degree of locality!

Virtual Memory Management CS-3013 A-term Summary of this Topic A quantitative way of estimating how large the cache needs to be to avoid excessive thrashing, where –Cache = Working set in physical memory –Cache = TLB size in hardware Applicable to all forms of caching

Virtual Memory Management CS-3013 A-term General Observation on Caching We create caches because There is not enough fast memory to hold everything we need Memory that is large enough is too slow Performance metric for all caches is EAT Effective Access Time Goal is to make overall performance close to cache memory performance By taking advantage of locality — temporal and spatial By burying a small number of accesses to slow memory under many, many accesses to fast memory

Virtual Memory Management CS-3013 A-term Next topic

Virtual Memory Management CS-3013 A-term Caching issues When to put something in the cache What to throw out to create cache space for new items How to keep cached item and stored item in sync after one or the other is updated How to keep multiple caches in sync across processors or machines Size of cache needed to be effective Size of cache items for efficiency … From previous topic

Virtual Memory Management CS-3013 A-term Physical Memory is a Cache of Virtual Memory, so … When to swap in a page On demand? or in anticipation? What to throw out Page Replacement Policy Keeping dirty pages in sync with disk Flushing strategy Keeping pages in sync across processors or machines Defer to another time Size of physical memory to be effective See previous discussion Size of pages for efficiency One size fits all, or multiple sizes?

Virtual Memory Management CS-3013 A-term Physical Memory as cache of Virtual Memory When to swap in a page On demand? or in anticipation? What to throw out Page Replacement Policy Keeping dirty pages in sync with disk Flushing strategy Keeping pages in sync across processors or machines Defer to another time Size of physical memory to be effective See previous discussion Size of pages for efficiency One size fits all, or multiple sizes?

Virtual Memory Management CS-3013 A-term VM Page Replacement If there is an unused frame, use it. If there are no unused frames available, select a victim (according to policy) and –If it contains a dirty page (M == 1) write it to disk –Invalidate its PTE and TLB entry –Load in new page from disk (or create new page) –Update the PTE and TLB entry! –Restart the faulting instruction What is cost of replacing a page? How does the OS select the page to be evicted?

Virtual Memory Management CS-3013 A-term Page Replacement Algorithms Want lowest page-fault rate. Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string. Reference string – ordered list of pages accessed as process executes Ex. Reference String is A B C A B D A D B C B

Virtual Memory Management CS-3013 A-term The Best Page to Replace The best page to replace is the one that will never be accessed again Optimal Algorithm – Belady’s Rule –Lowest fault rate for any reference string –Basically, replace the page that will not be used for the longest time in the future. –Belady’s Rule is a yardstick –We want to find close approximations

Virtual Memory Management CS-3013 A-term Page Replacement – NRU (Not Recently Used) Periodically (e.g., on a clock interrupt) Clear R bit from all PTE’s When needed, rank order pages as follows 1.R = 0, M = 0 2.R = 0, M = 1 3.R = 1, M = 0 4.R = 1, M = 1 Evict a page at random from lowest non-empty class Write out if M = 1; clear M when written Characteristics Easy to understand and implement Not optimal, but adequate in some cases

Virtual Memory Management CS-3013 A-term Typical Page Table Entry Valid bit gives state of this entry –says whether or not a virtual address is valid – in memory and VA range –If not set, page might not be in memory or may not even exist! Reference bit says whether the page has been accessed –it is set by hardware whenever a page has been read or written to Modify bit says whether or not the page is dirty –it is set by hardware during every write to the page Protection bits control which operations are allowed –read, write, execute, etc. Page frame number (PFN) determines the physical page –physical page start address Other bits dependent upon machine architecture page frame numberprotMRV

Virtual Memory Management CS-3013 A-term Page Replacement – FIFO (First In, First Out) Easy to implement When swapping a page in, place its page id on end of list Evict page at head of list Page to be evicted has been in memory the longest time, but … Maybe it is being used, very active even We just don’t know A weird phenomenon:– Belady’s Anomaly fault rate may increase when there is more physical memory! FIFO is rarely used in practice

Virtual Memory Management CS-3013 A-term FIFO Illustrating Belady’s Anomaly

Virtual Memory Management CS-3013 A-term Second Chance Maintain FIFO page list When a page frame is needed, check reference bit of top page in list If R == 1 then move page to end of list and clear R, repeat If R == 0 then evict page I.e., a page has to move to top of list at least twice I.e., once after the last time R-bit was cleared Disadvantage Moves pages around on list a lot (bookkeeping overhead)

Virtual Memory Management CS-3013 A-term Clock Replacement (A slight variation of Second Chance) Create circular list of PTEs in FIFO Order One-handed Clock – pointer starts at oldest page –Algorithm – FIFO, but check Reference bit If R == 1, set R = 0 and advance hand evict first page with R == 0 –Looks like a clock hand sweeping PTE entries –Fast, but worst case may take a lot of time

Virtual Memory Management CS-3013 A-term Clock Algorithm (illustrated)

Virtual Memory Management CS-3013 A-term Enhanced Clock Algorithm Two-handed clock – add another hand that is n PTEs ahead –Extra hand clears Reference bit –Allows very active pages to stay in longer Also rank order the frames 1.R = 0, M = 0 2.R = 0, M = 1 3.R = 1, M = 0 4.R = 1, M = 1 Select first entry in lowest category May require multiple passes Gives preference to modified pages

Virtual Memory Management CS-3013 A-term Least Recently Used (LRU) Replace the page that has not been used for the longest time 3 Page Frames Reference String - A B C A B D A D B C A B C A B D A D B C LRU – 5 faults On the assumption that it is least likely to be needed again soon

Virtual Memory Management CS-3013 A-term LRU Past experience may indicate future behavior Perfect LRU requires some form of timestamp to be associated with a PTE on every memory reference !!! Counter implementation –Every page entry has a counter; each time page is referenced through this entry, copy the clock into the counter. –When a page needs to be changed, look at the counters to determine which to select Stack implementation – keep a stack of page numbers in a double link form: –Page referenced: move it to the top –No search for replacement

Virtual Memory Management CS-3013 A-term LRU Approximations Aging –Keep a counter for each PTE –Periodically (clock interrupt) – check R-bit If R = 0 increment counter (page has not been used) If R = 1 clear the counter (page has been used) Clear R = 0 –Counter contains # of intervals since last access –Replace page having largest counter value Alternatives –§§ in Tanenbaum

Virtual Memory Management CS-3013 A-term When to Evict Pages (Cleaning Policy) An OS process called the paging daemon –wakes periodically to inspect pool of frames –if insufficient # of free frames Mark pages for eviction according to policy, set valid bit to zero Schedule disk to write dirty pages –on page fault If desired page is marked but still in memory, use it Otherwise, replace first clean marked page in pool Advantage Writing out dirty pages is not in critical path to swapping in I.e., a kernel thread

Virtual Memory Management CS-3013 A-term Physical Memory as cache of Virtual Memory When to swap in a page On demand? or in anticipation? What to throw out Page Replacement Policy Keeping dirty pages in sync with disk Flushing strategy Keeping pages in sync across processors or machines Defer to another time Size of physical memory to be effective See previous discussion Size of pages for efficiency One size fits all, or multiple sizes?

Virtual Memory Management CS-3013 A-term What to Page in Demand paging brings in the faulting page –To bring in more pages, we need to know the future Users don’t really know the future, but a few OSs have user-controlled pre-fetching In real systems, –load the initial page –Start running –Some systems (e.g. WinNT & WinXP) will bring in additional neighboring pages (clustering) Alternatively –Figure out working set from previous activity –Page in entire working set of a swapped out process

Virtual Memory Management CS-3013 A-term Working Set A working set of a process is used to model the dynamic locality of its memory usage –Working set = set of pages a process currently needs to execute without too many page faults –Denning in late 60’s Definition: –WS(t,w) = set of pages referenced in the interval between time t-w and time t t is time and w is working set window (measured in page refs) Page is in working set only if it was referenced in last w references

Virtual Memory Management CS-3013 A-term Working Set w  working-set window  a fixed number of page references Example: 10,000 – 2,000,000 instructions WS i (working set of Process P i ) = set of pages referenced in the most recent w (varies in time) –if w too small will not encompass entire locality. –if w too large will encompass several localities. –as w  , encompasses entire program.

Virtual Memory Management CS-3013 A-term Assume 3 page frames Let interval be w = w={1,2,3} w={3,4,7} w={1,2} –if w too small, will not encompass locality –if w too large, will encompass several localities –if w  infinity, will encompass entire program if Total WS > physical memory  thrashing –Need to free up some physical memory –E.g., suspend a process, swap all of its pages out Working Set Example

Virtual Memory Management CS-3013 A-term Working Set Page Replacement In practice, convert references into time –E.g. 100ns/ref, 100,000 references  10msec WS algorithm in practice –On each clock tick, clear all R bits and record process virtual time t –When looking for eviction candidates, scan all pages of process in physical memory If R == 1 Store t in LTU (last time used) of PTE and clear R If R == 0 If (t – LTU) > WS_Interval (i.e., w), evict the page (because it is not in working set) Else select page with the largest difference

Virtual Memory Management CS-3013 A-term Working Set Page Replacement In practice, convert references into time –E.g. 100ns/ref, 100,000 references  10msec WS algorithm in practice –On each clock tick, clear all R bits and record process virtual time t –When looking for eviction candidates, scan all pages of process in physical memory If R == 1 Store t in LTU (last time used) of PTE and clear R If R == 0 If (t – LTU) > WS_Interval (i.e., w), evict the page (because it is not in working set) Else select page with the largest difference See Tanenbaum, §3.4.8

Virtual Memory Management CS-3013 A-term WSClock (combines Clock and WS algorithms) WSClock –Circular list of entries containing R, M, time of last use R and time are updated on each clock tick –Clock “hand” progresses around list If R = 1, reset and update time If R = 0, and if age > WS_interval, and if clean, then claim it. If R = 0, and if age > WS_interval, and if dirty, then schedule a disk write Step “hand” to next entry on list Very common in practice

Virtual Memory Management CS-3013 A-term WSClock (combines Clock and WS algorithms) WSClock –Circular list of entries containing R, M, time of last use R and time are updated on each clock tick –Clock “hand” progresses around list If R = 1, reset and update time If R = 0, and if age > WS_interval, and if clean, then claim it. If R = 0, and if age > WS_interval, and if dirty, then schedule a disk write Step “hand” to next entry on list Very common in practice See Tanenbaum, §3.4.9

Virtual Memory Management CS-3013 A-term Review of Page Replacement Algorithms Tanenbaum, Fig 3-22

Virtual Memory Management CS-3013 A-term Virtual Memory Subsystem All about managing the page cache in RAM of virtual memory … … which lives primarily on disk See also Chapter 15 of Linux Kernel Development, by Robert Love

Virtual Memory Management CS-3013 A-term More on Segmentation Paging is (mostly) invisible to programmer, but segmentation is not Even paging with two-level page tables is invisible Segment: an open-ended piece of VM Multics (H6000) : 2 18 segments of 64K words each Pentium: 16K segments of 2 30 bytes each –8K global segments, plus 8K local segments per process –Each segment may be paged or not –Each segment assigned to one of four protection levels Program consciously loads segment descriptors when accessing a new segment Only OS/2 used full power of Pentium segments Linux concatenates 3 segments to simulate contiguous VM

Virtual Memory Management CS-3013 A-term OS Design Issue — Where does Kernel execute? In physical memory Old systems (e.g., IBM 360/67) Extra effort needed to look inside of VM of any process In virtual memory Most modern systems Shared segment among all processes Advantages of kernel in virtual memory Easy to access, transfer to/from VM of any process No context switch needed for traps, page faults No context switch needed for purely kernel interrupts

Virtual Memory Management CS-3013 A-term Kernel Memory Requirements Interrupt handlers Must be pinned into physical memory At locations known to hardware Critical kernel code Pinned, never swapped out I/O buffers (user and kernel) Must be pinned and in contiguous physical memory Kernel data (e.g., PCB’s, file objects, semaphores, etc.) Pinned in physical memory Dynamically allocated & freed Not multiples of page size; fragmentation is an issue Definition: Pinned – not subject to being swapped out! Reason:– I/O and other devices don’t recognize paging!

Virtual Memory Management CS-3013 A-term Definition Pinned: not subject to being swapped or paged out. –i.e., one or more contiguous pages of virtual memory that are stored in specific, identifiable, contiguous page frames in physical memory

Virtual Memory Management CS-3013 A-term Kernel Memory Allocation E.g., Linux PCB ( struct task_struct ) > 1.7 Kbytes each Created on every fork and every thread create –clone() deleted on every exit Kernel memory allocators Buddy system Slab allocation

Virtual Memory Management CS-3013 A-term Buddy System Maintain a segment of contiguous pinned VM Round up each request to nearest power of 2 Recursively divide a chunk of size 2 k into two “buddies” of size 2 k-1 to reach desired size When freeing an object, recursively coalesce its block with adjacent free buddies Problem, still a lot of internal fragmentation –E.g., 11 Kbyte page table requires 16 Kbytes

Virtual Memory Management CS-3013 A-term Buddy System (illustrated)

Virtual Memory Management CS-3013 A-term Slab Allocation Maintain a separate “cache” for each major data type E.g., task_struct, inode in Linux Slab: fixed number of contiguous physical pages assigned to one particular “cache” Upon kernel memory allocation request Recycle an existing object if possible Allocate a new one within a slab if possible Else, create an additional slab for that cache When finished with an object Return it to “cache” for recycling Benefits Minimize fragmentation of kernel memory Most kernel memory requests can be satisfied quickly Misuse of word cache

Virtual Memory Management CS-3013 A-term Slab Allocation (illustrated)

Virtual Memory Management CS-3013 A-term Unix VM Physical Memory –Core map (pinned) – page frame info –Kernel (pinned) – rest of kernel –Frames – remainder of memory Page replacement –Page daemon runs periodically to free up page frames Global replacement – multiple parameters Current BSD system uses 2-handed clock –Swapper – helps page daemon Look for processes idle 20 sec. or more and swap out longest idle Next, swap out one of 4 largest – one in memory the longest Check for processes to swap in

Virtual Memory Management CS-3013 A-term Linux VM Kernel is pinned Rest of frames used –Processes –Buffer cache –Page Cache Multilevel paging –3 levels –Contiguous slab memory allocation using Buddy Algorithm Replacement – goal keep a certain number of pages free –Daemon (kswapd) runs once per second Clock algorithm on page and buffer caches Clock on unused shared pages Modified clock (by VA order) on user processes (by # of frames)

Virtual Memory Management CS-3013 A-term Windows NT Uses demand paging with clustering. Clustering brings in pages surrounding the faulting page. Processes are assigned working set minimum (20-50) and working set maximum (45-345) Working set minimum is the minimum number of pages the process is guaranteed to have in memory. A process may be assigned as many pages up to its working set maximum. When the amount of free memory in the system falls below a threshold, automatic working set trimming is performed to restore the amount of free memory. (Balance set manager) Working set trimming removes pages from processes that have pages in excess of their working set minimum

Virtual Memory Management CS-3013 A-term VM Summary Memory Management – from simple multiprogramming support to efficient use of multiple system resources Models and measurement exist to determine the goodness of an implementation In real systems, must tradeoff –Implementation complexity –Management overhead –Access time overhead

Virtual Memory Management CS-3013 A-term Reading Assignment Tanenbaum –§§ 3.1–3.3 (previous topics) Memory Management Paging –§§ 3.4–3.6 (this topic) Page Replacement Algorithms Design Issues for Paging Systems Implementation Issues for Paging Systems –§ 3.7 More on Segmentation

Virtual Memory Management CS-3013 A-term Questions?

Virtual Memory Management CS-3013 A-term More on Thrashing With multiprogramming, physical memory is shared –Kernel code –System buffers and system information (e.g. PTs) –Some for each process Each process needs a minimum number of pages in memory to avoid thrashing

Virtual Memory Management CS-3013 A-term Thrashing Degree of multiprogramming CPU utilization

Virtual Memory Management CS-3013 A-term Cause of Thrashing Why does thrashing occur? –sum of working sets of active processes > total physical memory available (excluding kernel and pinned pages) Bounding the problem – page replacement algorithm does not matter –If system has more than enough memory (overprovisioning) –If system has too little memory (overcommitted)

Virtual Memory Management CS-3013 A-term Page Fault Frequency increase number of frames decrease number of frames upper bound lower bound Page Fault Rate Number of Frames

Virtual Memory Management CS-3013 A-term What should OS do? Establish “acceptable” page-fault rate for each process –If rate too low, process loses frame –If rate too high, process gains frame If overall system rate is to high, suspend a process

Virtual Memory Management CS-3013 A-term Frame Allocation - Policies Equal allocation – e.g., if 100 frames and 5 processes, give each 20 pages. Proportional allocation – Allocate according to the size of process bigger process are allocated more frames and smaller processes Priority allocation Use a proportional allocation scheme using process priorities rather than size.

Virtual Memory Management CS-3013 A-term Frame Allocation and Page Replacement Frame Allocation and Page Replacement interact Global replacement – process selects a replacement frame from the set of all frames; one process can take a frame from another. Local replacement – each process selects from only its own set of allocated frames.