[t] Refactor some namespace pmc tests to use throws_like
[parrot.git] / docs / memory_internals.pod
blob034cb21da59b69336f5fbd8637d04fa830158b51
1 # Copyright (C) 2001-2009, Parrot Foundation.
2 # $Id$
4 =head1 NAME
6 docs/memory_internals.pod - Memory Internals
8 =head1 ABSTRACT
10 This document tries to explain the internals of the Parrot memory subsystem,
11 and the data structures related to memory management and garbage collection.
13 =head1 OVERVIEW
15 All allocated items are managed in memory pools. Memory pools hold collections
16 of similar items, items of the same data type and usually of the same size.
17 These can be roughly divided into two kinds: pools holding fixed sized items
18 and pools holding variable sized items.
20 A pool is organized as a linked list of big chunks, called I<Arenas>. Each
21 Arena holds and manages a set number of items of a single size and shape.
23 =head2 Abbreviations used here
25 =over 4
27 =item * GC
29 Garbage collector. A garbage collector is a system that finds memory objects
30 that are not being used by the system, and frees them automatically. Garbage
31 collection is broken into two distinct phases: the I<mark phase> that searches
32 for unreferenced objects, and the I<sweep phase> that frees all dead objects.
34 =item * DOD
36 Dead object detection, a deprecated term that refers to the I<mark phase>
37 of the garbage collector.
39 =back
41 By scanning through all the interpreter's registers, stacks and the processor
42 stack, all objects that are detected by the garbage collector are marked as
43 alive. Objects in all pools not alive at the end of the mark phase are
44 considered dead and are collected.
46 See F<docs/pdds/pdd09_gc.pod> for details about the garbage collector system.
48 =head1 Top down: the interpreter
50 A overall layout of the interpreter's memory management looks like so:
52     typedef struct Interp {
53         ...
54         struct Arenas *arena_base;
55         ...
56     } Interp;
58 All object-like things that get allocated during the execution of parrot
59 bytecode are managed from the C<arena_base> member of the interpreter
60 structure.
62 =head1 Arenas
64 C<struct Arenas> holds pointers to a variety of different kinds of managed
65 memory. A simplification looks similar to this:
67     typedef struct Arenas {
68         struct Memory_Pool *memory_pool;
69         ...
70         struct Small_Object_Pool * header_pool;
71         struct Small_Object_Pool ** sized_pools;
72     } Arenas;
74 C<memory_pool> and C<header_pool> are variable and fixed sized pool pointers
75 respectively. These are just two examples, there are other C<Memory_Pool>s
76 and C<Small_Object_Pool>s in the Parrot system. Pools of type
77 C<struct Memory_Pool> are for variable-size objects, such as constant string
78 buffers. Pools of type C<struct Small_Object_Pool> are for fixed-size objects
79 such as headers or PMCs.
81 =head1 Fixed sized items
83 Fixed-size items are either objects by themselves, like a C<PMC>, or are
84 header structures of variable-sized objects like C<STRING>.  The general
85 object of this kind is a buffer-like object, which consists of a
86 C<Buffer> or a C<PObj> header at the beginning connected to a variable-sized
87 memory block for data.
89 Buffer-like objects of the same size are maintained in the list of
90 C<sized_pools>, which manage objects of the same size in one slot.
92 Every garbage collector must supply 4 function pointers: an initialization
93 routine, a de-initialization routine, a function to initialize a new memory
94 pool, and a function to initiate a garbage collection run. Every pool also
95 requires the garbage collector to supply 4 additional function pointers: a
96 function to allocate a new arena for the pool, a function to find more items
97 (possibly through running the garbage collector, or allocating objects
98 otherwise), a function to add an unused item to the free list, and a function
99 to retrieve a new free item from the pool.
101 Every pool maintains a list of unused items within the pool, called the
102 C<free_list> When there is need for a new object it is taken off the free
103 list, and when it stops being used, it will be put back on the free list
104 again. GC mark determines which objects are not being used, and GC sweep
105 adds those unused items back to the free list.
107 If the free list is empty then a GC run could be initiated. This may be
108 able to find some dead objects and free them up for immediate reuse.
109 However, if there are no objects that can be immediately recycled, a new
110 arena will be allocated.
112 Arenas, which contain the fixed-sized items used by Parrot, are never returned
113 to the operating system, even if they are empty. In the future, this behavior
114 may change, however. Variable-sized buffers may be returned when they are no
115 longer being used.
117 =head2 General structure of a buffer-like item
119     struct parrot_object_t {
120         unsigned flags;
121         ...
122     } PObj;
124 The flags field may contain a series of flags which indicate the type, status,
125 configuration, and special requirements of each item. C<Buffer>s, C<PMC>s, and
126 C<PObj>s all have this basic field in common.
128 C<PMC>s and C<Buffer>s each have an additional field which contain a pointer
129 to a block of data.
131 =head2 GC-related PObj flags
133 Only three flags need to be checked during a mark run: C<PObj_live_FLAG>,
134 C<PObj_on_free_list_FLAG>, and C<PObj_is_special_PMC_FLAG>.  Normally these
135 flags are stored in C<PObj-E<gt>flags>, meaning that each PMC must be accessed
136 during the mark run.
138 An alternative approach is to store the GC-Flags together somewhere, such as
139 in the individual arenas, as a packed array of bits. This approach, called
140 cardmarking, should be indicated by defining the preprocessor variable
141 C<ARENA_GC_FLAGS> to 1.
143 {{ ARENA_GC_FLAGS (nee ARENA_DOD_FLAGS) seems to have been deprecated or
144 changed without concomitant update to this document.  Someone should figure out
145 what this macro used to be and whether it's still relevant.  -cotto }}
147 F<pobj.h> provides macros to facilitate referencing individual object flags:
148 C<gc_flag_SET>, C<gc_flag_CLEAR> and C<gc_flag_TEST>. They make up a portable
149 way of manipulating the GC-relevant object flags.
151 =head1 Variable sized items
153 Variable-sized items do not exist by themselves, they are always preceded by
154 a buffer structure that contains information about them. These buffer
155 structures are described above, and the C<UnionVal cache> item typically
156 points to the memory block that contains the data. The variable-sized data
157 items are managed in two different pools: the C<memory_pool>, which contains
158 a general mish-mash of data types, and the C<constant_string_pool> which
159 contains immutable string buffers used by programs running on Parrot.
161 Here, different memory allocation schemes jump in:
163 =head2 Copying GC
165 A C<memory_pool> gets allocated in big blocks, namely a C<Memory_Block>.
166 When some part is needed, e.g. to store a string, this part is carved out of
167 the memory block, until this block is used up. If no more space is available in
168 this memory block, a special copying garbage collection (GC) is started.
169 This copies all living items of all used memory blocks into one new block,
170 which holds thereafter only used items tightly packed together.
172 The old memory blocks, containing sparse unused parts and used parts already
173 copied to the new place, are then unused altogether and get C<free()>ed
174 thereafter. When GC doesn't provide enough free space needed for a new item,
175 a new block is added to the memory pool.
177 This also implies that buffers are moved around during their life. Users of
178 these buffers are therefore not allowed to hold pointers to buffers over pieces
179 of code that might trigger a GC run, like C<Parrot_allocate()> or
180 C<Parrot_str_compare()>.
182 =head2 Defragmenting allocator
184 An alternative to the above is to use a memory allocator, which is as fast as
185 the above and does reuse C<free()>ed items in a memory-conserving way.
186 Actually, the malloc implementations in some systems' F<libc> efficiently
187 provide this, such as the glibc malloc based on Doug Lea's allocator.
189 Using this allocator, all variable sized items are just allocated via a plain
190 C<malloc()> call, or resized with C<realloc()>, and after they lose their
191 existence (ie when the mark phase detects that the managing buffer header is no
192 longer in use) they are C<free()>ed. That's all. The underlying allocator
193 collects these pieces, coalesces them if possible to form bigger pieces, and
194 then puts them on free lists, sorted by size.  Eventually, when a new
195 allocation request arrives, it may give them back to Parrot.
197 So here, the variable length C<memory_pool> is unused. You can consider this
198 pool to live inside the allocator. Buffers allocated this way don't move
199 around, except when reallocated, of course.
201 The F<Configure.pl> option C<--gc> allows one to use either method.
203 =head2 Buffer_Tail and COW
205 Both implementations have the same problem to solve: C<STRING>s that get
206 copied, or parts of strings as the result of a substr() call, do not allocate
207 new space in the memory pool to hold this string or part of string. They just
208 use a C<new_string_header()> and set up a pointer (C<strstart>) pointing
209 somewhere into the original string and remember the used length there in
210 C<bufused>.
212 This is all OK, as long as the original and the lazy copy of the string are not
213 modified. So that's well-known and called COW (copy on write). We don't have
214 to make a copy of the buffer until one of the copies tries to modify it. In
215 practice, strings often get copied from place to place but do not often get
216 modified, so this optimization saves us a lot of time.
218 Now, during GC (or freeing buffers) the problem arises: to whom does this
219 string belong? You shouldn't copy the same string to different places, thus
220 rendering COW to a noop, nor are you allowed to free this same string more then
221 once. Both allocation schemes therefore use a part of the allocated string to
222 do this bookkeeping.
224 Copying GC uses a C<Buffer_Tail> after the end of the actual variable length
225 string and marks such COW strings with C<TAIL_moved> and stores the new address
226 in the buffer header, so other users of this string can be updated to reuse the
227 same string (RT#47764 one or all other users?).
229 The C<malloc()>/C<free()> approach stores a refcount at C<bufstart>. During the
230 mark phase all dead users increment the refcount, living users set it to an
231 huge value.  When freeing the buffer, the string is only freed if the refcount
232 reaches zero.
234 =head1 Simplified Figure
236                              +--------+
237       +------------------<---| Arenas |<-----------+
238       |                      +--------+-->--+      |
239       |                                     |      |
240       |         +------+    +-----------+   |  +=============+
241       |         | S0   |<---| Registers |<--)--| Interpreter |
242       |         +------+    +-----------+   |  +=============+
243       |     +---| S1   |                    |
244       |     |   +------+                    +----------+
245  +-------+  |                                          |
246  | Blk 1 |--)-->+----------+    +--------------+   +---------+
247  +-------+  |   | Buffer 1 |    | OnestringAno |   | Block 1 |
248  | Blk 2 |  |   +----------+    | therstring.. |   +---------+
249  +-------+  |   | Buffer 2 |    | ..String...  |<--| Block 2 |
250  | .     |  |   +----------+    +--------------+   +---------+
251  +-------+  |   | ...      |        ^    ^         | ...     |
252  Small Obj  |   +----------+        |    |         +---------+
253  Pool       +-->| Buffer N |--------+----+         Memory Pool
254                 +----------+
255                  Buffer          Memory Block
257 Now consider that the I<Interpreter> shall be a C<PMC> living in C<Buffer X>
258 of the underlying interpreter and is currently running F<perldoc
259 docs/memory_internals.pod>, and then redo this figure, with all the blanks
260 filled in.
262 =head1 FILES
264 mark_sweep.[ch], src/gc/pools.c, resources.[ch], res_lea.c, src/gc/api.c,
265 string.[ch], pobj.h. Other garbage collector implementations may use separate
266 files as well.
268 =head1 BUGS
270 To minimize this bugs section - please patch this document and keep it up to
271 date - thanks.
273 =head1 AUTHOR
275 Leopold Tötsch C<lt@toetsch.at>
277 =head1 VERSION
279 0.1.1 June 2008