Add +RTS -vg flag for requesting some GC trace messages, outside DEBUG
[ghc-hetmet.git] / rts / sm / GC.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 1998-2006
4  *
5  * Generational garbage collector
6  *
7  * Documentation on the architecture of the Garbage Collector can be
8  * found in the online commentary:
9  * 
10  *   http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
11  *
12  * ---------------------------------------------------------------------------*/
13
14 #ifndef GC_H
15 #define GC_H
16
17 #include "OSThreads.h"
18
19 /* -----------------------------------------------------------------------------
20    General scheme
21    
22    ToDo: move this to the wiki when the implementation is done.
23
24    We're only going to try to parallelise the copying GC for now.  The
25    Plan is as follows.
26
27    Each thread has a gc_thread structure (see below) which holds its
28    thread-local data.  We'll keep a pointer to this in a thread-local
29    variable, or possibly in a register.
30
31    In the gc_thread structure is a step_workspace for each step.  The
32    primary purpose of the step_workspace is to hold evacuated objects;
33    when an object is evacuated, it is copied to the "todo" block in
34    the thread's workspace for the appropriate step.  When the todo
35    block is full, it is pushed to the global step->todos list, which
36    is protected by a lock.  (in fact we intervene a one-place buffer
37    here to reduce contention).
38
39    A thread repeatedly grabs a block of work from one of the
40    step->todos lists, scavenges it, and keeps the scavenged block on
41    its own ws->scavd_list (this is to avoid unnecessary contention
42    returning the completed buffers back to the step: we can just
43    collect them all later).
44
45    When there is no global work to do, we start scavenging the todo
46    blocks in the workspaces.  This is where the scan_bd field comes
47    in: we can scan the contents of the todo block, when we have
48    scavenged the contents of the todo block (up to todo_bd->free), we
49    don't want to move this block immediately to the scavd_list,
50    because it is probably only partially full.  So we remember that we
51    have scanned up to this point by saving the block in ws->scan_bd,
52    with the current scan pointer in ws->scan.  Later, when more
53    objects have been copied to this block, we can come back and scan
54    the rest.  When we visit this workspace again in the future,
55    scan_bd may still be the same as todo_bd, or it might be different:
56    if enough objects were copied into this block that it filled up,
57    then we will have allocated a new todo block, but *not* pushed the
58    old one to the step, because it is partially scanned.
59
60    The reason to leave scanning the todo blocks until last is that we
61    want to deal with full blocks as far as possible.
62    ------------------------------------------------------------------------- */
63
64
65 /* -----------------------------------------------------------------------------
66    Step Workspace
67   
68    A step workspace exists for each step for each GC thread. The GC
69    thread takes a block from the todos list of the step into the
70    scanbd and then scans it.  Objects referred to by those in the scan
71    block are copied into the todo or scavd blocks of the relevant step.
72   
73    ------------------------------------------------------------------------- */
74
75 typedef struct step_workspace_ {
76     step * stp;                 // the step for this workspace 
77     struct gc_thread_ * gct;    // the gc_thread that contains this workspace
78
79     // block that is currently being scanned
80     bdescr *     scan_bd;
81     StgPtr       scan;               // the scan pointer
82
83     // where objects to be scavenged go
84     bdescr *     todo_bd;
85     StgPtr       todo_free;            // free ptr for todo_bd
86     StgPtr       todo_lim;             // lim for todo_bd
87
88     bdescr *     buffer_todo_bd;     // buffer to reduce contention
89                                      // on the step's todos list
90
91     // where large objects to be scavenged go
92     bdescr *     todo_large_objects;
93
94     // Objects that need not be, or have already been, scavenged.
95     bdescr *     scavd_list;
96     lnat         n_scavd_blocks;     // count of blocks in this list
97
98 } step_workspace;
99
100 /* ----------------------------------------------------------------------------
101    GC thread object
102
103    Every GC thread has one of these. It contains all the step specific
104    workspaces and other GC thread loacl information. At some later
105    point it maybe useful to move this other into the TLS store of the
106    GC threads
107    ------------------------------------------------------------------------- */
108
109 typedef struct gc_thread_ {
110 #ifdef THREADED_RTS
111     OSThreadId id;                 // The OS thread that this struct belongs to
112     Mutex      wake_mutex;
113     Condition  wake_cond;          // So we can go to sleep between GCs
114     rtsBool    wakeup;
115     rtsBool    exit;
116 #endif
117     nat thread_index;              // a zero based index identifying the thread
118
119     bdescr * free_blocks;          // a buffer of free blocks for this thread
120                                    //  during GC without accessing the block
121                                    //   allocators spin lock. 
122
123     lnat gc_count;                 // number of gc's this thread has done
124
125     // --------------------
126     // evacuate flags
127
128     step *evac_step;               // Youngest generation that objects
129                                    // should be evacuated to in
130                                    // evacuate().  (Logically an
131                                    // argument to evacuate, but it's
132                                    // static a lot of the time so we
133                                    // optimise it into a per-thread
134                                    // variable).
135
136     rtsBool failed_to_evac;        // failure to evacuate an object typically 
137                                    // causes it to be recorded in the mutable 
138                                    // object list
139
140     rtsBool eager_promotion;       // forces promotion to the evac gen
141                                    // instead of the to-space
142                                    // corresponding to the object
143
144     lnat thunk_selector_depth;     // ummm.... not used as of now
145
146 #ifdef USE_PAPI
147     int papi_events;
148 #endif
149
150     // -------------------
151     // stats
152
153     lnat copied;
154     lnat any_work;
155     lnat scav_global_work;
156     lnat scav_local_work;
157
158     // -------------------
159     // workspaces
160
161     // array of workspaces, indexed by stp->abs_no.  This is placed
162     // directly at the end of the gc_thread structure so that we can get from
163     // the gc_thread pointer to a workspace using only pointer
164     // arithmetic, no memory access.  This happens in the inner loop
165     // of the GC, see Evac.c:alloc_for_copy().
166     step_workspace steps[];
167 } gc_thread;
168
169 extern nat N;
170 extern rtsBool major_gc;
171
172 extern gc_thread **gc_threads;
173 register gc_thread *gct __asm__("%rbx");
174 // extern gc_thread *gct;  // this thread's gct TODO: make thread-local
175
176 extern StgClosure* static_objects;
177 extern StgClosure* scavenged_static_objects;
178
179 extern bdescr *mark_stack_bdescr;
180 extern StgPtr *mark_stack;
181 extern StgPtr *mark_sp;
182 extern StgPtr *mark_splim;
183
184 extern rtsBool mark_stack_overflowed;
185 extern bdescr *oldgen_scan_bd;
186 extern StgPtr  oldgen_scan;
187
188 extern long copied;
189
190 #ifdef THREADED_RTS
191 extern SpinLock static_objects_sync;
192 #endif
193
194 #ifdef DEBUG
195 extern nat mutlist_MUTVARS, mutlist_MUTARRS, mutlist_MVARS, mutlist_OTHERS;
196 #endif
197
198 StgClosure * isAlive(StgClosure *p);
199
200 #endif /* GC_H */