Buddy Allocation CS 161: Lecture 5 2/11/19.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Advertisements

Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Note on malloc() and slab allocation CS-502 (EMC) Fall A Note on malloc() and Slab Allocation CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
The Linux Kernel: Memory Management
Memory Management in Linux (Chap. 8 in Understanding the Linux Kernel)
Kernel memory allocation
Memory management.
File Systems.
KERNEL MEMORY ALLOCATION Unix Internals, Uresh Vahalia Sowmya Ponugoti CMSC 691X.
Operating System Support Focus on Architecture
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
1 Memory Management in Representative Operating Systems.
Memory Allocation CS Introduction to Operating Systems.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Review of Memory Management, Virtual Memory CS448.
Introduction to Processes CS Intoduction to Operating Systems.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Memory Management – Page 1 of 49CSCI 4717 – Computer Architecture Memory Management Uni-program – memory split into two parts –One for Operating System.
Operating Systems CMPSC 473 Virtual Memory Management (4) November – Lecture 22 Instructor: Bhuvan Urgaonkar.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
1 Advanced Memory Management Techniques  static vs. dynamic kernel memory allocation  resource map allocation  power-of-two free list allocation  buddy.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 9: Virtual Memory.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
Processes and Virtual Memory
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
LINUX 15 : Memory Management
Memory Management: Overlays and Virtual Memory. Agenda Overview of Virtual Memory –Review material based on Computer Architecture and OS concepts Credits.
Virtual Memory – Managing Physical Memory
Virtual Memory Pranav Shah CS147 - Sin Min Lee. Concept of Virtual Memory Purpose of Virtual Memory - to use hard disk as an extension of RAM. Personal.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 4.
Chapter 17 Free-Space Management
Chapter 2: The Linux System Part 4
Memory Management.
Non Contiguous Memory Allocation
Jonathan Walpole Computer Science Portland State University
CS703 - Advanced Operating Systems
Day 21 Virtual Memory.
Day 22 Virtual Memory.
Swapping Segmented paging allows us to have non-contiguous allocations
PA1 is out Best by Feb , 10:00 pm Enjoy early
The Hardware/Software Interface CSE351 Winter 2013
Chapter 8: Main Memory.
CSCI206 - Computer Organization & Programming
CS Introduction to Operating Systems
Making Virtual Memory Real: The Linux-x86-64 way
System Structure and Process Model
So far… Text RO …. printf() RW link printf Linking, loading
CS399 New Beginnings Jonathan Walpole.
Chapter 9: Virtual Memory
Virtual Memory Hardware
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Lecture 3: Main Memory.
CSE 451: Operating Systems Autumn 2005 Memory Management
Operating System Chapter 7. Memory Management
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
CSE 451 Autumn 2003 November 13 Section.
Kernel Memory Chris Gill, David Ferry, Brian Kocoloski
CSCI 380: Operating Systems William Killian
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Lecture 7: Flexible Address Translation
CS703 - Advanced Operating Systems
Dynamic Memory Allocation
COMP755 Advanced Operating Systems
Chapter 8 & 9 Main Memory and Virtual Memory
Presentation transcript:

Buddy Allocation CS 161: Lecture 5 2/11/19

Allocating memory in the kernel Physical RAM Kernels often need to dynamically allocate memory Ex: Creating a new address space during fork() Page table struct proc Copies of user-mode pages from the parent process Child code+heap+stack pages Parent code+heap+stack pages Child page table entries for low canonical addresses Parent page table entries for low canonical addresses Child struct proc Parent struct proc Kernel text (includes page table entries for high canonical addresses + kernel text)

Allocating memory in the kernel Kernels often need to dynamically allocate memory Ex: Creating a new address space during fork() Page table struct proc Copies of user-mode pages from the parent process Ex: Allocating buffers to hold incoming IO data IO devices often unaware of virtual addressing Consequently: Kernel must specify a physical start address for a device-to-RAM transfer If destination memory buffer is multiple pages in size, those pages must be contiguous in physical memory! Network card

Allocating memory in the kernel Virtual address space (low canonical) Code Static data Heap Stack Kernels often need to dynamically allocate memory Ex: Creating a new address space during fork() Page table struct proc Copies of user-mode pages from the parent process Ex: Allocating buffers to hold incoming IO data Ex: Growing the user-level heap via the sbrk(size_t increment) syscall (which is internally invoked by malloc()) Many (but not all) kernel allocations have a page-sized granularity Linux stacks Automatically grown by kernel after a page fault . . . . . . but only up to a limit (e.g., 8MB; run ulimit –s) [A Linux process can also add pages to the virtual address space via mmap(). Chickadee’s SYS_PAGE_ALLOC is roughly analogous, inasmuch as it allows a process to map a new page into the virtual address space.] [What about the stack—does it automatically grow if, e.g., user-space code makes a lot of nested function calls? It could; the OS could choose to detect page faults to region immediately beneath the current top of stack, and automatically allocate a new page. Linux does this. However, Linux also limits the maximum size of each thread’s stack to a fixed amount like 8 MB. On Linux, the command ‘ulimit –s’ will show the default stack size; a sysadmin can also use ulimit to change the default.] [Chickadee stacks do not grow on a page fault. Also note that the p-allocator.cc program maintains a variable called heap_top that is like the brk pointer that a Linux malloc implementation would track.]

x86-64 Linux: void *kmalloc(size_t size,…) Returns a chunk of memory that is both virtually-contiguous and physically-contiguous Returned virtual address resides in the physical memory map that uses direct mapping of virtual-to- physical addresses So, kmalloc() doesn’t need to manipulate page tables to satisfy a request! Uses slab allocation Maintains several memory pools Each pool satisfies requests for a certain size (e.g., 64 bytes, 1 MB) Requests satisfied by smallest possible slab Virtual address space (high canonical) Direct map of all physical RAM Kernel text (direct-mapped to bottom of physical RAM) pset2.a is basically Linux kmalloc() without slab allocation

x86-64 Linux: void *vmalloc(unsigned long size) Returns a chunk of memory that is virtually-contiguous, but possibly not physically-contiguous Allocates objects at a page-size granularity Internally, vmalloc() repeatedly: Finds a free physical page Modifies page table entries to map the physical page into the next contiguous virtual page Disadvantage: Page table modifications take time Advantage: More likely to succeed than kmalloc() if memory pressure is high Chickadee has no equivalent of vmalloc() Virtual address space (high canonical) Direct map of all physical RAM Kernel text (direct-mapped to bottom of physical RAM) Physical RAM vmalloc() area (uses arbitrary mappings) [A second disadvantage of vmalloc() is that, since the allocated physical pages are not contiguous, they may not be able to be covered by a small number of “huge” TLB mappings (i.e., TLB mapping that cover more than a 4 KB page—either 2 MB or 1 GB).] [Note that memory returned by kmalloc() should be released by kfree(); similarly, memory returned by vmalloc() should be released using vfree().] [Reference: https://www.kernel.org/doc/Documentation/x86/x86_64/mm.txt //Shows that the virtual address region used by //vmalloc() is different than the one used by the //physical memory map.]

Buddy allocation Memory is allocated in units of 2o bytes A request of size s bytes receives a block of 2o bytes, where o = ⌈log2s⌉ Allocator has a min order and a max order (e.g., 12 and 21) Allocation splits large free blocks until one of size o is created Freeing combines buddies to create large blocks

Buddy allocation Memory is allocated in units of 2o bytes A request of size s bytes receives a block of 2o bytes, where o = ⌈log2s⌉ Allocator has a min order and a max order (e.g., 12 and 21) def allocate(o): while True: if isFreeBlockOfOrder(o): return getFreeBlockOfOrder(o) else: block, x = getSmallestFreeBlock() #x > o if block != None: splitIntoBuddies(block) #Buddies of order x-1 continue #Hopefully isFreeBlockOfOrder(o) is #now True return None

Buddy allocation Memory is allocated in units of 2o bytes A request of size s bytes receives a block of 2o bytes, where o = ⌈log2s⌉ Allocator has a min order and a max order (e.g., 12 and 21) def free(freedBlock, o): freedBlock.free = True while freedBlock.buddyOfOrder(o).free: freedBlock = coalesce(freedBlock, freedBlock.buddyOfOrder(o)) o += 1

A=83KB B=37KB C=79KB 1024 KB 512 KB 256 KB 512 KB 128 KB 256 KB 512 KB

free(A) D=63KB free(B) free(D)

free(C) 512 KB 128 KB 256 KB 512 KB 128 KB 256 KB 512 KB 256 KB 512 KB