CS 5523 Operating Systems: Memory Management

Similar documents
CS 5523 Operating Systems: Memory Management (SGG-8)

Memory Management. Outline. Memory. Virtual Memory. Instructor: Dr. Tongping Liu

Topics: Virtual Memory (SGG, Chapter 09) CS 3733 Operating Systems

Chapter 8: Virtual Memory. Operating System Concepts

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

CS 3733 Operating Systems:

Virtual Memory Outline

Chapter 9: Virtual Memory

Chapter 9: Virtual Memory

Virtual Memory. CSCI 315 Operating Systems Design Department of Computer Science

Chapter 9: Virtual Memory. Chapter 9: Virtual Memory. Objectives. Background. Virtual-address address Space

CS 3733 Operating Systems:

OPERATING SYSTEM. Chapter 9: Virtual Memory

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Chapter 9: Virtual Memory

Chapter 9: Virtual-Memory

Chapter 9: Virtual Memory

CS307: Operating Systems

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

First-In-First-Out (FIFO) Algorithm

Chapter 10: Virtual Memory

Chapter 9: Virtual Memory. Operating System Concepts 9th Edition

Chapter 6: Demand Paging

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

CS370 Operating Systems

Operating System Concepts 9 th Edition

CS450/550 Operating Systems

Chapter 10: Virtual Memory. Background

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

Virtual Memory. CSCI 315 Operating Systems Design Department of Computer Science

Chapter 4: Memory Management. Part 1: Mechanisms for Managing Memory

CS370 Operating Systems

Operating System Concepts

Basic Memory Management

Addresses in the source program are generally symbolic. A compiler will typically bind these symbolic addresses to re-locatable addresses.

Chapter 8: Virtual Memory. Operating System Concepts Essentials 2 nd Edition

Chapter 10: Virtual Memory. Background. Demand Paging. Valid-Invalid Bit. Virtual Memory That is Larger Than Physical Memory

Where are we in the course?

Module 9: Virtual Memory

Chapter 3: Virtual Memory ว ตถ ประสงค. Background สามารถอธ บายข อด ในการท ระบบใช ว ธ การจ ดการหน วยความจ าแบบเสม อนได

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Module 9: Virtual Memory

ADRIAN PERRIG & TORSTEN HOEFLER Networks and Operating Systems ( ) Chapter 6: Demand Paging

Virtual Memory Management

Chapters 9 & 10: Memory Management and Virtual Memory

Virtual Memory. Virtual Memory. Demand Paging. valid-invalid bit. Virtual Memory Larger than Physical Memory

!! What is virtual memory and when is it useful? !! What is demand paging? !! When should pages in memory be replaced?

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

Chapter 9: Virtual-Memory Management. Operating System Concepts 8 th Edition,

Virtual Memory COMPSCI 386

Background. Virtual Memory (2/2) Demand Paging Example. First-In-First-Out (FIFO) Algorithm. Page Replacement Algorithms. Performance of Demand Paging

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Virtual Memory. Reading: Silberschatz chapter 10 Reading: Stallings. chapter 8 EEL 358

Swapping. Operating Systems I. Swapping. Motivation. Paging Implementation. Demand Paging. Active processes use more physical memory than system has

Page Replacement Algorithms

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Memory Management Virtual Memory

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Memory Management

Memory Management. Chapter 4 Memory Management. Multiprogramming with Fixed Partitions. Ideally programmers want memory that is.

Chapter 4 Memory Management

Chapter 4 Memory Management. Memory Management

CS399 New Beginnings. Jonathan Walpole

Page Replacement. 3/9/07 CSE 30341: Operating Systems Principles

Memory management: outline

Memory management: outline

Background. Demand Paging. valid-invalid bit. Tevfik Koşar. CSC Operating Systems Spring 2007

PAGE REPLACEMENT. Operating Systems 2015 Spring by Euiseong Seo

CS6401- Operating System UNIT-III STORAGE MANAGEMENT

VIRTUAL MEMORY READING: CHAPTER 9

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

Memory Management Outline. Operating Systems. Motivation. Paging Implementation. Accessing Invalid Pages. Performance of Demand Paging

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

Optimal Algorithm. Replace page that will not be used for longest period of time Used for measuring how well your algorithm performs

Course Outline. Processes CPU Scheduling Synchronization & Deadlock Memory Management File Systems & I/O Distributed Systems

Basic Page Replacement

Principles of Operating Systems

Memory management, part 2: outline

Chapter 8: Main Memory. Operating System Concepts 8th Edition

CS370 Operating Systems

How to create a process? What does process look like?

Move back and forth between memory and disk. Memory Hierarchy. Two Classes. Don t

Memory Management Ch. 3

Virtual Memory. Overview: Virtual Memory. Virtual address space of a process. Virtual Memory. Demand Paging

Memory Management. To improve CPU utilization in a multiprogramming environment we need multiple programs in main memory at the same time.

Memory management, part 2: outline. Operating Systems, 2017, Danny Hendler and Amnon Meisels

Chapter 9: Virtual Memory

VII. Memory Management

Chapter 8: Memory Management. Background Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging

MEMORY MANAGEMENT/1 CS 409, FALL 2013

Demand Paging. Valid-Invalid Bit. Steps in Handling a Page Fault. Page Fault. Transfer of a Paged Memory to Contiguous Disk Space

Motivation. Memory Management. Memory Management. Computer Hardware Review

Multi-Process Systems: Memory (2) Memory & paging structures: free frames. Memory & paging structures. Physical memory

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

Memory Management Prof. James L. Frankel Harvard University

12: Memory Management

Operating Systems. IV. Memory Management

Address spaces and memory management

Chapter 8. Virtual Memory

Operating Systems. Overview Virtual memory part 2. Page replacement algorithms. Lecture 7 Memory management 3: Virtual memory

Transcription:

CS 5523 Operating Systems: Memory Management Instructor: Dr Tongping Liu Thank Dr Dakai Zhu, Dr Palden Lama, and Dr Tim Richards (UMASS) for providing their slides Outline Simple memory management: swap etc Virtual memory and paging Ø Page table and address translation Ø Translation lookaside buffer (TLB) Multi-level page table Page replacement algorithms and modeling Working set of processes 2 Memory Hierarchy n CPU can directly access main memory and registers only n But programs and data must be brought (from disk) into memory n Memory accesses can be the bottleneck l Cache between memory and CPU registers n Memory Hierarchy l Cache: small, fast, expensive; SRAM; l Main memory: medium-speed, not that expensive; DRAM l Disk: many gigabytes, slow, cheap, nonvolatile storage CPU (Processor/ALU) Internal Memory I/O Devices (disks) Memory The ideal memory is Ø Very large Ø Very fast Ø Non-volatile (doesn t go away when power is turned off) The real memory is Ø Not very large Ø Not very fast Ø Affordable (cost)! Pick any two Memory management goal: make the real world look as much like the ideal world as possible J 4

Limitations without virtual memory Protection of memory: using base and limit registers Swapping Consider a multi-programming environment: l Each program must be in the memory to be executed l Processes come into memory and l Leave memory when execution is completed C C C C C B B B B A A A A D D D OS OS OS OS OS OS OS D Many Questions: How to generate the addresses for a process? 2 How to assign physical memory? What if no free region is Swap out an old process to a disk big enough? (waiting for long I/O, quantum expired etc) Reject it! But if you want to support more processes, Swapping A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution Ø Backing store fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images Ø Roll out, roll in swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed Swapping would be needed to free up memory for additional processes Swapping (cont d) Major part of swap time is transfer time; Ø Total transfer time is directly proportional to the amount of memory swapped (eg, MB process / 4MB per sec = 25 sec) Ø May take too much time to be used often When the old process is swapped in, can we relocate it? (depends on address binding)! Ø What if the swapped out process was waiting for I/O ü Let OS kernel handle all I/O, extra copy from kernel to user space Modified versions of swapping are found on many systems (ie, UNIX, Linux, and Windows), but it is often disabled! 2

Outline Simple memory management: swap etc Virtual memory and paging Ø Page table and address translation Ø Translation lookaside buffer (TLB) Multi-level page table Kernel memory management Working set of processes Virtual Memory Basic idea: allow OS to allocate more memory than the real Program uses virtual addresses Ø Addresses local to the process Ø Can be any size à limited by # of bits in address (32/64) Ø 32 bits: 4G Ø 64 bits: Virtual memory >> physical memory 2 N Auxiliary regions Stack Heap Text 9 Motivations for Virtual Memory Use physical DRAM as cache for the disk Ø Virtual pages of processes can exceed physical memory size Simplify memory management Ø Multiple processes resident in main memory ü Each with its own address space Ø Only active code and data is actually in memory Provide protection Ø One process can t interfere with another ü Because they operate in different address spaces Ø User process cannot access privileged information ü Different sections of address spaces have different permissions Virtual Memory for Multiprogramming Virtual memory (VM) is helpful in multiprogramming Ø Multiple processes in memory concurrently Ø Each process occupies small portion of memory Ø CPU schedules process B while process A waits for its long I/O operations ( eg, retrieve data from disks) Physical Memory de/allocation Ø Keep recently used content in physical memory Ø Move less recently used stuff to disk Ø Movement to/from disk handled by the OS How to get physical address from the virtual one?! 2 3

Virtual and Physical Addresses Paging and Page Systems CPU CPU chip Virtual addresses from CPU to MMU Physical addresses on bus, in memory MMU Memory Disk controller Virtual address space Ø Determined by instruction width Ø Same for all processes Physical memory indexed by physical addresses Ø Limited by bus size (# of bits) Ø Amount of available memory Paging: a memory-management scheme that permits address space of process to be non-continuous Virtual address Ø Divided into pages Physical memory Ø Divided into frames Page vs Frame Ø Same size address block Ø Unit of mapping/allocation A page is mapped to a frame Ø All addresses in the same virtual page are in the same physical frame à offset in a page 6 64K 56 6K 52 56K 48 52K 44 48K 4 44K 36 4K 32 36K 28 32K - - 5-3 - 24 28K 2 24K - 6 2K 2 6K 8 2K - 4 8K 4 4K 7 Virtual space 7 6 5 4 3 2 28 32K 24 28K 2 24K 6 2K 2 6K 8 2K 4 8K 4K Physical memory 3 4 Page Table Translate Virtual to Physical Address Each process has one page table Ø Map page number à physical frame number Number of PTEs in page table Ø Number of total pages in virtual space Ø Not just the pages in use, why? Page table is checked for every address translation Ø Where to store page table? Not all pages need map to frames at the same time Not all physical frame need be used 5 Frame 6 Frame Frame 3 Frame Frame 4 Frame 7 Split virtual address (from CPU) into two pieces Ø Page number (p) Ø Page offset (d) Page number: Index into an entry of the page table, with addresses of physical pages Page offset: Position inside a page Page size = 2 d bytes: determined by offset size page number p d page offset Page table m - n! n! 5 6 4

An Example of Virtual/Physical Addresses Example: Ø 64 KB virtual memory Ø 32 KB physical memory Ø 4 KB page/frame size à 2 bits as offset (d) Address Translation Page #:4bits Offset: 2 bits Virtual address: 6 bits How many virtual pages? Frame #:3bits Offset: 2 bits physical address: 5 bits How many physical frames? 7 Project is posted, due on 27 th A tiny computing system with K bytes physical memory, and the virtual address has 2 bits (496 bytes) Suppose that the size of virtual page/frame is 28 bytes (ie, with 7 bits in the address as offset) Ø What is the number of virtual pages for each process? 32 pages Ø How many physical frames in total? 8 frames Ø How many entries in the page table for each process? 32 8 Page Table Entry (PTE) Each entry in the page table contains Ø Frame number: bits depends on # of frames in physical memory Ø Valid bit: set if this logical page number has a corresponding physical frame in memory ü If not valid, remainder of PTE is irrelevant Ø Referenced bit: set if data on the page has been accessed Ø Dirty (modified) bit: set if data on the page has been modified Ø Protection information Size of each PTE: at least frame number plus 2/3 bits More on Page Table Different processes have different page tables Ø CR3 points to the page table Ø Change CR3 registers when context switches Page table resides in main (physical) memory Ø Continuous memory segment Why? Protection D R V Frame number Dirty bit Referenced bit Valid bit 9 2 5

Address Translation Architecture CPU Virtual address page number page offset p d page table p- p f frame number f d physical address How big the page table is? f- f f+ f+2 physical memory 2 Examples for Address Translation K bytes physical memory, and the virtual address has 2 bits, and the size of a page is 28 bytes Virtual Address x44 Physical Address Size of a page (28 B) 2 Page index and offset (, x44) 3 Frame index (2) 4 Starting address of the frame (2 * x8) 5 Physical address (2 * x8 + x44) = x44 22 Examples for Address Translation K bytes physical memory, and the virtual address has 2 bits, and the size of a page is 28 bytes Virtual Address x224 Physical Address Size of a page (28 B) 2 Page index and offset (4, x24) 3 Frame index (3) 4 Starting address of the frame (3 * x8) 5 Physical address (3 * x8 + x24) = xa4 23 Examples for Address Translation K bytes physical memory, and the virtual address has 2 bits, and the size of a page is 28 bytes Virtual Address x36 Physical Address 24 6

Page Table Size for 32bit System Outline Modern Systems/Applications Ø 32 bits virtual address Ø System with GB physical memory à 3 bits physical address Ø Suppose the size of one page/frame is 4KB (2 bits) Page table size Ø # of virtual pages: 32 2 = 2 bits à 2 2 PTEs Ø Page table size = PTE size * 2 2 = 4 MB per process à 2 frames If there are 28 processes Ø Page tables occupy 28 * 4MB = 52 MB Ø 5% of memory will be used by page tables? How can we get smaller page table?! Simple memory management: swap etc Virtual memory and paging Ø Page table and address translation Ø Translation lookaside buffer (TLB) Multi-level page table Track free memory: bitmaps or linked list Page replacement algorithms and modeling Working set of processes Other implementation issues Ø Dirty page and locking page etc 25 26 Two-Level Page Tables Two-Level Page Tables Solution: multi-level page tables Virtual address: three parts Ø Level-one page number ( bits) Ø Level-two page number ( bits) Ø Offset (2 bits) PTE in st level page table contains physical frame # for one 2nd level page table 2nd level page table has actual physical frame numbers for the memory address st level page table 2nd level page tables 22 657 4 25 63 96 884 96 955 main memory 27 Why it is good? Ø We don t have to allocate all levels initially, which reduce the size of page table Ø They don t have to continuous st level page table 2nd level page tables 22 657 4 25 63 96 884 96 955 main memory 7

Example: 2-level Address Translation Memory Requirement of Page Tables Page table base page number p = bits p 2 = bits offset = 2 bits p p 2 8 st level page table page offset physical address 8 2 frame number main memory Which tables should be in memory? Only the st level page table and the required 2 nd level page tables need to be in memory 32bit machine, page size 4k, each entry 4 bytes, using two-level page table, what is the size for the full page table Ø Level-: 24 entries*4bytes Ø Level-: 24*24entries=M entries * 4bytes Ø Total: 4M+4K 2nd level page table 29 3 Page table size 32bit machine, page size 4k, each entry 4 bytes, one level page table (full 4GB linear address) Page table size = 2^2 pages = 2^22 = 4M 32bit machine, page size 4k, each entry 4 bytes, two level page table (two pages:x, and xfffff) Page table size = (2^ level- entries) *4bytes + (2^ level- entries * 4 bytes) * 2 = 2 Kbytes 3 Memory Requirement of Page Tables Example: a process access 32 MB (recall that GB memory and 32 bits virtual address), what is the minimum and maximum memory for page table Ø 4KB / page à process has at most 8K virtual pages Ø One 2 nd level page table maps 2 pages; Computing the minimum memory consumption Ø Minimum number of 2 - level page entries needed (all pages are continuous): ü 8K Virtual pages /2^ = 8 ü Total (minimum) memory for page table: st level page table + 8; in total, we need 9 pages to hold page tables à 9 X 4KB = 36 KB 32 8

Memory Requirement of Page Tables 2 level page table entries Example: a process access 32 MB (recall that GB memory and 32 bits virtual address), what is the minimum and maximum memory for page table Ø 4KB / page à process has at most 8K virtual pages Ø One 2 nd level page table maps 2 pages; Computing the maximum memory consumption Ø 8K or more virtual pages will spread to all 2 nd level page tables, which only has 24 pages in total Ø Thus, in total, we will have page for st level page table + 24 pages for 2 nd level Thus, we will have the maximum as 4M+4K 33 Page table base 24 entries page number page offset p = bits p 2 = bits offset = 2 bits p p 2 8 st level page table 2nd level page table physical address 8 2 frame number main memory 34 Quiz Fragmentation Why the page table has to be physically continuous? If we have two pages (x and x2)), what is the size of page table? What are the tradeoffs to have small page size and large page size? What are the advantages and disadvantages of using a single-level page table? 35 External Fragmentation Ø total memory space exists to satisfy a request, but it is not contiguous Internal Fragmentation Ø allocated memory larger than requested; this size difference is called internal partition How can we reduce external fragmentation Ø Compaction: migrate memory contents to place all free memory together in one large block Ø Compaction is possible only if relocation is dynamic, and is done at execution time! 9

Paging: Internal Fragmentation Calculating internal fragmentation Ø Page size = 2,48 bytes Ø Process size = 72,766 bytes Ø 35 pages +,86 bytes Ø Internal fragmentation of 2,48 -,86 = 962 bytes Ø Worst case fragmentation = frame byte Ø On average fragmentation = / 2 frame size So small frame sizes desirable? à more entries Ø Each page table takes memory to track Page sizes growing over time Ø Solaris supports two page sizes 8 KB and 4 MB Size of Page/Frame: How Big? Determined by number of bits in offset (2Bità4KB) Smaller pages have advantages Ø Less internal fragmentation Ø Better fit for various data structures, code sections Larger pages are better because Ø Less overhead to keep track of them Ø More efficient to transfer larger pages to and from disk One principle: all entries of one-level page table à fit into one frame 32bits machine, bits for each level 38 Designing of Multi-level Page Table Suppose that a system has a 28-bit logical address space and is byte-addressable The amount of physical memory is MB (ie, the physical address has 2 bits) and the size of a page/frame is K bytes How to design a two-level page table? The size of page table entry will be 4 bytes (larger than 2 bits) 2 One page can hold 256 entries (K/4=256) 3 Thus, we need 8 bits for the 2 nd level page table bits 8 bits bits 39 Designing of Multi-level Page Table Suppose that a system has a 28-bit logical address space and is byte-addressable The amount of physical memory is MB (ie, the physical address has 2 bits) and the size of a page/frame is K bytes What about this design? 8 bits bits bits Second level bits, can t be fitted into one page Then we may need to have multiple pages (4 pages) that are continuous, which can t guarantee or increase the complexity of OS design 4

Linux s 3 level page table Linear Address converted to physical address using 3 levels How can we make the address translation faster? Index into Page Dir Index into Page Middle Dir 4 Index into Page Table Page Offset What is the benefit to use 3-level page table? What is the shortcoming? Benefits: Problems: () Reduce memory consumptions () Expensive looking up (2) Support different architectures, x86:2levels, SPARC: 3-levels Easily collapse 42 Translation Lookaside Buffer (TLB) Address Translation with TLB Small Hardware: fast Store recent accessed mapping of page à frame (64 ~ 24 entries) If desired logical page number is found, get frame number from TLB If not, Ø Get frame number from page table in memory Ø Use standard cache techniques Ø Replace an entry in the TLB with the logical & physical page numbers from this reference Ø Contains complete page table entries for small number of pages Logical page # 8 Physical frame # 3 CPU page number p d page offset frame number frame number unused 2 Virtual address f d 3 p f physical address 2 2 Base reg 29 6 in PCB f- TLB/hardware 22 f 7 4 f+ p f f+2 Example TLB P+ n Page fault: valid bit physical memory page table (in mem) 43 44 What happens when cpu performs a context switch?

Integrating VM and Cache CPU VA PA miss Trans- Cache lation Main Memory hit data Most caches physically addressed Ø Accessed by physical addresses Ø Allows multiple processes to have blocks in cache at same time else context switch == cache flush Ø Allows multiple processes to share pages Ø Cache doesn t need to be concerned with protection issues ü Access rights checked as part of address translation Perform address translation before cache lookup Ø Could involve memory access itself (to get PTE) Ø So page table entries can also be cached Integrating TLB and Cache Translation Lookaside Buffer (TLB) CPU hit VA PA miss TLB Cache Lookup miss Translation hit data Main Memory Basic overflow Memory Accesses Time Cpu will generate an virtual address 2 Then we will check the TLB to find the mapping of this page If the corresponding entry exists, then we could perform the translation and get the PA address Otherwise, we will go through the slow address translation, and then save the mapping into TLB (typically after the translation) 3 After getting the PA address, we can check whether the corresponding cache line is in the cache or not If yes, then we will have a cache hit and return the memory unit back to CPU 4 Otherwise, it is a cache miss We will fetch the cache line into the cache, and then return the memory unit back to CPU Assuming: Ø TLB lookup time = a Ø Memory access time = m Hit ratio (h) is percentage of time that a logical page number is found in the TLB Ø More TLB entries usually means higher h Effective Access Time (EAT) is calculated (don t include cache effect) Ø EAT = (m + a)h + (m + m + a)(-h) = a + (2-h)m Interpretation Ø Reference always requires TLB lookup, memory access Ø TLB misses also require an additional memory reference 47 48 2

Example of Memory Access Time Assuming TLB has an access time of 4 ns and the memory access time is 2 ns The disk access time is 8 ms Page/frame is K bytes Bring a page into memory only when it is needed DEMAND PAGING 49 Demand Paging Bring a page into memory only when it is needed Ø Less I/O needed Ø Less memory needed Ø Faster response Ø More users Page is needed reference to it Ø invalid reference abort Ø not-in-memory bring to memory Valid-Invalid Bit With each page table entry a valid invalid bit is associated, v in-memory,!i not-in-memory Initially bit is set to i on all entries During address translation, if valid invalid bit in page table entry is i page fault (trap) 3

Page Fault Reference to a page, If Invalid reference abort 2 If not in memory, page fault occurs (trap to OS) 3 OS allocates an empty frame 4 Swap page into frame 5 Reset page tables, set validation bit = v 6 Restart the instruction that caused the page fault Performance of Demand Paging Page Fault Rate p Ø if p = no page faults Ø if p =, every reference is a fault Effective Access Time (EAT) EAT = ( p) x memory_access + p x page_fault_time page_fault_time depends on several factors Ø Save user reg and proc state, check page ref, read from the disk there might be a queue, (CPU can be given to another proc), get interrupt, save other user reg and proc state, correct the page table, put this process into ready queue Due to queues, the page_fault_time is a random variable Demand Paging Example Memory access time = 2 nanoseconds Average page-fault service time = 8 milliseconds EAT = ( p) x 2 + p (8 milliseconds) = ( p) x 2 + p x 8,, = 2 + p x 7,999,8 If one out of, access causes a page fault, then EAT = 82 microseconds This is a slowdown by a factor of 4!! If we want just % performance degradation, then p should be 22 > ( p) x 2 + p (8 milliseconds) p < 25, ie, page fault out of 4, accesses Processes are busy swapping pages in and out THRASHING 4

Thrashing Locality and Thrashing If a process does not have enough pages, the pagefault rate is very high Eg a process needs 6 pages, but only have 5 frames Thus it will evict a page from existing 5 pages Frequent faults This leads to: Ø low CPU utilization Ø OS increase the degree of multiprogramming Ø another process added to the system, worse case To prevent thrashing we should give enough frames to each process But how much is enough Locality model Ø Process migrates from one locality to another! Ø Localities may overlap When Σ size of locality > total memory size thrashing occurs Increase locality in your programs! Working-Set Model Working-Set Definition Δ working-set window a fixed number of page references, example:, instruction WSS i (working set of Process P i ) = total number of pages referenced in the most recent Δ Ø if Δ too small, will not encompass entire locality Ø if Δ too large, will encompass localities Ø if Δ = will encompass entire program Informal Definition: the collection of pages that a process is working with, and which must thus be resident if the process is to avoid thrashing The idea is to use the recent needs of a process to predict its future needs Ø Choose Δ, the working set parameter At any given time, all pages referenced by a process in its last Δ seconds of execution are considered to comprise its working set Ø Pages outside the working set may be discarded at any time D = Σ WSS i total demand frames if D > (available frames) m Thrashing Thus, if D > m, then suspend one of the processes 5

Keeping Track of the Working Set Approximate with interval timer + a reference bit Example: Δ =, Ø Timer interrupts after every 5 time units Ø Keep reference bit and in-memory bit for each page Ø At a timer interrupt, copy and set the values of all reference bits to Ø If one of the bits in memory = page in working set Why is this not completely accurate? Improve = bits and interrupt every time units Balance Set Working set is not enough to control thrashing If the sum of the working sets of all runnable processes is greater than the size of memory, then refuse to run some of the processes Divide runnable processes up into two groups: active and inactive Ø When a process is made active its working set is loaded, when it is made inactive its working set is allowed to migrate back to disk Ø The collection of active processes is called the balance set Page-Fault Frequency (PFF) Scheme Working set is a clumsy way to control thrashing PFF is a more direct way Ø High PFF à more thrashing Ø Establish acceptable pagefault rate ü If actual rate is too low, process loses frame ü If actual rate is too high, process gains frame Ø Suspend a process if PFF is above upper bound and there is no free frames! What happens if there is no free frame? Terminate a user program Swap out some page PAGE REPLACEMENT 6

Page Replacement No free frame in memory, a page needs to be replaced Pages that are replaced might be needed again later We need algorithms to minimize the number of page faults Include other improvement, eg, use modify (dirty) bit to reduce overhead of page transfers only modified pages are written to disk Basic Page Replacement Find the desired page on disk If there is a free frame, use it If there is no free frame, use a page replacement algorithm Select a victim frame, swap it out (use dirty bit to swap out only modified frames)! 2 Bring the desired page into the (newly) free frame; 3 update the page and frame tables Restart the process not real implementation of existing OS Page Replacement Algorithms How to select the victim frame? Ø You can select any frame, the page replacement will work; but the performance??? Ø Gives the lowest page-fault rate Evaluate an algorithm by running it on a particular string of memory references (reference string) and compute the number of page faults on that string In all our examples, we will have 3 frames and the following reference string First-In-First-Out (FIFO) Algorithm Maintain an FIFO buffer Ø + The page used before may not be needed Ø - An array used early, might be used again and again Easy to implement Belady s Anomaly: more frames more page faults 7

FIFO Illustrating Belady s Anomaly Reference string (2 accesses), 2, 3, 4,, 2, 5,, 2, 3, 4, 5 Optimal Algorithm Replace a page that will not be used for longest time, 2, 3, 4,, 2, 5,, 2, 3, 4, 5 2 2 3 3 4 X 5 How do you know the future? Used for measuring how well your algorithm performs Least Recently Used (LRU) Algorithm Use recent past as an approximation of the future Select the page that is not used for a long time Ø OPT if you look at from backward Ø NO Belady s Anomaly: so more frames less page faults Given the reference string of page accesses: 2 3 4 2 3 4 2 3 4 and a system with three page frames, what is the final configuration of the three frames after the true LRU algorithm is applied? Problem of LRU: How to implement it efficiently? Full LRU needs to sort all time of reference 8

LRU Algorithm (Cont) Counter (logical clock) implementation Ø Increase the counter every time a page is referenced Ø Save it into time-of-use field Ø Find one with the smallest time-of-use value Ø Problems: Counter overflow and linear search perfor Stack implementation keep a stack of page numbers in a double link form: Ø Page referenced: ü move it to the top ü requires 6 pointer ops to be changed Ø No search for replacement ü Least recently used one is at the bottom Need Hardware support LRU Approximation Algorithms Reference bit Ø With each page associate a reference bit, initially = Ø When page is referenced, set this bit to by hardware Ø Replace the one which is (if one exists) ü We do not know the order, however ü Additional bits can help to gain more ordering information Second chance Alg (uses one reference bit) Ø FIFO with an inspection of ref bit Ø If ref bit is, ü replace that page ü set its ref bit to Ø If ref bit is, /* give a second chance */! ü set ref bit to ü leave page in memory ü Arrival time set to current time ü go to next one Ø Enhance it modify bit, avoid replacing modified pages What if all bits are All pages will get second chance Degenerates FIFO Global vs Local Allocation Summary: Page Replacement Algorithms Global replacement process selects a replacement frame from the set of all frames; one process can take a frame from another Ø High priority processes can take all frames from low priority ones (cause thrashing) Ø A process cannot control its page fault rate Local replacement each process selects from only its own set of allocated frames Ø Consistent performance Ø Lower utilization of memory and less throughput Algorithm FIFO (First-In, First Out) Second chance LRU (Least Recently Used) OPT (Optimal) Comment Might throw out useful pages Big improvement over FIFO Excellent, but hard to implement exactly Not implementable, but useful as a benchmark 76 9

Kernel Memory Allocation Treated differently from user memory (allocate page even when byte is needed) Often allocated from a different free-memory pool Kernel requests memory for structures of varying sizes Some kernel memory needs to be contiguous ALLOCATING KERNEL MEMORY Eg, Linux PCB (struct task_struct) ü > 7 Kbytes each ü Created on every fork and every thread create clone() ü deleted on every exit Kernel memory allocators ü Buddy system ü Slab allocation Buddy System (Dividing) Free Page s List Two continuous blocks with the same size the first one will start as 2^n 8 2

Page Allocation Buddy Allocation Example: Need to allocate 65 contiguous page frames Ø Look in list of free 28-page-frame blocks Ø If free block exists, allocate it, else look in next highest order list (here, 256-page-frame blocks) Ø If first free block is in 256-page-frame list, allocate a 28- page-frame block and put remaining 28-page-frame block in lower order list Ø If first free block is in 52-page-frame list, allocate a 28- page-frame block and split remaining 384 page frames into 2 blocks of 256 and 28 page frames These blocks are allocated to the corresponding free lists Question: What is the worst-case internal fragmentation? 8 Buddy De-Allocation When blocks of page frames are released the kernel tries to merge pairs of buddy blocks of size b into blocks of size 2b Two blocks are buddies if: Ø They have equal size b Ø They are located at contiguous physical addresses Ø The address of the first page frame in the first block is aligned on a multiple of 2b*2 2 The process repeats by attempting to merge buddies of size 2b, 4b, 8b etc Slab Allocator Performs the following functions Ø Allocate memory Ø Initialize objects/structures Ø Use objects/structures Ø Deconstruct objects/structures Ø Free memory /proc/slabinfo gives full information about memory usage on the slab level (see also /usr/bin/slabtop) 2

Slab Allocator 85 Slab is one or more physically contiguous pages Cache consists of one or more slabs Single cache for each unique kernel data structure (process descriptions, file objects, semaphores)! Ø 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, next object is allocated from empty slab If no empty slabs, new slab allocated Benefits include n no fragmentation, n memory request is satisfied quickly Memory allocation (using mmap/brk) WHAT HAPPENS WHEN ALLOCATING MEMORY #include <stdioh> #include <stdlibh> int main() { int * ptr = malloc(4); *ptr = ; free(ptr); } 848-849 r-xp test 849-84a r p test 84a-84b rw-p test b7e7b-b7e7c rw-p b7e7c-b7fd8 r-xp libc-29so b7fd8-b7fd9 ---p libc-29so b7fd9-b7fdb r--p libc-29so b7fdb-b7fdc rw-p libc-29so b7fdc-b7fe rw-p b7fe-b7fe2 r-xp [vdso] b7fe2-b7ffe r-xp ld-29so b7ffe-b7fff r p ld-29so b7fff-b8 rw-p ld-29so bffeb-c rw-p [stack] Currently, no heap space at all because we didn t use any heap 22

Memory allocation #include <stdioh> #include <stdlibh> int main() { int * ptr = malloc(4); *ptr = ; free(ptr); } 848-849 r-xp test 849-84a r p test 84a-84b rw-p test 84b-86c rw-p [heap] b7e7b-b7e7c rw-p b7e7c-b7fd8 r-xp libc-29so b7fd8-b7fd9 ---p libc-29so b7fd9-b7fdb r--p libc-29so b7fdb-b7fdc rw-p libc-29so b7fdc-b7fe rw-p b7fe-b7fe2 r-xp [vdso] b7fe2-b7ffe r-xp ld-29so b7ffe-b7fff r p ld-29so b7fff-b8 rw-p ld-29so bffeb-c rw-p [stack] Now, the heap is allocated from the kernel, which means the virtual address from x84b to x86c (total 33K) are usable ptr is actually x84b8 Memory Mapping (mmap or brk) #include <stdioh> #include <stdlibh> int main() { int * ptr = malloc(4); *ptr = ; free(ptr); } 84b-86c rw-p [heap] 84b 86c Valid page table Memory Mapping (mmap or brk) #include <stdioh> #include <stdlibh> int main() { int * ptr = malloc(4); *ptr = ; free(ptr); } 84b-86c rw-p [heap] 84b 86c Valid page table Physical Page What we learn here? Typically, the user will ask one big block of memory and setup its page table Then this memory will be managed by user space memory manager Ø How to manage the memory inside user space? 92 23

Memory Allocator for multithreaded programs (Hoard) Memory Allocator for multithreaded programs (Hoard) Super Blocks Thread Thread n 93 94 Summary Simple memory management: swap etc Virtual memory and paging Ø Page table and address translation Translation lookaside buffer (TLB) Multi-level page table Page replacement algorithms Working set of processes Kernel Memory Mangement 95 24