Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

Similar documents
CSE 361S Intro to Systems Software Final Project

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

1 Introduction. 2 Logistics. 3 Hand Out Instructions

CSE 361 Fall 2017 Malloc Lab: Writing a Dynamic Storage Allocator Assigned: Monday Nov. 13, Due: Monday Dec. 05, 11:59PM

ECE454, Fall 2014 Homework3: Dynamic Memory Allocation Assigned: Oct 9th, Due: Nov 6th, 11:59PM

CS201: Lab #4 Writing a Dynamic Storage Allocator

CS 3214, Fall 2015 Malloc Lab: Writing a Dynamic Storage Allocator Due date: November 16, 2014, 11:59pm

COMP 321: Introduction to Computer Systems

Writing a Dynamic Storage Allocator

CSCI 2021, Fall 2018 Malloc Lab: Writing a Dynamic Storage Allocator Assigned: Monday November 5th Due: Monday November 19th, 11:55PM

CS 213, Fall 2002 Malloc Lab: Writing a Debugging Dynamic Storage Allocator Assigned: Fri Nov. 1, Due: Tuesday Nov. 19, 11:59PM

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

CSCI0330 Intro Computer Systems Doeppner. Project Malloc. Due: 11/28/18. 1 Introduction 1. 2 Assignment Specification Support Routines 4

Project 2. Assigned: 02/20/2015 Due Date: 03/06/2015

ANITA S SUPER AWESOME RECITATION SLIDES

Recitation #11 Malloc Lab. November 7th, 2017

Project 4: Implementing Malloc Introduction & Problem statement

HW 3: Malloc CS 162. Due: Monday, March 28, 2016

CS356: Discussion #12-13 Dynamic Memory and Allocation Lab. Marco Paolieri Illustrations from CS:APP3e textbook

Assignment 5. CS/ECE 354 Spring 2016 DUE: April 22nd (Friday) at 9 am

Recitation #12 Malloc Lab - Part 2. November 14th, 2017

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

CSE351 Spring 2010 Final Exam (9 June 2010)

Recitation 11: More Malloc Lab

Carnegie Mellon. Malloc Boot Camp. Stan, Nikhil, Kim

In Java we have the keyword null, which is the value of an uninitialized reference type

CSE351 Spring 2010 Final Exam (9 June 2010)

Lab 1: Dynamic Memory: Heap Manager

Dynamic Memory Allocation. Basic Concepts. Computer Organization 4/3/2012. CSC252 - Spring The malloc Package. Kai Shen

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Dynamic Memory Allocation I Nov 5, 2002

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Project #1: Tracing, System Calls, and Processes

Project 3a: Malloc and Free

Dynamic Memory Allocation I

Machine Problem 1: A Simple Memory Allocator

Project 2 Overview: Part A: User space memory allocation

CS 33. Storage Allocation. CS33 Intro to Computer Systems XXVII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Dynamic Memory Allocation: Basic Concepts

My malloc: mylloc and mhysa. Johan Montelius HT2016

BIL220, Spring 2012 Data Lab: Manipulating Bits Assigned: Feb. 23, Due: Wed., Mar. 8, 23:59PM

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Lecture 8 Dynamic Memory Allocation

Dynamic Memory Allocation

ECE 2400 Computer Systems Programming, Fall 2018 PA2: List and Vector Data Structures

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Memory management. Johan Montelius KTH

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Class Information ANNOUCEMENTS

CS 356, Fall 2018 Data Lab (Part 1): Manipulating Bits Due: Wednesday, Sep. 5, 11:59PM

CS 356, Fall 2018 Data Lab (Part 2): Manipulating Bits Due: Mon, Sep. 17, 11:59PM

Recitation 10: Malloc Lab

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

ECEn 424 Data Lab: Manipulating Bits

Heap Arrays and Linked Lists. Steven R. Bagley

Exercise Session 2 Systems Programming and Computer Architecture

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSE351 Winter 2016, Final Examination March 16, 2016

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Creating a String Data Type in C

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

Dynamic Memory Allocation: Basic Concepts

Motivation was to facilitate development of systems software, especially OS development.

Dynamic Memory Allocation

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

C Programming Basics II

PROJECT 2 - MEMORY ALLOCATOR Computer Systems Principles. October 1, 2010

CS 105 Lab 1: Manipulating Bits

Dynamic Memory Allocation: Advanced Concepts

Data Lab: Manipulating Bits

Heap Arrays. Steven R. Bagley

CMSC 330: Organization of Programming Languages

Dynamic Memory Management! Goals of this Lecture!

CS61C : Machine Structures

CS 429H, Spring 2012 Optimizing the Performance of a Pipelined Processor Assigned: March 26, Due: April 19, 11:59PM

Final CSE 131B Spring 2004

Limitations of the stack

Review! Lecture 5 C Memory Management !

PRINCIPLES OF OPERATING SYSTEMS

Advanced Memory Allocation

CS61C : Machine Structures

CSE351 Autumn 2013 Final Exam (11 Dec 2013)

Exercise Session 6 Computer Architecture and Systems Programming

It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such as GitHub.

Secure Software Programming and Vulnerability Analysis

CS-281, Spring 2011 Data Lab: Manipulating Bits Assigned: Jan. 17, Due: Mon., Jan. 31, 11:59PM

Programming Tips for CS758/858

Machine Problem 1: A Simple Memory Allocator. 100 points Due date: To Be Announced

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

o Code, executable, and process o Main memory vs. virtual memory

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Heap Management. Heap Allocation

CS 135, Fall 2010 Project 4: Code Optimization Assigned: November 30th, 2010 Due: December 12,, 2010, 12noon

CS201- Introduction to Programming Current Quizzes

EECS 213, Spring 2013 Data Lab: Manipulating Bits

CS61 Section Notes. Section 5 (Fall 2011) Topics to be covered Common Memory Errors Dynamic Memory Allocation Assignment 3: Malloc

Transcription:

1. Introduction Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator Assigned: Mar. 03 Due: Mar. 17, 15:59 In this lab you will be writing a dynamic memory allocator for C programs, i.e., your own version of the malloc, free and realloc routines. You are encouraged to explore the design space creatively and implement an allocator that is correct, efficient and fast. 2. Logistics You should work individually in solving the problems in this lab. Any clarifications and revisions to the lab will be posted on the course website. (http://ipads.se.sjtu.edu.cn/courses/ics) 3. Hand Out Instructions You should use svn tools to get lab7 from server like lab6. The URL of svn repository is still svn://ipads.se.sjtu.edu.cn/ics-se14/[account]. You can see a directory named lab7 under your path. The only file you will be modifying and handing in is mm.c. The mdriver.c program is a driver program that allows you to evaluate the performance of your solution. Use the command make to generate the driver code and run it with the command./mdriver -V. (The V flag displays helpful summary information.) Looking at the file mm.c you ll notice a C structure team info which you should insert the requested identifying information about yourself. The

team name is your student id, and the full name is the PINYIN of your name. Do this right away so you don t forget. Please carefully read README file to get more detail information about all files. 4. How to Work on the Lab Your dynamic storage allocator will consist of the following four functions (mm_init, mm_malloc, mm_free and mm_realloc), which are declared in mm.h and defined in mm.c. int mm_init (void); void *mm_malloc (size_t size); void mm_free (void *ptr); void *mm_realloc(void *ptr, size_t size); The mm.c file we have given you implements the simplest but still functionally correct malloc package that we could think of. Using this as a starting place, modify these functions (and possibly define other private static functions), so that they obey the following semantics: 1) mm_init: Before calling mm_malloc mm_realloc or mm_free, the application program (i.e., the trace-driven test harness that you will use to evaluate your implementation) calls mm_init to perform any necessary initializations, such as allocating the initial heap area. The return value should be -1 if there was a problem in performing the initialization, 0 otherwise. 2) mm_malloc: The mm_malloc routine returns a pointer to an allocated block payload of at least size bytes. The entire allocated block should lie within the heap region and should not overlap with any other allocated chunk. We will compare your implementation to the version of malloc supplied in the standard C library (libc). Since the libc malloc always returns payload pointers that are aligned to 8 bytes, your malloc implementation should do likewise and always return 8-byte aligned pointers.

3) mm_free: The mm_free routine frees the block pointed to by ptr. It returns nothing. This routine is only guaranteed to work when the passed pointer (ptr) was returned by an earlier call to mm_malloc or mm_realloc and has not yet been freed. 4) mm_realloc: The mm_realloc routine returns a pointer to an allocated region of at least size bytes with the following constraints. o if ptr is NULL, the call is equivalent to mm_malloc(size); o if size is equal to zero, the call is equivalent to mm_free(ptr); o if ptr is not NULL, it must have been returned by an earlier call to mm_malloc or mm_realloc. The call to mm_realloc changes the size of the memory block pointed to by ptr (the old block) to size bytes and returns the address of the new block. Notice that the address of the new block might be the same as the old block, or it might be different, depending on your implementation, the amount of internal fragmentation in the old block, and the size of the realloc request. The contents of the new block are the same as those of the old ptr block, up to the minimum of the old and new sizes. Everything else is uninitialized. For example, if the old block is 8 bytes and the new block is 12 bytes, then the first 8 bytes of the new block are identical to the first 8 bytes of the old block and the last 4 bytes are uninitialized. Similarly, if the old block is 8 bytes and the new block is 4 bytes, then the contents of the new block are identical to the first 4 bytes of the old block. These semantics match the semantics of the corresponding Linux malloc, realloc, and free routines. (Type man malloc to the shell for complete documentation.) Heap Consistency Checker. Dynamic memory allocators are notoriously tricky beasts to program correctly and efficiently. They are difficult to program correctly because they involve a lot of (void *) pointer references. You will find it very helpful to write a heap checker that scans the heap and checks it for consistency. Some examples of what a heap checker might check are:

o Is every block in the free list marked as free? o Are there any contiguous free blocks that somehow escaped coalescing? o Is every free block actually in the free list? o Do the pointers in the free list point to valid free blocks? o Do any allocated blocks overlap? o Do the pointers in a heap block point to valid heap addresses? Your heap checker will consist of the function int mm_check(void) in mm.c. It will check any invariants or consistency conditions you consider prudent. It returns a nonzero value if and only if your heap is consistent. You are not limited to the listed suggestions nor are you required to check all of them. You are encouraged to print out error messages when mm_check fails. This consistency checker is for your own debugging during development. When you submit mm.c, make sure to remove any calls to mm_check as they will slow down your throughput. Style points will be given for your mm_check. Make sure to put in comments and document what you are checking. Support routines The memlib.c package simulates the memory system for your dynamic memory allocator. You can invoke the following functions in memlib.c : o void *mem_sbrk(int incr): Expands the heap by incr bytes, where incr is a positive non-zero integer and returns a generic pointer to the first byte of the newly allocated heap area. The semantics are identical to the Unix sbrk function, except that mem_sbrk accepts only a positive non-zero integer argument. o void *mem_heap_lo(void): Returns a generic pointer to the first byte in the heap. o void *mem_heap_hi(void): Returns a generic pointer to the last byte in the heap. o size t mem_heapsize(void): Returns the current size of the heap in bytes. o size t mem_pagesize(void): Returns the system s page size in bytes (4K on Linux systems).

The Trace -driven Driver Program The driver program mdriver.c in the malloc lab distribution tests your mm.c package for correctness, space utilization, and throughput. The driver program is controlled by a set of trace files under the traces directory. Each trace file contains a sequence of allocate, reallocate, and free directions that instruct the driver to call your mm_malloc, mm_realloc, and mm_free routines in some sequence. The driver and the trace files are the same ones we will use when we grade your handin mm.c file. The driver mdriver.c accepts the following command line arguments: o -t <tracedir>: Look for the default trace files in directory tracedir instead of the default directory defined in config.h. o -f <tracefile>: Use one particular tracefile for testing instead of the default set of tracefiles. o -h: Print a summary of the command line arguments. o -l: Run and measure libc malloc in addition to the student s malloc package. o -v: Verbose output. Print a performance breakdown for each tracefile in a compact table. o -V: More verbose output. Prints additional diagnostic information as each trace file is processed. Useful during debugging for determining which trace file is causing your malloc package to fail. 5. Programming Rules o You are not allowed to change any of the interfaces in mm.c. o You should not invoke any memory-management related library calls or system calls. This excludes the use of malloc, calloc, free, realloc, sbrk, brk or any variants of these calls in your code. o You are not allowed to define any global or static compound data structures such as arrays, structs, trees, or lists in your mm.c program. However, you are allowed to declare global scalar variables such as integers, floats, and pointers in mm.c. o For consistency with the libc malloc package, which returns blocks aligned on 8-byte boundaries, your allocator must always return

pointers that are aligned to 8-byte boundaries. The driver will enforce this requirement for you. 6. Evaluation You will receive zero points if you break any of the rules or your code is buggy. Otherwise, your grade will be calculated as follows: Correctness (20 points): You will receive full points if your solution passes the correctness tests performed by the driver program. You will receive partial credit for each correct trace. (i.e., you pass the first 9 trace files). Performance (35 points): Two performance metrics will be used to evaluate your solution: o Space utilization: The peak ratio between the aggregate amount of memory used by the test harness (i.e., allocated via mm_malloc or mm_realloc but not yet freed via mm_free) and the size of the heap used by your allocator. The optimal ratio equals to 1. You should find good policies to minimize fragmentation in order to make this ratio as close as possible to the optimal. o Throughput: The average number of operations completed per second. The driver program summarizes the performance of your allocator by computing a performance index, P, which is a weighted sum of the space utilization and throughput P = wu + (1-w)min(1, T/T libc ) Where U is your space utilization, T is your throughput, and Tlibc is the estimated throughout of libc malloc on your system on the default trace (5000Kops/sec). The perfromance index favors space utilization over throughput, with a default of w = 0.6 Observing that both memory space and CPU cycles are expensive system resources, we adopt this formula to encourage balanced optimization of both. Ideally, the performance index will reach P = w + (1-w) = 1 or 100%. Since each metric will contribute at most w and 1-w to the performance index, respectively, you should not go to extremes to optimize either the

memory utilization or the throughput only. To receive a good score, you must achieve a balance between utilization and throughput. To get full credit for this part of the lab, you will need to achieve a performance index of 95%. Your score will be calculated by adding 5% to your performance index and multiplying by 35 points. You cannot get more than 35 points for this part of the lab. MM_Check (5 points): You will be awarded 5 points for a good heap consistency checker and good program structure and comments. 7. Handin Instructions When you have completed the lab, you only need to commit mm.c to svn server, which contains your solution. We strongly recommend you to multiple commit your code to svn during implementation. 8. Hints Watch README first to get start. Use the mdriver -f option. During initial development, using tiny trace files will simplify debugging and testing. We have included two such trace files (short1,2-bal.rep) that you can use for initial debugging. Use the mdriver -v and -V options. The -v option will give you a detailed summary for each trace file. The -V will also indicate when each trace file is read, which will help you isolate errors. Compile with gcc -g and use a debugger. A debugger will help you isolate and identify out of bounds memory references. Understand every line of the malloc implementation in the textbook. The textbook has a detailed example of a simple allocator based on an implicit free list. Use this is a point of departure. Don t start working on your allocator until you understand everything about the simple implicit list allocator.

Encapsulate your pointer arithmetic in C preprocessor macros. Pointer arithmetic in memory managers is confusing and error-prone because of all the casting that is necessary. You can reduce the complexity significantly by writing macros for your pointer operations. See the text for examples. Do your implementation in stages. The first 9 traces contain requests to malloc and free. The last 2 traces contain requests for realloc, malloc, and free. We recommend that you start by getting your malloc and free routines working correctly and efficiently on the first 9 traces. Only then should you turn your attention to the realloc implementation. For starters, build realloc on top of your existing malloc and free implementations. But to get really good performance, you will need to build a stand-alone realloc. Use a profiler. You may find the gprof tool helpful for optimizing performance. Start early! It is possible to write an efficient malloc package with a few pages of code. However, we can guarantee that it will be some of the most sophisticated code you have written so far in your career. So start early and good luck!