Due: December, 11, 11:59PM
Download the skeleton code for the project here.
This project will be done individually, as with the previous two projects. Your task in this assignment is to implement a mark and sweep garbage collector for the L3 virtual machine.
The skeleton code you are given for this assignment contains two elements:
The virtual machine contains two main components:
The virtual machine also contains basic infrastructure like a makefile and 4 tests you can use to check your GC implementation.
To start, unpack the skeleton code as you have done for the last assignments. Now, you will have the vm directory in l3/. Go there and run make:
$ cd vm/ $ make Use the following targets: - 'make vm' to use your mark-sweep GC - 'make test' to test the VM - 'make clean' to clean the VM $ make test rm -rf bin mkdir -p bin cc -std=c99 -g -Wall -O3 src/engine.c src/fail.c src/loader.c src/main.c src/memory.c -o bin/vm Queens test failed! Bignums test failed! Pascal test failed! Maze test failed!
You can also run the tests individually, since they're included in l3/examples::
$ bin/vm ../examples/queens.asm enter size (0 to exit)> 10 Error: no memory left (block of size 2 requested)
Your task is simple: make the tests pass by implementing a mark and sweep garbage collector instead of the no-GC memory system currently implemented in l3/vm/src/memory_nofree.c.
By the way, the challenge for the previous assignment was running maze for size 12 and seed 12. You can now see the exact code patterns that you were generating and tweak them. :)
In case you plan to develop using Eclipse, you will have to install the Eclipse C Development Tools (you can use a stand-alone installation of Eclipse or you can add CDT along with the Scala IDE in Eclipse).
To create the Eclipse project, follow these steps:
Okay, that's it with the intro. Now for real work.
We first breifly describe how the VM engine interacts with the memory manager and subsequently detail the implementation that is required for this assignement. When the virtual machine is started, the first call is to
memory_setup. This allows the memory manager to allocate the total memory (the amount can be changed using the
-m <size> command line option of the vm). The "total_size" parameter passed to this method indicates the total number of bytes needed. The method
memory_get_start returns a pointer to the beginning of this allocated memory. The total memory is used to store the program code and the heap.
The virtual machine then loads the program code into the memory. Afterwards, the method
memory_set_heap_start is called, indicating the first address that directly follows the program code. The memory starting from this address can be used by the memory manager to store its data structures and for allocating blocks in the heap:
memory_allocate is invoked when a heap block needs to be allocated.
value_tin the VM. Every entry in the array can contain arbitrary 32 bit information: tagged values, virtual addresses (explained below), block headers or registers (which are also stored in the heap)
value_t*, are pointers to the elements of the memory array.
value_t, are relative to the starting (physical) address of the memory and only point to individual words, so their last two bits are always 0. This satisfies the tagging requirement for references, which states that they should end in bits 00;
addr_p_to_vmethods in the file
engine.cconvert between the two address types. You have to copy the conversion functions in your gc (do not remove the static modifier from the existing ones, this will prevent inlining and make your virtual machine slower!)
engine_get_base_register) are physical addresses.
memory_allocatefunction is numbers of words that have to be allocated.
You are required to write a memory manager that implements the interfaces defined in the "memory.h" file and performs mark-sweep garbage collection. Below we provide an overview of the data structures that have to be implemented and also a few tips and tricks.
As discussed in the lectures, memory is allocated and freed in chucks of words referred to as blocks. Each block has a tag and a size which are passed as parameters to the "block-alloc" primitive. We refer to the starting address of a block as a "block pointer". Use the first word of a block to store the tag and size of the block (which are referred to as block headers). Therefore, to allocate a block of size n you will need n+1 words.
You GC must have a (singly-linked) free list containing all the free blocks. The second word of a free block can be used to store the address of the next element of the free list.
There is a slight trick with free lists. A free list entry contains at least 2 words. But the library allocates blocks of size 0 (and thus 1 word) that your GC can later free. This produces 1-word entries in the free list, which won't work. To overcome this, the easiest solution is to allocated at least two words, even for blocks of size 0.
The free list is used to allocate blocks. When having multiple free list entries, you should either pick the smallest one that fits the necessary size (best fit strategy) or the first one that fits (first fit strategy).
Instead of having a single free list, it is much more efficient to have several free lists, one per block size up to a maximum block size, plus one free list for the bigger blocks. That way, in many cases, no iteration is necessary to find a free block of a given size.
We suggest you write a first version of your GC with a single free list, and once it works, you update it to have 32 segregated free lists. As described below, only a GC using segregated free lists could get you the maximum number of points for this assignment.
You need to maintain a bitmap with one bit per valid heap address. This bitmap is used for two purposes:
(a) to determine if a value stored in the heap is a valid block pointer. Even though we use tagged values, it is possible that, during an arithmetic operation, an untagged value is left in one of the registers. Since the registers of our virtual machine are also stored in the heap, we may mistake an untagged value for a virtual address in the marking phase. So although it looks like a virtual address, the untagged value may point anywhere, including invalid locations and in the middle of blocks. In order to prevent the GC from following incorrect addresses, we mark the beginning of each block in the bitmap.
(b) during marking, we reset the bit to mark a block; therefore, at the end of the marking phase, the marked blocks will be those whose bit is not set in the bitmap. During the sweeping phase, you will also have to coalesce successive entries in the free list, so the heap memory does not get too fragmented. Also don't forget to update the bitmap so that allocated blocks have their bit set, and free blocks have their bit cleared.
To complete the above picture, the memory layout would actually look like this:
You can either use the
make test command to execute the tests automatically, or you can run each individual test by hand (good for debugging):
$ bin/vm ../examples/asm/queens.asm enter size (0 to exit)> 10 Error: no memory left (block of size 16 requested)
The expected sizes your program should run on are:
When debugging you may want to trigger a garbage collection early on. To do so, print the code size and adjust the memory with
bin/vm -m <bytes> such that the size is just above the code size + twice the bitmap size. This will let you trigger the garbage collection early, when the program hasn't yet allocated too much space, allowing easier debugging.
Implement a procedure to check the data strcuture invariants
For ease of debugging, it is recommended that you implement a procedure that traverses every block in the heap and checks for the correctness of the headers and the freelist. To traverse all blocks in the heap you can start from the first block and use the block size (stored in the header) to jump to the start of the next block and so on. Also, use "assert"s (defined in assert.h header file) in all places in the code where you think an invariant must hold. This will greatly help in debugging. Of couse, this is only recommended and not mandatory.
The assignment will be evaluated based on the correctness of the implementation, its memory efficiency and execution time.
Here are several points to keep in mind:
These criteria are can influence your grade, so please take them into account!