62fa83317335f5415b22c12576788d5bd1adbc8b
[ghc.git] / includes / rts / Constants.h
1 /* ----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2009
4 *
5 * Constants
6 *
7 * NOTE: this information is used by both the compiler and the RTS.
8 * Some of it is tweakable, and some of it must be kept up to date
9 * with various other parts of the system.
10 *
11 * Constants which are derived automatically from other definitions in
12 * the system (eg. structure sizes) are generated into the file
13 * DerivedConstants.h by a C program (mkDerivedConstantsHdr).
14 *
15 * To understand the structure of the RTS headers, see the wiki:
16 * http://ghc.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
17 *
18 * -------------------------------------------------------------------------- */
19
20 #ifndef RTS_CONSTANTS_H
21 #define RTS_CONSTANTS_H
22
23 /* -----------------------------------------------------------------------------
24 Minimum closure sizes
25
26 This is the minimum number of words in the payload of a
27 heap-allocated closure, so that the closure has enough room to be
28 overwritten with a forwarding pointer during garbage collection.
29 -------------------------------------------------------------------------- */
30
31 #define MIN_PAYLOAD_SIZE 1
32
33 /* -----------------------------------------------------------------------------
34 Constants to do with specialised closure types.
35 -------------------------------------------------------------------------- */
36
37 /* We have some pre-compiled selector thunks defined in rts/StgStdThunks.hc.
38 * This constant defines the highest selectee index that we can replace with a
39 * reference to the pre-compiled code.
40 */
41
42 #define MAX_SPEC_SELECTEE_SIZE 15
43
44 /* Vector-apply thunks. These thunks just push their free variables
45 * on the stack and enter the first one. They're a bit like PAPs, but
46 * don't have a dynamic size. We've pre-compiled a few to save
47 * space.
48 */
49
50 #define MAX_SPEC_AP_SIZE 7
51
52 /* Specialised FUN/THUNK/CONSTR closure types */
53
54 #define MAX_SPEC_THUNK_SIZE 2
55 #define MAX_SPEC_FUN_SIZE 2
56 #define MAX_SPEC_CONSTR_SIZE 2
57
58 /* Range of built-in table of static small int-like and char-like closures.
59 *
60 * NB. This corresponds with the number of actual INTLIKE/CHARLIKE
61 * closures defined in rts/StgMiscClosures.cmm.
62 */
63 #define MAX_INTLIKE 16
64 #define MIN_INTLIKE (-16)
65
66 #define MAX_CHARLIKE 255
67 #define MIN_CHARLIKE 0
68
69 /* Each byte in the card table for an StgMutaArrPtrs covers
70 * (1<<MUT_ARR_PTRS_CARD_BITS) elements in the array. To find a good
71 * value for this, I used the benchmarks nofib/gc/hash,
72 * nofib/gc/graph, and nofib/gc/gc_bench.
73 */
74 #define MUT_ARR_PTRS_CARD_BITS 7
75
76 /* -----------------------------------------------------------------------------
77 STG Registers.
78
79 Note that in MachRegs.h we define how many of these registers are
80 *real* machine registers, and not just offsets in the Register Table.
81 -------------------------------------------------------------------------- */
82
83 #define MAX_VANILLA_REG 10
84 #define MAX_FLOAT_REG 6
85 #define MAX_DOUBLE_REG 6
86 #define MAX_LONG_REG 1
87 #define MAX_XMM_REG 6
88
89 /* -----------------------------------------------------------------------------
90 Semi-Tagging constants
91
92 Old Comments about this stuff:
93
94 Tags for indirection nodes and ``other'' (probably unevaluated) nodes;
95 normal-form values of algebraic data types will have tags 0, 1, ...
96
97 @INFO_IND_TAG@ is different from @INFO_OTHER_TAG@ just so we can count
98 how often we bang into indirection nodes; that's all. (WDP 95/11)
99
100 ToDo: find out if we need any of this.
101 -------------------------------------------------------------------------- */
102
103 #define INFO_OTHER_TAG (-1)
104 #define INFO_IND_TAG (-2)
105 #define INFO_FIRST_TAG 0
106
107 /* -----------------------------------------------------------------------------
108 How much C stack to reserve for local temporaries when in the STG
109 world. Used in StgCRun.c.
110 -------------------------------------------------------------------------- */
111
112 #define RESERVED_C_STACK_BYTES (2048 * SIZEOF_LONG)
113
114 /* -----------------------------------------------------------------------------
115 How large is the stack frame saved by StgRun?
116 world. Used in StgCRun.c.
117 -------------------------------------------------------------------------- */
118 #if defined(x86_64_HOST_ARCH)
119 # if defined(mingw32_HOST_OS)
120 /* 8 larger than necessary to make the alignment right*/
121 # define STG_RUN_STACK_FRAME_SIZE 80
122 # else
123 # define STG_RUN_STACK_FRAME_SIZE 48
124 # endif
125 #endif
126
127
128 /* -----------------------------------------------------------------------------
129 How much Haskell stack space to reserve for the saving of registers
130 etc. in the case of a stack/heap overflow.
131
132 This must be large enough to accommodate the largest stack frame
133 pushed in one of the heap check fragments in HeapStackCheck.hc
134 (ie. currently the generic heap checks - 3 words for StgRetDyn,
135 18 words for the saved registers, see StgMacros.h).
136 -------------------------------------------------------------------------- */
137
138 #define RESERVED_STACK_WORDS 21
139
140 /* -----------------------------------------------------------------------------
141 The limit on the size of the stack check performed when we enter an
142 AP_STACK, in words. See raiseAsync() and bug #1466.
143 -------------------------------------------------------------------------- */
144
145 #define AP_STACK_SPLIM 1024
146
147 /* -----------------------------------------------------------------------------
148 Storage manager constants
149 -------------------------------------------------------------------------- */
150
151 /* The size of a block (2^BLOCK_SHIFT bytes) */
152 #define BLOCK_SHIFT 12
153
154 /* The size of a megablock (2^MBLOCK_SHIFT bytes) */
155 #define MBLOCK_SHIFT 20
156
157 /* -----------------------------------------------------------------------------
158 Bitmap/size fields (used in info tables)
159 -------------------------------------------------------------------------- */
160
161 /* In a 32-bit bitmap field, we use 5 bits for the size, and 27 bits
162 * for the bitmap. If the bitmap requires more than 27 bits, then we
163 * store it in a separate array, and leave a pointer in the bitmap
164 * field. On a 64-bit machine, the sizes are extended accordingly.
165 */
166 #if SIZEOF_VOID_P == 4
167 #define BITMAP_SIZE_MASK 0x1f
168 #define BITMAP_BITS_SHIFT 5
169 #elif SIZEOF_VOID_P == 8
170 #define BITMAP_SIZE_MASK 0x3f
171 #define BITMAP_BITS_SHIFT 6
172 #else
173 #error unknown SIZEOF_VOID_P
174 #endif
175
176 /* -----------------------------------------------------------------------------
177 Lag/Drag/Void constants
178 -------------------------------------------------------------------------- */
179
180 /*
181 An LDV word is divided into 3 parts: state bits (LDV_STATE_MASK), creation
182 time bits (LDV_CREATE_MASK), and last use time bits (LDV_LAST_MASK).
183 */
184 #if SIZEOF_VOID_P == 8
185 #define LDV_SHIFT 30
186 #define LDV_STATE_MASK 0x1000000000000000
187 #define LDV_CREATE_MASK 0x0FFFFFFFC0000000
188 #define LDV_LAST_MASK 0x000000003FFFFFFF
189 #define LDV_STATE_CREATE 0x0000000000000000
190 #define LDV_STATE_USE 0x1000000000000000
191 #else
192 #define LDV_SHIFT 15
193 #define LDV_STATE_MASK 0x40000000
194 #define LDV_CREATE_MASK 0x3FFF8000
195 #define LDV_LAST_MASK 0x00007FFF
196 #define LDV_STATE_CREATE 0x00000000
197 #define LDV_STATE_USE 0x40000000
198 #endif /* SIZEOF_VOID_P */
199
200 /* -----------------------------------------------------------------------------
201 TSO related constants
202 -------------------------------------------------------------------------- */
203
204 /*
205 * Constants for the what_next field of a TSO, which indicates how it
206 * is to be run.
207 */
208 #define ThreadRunGHC 1 /* return to address on top of stack */
209 #define ThreadInterpret 2 /* interpret this thread */
210 #define ThreadKilled 3 /* thread has died, don't run it */
211 #define ThreadComplete 4 /* thread has finished */
212
213 /*
214 * Constants for the why_blocked field of a TSO
215 * NB. keep these in sync with GHC/Conc/Sync.hs: threadStatus
216 */
217 #define NotBlocked 0
218 #define BlockedOnMVar 1
219 #define BlockedOnMVarRead 14 /* TODO: renumber me, see #9003 */
220 #define BlockedOnBlackHole 2
221 #define BlockedOnRead 3
222 #define BlockedOnWrite 4
223 #define BlockedOnDelay 5
224 #define BlockedOnSTM 6
225
226 /* Win32 only: */
227 #define BlockedOnDoProc 7
228
229 /* Only relevant for THREADED_RTS: */
230 #define BlockedOnCCall 10
231 #define BlockedOnCCall_Interruptible 11
232 /* same as above but permit killing the worker thread */
233
234 /* Involved in a message sent to tso->msg_cap */
235 #define BlockedOnMsgThrowTo 12
236
237 /* The thread is not on any run queues, but can be woken up
238 by tryWakeupThread() */
239 #define ThreadMigrating 13
240
241 /* WARNING WARNING top number is BlockedOnMVarRead 14, not 13!! */
242
243 /*
244 * These constants are returned to the scheduler by a thread that has
245 * stopped for one reason or another. See typedef StgThreadReturnCode
246 * in TSO.h.
247 */
248 #define HeapOverflow 1 /* might also be StackOverflow */
249 #define StackOverflow 2
250 #define ThreadYielding 3
251 #define ThreadBlocked 4
252 #define ThreadFinished 5
253
254 /*
255 * Flags for the tso->flags field.
256 */
257
258 /*
259 * TSO_LOCKED is set when a TSO is locked to a particular Capability.
260 */
261 #define TSO_LOCKED 2
262
263 /*
264 * TSO_BLOCKEX: the TSO is blocking exceptions
265 *
266 * TSO_INTERRUPTIBLE: the TSO can be interrupted if it blocks
267 * interruptibly (eg. with BlockedOnMVar).
268 *
269 * TSO_STOPPED_ON_BREAKPOINT: the thread is currently stopped in a breakpoint
270 */
271 #define TSO_BLOCKEX 4
272 #define TSO_INTERRUPTIBLE 8
273 #define TSO_STOPPED_ON_BREAKPOINT 16
274
275 /*
276 * Used by the sanity checker to check whether TSOs are on the correct
277 * mutable list.
278 */
279 #define TSO_MARKED 64
280
281 /*
282 * Used to communicate between stackSqueeze() and
283 * threadStackOverflow() that a thread's stack was squeezed and the
284 * stack may not need to be expanded.
285 */
286 #define TSO_SQUEEZED 128
287
288 /*
289 * Enables the AllocationLimitExceeded exception when the thread's
290 * allocation limit goes negative.
291 */
292 #define TSO_ALLOC_LIMIT 256
293
294 /*
295 * The number of times we spin in a spin lock before yielding (see
296 * #3758). To tune this value, use the benchmark in #3758: run the
297 * server with -N2 and the client both on a dual-core. Also make sure
298 * that the chosen value doesn't slow down any of the parallel
299 * benchmarks in nofib/parallel.
300 */
301 #define SPIN_COUNT 1000
302
303 /* -----------------------------------------------------------------------------
304 Spare workers per Capability in the threaded RTS
305
306 No more than MAX_SPARE_WORKERS will be kept in the thread pool
307 associated with each Capability.
308 -------------------------------------------------------------------------- */
309
310 #define MAX_SPARE_WORKERS 6
311
312 /*
313 * The maximum number of NUMA nodes we support. This is a fixed limit so that
314 * we can have static arrays of this size in the RTS for speed.
315 */
316 #define MAX_NUMA_NODES 16
317
318 #endif /* RTS_CONSTANTS_H */