CS201: Lab #4 Writing a Dynamic Storage Allocator

Similar documents
CSE 361S Intro to Systems Software Final Project

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

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

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

1 Introduction. 2 Logistics. 3 Hand Out Instructions

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

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

COMP 321: Introduction to Computer Systems

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

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

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

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

Writing a Dynamic Storage Allocator

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

ANITA S SUPER AWESOME RECITATION SLIDES

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

Recitation #11 Malloc Lab. November 7th, 2017

Recitation 11: More Malloc Lab

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

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

Project 4: Implementing Malloc Introduction & Problem statement

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

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

Exercise Session 2 Systems Programming and Computer Architecture

Dynamic Memory Allocation I

Kurt Schmidt. October 30, 2018

Dynamic Memory Allocation

Dynamic Memory Allocation I Nov 5, 2002

PRINCIPLES OF OPERATING SYSTEMS

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

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

CSE351 Spring 2010 Final Exam (9 June 2010)

Project 3a: Malloc and Free

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

Lecture 8 Dynamic Memory Allocation

Lab 1: Dynamic Memory: Heap Manager

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

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

Creating a String Data Type in C

Dynamic Memory Allocation: Basic Concepts

Class Information ANNOUCEMENTS

CSE351 Spring 2010 Final Exam (9 June 2010)

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

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

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

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

UW CSE 351, Winter 2013 Final Exam

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

My malloc: mylloc and mhysa. Johan Montelius HT2016

Project 2 Overview: Part A: User space memory allocation

To obtain the current global trace mask, call meitraceget(...). To modify the global trace mask, call meitraceset(...).

Memory management. Johan Montelius KTH

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

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

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

Secure Software Programming and Vulnerability Analysis

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

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

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

Dynamic Memory Allocation: Basic Concepts

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Recitation 10: Malloc Lab

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

NOTE: Debug and DebugSingle are the only MPI library configurations that will produce trace output.

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Project #1: Tracing, System Calls, and Processes

CSE 124 Discussion (10/3) C/C++ Basics

CS 11 C track: lecture 5

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

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

JTSK Programming in C II C-Lab II. Lecture 3 & 4

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Dynamic Memory Allocation

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS61, Fall 2012 Section 2 Notes

Machine Problem 1: A Simple Memory Allocator

Dynamic Memory Allocation. Zhaoguo Wang

Understanding Pointers

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Programs in memory. The layout of memory is roughly:

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS61C : Machine Structures

Dynamic Memory Management! Goals of this Lecture!

CS61C : Machine Structures

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

6.S096: Introduction to C/C++

Dynamic Memory Alloca/on: Basic Concepts

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

HW1 due Monday by 9:30am Assignment online, submission details to come

CSE320 Spring Homework 3

Data Structure Series

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Lectures 13 & 14. memory management

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Lecture 2: C Programm

CS Programming In C

Transcription:

CS201: Lab #4 Writing a Dynamic Storage Allocator In this lab you will write a dynamic storage 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. The allocator presented in the text, that uses implicit free lists, fails the grade when it comes to being efficient and fast. Traversing both allocated and free nodes is a big performance hit. For this reason you must implement an allocator implemented with an explicit free list. Solutions utilizing implicit lists will not be accepted. Obtain the assignment as follows: lab cs201 g4 Included in the tar archive are the following files: mm.{c,h} Source code for the implicit free list solution. driver.c The malloc driver that tests your mm.c file config.h Configures the malloc lab driver fsecs.{c,h} Wrapper function for the different timer packages clock.{c,h} Routines for accessing the Pentium and Alpha cycle counters fcyc.{c,h} Timer functions based on cycle counters ftimer.{c,h} Timer functions based on interval timers and gettimeofday() memlib.{c,h} Models the heap and sbrk function Makefile Builds the driver short{1,2}.rep Two tiny tracefiles to help you get started trace A directory containing larger trace files for testing The only file you will be modifying and handing in is mm.c. File driver.c contains the driver program that allows you to evaluate the performance of your solution. Function Specifications Your dynamic storage allocator will consist of the following four functions that 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); File mm.c implements the implicit free list solution from the text. Using this as a starting point code a solution that impliments explicit free lists. Your solution will be compared with the version of malloc supplied in the standard C library (libc.a), and should behave as follows:

mm_init. Before calling mm_malloc, mm_realloc, or mm_free, the application program (i.e., the trace-driven driver program 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. 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. Since malloc always returns payload pointers that are aligned to 8 bytes, mm_malloc should do likewise. 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. If passed a NULL pointer then mm_free simply returns. 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 o If size is equal to zero, the call is equivalent to mm_free(ptr) 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 the semantics of the corresponding libc malloc, realloc, and free routines. Support Routines The memlib.c package simulates the memory system for your dynamic memory allocator. You can invoke the following functions in memlib.c: 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 nonzero integer argument. void *mem_heap_lo(void). Returns a generic pointer to the first byte in the heap. void *mem_heap_hi(void). Returns a generic pointer to the last byte in the heap. size_t mem_heapsize(void). Returns the current size of the heap in bytes. 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 driver.c tests mm.c for correctness, space utilization, and throughput. The driver program is controlled by a set of trace files. Each trace file contains a sequence of allocate, reallocate, and free directions that instruct the driver to call your mm_alloc, mm_realloc, and mm_free routines in some sequence. The driver and the trace files are the same ones used to grade your program. Driver driver.c accepts the following command line arguments:

-t <tracedir>. Look for the default trace files in directory tracedir instead of the default directory./trace. -f <tracefile>. Use one particular tracefile in the current directory for testing. -h. Print a summary of the command line arguments. -l. Run and measure libc malloc in addition to the student s malloc package. -v. Verbose output. Print a performance breakdown for each tracefile in a compact table. -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. Initially you'll want to run against short1.rep and short2.rep, two small trace files in the distribution. driver Vf short1.rep driver Vf short2.rep To run against all the files in the trace directory enter the following. driver V driver v # very verbose output # verbose output For grading purposes driver will be run with the following arguments: driver v Programming rules You should not change any of the interfaces in mm.c. You should not invoke any memory-management related library calls or system calls. This includes the use of malloc, calloc, free, realloc, sbrk, brk or any variants of these calls in your code. Links for the explicit free list should be contained within the payload of each memory block. You may define additional global pointers and structures as needed. Evaluation Your grade will be based on correctness and performance. Two metrics will be used to evaluate performance: Space utilization: The peak ratio between the aggregate amount of memory used by the driver (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. Throughput: The average number of operations completed per second. The driver program summarizes the performance of your allocator by computing a performance index P that is a weighted sum of the space utilization and throughput P = wu + (1 w) min(1, where U is space utilization, T is throughput, and T libc is the estimated throughput of libc malloc on the school's unix system using default traces. The value for T libc is a constant in the driver. The performance index favors space utilization over throughput, with a default w = 0.6. T T libc )

Observing that both memory and CPU cycles are expensive system resources, this formula encourages balanced optimization of both memory utilization and throughput. 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. For a passing grade of "C" or better your solution should correctly process all traces. Solutions that perform significantly better than the implicit solution will receive a grade of "B" or better. Hints Use the driver -f option. During initial development use tiny trace files to simplify debugging and testing. Included are two such trace files (short1.rep and short2.rep). If you need to experiment with one of the files in the trace subdirectory, copy the file to the current directory and run tests on that file. Use the driver -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. Do your implementation in stages. Get short1.rep and short2.rep working before tackling larger trace files. The first 9 traces contain requests to malloc and free. The last 2 traces contain requests for realloc, malloc, and free. I recommend that you start by getting the 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 standalone realloc. Use a struct for freelist nodes. Keep existing macros to update headers and footers. But for the freelist define a structure and cast a pointer to the structure. e.g., typedef node { struct node *next; struct node *prev; } Node; Then cast the block pointer to the structure. Also consider using a 2-way circular list with a sentinel node as we used on the first lab. Use mm_checkheap for debugging. Code mm_checkheap first thing and use it as a debugging tool. This is probably the most important hint. Debug Strategy I can almost guarantee you'll get a segmentation violation. This happens when you try to dereference a pointer that's pointing to memory not owned by your process. To determine the source of the problem recompile your code with the g option. Include this option in the CFLAGS statement in your Makefile, do a make clean, followed by a make, and you'll have objects that include debug information.

Then debug using gdb. gdb driver r (specify "r f filename" if you want to run a specific file) The debugger will stop at the error and display the current file name and line number. One problem with this strategy is the fact that the error may have occured hundreds of lines prior to the segmentation violation, so often this information is worthless. Let me emphasize importance of the heap integrity checker mm_checkheap. Your code includes an mm_checkheap function that validates the implicit list. Add code to the checker to validate your free list. Then sprinkle calls to mm_checkheap throughout your code. For example, you may want to do an mm_checkheap on entry to mm_malloc, and on exit from mm_malloc, to make sure mm_malloc is not trashing the heap. With a bit of luck you'll soon isolate segmentation violations at the source. Finally, here's a technique you can use to limit debug output. Define your own debug version of printf, called dprintf, that works just like printf. This is easier than it sounds, and provides the ability to insert additional logic to control output. In the following example dprintf will start producing output the 500 th time it's called. With a few minor changes you can redirect trace information to a file. #include <stdio.h> #include <stdarg.h> int ct = 0; void dprintf(char *fmt,...) { char buf[133]; va_list ap; int len; // arbitrary logic to control trace info if (++ct < 500) return; } va_start(ap, fmt); len = vsprintf(buf, fmt, ap); fwrite(buf, len, 1, stdout); va_end(ap); void loopy(void) { int i; for (i = 0; i < 1000; i++) dprintf("i = %d\n", i); } Turnin To submit this lab execute the following command: lab cs201 p4 mm.c