Assignment 7: Dynamic Storage Allocator
DUE DATES:
For the “trivia” assignment: 11:59 pm, Thursday, December 3rd.
For the main assignment: 11:59pm, Wednesday, December 9th.
If you want to use slip days you have to tell us before the deadline by
emailing Shuang Zhai.
Introduction
In this lab you will be writing 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.
Logistics
You may work in a group of up to two people. Any clarifications
and revisions to the assignment will be posted on the
blackboard discussion forum.
Hand Out Instructions
Start by copying /u/cs252/labf20/malloclab/malloclab-handout.tar to a
protected directory in which you plan to do your work. Then give
the command:
tar xvf malloclab-handout.tar
This will cause a number of files to be unpacked into the directory.
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 into which you should insert the requested identifying
information about the one or two individuals comprising your programming
team.
Do this right away so you don’t forget.
When you have completed the lab, you will hand in only one file
(mm.c), which contains your solution.
How to Work on the Lab
Your dynamic storage allocator will consist of the following four
functions, 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:
These semantics match the the semantics of the corresponding libc
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 untyped pointer manipulation. 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:
- Is every block in the free list marked as free?
- Are there any contiguous free blocks that somehow escaped
coalescing?
- Is every free block actually in the free list?
- Do the pointers in the free list point to valid free blocks?
- Do any allocated blocks overlap?
- 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 function. Make
sure to put in comments and document what you are checking.
Support Routines
The memlib.c package simulates the OS portion of 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
based on the Unix sbrk function, with two notable exceptions:
- the built-in sbrk returns
-1
on an allocation
error, mem_sbrk returns NULL
- mem_sbrk accepts only a positive non-zero 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 mdriver.c in the malloc-handout.tar
distribution tests your mm.c package for correctness, space
utilization, and throughput. The driver program is controlled by a
set of trace files. Some small traces are included in the
malloc-handout.tar distribution. The larger traces that we will
test your file with are located at /u/cs252/labf20/malloclab/malloctraces/.
These larger traces are automatically run if you execute mdriver
without a -f argument. 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 mdriver.c accepts the following command line
arguments:
- –t tracedir:
Look for the default trace files in directory tracedir
instead of the default directory defined in config.h.
- –f tracefile:
Use one particular tracefile for testing instead of the
default set of tracefiles.
- –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.
Programming Rules
This is a reminder of the honor code policy.
You may use your text, course notes, and any other reference material
(such as C reference books, C reference web pages, or Unix man pages).
However,
you may not obtain any code from anyone, regardless of whether or not they are in
the course (aside from code provided as a part of the course, in reference books,
or in standard code libraries).
You may not consult solutions from prior semesters of this course or from similar
courses at other universities or on the web.
You may discuss problems, general strategies, or algorithms with other
students in the course.
However, you may not collaborate on the implementation of
those strategies or algorithms with anyone outside your group
and each group must write all of their own code.
Specific programming rules for this assignment:
- 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 excludes the use of malloc,
calloc, free, realloc, sbrk, brk
or any variants of these calls in your code.
- 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.
- 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.
Evaluation
You will receive zero points if you break any of the rules or
your code is buggy and crashes the driver. 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.
- Performance (35 points). Two performance metrics will be
used to evaluate your solution:
- 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 (unachievable) ratio would be
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, which is a weighted sum
of the space utilization and throughput
P = wU + (1−w) min (1, T / Tlibc)
where U is your space utilization, T is your throughput,
and Tlibc is the estimated throughput of libc
malloc on your system on the default traces[1]. (The value for
Tlibc is a constant in the driver (15000 Kops/s) that
the TAs established when they configured the release). The
performance index favors space utilization over throughput, with a
default of w = 0.6.
Observing that both memory and CPU cycles are expensive system
resources, we adopt this formula to encourage 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. To
receive a good score, you must achieve a balance between utilization and
throughput.
- Style (10 points).
-
Your code should be decomposed into functions and use as few
global variables as possible.
-
Your code should begin with a header comment that describes the
structure of your free and allocated blocks, the organization of
the free list, and how your allocator manipulates the free
list. Each function should be preceeded by a header
comment that describes what the function does.
-
Each subroutine should have a header comment that describes
what it does and how it does it.
-
Your heap consistency checker mm_check should be
thorough and well documented.
You will be awarded 5 points for a good heap consistency checker and
5 points for good program structure and comments.
Hints
- Use the mdriver –f option. During
initial development, using tiny trace files will simplify debugging
and testing. We have included two such trace files
(short{1,2}-bal.rep) that you can use for initial debugging.
Other trace files are located in /u/cs252/labf20/malloclab/malloctraces/.
- 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 as 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 or gcc in-line functions. 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 difficult and sophisticated code you have
written so far in your career. So start early, and good luck!
“Trivia” Assignment
Before midnight on Thursday December 3rd, answer the following questions on blackboard.
-
Are you working alone or in a team of two?
If the latter, who is your partner?
-
After you have input your team information, what output do you get
when you run the following command?
mdriver -V -f short2-bal.rep
-
What is the value of ALIGNMENT defined in mm.c?
-
What does the mem_sbrk function do?
Turn In Instructions
The “trivia” assignment will be submitted via blackboard. The main assignment
will be submitted using the script /u/cs252/bin/TURN_IN.
/u/cs252/bin/TURN_IN .
You only need to turn in your completed mm.c. Watch the
blackboard for details, and for
any clarifications or revisions to the assignment.
Before running the TURNIN script, be sure that you have
- included your full name and email address in the comment at the top
of mm.c
- removed any extraneous print statements
- included any appropriate commentary on your code in a separate
README file or as C comments in mm.c