Changes between Version 8 and Version 9 of Commentary/Rts/Storage


Ignore:
Timestamp:
Oct 20, 2006 9:04:42 AM (7 years ago)
Author:
simonmar
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Commentary/Rts/Storage

    v8 v9  
    1313 * [wiki:Commentary/Rts/Storage/GC The Garbage Collector] 
    1414 * [wiki:Commentary/Rts/Storage/CAFs Garbage Collecting CAFs] 
    15  
    16 == The Block Allocator == 
    17  
    18 Source: [[GhcFile(includes/Block.h)]], [[GhcFile(rts/BlockAlloc.h)]], [[GhcFile(rts/BlockAlloc.c)]], [[GhcFile(rts/MBlock.h)]], [[GhcFile(rts/MBlock.c)]]. 
    19  
    20 The block allocator is where the storage manager derives much of its flexibilty.  Rather than keep our heap in a single contiguous region of memory, or one contiguous region per generation, we manage linked lists of memory blocks.  Managing contiguous regions is difficult, especially when you want to change the size of some of the areas.  A block-structured storage arrangement has several advantages: 
    21  
    22  * resizing areas of memory is easy: just chain more blocks onto the list. 
    23  
    24  * managing large objects without copying is easy: allocate each one a complete block, and use the block linkage to 
    25    chain them together. 
    26  
    27  * free memory can be recycled faster, because a block is a block. 
    28  
    29 The concept relies on the property that most data objects are significantly smaller than a block, and only rarely do we need to allocate objects that approach or exceed the size of a block. 
    30  
    31 === Structure of blocks === 
    32  
    33 We want to allocate memory in units of a small block (around 4k, say).  Furthermore, we want each block to have an associated small structure called a ''block descriptor'', which contains information about the block: its link field, which generation it belongs to, and so on.  This is similar to the well-known "BiBOP" (Big Bag of Pages) technique, where objects with similar tags are collected together on a page so as to avoid needing to store an individual tag with each object. 
    34  
    35 We want a function `Bdescr(p)`, that, given an arbitrary pointer into a block, returns the address of the block descriptor that corresponds to the block containing that pointer. 
    36  
    37 There are two options: 
    38  
    39  * Put the block descriptor at the start of the block.  `Bdescr(p) = p & ~BLOCK_SIZE`.  This option has problems if 
    40    we need to allocate a contiguous region larger than a single block (GHC does this occasionally when allocating 
    41    a large number of objects in one go). 
    42  
    43  * Allocate memory in larger units (a ''megablock''), divide the megablock into blocks, and put all the block 
    44    descriptors at the beginning.  The megablock is aligned, so that the address of the block descriptor for 
    45    a block is a simple function of its address.  The 'Bdescr' function is more complicated than the first 
    46    method, but it is easier to allocate contiguous regions (unless the contiguous region is larger than 
    47    a megablock...). 
    48  
    49 We adopt the second approach.  The following diagram shows a megablock: 
    50  
    51 [[Image(sm-block.png)]] 
    52  
    53 We currently have megablocks of 1Mb in size (m = 20) with blocks of 4k in size (k = 12), and these sizes are easy to change  ([[GhcFile(includes/Constants.h)]]).   
    54  
    55 Block descriptors are currently 32 or 64 bytes depending on the word size (d = 5 or 6).  The block descriptor itself is  
    56 the structure `bdescr` defined in [[GhcFile(includes/Block.h)]], and that file also defines the `Bdescr()` macro. 
    57  
    58 The block allocator has a the following structure: 
    59  
    60  * At the bottom, talking to the OS, is the megablock allocator ([[GhcFile(rts/MBlock.c)]], [[GhcFile(rts/MBlock.h)]]). 
    61    It is responsible for delivering megablocks, correctly aligned, to the upper layers.  It is also responsible for 
    62    implementing `HEAP_ALLOCED()`: the predicate that tests whether a pointer points to dynamically allocated memory 
    63    or not.  This is implemented as a simple bitmap lookup on a 32-bit machine, and something more complex on 
    64    64-bit addressed machines.  See [[GhcFile(rts/MBlock.h)]] for details. 
    65    [[br]][[br]] 
    66    Currently, megablocks are never freed back to the OS, except at the end of the program.  This is a potential 
    67    improvement that could be made. 
    68  
    69  * Sitting on top of the megablock allocator is the block layer ([[GhcFile(includes/Block.h)]], [[GhcFile(rts/BlockAlloc.c)]]). 
    70    This layer is responsible for providing: 
    71 {{{ 
    72    void *allocGroup(int) 
    73    void freeGroup(void *) 
    74 }}} 
    75   These functions allocate and deallocate a block ''group'': a contiguous sequence of blocks (the degenerate, and common, case 
    76   is a single block).  The block allocator is responsible for keeping track of free blocks.  Currently it does this by 
    77   maintaining an ordered (by address) list of free blocks, with contiguous blocks coallesced.  However this is certanly 
    78   not optimal, and has been shown to be a bottleneck in certain cases - improving this allocation scheme would be good. 
    79  
    80 == The Garbage Collector == 
    81  
    82 === Copying Collection === 
    83  
    84 === Compacting Collection ===