Computer Systems Lab Stefan M. Freudenberger Computer Systems, ETH Zürich
Introduction Course Schedule Lecture: Monday 4:15 – 5:00 p.m. (RZ F21) Recitation: TBD Assignments: in teams, time to be arranged individually Contact Stefan Freudenberger, RZ H8.1, stefan.freudenberger@inf.ethz.ch TBD Resources http://www.lst.inf.ethz.ch/teaching/lectures/current/2100/index.php 2011-02-28 Computer Systems Lab 2
Course schedule (tentative) Date Lecture Project 2011-02-21 Introduction 2011-02-28 Memory Allocation Team registration – due 2011-03-03 2011-03-07 Computer Science Colloquium - Prof. Dr. David Padua: And Malloc Lab – due 2011-03-20 the Answer Is: Automatic Parallel Programming. Starts at 16:15 CAB G61 (ends at ca. 17:20) 2011-03-14 2011-03-21 – due 2011-04-03 2011-03-28 2011-04-04 – due 2011-04-17 2011-04-11 Sechseläuten: holiday 2011-04-18 2011-04-25 Easter Monday: holiday 2011-05-02 – due 2011-05-15 2011-05-09 2011-05-16 – due 2011-05-29 2011-05-23 2011-05-30 2011-02-28 Computer Systems Lab 3
Admin Team registration: E-mail with team members: Include names, legi numbers, nethz-logins Subject: “ CSLab: …” Due: Thursday, March 3 Get team name assignment back Due: Monday, March 7 Check that you can access your team’s area: Do this early! Assignment will be posted on the web site on Monday, March 7 Assignment will be due midnight March 20 2011-02-28 Computer Systems Lab 4
Project 1: Memory Allocation Summary Implement a memory allocator: malloc, free, realloc Goals Learn how to manage storage allocation Good exercise to master pointers Environment Unix like system C Resources Computer Systems — A Programmer’s Perspective, Chapter 10.9 (Dynamic Memory Allocation), pages 730–755 (1 st Edition). 2011-02-28 Computer Systems Lab 5
The Heap Area of a process’s (virtual) memory User stack that is dynamically allocated Example (Unix) Grows upwards Memory mapped region The brk pointer marks the top of the for shared libraries heap Top of the heap ( brk ) Heap Uninitialized data (.bss) Initialized data (.data) Program (.text) 2011-02-28 Computer Systems Lab 6
Memory Management The memory manager (user space) maintains a process’s heap : a collection of blocks (contiguous chunks of memory) which are either allocated or free Allocation is explicit ( e.g. , malloc in C or new in Java) Deallocation can be: explict ( e.g. , free in C, delete in C++) implicit (garbage collection in Java, .NET) Issues The memory manager needs to: Distinguish block boundaries Distinguish between free and allocated blocks 2011-02-28 Computer Systems Lab 7
Memory Allocation: Strategies first-fit: use the first free block which is big enough best-fit: take smallest fitting block worst-fit: take biggest available block quick-fit: best-fit but multiple free-lists (one per block size) 32 8 16 8 16 32 8 8 32 16 8 8 32 16 16 64 8 16 8 32 2011-02-28 Computer Systems Lab 8
Memory Allocation: Bitmaps The simplest (but often slowest) way to manage memory blocks is to mark in a bitmap if a block is free or allocated Space For a 4GB memory space and 4KB blocks a 128MB table is necessary to store the free/allocated information Speed Slow scan to find a free block 32 8 16 8 0 0 0 0 1 1 1 0 16 32 8 8 0 0 1 1 1 1 0 1 32 16 8 8 1 1 1 1 0 0 0 1 32 16 16 0 0 0 0 0 0 1 1 64 0 0 0 0 0 0 0 0 8 16 8 32 0 1 1 0 1 1 1 1 2011-02-28 Computer Systems Lab 9
Memory Allocation: Dynamic Data Structures Free blocks are stored in a dynamic data structure For example, ordered list, ordered tree, … Lists are usually ordered by allocation address to allow a faster merging of free blocks 32 8 16 8 free list 16 32 8 8 32 16 8 8 32 16 16 64 8 16 8 32 2011-02-28 Computer Systems Lab 10
Memory Allocation: Dynamic Data Structures Free blocks are stored in a dynamic data structure For example, ordered list, ordered tree, … Lists are usually ordered by allocation address to allow a faster merging of free blocks When two adjacent blocks are both free, they are coalesced into a single block Initially: 8 16 8 After free: 8 16 8 After coalescing: 32 2011-02-28 Computer Systems Lab 11
Memory Allocation: Lists Space Link information as well as some flags ( i.e. , free or allocated) has to be stored in the block Speed Depending on the chosen data structure operations can be implemented efficiently 2011-02-28 Computer Systems Lab 12
Block Format Most allocators store information (allocated/free, size, …) in the blocks themselves Example: size flags size flags ptr link information ptr payload payload padding (optional) padding (optional) Link information: implicit : blocks are traversed using the size field only explicit : link information is embedded in the block 2011-02-28 Computer Systems Lab 13
Block Format (Footers) Implicit allocators have the problem that to perform coalescing they need information about the previous block. Knuth [1973] suggested to put a copy size flags of the block header at the end of the link information block (footer) When freeing a block an implicit allocator payload can then check the header of the next block and the footer of the previous one Footers are not necessary for allocated padding (optional) size flags blocks if we store the allocated/free information in the header of the next block D. E. Knuth. The Art of Computer Programming. Addison Wesley, 1973. Volume 1, Chapter 2, pp. 228–463. 2011-02-28 Computer Systems Lab 14
Memory Allocation: Space Utilization External fragmentation Unused space among the blocks – depends on the allocation strategy Internal fragmentation Unused space inside the blocks – depends on the possible block sizes (granularity) Data structures Space used to maintain data structures cannot be allocated (bitmaps, pointers, flags, …) 2011-02-28 Computer Systems Lab 15
Memory Allocation: Buddy System An example of allocation strategy: Each block has a size of 2 k The system maintains a list for each size Block allocation: Find a block which fits If necessary split a larger block, and put the remaining part in the correct list Free: Mark the block as free If is possible merge with a free neighbor The buddy system is used in several operating systems (Windows, Linux, Oberon, …). 2011-02-28 Computer Systems Lab 16
Memory Allocation: Buddy System (example) Free: Free list: Initial: 8 8 8 8 16 16 16 32 2011-02-28 Computer Systems Lab 17
Memory Allocation: Buddy System (example) Free: Free list: Initial: 8 8 8 8 8 16 16 16 Free: 32 8 8 16 2011-02-28 Computer Systems Lab 18
Memory Allocation: Buddy System (example) Free: Free list: Initial: 8 8 8 16 16 16 16 Free: 32 8 8 16 Coalesce: 16 16 2011-02-28 Computer Systems Lab 19
Memory Allocation: Buddy System (example) Free: Free list: Initial: 8 8 8 16 16 Free: 32 32 8 8 16 Coalesce: 16 16 Coalesce again: 32 2011-02-28 Computer Systems Lab 20
Memory Allocation: Buddy System (example) Allocate(8): Free list: Initial: 8 32 16 32 32 2011-02-28 Computer Systems Lab 21
Memory Allocation: Buddy System (example) Allocate(8): Free list: Initial: 8 32 16 16 16 Split free block: 32 16 16 2011-02-28 Computer Systems Lab 22
Memory Allocation: Buddy System (example) Allocate(8): Free list: Initial: 8 8 8 32 16 16 Split free block: 32 16 16 Split free block again: 8 8 16 2011-02-28 Computer Systems Lab 23
Memory Allocation: Buddy System (example) Allocate(8): Free list: Initial: 8 8 32 16 16 Split free block: 32 16 16 Split free block again: 8 8 16 Allocate: 8 8 16 2011-02-28 Computer Systems Lab 24
Memory Allocation: Slab Allocator Disadvantages of the buddy system: Internal fragmentation (max 50%) Bad distribution of the block addresses (bad caches performance) Solaris introduced the concept of a slab allocator : Based on the idea that processes are likely to request a lot of objects of the same size: these objects can be kept and reused Slabs are collections of objects of the same size Sizes (and addresses) are not geometrically distributed Used by AmigaOS (4), Linux ( ≥ 2.2) and Solaris (≥ 2.4) J. Bonwick. The Slab Allocator: An Object-Caching Kernel Memory Allocator. Proc. 1994 USENIX Annual Tech. Conf, pp. 87–98, June 1994, Boston, MA. 2011-02-28 Computer Systems Lab 25
Lab: Task Write a dynamic memory allocator, i.e. , implement: malloc free realloc Goals of the lab: Understand how a real memory allocator works Evaluate different design strategies A nice exercise for low level C programming 2011-02-28 Computer Systems Lab 26
Recommend
More recommend