Revert "Default to infinite stack size (#8189)"
[ghc.git] / includes / rts / storage / GC.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2004
4 *
5 * External Storage Manger Interface
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #ifndef RTS_STORAGE_GC_H
10 #define RTS_STORAGE_GC_H
11
12 #include <stddef.h>
13 #include "rts/OSThreads.h"
14
15 /* -----------------------------------------------------------------------------
16 * Generational GC
17 *
18 * We support an arbitrary number of generations, with an arbitrary number
19 * of steps per generation. Notes (in no particular order):
20 *
21 * - all generations except the oldest should have the same
22 * number of steps. Multiple steps gives objects a decent
23 * chance to age before being promoted, and helps ensure that
24 * we don't end up with too many thunks being updated in older
25 * generations.
26 *
27 * - the oldest generation has one step. There's no point in aging
28 * objects in the oldest generation.
29 *
30 * - generation 0, step 0 (G0S0) is the allocation area. It is given
31 * a fixed set of blocks during initialisation, and these blocks
32 * normally stay in G0S0. In parallel execution, each
33 * Capability has its own nursery.
34 *
35 * - during garbage collection, each step which is an evacuation
36 * destination (i.e. all steps except G0S0) is allocated a to-space.
37 * evacuated objects are allocated into the step's to-space until
38 * GC is finished, when the original step's contents may be freed
39 * and replaced by the to-space.
40 *
41 * - the mutable-list is per-generation (not per-step). G0 doesn't
42 * have one (since every garbage collection collects at least G0).
43 *
44 * - block descriptors contain pointers to both the step and the
45 * generation that the block belongs to, for convenience.
46 *
47 * - static objects are stored in per-generation lists. See GC.c for
48 * details of how we collect CAFs in the generational scheme.
49 *
50 * - large objects are per-step, and are promoted in the same way
51 * as small objects, except that we may allocate large objects into
52 * generation 1 initially.
53 *
54 * ------------------------------------------------------------------------- */
55
56 // A count of blocks needs to store anything up to the size of memory
57 // divided by the block size. The safest thing is therefore to use a
58 // type that can store the full range of memory addresses,
59 // ie. StgWord. Note that we have had some tricky int overflows in a
60 // couple of cases caused by using ints rather than longs (e.g. #5086)
61
62 typedef StgWord memcount;
63
64 typedef struct nursery_ {
65 bdescr * blocks;
66 memcount n_blocks;
67 } nursery;
68
69 typedef struct generation_ {
70 nat no; // generation number
71
72 bdescr * blocks; // blocks in this gen
73 memcount n_blocks; // number of blocks
74 memcount n_words; // number of used words
75
76 bdescr * large_objects; // large objects (doubly linked)
77 memcount n_large_blocks; // no. of blocks used by large objs
78 memcount n_large_words; // no. of words used by large objs
79 memcount n_new_large_words; // words of new large objects
80 // (for doYouWantToGC())
81
82 memcount max_blocks; // max blocks
83
84 StgTSO * threads; // threads in this gen
85 // linked via global_link
86 StgWeak * weak_ptr_list; // weak pointers in this gen
87
88 struct generation_ *to; // destination gen for live objects
89
90 // stats information
91 nat collections;
92 nat par_collections;
93 nat failed_promotions;
94
95 // ------------------------------------
96 // Fields below are used during GC only
97
98 #if defined(THREADED_RTS)
99 char pad[128]; // make sure the following is
100 // on a separate cache line.
101 SpinLock sync; // lock for large_objects
102 // and scavenged_large_objects
103 #endif
104
105 int mark; // mark (not copy)? (old gen only)
106 int compact; // compact (not sweep)? (old gen only)
107
108 // During GC, if we are collecting this gen, blocks and n_blocks
109 // are copied into the following two fields. After GC, these blocks
110 // are freed.
111 bdescr * old_blocks; // bdescr of first from-space block
112 memcount n_old_blocks; // number of blocks in from-space
113 memcount live_estimate; // for sweeping: estimate of live data
114
115 bdescr * scavenged_large_objects; // live large objs after GC (d-link)
116 memcount n_scavenged_large_blocks; // size (not count) of above
117
118 bdescr * bitmap; // bitmap for compacting collection
119
120 StgTSO * old_threads;
121 StgWeak * old_weak_ptr_list;
122 } generation;
123
124 extern generation * generations;
125 extern generation * g0;
126 extern generation * oldest_gen;
127
128 /* -----------------------------------------------------------------------------
129 Generic allocation
130
131 StgPtr allocate(Capability *cap, W_ n)
132 Allocates memory from the nursery in
133 the current Capability. This can be
134 done without taking a global lock,
135 unlike allocate().
136
137 StgPtr allocatePinned(Capability *cap, W_ n)
138 Allocates a chunk of contiguous store
139 n words long, which is at a fixed
140 address (won't be moved by GC).
141 Returns a pointer to the first word.
142 Always succeeds.
143
144 NOTE: the GC can't in general handle
145 pinned objects, so allocatePinned()
146 can only be used for ByteArrays at the
147 moment.
148
149 Don't forget to TICK_ALLOC_XXX(...)
150 after calling allocate or
151 allocatePinned, for the
152 benefit of the ticky-ticky profiler.
153
154 -------------------------------------------------------------------------- */
155
156 StgPtr allocate ( Capability *cap, W_ n );
157 StgPtr allocatePinned ( Capability *cap, W_ n );
158
159 /* memory allocator for executable memory */
160 typedef void* AdjustorWritable;
161 typedef void* AdjustorExecutable;
162
163 AdjustorWritable allocateExec(W_ len, AdjustorExecutable *exec_addr);
164 void freeExec (AdjustorExecutable p);
165
166 // Used by GC checks in external .cmm code:
167 extern W_ large_alloc_lim;
168
169 /* -----------------------------------------------------------------------------
170 Performing Garbage Collection
171 -------------------------------------------------------------------------- */
172
173 void performGC(void);
174 void performMajorGC(void);
175
176 /* -----------------------------------------------------------------------------
177 The CAF table - used to let us revert CAFs in GHCi
178 -------------------------------------------------------------------------- */
179
180 StgWord newCAF (StgRegTable *reg, StgClosure *caf, StgClosure *bh);
181 StgWord newDynCAF (StgRegTable *reg, StgClosure *caf, StgClosure *bh);
182 void revertCAFs (void);
183
184 // Request that all CAFs are retained indefinitely.
185 void setKeepCAFs (void);
186
187 /* -----------------------------------------------------------------------------
188 Stats
189 -------------------------------------------------------------------------- */
190
191 typedef struct _GCStats {
192 StgWord64 bytes_allocated;
193 StgWord64 num_gcs;
194 StgWord64 num_byte_usage_samples;
195 StgWord64 max_bytes_used;
196 StgWord64 cumulative_bytes_used;
197 StgWord64 bytes_copied;
198 StgWord64 current_bytes_used;
199 StgWord64 current_bytes_slop;
200 StgWord64 max_bytes_slop;
201 StgWord64 peak_megabytes_allocated;
202 StgWord64 par_tot_bytes_copied;
203 StgWord64 par_max_bytes_copied;
204 StgDouble mutator_cpu_seconds;
205 StgDouble mutator_wall_seconds;
206 StgDouble gc_cpu_seconds;
207 StgDouble gc_wall_seconds;
208 StgDouble cpu_seconds;
209 StgDouble wall_seconds;
210 } GCStats;
211 void getGCStats (GCStats *s);
212 rtsBool getGCStatsEnabled (void);
213
214 // These don't change over execution, so do them elsewhere
215 // StgDouble init_cpu_seconds;
216 // StgDouble init_wall_seconds;
217
218 typedef struct _ParGCStats {
219 StgWord64 tot_copied;
220 StgWord64 max_copied;
221 } ParGCStats;
222 void getParGCStats (ParGCStats *s);
223
224 /*
225 typedef struct _TaskStats {
226 StgWord64 mut_time;
227 StgWord64 mut_etime;
228 StgWord64 gc_time;
229 StgWord64 gc_etime;
230 } TaskStats;
231 // would need to allocate arbitrarily large amount of memory
232 // because it's a linked list of results
233 void getTaskStats (TaskStats **s);
234 // Need to stuff SparkCounters in a public header file...
235 void getSparkStats (SparkCounters *s);
236 */
237
238 // Returns the total number of bytes allocated since the start of the program.
239 HsInt64 getAllocations (void);
240
241 /* -----------------------------------------------------------------------------
242 This is the write barrier for MUT_VARs, a.k.a. IORefs. A
243 MUT_VAR_CLEAN object is not on the mutable list; a MUT_VAR_DIRTY
244 is. When written to, a MUT_VAR_CLEAN turns into a MUT_VAR_DIRTY
245 and is put on the mutable list.
246 -------------------------------------------------------------------------- */
247
248 void dirty_MUT_VAR(StgRegTable *reg, StgClosure *p);
249
250 /* set to disable CAF garbage collection in GHCi. */
251 /* (needed when dynamic libraries are used). */
252 extern rtsBool keepCAFs;
253
254 INLINE_HEADER void initBdescr(bdescr *bd, generation *gen, generation *dest)
255 {
256 bd->gen = gen;
257 bd->gen_no = gen->no;
258 bd->dest_no = dest->no;
259 }
260
261 #endif /* RTS_STORAGE_GC_H */