[project @ 2000-01-05 11:14:06 by rrt]
[ghc-hetmet.git] / ghc / docs / users_guide / runtime_control.sgml
1 <Sect1 id="runtime-control">
2 <Title>Running a compiled program
3 </Title>
4
5 <Para>
6 <IndexTerm><Primary>runtime control of Haskell programs</Primary></IndexTerm>
7 <IndexTerm><Primary>running, compiled program</Primary></IndexTerm>
8 <IndexTerm><Primary>RTS options</Primary></IndexTerm>
9 </Para>
10
11 <Para>
12 To make an executable program, the GHC system compiles your code and
13 then links it with a non-trivial runtime system (RTS), which handles
14 storage management, profiling, etc.
15 </Para>
16
17 <Para>
18 You have some control over the behaviour of the RTS, by giving special
19 command-line arguments to your program.
20 </Para>
21
22 <Para>
23 When your Haskell program starts up, its RTS extracts command-line
24 arguments bracketed between <Literal>+RTS</Literal><IndexTerm><Primary>+RTS option</Primary></IndexTerm> and
25 <Literal>-RTS</Literal><IndexTerm><Primary>-RTS option</Primary></IndexTerm> as its own.  For example:
26 </Para>
27
28 <Para>
29
30 <Screen>
31 % ./a.out -f +RTS -p -S -RTS -h foo bar
32 </Screen>
33
34 </Para>
35
36 <Para>
37 The RTS will snaffle <Literal>-p -S</Literal> for itself, and the remaining arguments
38 <Literal>-f -h foo bar</Literal> will be handed to your program if/when it calls
39 <Literal>System.getArgs</Literal>.
40 </Para>
41
42 <Para>
43 No <Literal>-RTS</Literal> option is required if the runtime-system options extend to
44 the end of the command line, as in this example:
45 </Para>
46
47 <Para>
48
49 <Screen>
50 % hls -ltr /usr/etc +RTS -A5m
51 </Screen>
52
53 </Para>
54
55 <Para>
56 If you absolutely positively want all the rest of the options in a
57 command line to go to the program (and not the RTS), use a
58 <Literal>--RTS</Literal><IndexTerm><Primary>--RTS option</Primary></IndexTerm>.
59 </Para>
60
61 <Para>
62 As always, for RTS options that take <Literal>&lt;size&gt;</Literal>s: If the last
63 character of <Literal>size</Literal> is a K or k, multiply by 1000; if an M or m, by
64 1,000,000; if a G or G, by 1,000,000,000.  (And any wraparound in the
65 counters is <Emphasis>your</Emphasis> fault!)
66 </Para>
67
68 <Para>
69 Giving a <Literal>+RTS -f</Literal><IndexTerm><Primary>-f RTS option</Primary></IndexTerm> option will print out the
70 RTS options actually available in your program (which vary, depending
71 on how you compiled).
72 </Para>
73
74 <Para>
75 NOTE: to send RTS options to the compiler itself, you need to prefix
76 the option with <Literal>-optCrts</Literal>, eg. to increase the maximum heap size for
77 a compilation to 128M, you would add <Literal>-optCrts-M128m</Literal> to the command
78 line.  The compiler understands some options directly without needing
79 <Literal>-optCrts</Literal>: these are <Literal>-H</Literal> and <Literal>-K</Literal>.
80 </Para>
81
82 <Sect2 id="rts-options-gc">
83 <Title>RTS options to control the garbage-collector
84 </Title>
85
86 <Para>
87 <IndexTerm><Primary>RTS options, garbage-collection</Primary></IndexTerm>
88 </Para>
89
90 <Para>
91 There are several options to give you precise control over garbage
92 collection.  Hopefully, you won't need any of these in normal
93 operation, but there are several things that can be tweaked for
94 maximum performance.
95 </Para>
96
97 <Para>
98 <VariableList>
99
100 <VarListEntry>
101 <Term><Literal>-A&lt;size&gt;</Literal>:</Term>
102 <ListItem>
103 <Para>
104 <IndexTerm><Primary>-A&lt;size&gt; RTS option</Primary></IndexTerm>
105 <IndexTerm><Primary>allocation area, size</Primary></IndexTerm>
106 </Para>
107
108 <Para>
109 &lsqb;Default: 256k&rsqb; Set the allocation area size used by the garbage
110 collector.  The allocation area (actually generation 0 step 0) is
111 fixed and is never resized (unless you use <Literal>-H</Literal>, below).
112 </Para>
113
114 <Para>
115 Increasing the allocation area size may or may not give better
116 performance (a bigger allocation area means worse cache behaviour but
117 fewer garbage collections and less promotion).
118 </Para>
119
120 <Para>
121 With only 1 generation (<Literal>-G1</Literal>) the <Literal>-A</Literal> option specifies the
122 minimum allocation area, since the actual size of the allocation area
123 will be resized according to the amount of data in the heap (see
124 <Literal>-F</Literal>, below).
125 </Para>
126 </ListItem>
127 </VarListEntry>
128 <VarListEntry>
129 <Term><Literal>-F&lt;factor&gt;</Literal>:</Term>
130 <ListItem>
131 <Para>
132 <IndexTerm><Primary>-F&lt;factor&gt; RTS option</Primary></IndexTerm>
133 <IndexTerm><Primary>heap size, factor</Primary></IndexTerm>
134 </Para>
135
136 <Para>
137 &lsqb;Default: 2&rsqb; This option controls the amount of memory reserved for
138 the older generations (and in the case of a two space collector the
139 size of the allocation area) as a factor of the amount of live data.
140 For example, if there was 2M of live data in the oldest generation
141 when we last collected it, then by default we'll wait until it grows
142 to 4M before collecting it again.
143 </Para>
144
145 <Para>
146 The default seems to work well here.  If you have plenty of memory, it
147 is usually better to use <Literal>-H&lt;size&gt;</Literal> than to increase
148 <Literal>-F&lt;factor&gt;</Literal>.
149 </Para>
150
151 <Para>
152 The <Literal>-F</Literal> setting will be automatically reduced by the garbage
153 collector when the maximum heap size (the <Literal>-M&lt;size&gt;</Literal> setting)
154 is approaching.
155 </Para>
156 </ListItem>
157 </VarListEntry>
158 <VarListEntry>
159 <Term><Literal>-G&lt;generations&gt;</Literal>:</Term>
160 <ListItem>
161 <Para>
162 <IndexTerm><Primary>-G&lt;generations&gt; RTS option</Primary></IndexTerm>
163 <IndexTerm><Primary>generations, number of</Primary></IndexTerm>
164 </Para>
165
166 <Para>
167 &lsqb;Default: 2&rsqb; Set the number of generations used by the garbage
168 collector.  The default of 2 seems to be good, but the garbage
169 collector can support any number of generations.  Anything larger than
170 about 4 is probably not a good idea unless your program runs for a
171 <Emphasis>long</Emphasis> time, because the oldest generation will never get
172 collected.
173 </Para>
174
175 <Para>
176 Specifying 1 generation with <Literal>+RTS -G1</Literal> gives you a simple 2-space
177 collector, as you would expect.  In a 2-space collector, the <Literal>-A</Literal>
178 option (see above) specifies the <Emphasis>minimum</Emphasis> allocation area size,
179 since the allocation area will grow with the amount of live data in
180 the heap.  In a multi-generational collector the allocation area is a
181 fixed size (unless you use the <Literal>-H</Literal> option, see below).
182 </Para>
183 </ListItem>
184 </VarListEntry>
185 <VarListEntry>
186 <Term><Literal>-H&lt;size&gt;</Literal>:</Term>
187 <ListItem>
188 <Para>
189 <IndexTerm><Primary>-H&lt;size&gt; RTS option</Primary></IndexTerm>
190 <IndexTerm><Primary>heap size, suggested</Primary></IndexTerm>
191 </Para>
192
193 <Para>
194 &lsqb;Default: 0&rsqb; This option provides a "suggested heap size" for the
195 garbage collector.  The garbage collector will use about this much
196 memory until the program residency grows and the heap size needs to be
197 expanded to retain reasonable performance.
198 </Para>
199
200 <Para>
201 By default, the heap will start small, and grow and shrink as
202 necessary.  This can be bad for performance, so if you have plenty of
203 memory it's worthwhile supplying a big <Literal>-H&lt;size&gt;</Literal>.  For
204 improving GC performance, using <Literal>-H&lt;size&gt;</Literal> is usually a better
205 bet than <Literal>-A&lt;size&gt;</Literal>.
206 </Para>
207 </ListItem>
208 </VarListEntry>
209 <VarListEntry>
210 <Term><Literal>-k&lt;size&gt;</Literal>:</Term>
211 <ListItem>
212 <Para>
213 <IndexTerm><Primary>-k&lt;size&gt; RTS option</Primary></IndexTerm>
214 <IndexTerm><Primary>stack, minimum size</Primary></IndexTerm>
215 </Para>
216
217 <Para>
218 &lsqb;Default: 1k&rsqb; Set the initial stack size for new threads.  Thread
219 stacks (including the main thread's stack) live on the heap, and grow
220 as required.  The default value is good for concurrent applications
221 with lots of small threads; if your program doesn't fit this model
222 then increasing this option may help performance.
223 </Para>
224
225 <Para>
226 The main thread is normally started with a slightly larger heap to cut
227 down on unnecessary stack growth while the program is starting up.
228 </Para>
229 </ListItem>
230 </VarListEntry>
231 <VarListEntry>
232 <Term><Literal>-K&lt;size&gt;</Literal>:</Term>
233 <ListItem>
234 <Para>
235 <IndexTerm><Primary>-K&lt;size&gt; RTS option</Primary></IndexTerm>
236 <IndexTerm><Primary>stack, maximum size</Primary></IndexTerm>
237 </Para>
238
239 <Para>
240 &lsqb;Default: 1M&rsqb; Set the maximum stack size for an individual thread to
241 <Literal>&lt;size&gt;</Literal> bytes.  This option is there purely to stop the program
242 eating up all the available memory in the machine if it gets into an
243 infinite loop.
244 </Para>
245 </ListItem>
246 </VarListEntry>
247 <VarListEntry>
248 <Term><Literal>-m&lt;n&gt;</Literal>:</Term>
249 <ListItem>
250 <Para>
251 <IndexTerm><Primary>-m&lt;n&gt; RTS option</Primary></IndexTerm>
252 Minimum &percnt; <Literal>&lt;n&gt;</Literal> of heap which must be available for allocation.
253 The default is 3&percnt;.
254 <IndexTerm><Primary>heap, minimum free</Primary></IndexTerm>
255 </Para>
256 </ListItem>
257 </VarListEntry>
258 <VarListEntry>
259 <Term><Literal>-M&lt;size&gt;</Literal>:</Term>
260 <ListItem>
261 <Para>
262 <IndexTerm><Primary>-M&lt;size&gt; RTS option</Primary></IndexTerm>
263 <IndexTerm><Primary>heap size, maximum</Primary></IndexTerm>
264 </Para>
265
266 <Para>
267 &lsqb;Default: 64M&rsqb; Set the maximum heap size to <Literal>&lt;size&gt;</Literal> bytes.  The heap
268 normally grows and shrinks according to the memory requirements of the
269 program.  The only reason for having this option is to stop the heap
270 growing without bound and filling up all the available swap space,
271 which at the least will result in the program being summarily killed
272 by the operating system.
273 </Para>
274 </ListItem>
275 </VarListEntry>
276 <VarListEntry>
277 <Term><Literal>-s&lt;file&gt;</Literal> or <Literal>-S&lt;file&gt;</Literal>:</Term>
278 <ListItem>
279 <Para>
280 <IndexTerm><Primary>-S&lt;file&gt; RTS option</Primary></IndexTerm>
281 <IndexTerm><Primary>-s&lt;file&gt; RTS option</Primary></IndexTerm>
282 Write modest (<Literal>-s</Literal>) or verbose (<Literal>-S</Literal>) garbage-collector
283 statistics into file <Literal>&lt;file&gt;</Literal>. The default <Literal>&lt;file&gt;</Literal> is
284 <Literal>&lt;program&gt;@.stat</Literal>. The <Literal>&lt;file&gt;</Literal> <Literal>stderr</Literal> is treated
285 specially, with the output really being sent to <Literal>stderr</Literal>.
286 </Para>
287
288 <Para>
289 This option is useful for watching how the storage manager adjusts the
290 heap size based on the current amount of live data.
291 </Para>
292 </ListItem>
293 </VarListEntry>
294 </VariableList>
295 </Para>
296
297 </Sect2>
298
299 <Sect2>
300 <Title>RTS options for profiling and Concurrent/Parallel Haskell</Title>
301
302 <Para>
303 The RTS options related to profiling are described in <XRef LinkEnd="prof-rts-options">; and those for concurrent/parallel stuff, in
304 <XRef LinkEnd="parallel-rts-opts">.
305 </Para>
306
307 </Sect2>
308
309 <Sect2>
310 <Title>RTS options for hackers, debuggers, and over-interested souls</Title>
311
312 <Para>
313 <IndexTerm><Primary>RTS options, hacking/debugging</Primary></IndexTerm>
314 </Para>
315
316 <Para>
317 These RTS options might be used (a)&nbsp;to avoid a GHC bug, (b)&nbsp;to see
318 ``what's really happening'', or (c)&nbsp;because you feel like it.  Not
319 recommended for everyday use!
320 </Para>
321
322 <Para>
323 <VariableList>
324
325 <VarListEntry>
326 <Term><Literal>-B</Literal>:</Term>
327 <ListItem>
328 <Para>
329 <IndexTerm><Primary>-B RTS option</Primary></IndexTerm>
330 Sound the bell at the start of each (major) garbage collection.
331 </Para>
332
333 <Para>
334 Oddly enough, people really do use this option!  Our pal in Durham
335 (England), Paul Callaghan, writes: ``Some people here use it for a
336 variety of purposes&mdash;honestly!&mdash;e.g., confirmation that the
337 code/machine is doing something, infinite loop detection, gauging cost
338 of recently added code. Certain people can even tell what stage &lsqb;the
339 program&rsqb; is in by the beep pattern. But the major use is for annoying
340 others in the same office&hellip;''
341 </Para>
342 </ListItem>
343 </VarListEntry>
344 <VarListEntry>
345 <Term><Literal>-r&lt;file&gt;</Literal>:</Term>
346 <ListItem>
347 <Para>
348 <IndexTerm><Primary>-r &lt;file&gt; RTS option</Primary></IndexTerm>
349 <IndexTerm><Primary>ticky ticky profiling</Primary></IndexTerm>
350 Produce ``ticky-ticky'' statistics at the end of the program run.
351 The <Literal>&lt;file&gt;</Literal> business works just like on the <Literal>-S</Literal> RTS option (above).
352 </Para>
353
354 <Para>
355 ``Ticky-ticky'' statistics are counts of various program actions
356 (updates, enters, etc.)  The program must have been compiled using
357 <Literal>-ticky</Literal><IndexTerm><Primary>-ticky option</Primary></IndexTerm> (a.k.a. ``ticky-ticky profiling''),
358 and, for it to be really useful, linked with suitable system
359 libraries.  Not a trivial undertaking: consult the installation guide
360 on how to set things up for easy ``ticky-ticky'' profiling.  For more
361 information, see <XRef LinkEnd="ticky-ticky">.
362 </Para>
363 </ListItem>
364 </VarListEntry>
365 <VarListEntry>
366 <Term><Literal>-D&lt;num&gt;</Literal>:</Term>
367 <ListItem>
368 <Para>
369 <IndexTerm><Primary>-D RTS option</Primary></IndexTerm>
370 An RTS debugging flag; varying quantities of output depending on which
371 bits are set in <Literal>&lt;num&gt;</Literal>.  Only works if the RTS was compiled with the
372 <Literal>DEBUG</Literal> option.
373 </Para>
374 </ListItem>
375 </VarListEntry>
376 <VarListEntry>
377 <Term><Literal>-Z</Literal>:</Term>
378 <ListItem>
379 <Para>
380 <IndexTerm><Primary>-Z RTS option</Primary></IndexTerm>
381 Turn <Emphasis>off</Emphasis> ``update-frame squeezing'' at garbage-collection
382 time.  (There's no particularly good reason to turn it off, except to
383 ensure the accuracy of certain data collected regarding thunk entry
384 counts.)
385 </Para>
386 </ListItem>
387 </VarListEntry>
388 </VariableList>
389 </Para>
390
391 </Sect2>
392
393 <Sect2 id="rts-hooks">
394 <Title>``Hooks'' to change RTS behaviour
395 </Title>
396
397 <Para>
398 <IndexTerm><Primary>hooks, RTS</Primary></IndexTerm>
399 <IndexTerm><Primary>RTS hooks</Primary></IndexTerm>
400 <IndexTerm><Primary>RTS behaviour, changing</Primary></IndexTerm>
401 </Para>
402
403 <Para>
404 GHC lets you exercise rudimentary control over the RTS settings for
405 any given program, by compiling in a ``hook'' that is called by the
406 run-time system.  The RTS contains stub definitions for all these
407 hooks, but by writing your own version and linking it on the GHC
408 command line, you can override the defaults.
409 </Para>
410
411 <Para>
412 The function <Literal>defaultsHook</Literal><IndexTerm><Primary>defaultHook</Primary></IndexTerm> lets you change various
413 RTS options.  The commonest use for this is to give your program a
414 default heap and/or stack size that is greater than the default.  For
415 example, to set <Literal>-H8m -K1m</Literal>:
416 </Para>
417
418 <Para>
419
420 <ProgramListing>
421 #include "Rts.h"
422 #include "RtsFlags.h"
423 void defaultsHook (void) {
424    RTSflags.GcFlags.stksSize =  1000002 / sizeof(W_);
425    RTSflags.GcFlags.heapSize =  8000002 / sizeof(W_);
426 }
427 </ProgramListing>
428
429 </Para>
430
431 <Para>
432 Don't use powers of two for heap/stack sizes: these are more likely to
433 interact badly with direct-mapped caches.  The full set of flags is
434 defined in <Literal>ghc/rts/RtsFlags.h</Literal> the the GHC source tree.
435 </Para>
436
437 <Para>
438 You can also change the messages printed when the runtime system
439 ``blows up,'' e.g., on stack overflow.  The hooks for these are as
440 follows:
441 </Para>
442
443 <Para>
444 <VariableList>
445
446 <VarListEntry>
447 <Term><Literal>void ErrorHdrHook (FILE *)</Literal>:</Term>
448 <ListItem>
449 <Para>
450 <IndexTerm><Primary>ErrorHdrHook</Primary></IndexTerm>
451 What's printed out before the message from <Literal>error</Literal>.
452 </Para>
453 </ListItem>
454 </VarListEntry>
455 <VarListEntry>
456 <Term><Literal>void OutOfHeapHook (unsigned long, unsigned long)</Literal>:</Term>
457 <ListItem>
458 <Para>
459 <IndexTerm><Primary>OutOfHeapHook</Primary></IndexTerm>
460 The heap-overflow message.
461 </Para>
462 </ListItem>
463 </VarListEntry>
464 <VarListEntry>
465 <Term><Literal>void StackOverflowHook (long int)</Literal>:</Term>
466 <ListItem>
467 <Para>
468 <IndexTerm><Primary>StackOverflowHook</Primary></IndexTerm>
469 The stack-overflow message.
470 </Para>
471 </ListItem>
472 </VarListEntry>
473 <VarListEntry>
474 <Term><Literal>void MallocFailHook (long int)</Literal>:</Term>
475 <ListItem>
476 <Para>
477 <IndexTerm><Primary>MallocFailHook</Primary></IndexTerm>
478 The message printed if <Literal>malloc</Literal> fails.
479 </Para>
480 </ListItem>
481 </VarListEntry>
482 <VarListEntry>
483 <Term><Literal>void PatErrorHdrHook (FILE *)</Literal>:</Term>
484 <ListItem>
485 <Para>
486 <IndexTerm><Primary>PatErrorHdrHook</Primary></IndexTerm>
487 The message printed if a pattern-match fails (the failures
488 that were not handled by the Haskell programmer).
489 </Para>
490 </ListItem>
491 </VarListEntry>
492 <VarListEntry>
493 <Term><Literal>void PreTraceHook (FILE *)</Literal>:</Term>
494 <ListItem>
495 <Para>
496 <IndexTerm><Primary>PreTraceHook</Primary></IndexTerm>
497 What's printed out before a <Literal>trace</Literal> message.
498 </Para>
499 </ListItem>
500 </VarListEntry>
501 <VarListEntry>
502 <Term><Literal>void PostTraceHook (FILE *)</Literal>:</Term>
503 <ListItem>
504 <Para>
505 <IndexTerm><Primary>PostTraceHook</Primary></IndexTerm>
506 What's printed out after a <Literal>trace</Literal> message.
507 </Para>
508 </ListItem>
509 </VarListEntry>
510 </VariableList>
511 </Para>
512
513 <Para>
514 For example, here is the ``hooks'' code used by GHC itself:
515
516 <ProgramListing>
517 #include &#60;stdio.h&#62;
518 #define W_ unsigned long int
519 #define I_ long int
520
521 void
522 ErrorHdrHook (FILE *where)
523 {
524     fprintf(where, "\n"); /* no "Fail: " */
525 }
526
527 void
528 OutOfHeapHook (W_ request_size, W_ heap_size) /* both sizes in bytes */
529 {
530     fprintf(stderr, "GHC's heap exhausted;\nwhile trying to 
531         allocate %lu bytes in a %lu-byte heap;\nuse the `-H&#60;size&#62;'
532         option to increase the total heap size.\n",
533         request_size,
534         heap_size);
535 }
536
537 void
538 StackOverflowHook (I_ stack_size)    /* in bytes */
539 {
540     fprintf(stderr, "GHC stack-space overflow: current size
541         %ld bytes.\nUse the `-K&#60;size&#62;' option to increase it.\n",
542         stack_size);
543 }
544
545 void
546 PatErrorHdrHook (FILE *where)
547 {
548     fprintf(where, "\n*** Pattern-matching error within GHC!\n\n
549         This is a compiler bug; please report it to
550         glasgow-haskell-bugs@haskell.org.\n\nFail: ");
551 }
552
553 void
554 PreTraceHook (FILE *where)
555 {
556     fprintf(where, "\n"); /* not "Trace On" */
557 }
558
559 void
560 PostTraceHook (FILE *where)
561 {
562     fprintf(where, "\n"); /* not "Trace Off" */
563 }
564 </ProgramListing>
565
566 </Para>
567
568 </Sect2>
569
570 </Sect1>