Document SMP support
[ghc.git] / docs / users_guide / runtime_control.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <sect1 id="runtime-control">
3 <title>Running a compiled program</title>
4
5 <indexterm><primary>runtime control of Haskell programs</primary></indexterm>
6 <indexterm><primary>running, compiled program</primary></indexterm>
7 <indexterm><primary>RTS options</primary></indexterm>
8
9 <para>To make an executable program, the GHC system compiles your
10 code and then links it with a non-trivial runtime system (RTS),
11 which handles storage management, profiling, etc.</para>
12
13 <para>You have some control over the behaviour of the RTS, by giving
14 special command-line arguments to your program.</para>
15
16 <para>When your Haskell program starts up, its RTS extracts
17 command-line arguments bracketed between
18 <option>+RTS</option><indexterm><primary><option>+RTS</option></primary></indexterm>
19 and
20 <option>-RTS</option><indexterm><primary><option>-RTS</option></primary></indexterm>
21 as its own. For example:</para>
22
23 <screen>
24 % ./a.out -f +RTS -p -S -RTS -h foo bar
25 </screen>
26
27 <para>The RTS will snaffle <option>-p</option> <option>-S</option>
28 for itself, and the remaining arguments <literal>-f -h foo bar</literal>
29 will be handed to your program if/when it calls
30 <function>System.getArgs</function>.</para>
31
32 <para>No <option>-RTS</option> option is required if the
33 runtime-system options extend to the end of the command line, as in
34 this example:</para>
35
36 <screen>
37 % hls -ltr /usr/etc +RTS -A5m
38 </screen>
39
40 <para>If you absolutely positively want all the rest of the options
41 in a command line to go to the program (and not the RTS), use a
42 <option>&ndash;&ndash;RTS</option><indexterm><primary><option>--RTS</option></primary></indexterm>.</para>
43
44 <para>As always, for RTS options that take
45 <replaceable>size</replaceable>s: If the last character of
46 <replaceable>size</replaceable> is a K or k, multiply by 1000; if an
47 M or m, by 1,000,000; if a G or G, by 1,000,000,000. (And any
48 wraparound in the counters is <emphasis>your</emphasis>
49 fault!)</para>
50
51 <para>Giving a <literal>+RTS -f</literal>
52 <indexterm><primary><option>-f</option></primary><secondary>RTS option</secondary></indexterm> option
53 will print out the RTS options actually available in your program
54 (which vary, depending on how you compiled).</para>
55
56 <para>NOTE: since GHC is itself compiled by GHC, you can change RTS
57 options in the compiler using the normal
58 <literal>+RTS ... -RTS</literal>
59 combination. eg. to increase the maximum heap
60 size for a compilation to 128M, you would add
61 <literal>+RTS -M128m -RTS</literal>
62 to the command line.</para>
63
64 <sect2 id="rts-optinos-environment">
65 <title>Setting global RTS options</title>
66
67 <indexterm><primary>RTS options</primary><secondary>from the environment</secondary></indexterm>
68 <indexterm><primary>environment variable</primary><secondary>for
69 setting RTS options</secondary></indexterm>
70
71 <para>RTS options are also taken from the environment variable
72 <envar>GHCRTS</envar><indexterm><primary><envar>GHCRTS</envar></primary>
73 </indexterm>. For example, to set the maximum heap size
74 to 128M for all GHC-compiled programs (using an
75 <literal>sh</literal>-like shell):</para>
76
77 <screen>
78 GHCRTS='-M128m'
79 export GHCRTS
80 </screen>
81
82 <para>RTS options taken from the <envar>GHCRTS</envar> environment
83 variable can be overridden by options given on the command
84 line.</para>
85
86 </sect2>
87
88 <sect2 id="rts-options-gc">
89 <title>RTS options to control the garbage collector</title>
90
91 <indexterm><primary>garbage collector</primary><secondary>options</secondary></indexterm>
92 <indexterm><primary>RTS options</primary><secondary>garbage collection</secondary></indexterm>
93
94 <para>There are several options to give you precise control over
95 garbage collection. Hopefully, you won't need any of these in
96 normal operation, but there are several things that can be tweaked
97 for maximum performance.</para>
98
99 <variablelist>
100
101 <varlistentry>
102 <term>
103 <option>-A</option><replaceable>size</replaceable>
104 <indexterm><primary><option>-A</option></primary><secondary>RTS option</secondary></indexterm>
105 <indexterm><primary>allocation area, size</primary></indexterm>
106 </term>
107 <listitem>
108 <para>&lsqb;Default: 256k&rsqb; Set the allocation area size
109 used by the garbage collector. The allocation area
110 (actually generation 0 step 0) is fixed and is never resized
111 (unless you use <option>-H</option>, below).</para>
112
113 <para>Increasing the allocation area size may or may not
114 give better performance (a bigger allocation area means
115 worse cache behaviour but fewer garbage collections and less
116 promotion).</para>
117
118 <para>With only 1 generation (<option>-G1</option>) the
119 <option>-A</option> option specifies the minimum allocation
120 area, since the actual size of the allocation area will be
121 resized according to the amount of data in the heap (see
122 <option>-F</option>, below).</para>
123 </listitem>
124 </varlistentry>
125
126 <varlistentry>
127 <term>
128 <option>-c</option>
129 <indexterm><primary><option>-c</option></primary><secondary>RTS option</secondary></indexterm>
130 <indexterm><primary>garbage collection</primary><secondary>compacting</secondary></indexterm>
131 <indexterm><primary>compacting garbage collection</primary></indexterm>
132 </term>
133 <listitem>
134 <para>Use a compacting algorithm for collecting the oldest
135 generation. By default, the oldest generation is collected
136 using a copying algorithm; this option causes it to be
137 compacted in-place instead. The compaction algorithm is
138 slower than the copying algorithm, but the savings in memory
139 use can be considerable.</para>
140
141 <para>For a given heap size (using the <option>-H</option>
142 option), compaction can in fact reduce the GC cost by
143 allowing fewer GCs to be performed. This is more likely
144 when the ratio of live data to heap size is high, say
145 &gt;30&percnt;.</para>
146
147 <para>NOTE: compaction doesn't currently work when a single
148 generation is requested using the <option>-G1</option>
149 option.</para>
150 </listitem>
151 </varlistentry>
152
153 <varlistentry>
154 <term><option>-c</option><replaceable>n</replaceable></term>
155
156 <listitem>
157 <para>&lsqb;Default: 30&rsqb; Automatically enable
158 compacting collection when the live data exceeds
159 <replaceable>n</replaceable>&percnt; of the maximum heap size
160 (see the <option>-M</option> option). Note that the maximum
161 heap size is unlimited by default, so this option has no
162 effect unless the maximum heap size is set with
163 <option>-M</option><replaceable>size</replaceable>. </para>
164 </listitem>
165 </varlistentry>
166
167 <varlistentry>
168 <term>
169 <option>-F</option><replaceable>factor</replaceable>
170 <indexterm><primary><option>-F</option></primary><secondary>RTS option</secondary></indexterm>
171 <indexterm><primary>heap size, factor</primary></indexterm>
172 </term>
173 <listitem>
174
175 <para>&lsqb;Default: 2&rsqb; This option controls the amount
176 of memory reserved for the older generations (and in the
177 case of a two space collector the size of the allocation
178 area) as a factor of the amount of live data. For example,
179 if there was 2M of live data in the oldest generation when
180 we last collected it, then by default we'll wait until it
181 grows to 4M before collecting it again.</para>
182
183 <para>The default seems to work well here. If you have
184 plenty of memory, it is usually better to use
185 <option>-H</option><replaceable>size</replaceable> than to
186 increase
187 <option>-F</option><replaceable>factor</replaceable>.</para>
188
189 <para>The <option>-F</option> setting will be automatically
190 reduced by the garbage collector when the maximum heap size
191 (the <option>-M</option><replaceable>size</replaceable>
192 setting) is approaching.</para>
193 </listitem>
194 </varlistentry>
195
196 <varlistentry>
197 <term>
198 <option>-G</option><replaceable>generations</replaceable>
199 <indexterm><primary><option>-G</option></primary><secondary>RTS option</secondary></indexterm>
200 <indexterm><primary>generations, number of</primary></indexterm>
201 </term>
202 <listitem>
203 <para>&lsqb;Default: 2&rsqb; Set the number of generations
204 used by the garbage collector. The default of 2 seems to be
205 good, but the garbage collector can support any number of
206 generations. Anything larger than about 4 is probably not a
207 good idea unless your program runs for a
208 <emphasis>long</emphasis> time, because the oldest
209 generation will hardly ever get collected.</para>
210
211 <para>Specifying 1 generation with <option>+RTS -G1</option>
212 gives you a simple 2-space collector, as you would expect.
213 In a 2-space collector, the <option>-A</option> option (see
214 above) specifies the <emphasis>minimum</emphasis> allocation
215 area size, since the allocation area will grow with the
216 amount of live data in the heap. In a multi-generational
217 collector the allocation area is a fixed size (unless you
218 use the <option>-H</option> option, see below).</para>
219 </listitem>
220 </varlistentry>
221
222 <varlistentry>
223 <term>
224 <option>-H</option><replaceable>size</replaceable>
225 <indexterm><primary><option>-H</option></primary><secondary>RTS option</secondary></indexterm>
226 <indexterm><primary>heap size, suggested</primary></indexterm>
227 </term>
228 <listitem>
229 <para>&lsqb;Default: 0&rsqb; This option provides a
230 &ldquo;suggested heap size&rdquo; for the garbage collector. The
231 garbage collector will use about this much memory until the
232 program residency grows and the heap size needs to be
233 expanded to retain reasonable performance.</para>
234
235 <para>By default, the heap will start small, and grow and
236 shrink as necessary. This can be bad for performance, so if
237 you have plenty of memory it's worthwhile supplying a big
238 <option>-H</option><replaceable>size</replaceable>. For
239 improving GC performance, using
240 <option>-H</option><replaceable>size</replaceable> is
241 usually a better bet than
242 <option>-A</option><replaceable>size</replaceable>.</para>
243 </listitem>
244 </varlistentry>
245
246 <varlistentry>
247 <term>
248 <option>-I</option><replaceable>seconds</replaceable>
249 <indexterm><primary><option>-I</option></primary>
250 <secondary>RTS option</secondary>
251 </indexterm>
252 <indexterm><primary>idle GC</primary>
253 </indexterm>
254 </term>
255 <listitem>
256 <para>(default: 0.3) In the threaded and SMP versions of the RTS (see
257 <option>-threaded</option>, <xref linkend="options-linker" />), a
258 major GC is automatically performed if the runtime has been idle
259 (no Haskell computation has been running) for a period of time.
260 The amount of idle time which must pass before a GC is performed is
261 set by the <option>-I</option><replaceable>seconds</replaceable>
262 option. Specifying <option>-I0</option> disables the idle GC.</para>
263
264 <para>For an interactive application, it is probably a good idea to
265 use the idle GC, because this will allow finalizers to run and
266 deadlocked threads to be detected in the idle time when no Haskell
267 computation is happening. Also, it will mean that a GC is less
268 likely to happen when the application is busy, and so
269 responsiveness may be improved. However, if the amount of live data in
270 the heap is particularly large, then the idle GC can cause a
271 significant delay, and too small an interval could adversely affect
272 interactive responsiveness.</para>
273
274 <para>This is an experimental feature, please let us know if it
275 causes problems and/or could benefit from further tuning.</para>
276 </listitem>
277 </varlistentry>
278
279 <varlistentry>
280 <term>
281 <option>-k</option><replaceable>size</replaceable>
282 <indexterm><primary><option>-k</option></primary><secondary>RTS option</secondary></indexterm>
283 <indexterm><primary>stack, minimum size</primary></indexterm>
284 </term>
285 <listitem>
286 <para>&lsqb;Default: 1k&rsqb; Set the initial stack size for
287 new threads. Thread stacks (including the main thread's
288 stack) live on the heap, and grow as required. The default
289 value is good for concurrent applications with lots of small
290 threads; if your program doesn't fit this model then
291 increasing this option may help performance.</para>
292
293 <para>The main thread is normally started with a slightly
294 larger heap to cut down on unnecessary stack growth while
295 the program is starting up.</para>
296 </listitem>
297 </varlistentry>
298
299 <varlistentry>
300 <term>
301 <option>-K</option><replaceable>size</replaceable>
302 <indexterm><primary><option>-K</option></primary><secondary>RTS option</secondary></indexterm>
303 <indexterm><primary>stack, maximum size</primary></indexterm>
304 </term>
305 <listitem>
306 <para>&lsqb;Default: 8M&rsqb; Set the maximum stack size for
307 an individual thread to <replaceable>size</replaceable>
308 bytes. This option is there purely to stop the program
309 eating up all the available memory in the machine if it gets
310 into an infinite loop.</para>
311 </listitem>
312 </varlistentry>
313
314 <varlistentry>
315 <term>
316 <option>-m</option><replaceable>n</replaceable>
317 <indexterm><primary><option>-m</option></primary><secondary>RTS option</secondary></indexterm>
318 <indexterm><primary>heap, minimum free</primary></indexterm>
319 </term>
320 <listitem>
321 <para>Minimum &percnt; <replaceable>n</replaceable> of heap
322 which must be available for allocation. The default is
323 3&percnt;.</para>
324 </listitem>
325 </varlistentry>
326
327 <varlistentry>
328 <term>
329 <option>-M</option><replaceable>size</replaceable>
330 <indexterm><primary><option>-M</option></primary><secondary>RTS option</secondary></indexterm>
331 <indexterm><primary>heap size, maximum</primary></indexterm>
332 </term>
333 <listitem>
334 <para>&lsqb;Default: unlimited&rsqb; Set the maximum heap size to
335 <replaceable>size</replaceable> bytes. The heap normally
336 grows and shrinks according to the memory requirements of
337 the program. The only reason for having this option is to
338 stop the heap growing without bound and filling up all the
339 available swap space, which at the least will result in the
340 program being summarily killed by the operating
341 system.</para>
342
343 <para>The maximum heap size also affects other garbage
344 collection parameters: when the amount of live data in the
345 heap exceeds a certain fraction of the maximum heap size,
346 compacting collection will be automatically enabled for the
347 oldest generation, and the <option>-F</option> parameter
348 will be reduced in order to avoid exceeding the maximum heap
349 size.</para>
350 </listitem>
351 </varlistentry>
352
353 <varlistentry>
354 <term>
355 <option>-s</option><replaceable>file</replaceable>
356 <indexterm><primary><option>-s</option></primary><secondary>RTS option</secondary></indexterm>
357 </term>
358 <term>
359 <option>-S</option><replaceable>file</replaceable>
360 <indexterm><primary><option>-S</option></primary><secondary>RTS option</secondary></indexterm>
361 </term>
362 <listitem>
363 <para>Write modest (<option>-s</option>) or verbose
364 (<option>-S</option>) garbage-collector statistics into file
365 <replaceable>file</replaceable>. The default
366 <replaceable>file</replaceable> is
367 <filename><replaceable>program</replaceable>.stat</filename>. The
368 <replaceable>file</replaceable> <constant>stderr</constant>
369 is treated specially, with the output really being sent to
370 <constant>stderr</constant>.</para>
371
372 <para>This option is useful for watching how the storage
373 manager adjusts the heap size based on the current amount of
374 live data.</para>
375 </listitem>
376 </varlistentry>
377
378 <varlistentry>
379 <term>
380 <option>-t<replaceable>file</replaceable></option>
381 <indexterm><primary><option>-t</option></primary><secondary>RTS option</secondary></indexterm>
382 </term>
383 <listitem>
384 <para>Write a one-line GC stats summary after running the
385 program. This output is in the same format as that produced
386 by the <option>-Rghc-timing</option> option.</para>
387
388 <para>As with <option>-s</option>, the default
389 <replaceable>file</replaceable> is
390 <filename><replaceable>program</replaceable>.stat</filename>. The
391 <replaceable>file</replaceable> <constant>stderr</constant>
392 is treated specially, with the output really being sent to
393 <constant>stderr</constant>.</para>
394 </listitem>
395 </varlistentry>
396 </variablelist>
397
398 </sect2>
399
400 <sect2>
401 <title>RTS options for profiling and parallelism</title>
402
403 <para>The RTS options related to profiling are described in <xref
404 linkend="rts-options-heap-prof"/>, those for concurrency in
405 <xref linkend="sec-using-concurrent" />, and those for parallelism in
406 <xref linkend="parallel-options"/>.</para>
407 </sect2>
408
409 <sect2 id="rts-options-debugging">
410 <title>RTS options for hackers, debuggers, and over-interested
411 souls</title>
412
413 <indexterm><primary>RTS options, hacking/debugging</primary></indexterm>
414
415 <para>These RTS options might be used (a)&nbsp;to avoid a GHC bug,
416 (b)&nbsp;to see &ldquo;what's really happening&rdquo;, or
417 (c)&nbsp;because you feel like it. Not recommended for everyday
418 use!</para>
419
420 <variablelist>
421
422 <varlistentry>
423 <term>
424 <option>-B</option>
425 <indexterm><primary><option>-B</option></primary><secondary>RTS option</secondary></indexterm>
426 </term>
427 <listitem>
428 <para>Sound the bell at the start of each (major) garbage
429 collection.</para>
430
431 <para>Oddly enough, people really do use this option! Our
432 pal in Durham (England), Paul Callaghan, writes: &ldquo;Some
433 people here use it for a variety of
434 purposes&mdash;honestly!&mdash;e.g., confirmation that the
435 code/machine is doing something, infinite loop detection,
436 gauging cost of recently added code. Certain people can even
437 tell what stage &lsqb;the program&rsqb; is in by the beep
438 pattern. But the major use is for annoying others in the
439 same office&hellip;&rdquo;</para>
440 </listitem>
441 </varlistentry>
442
443 <varlistentry>
444 <term>
445 <option>-D</option><replaceable>num</replaceable>
446 <indexterm><primary>-D</primary><secondary>RTS option</secondary></indexterm>
447 </term>
448 <listitem>
449 <para>An RTS debugging flag; varying quantities of output
450 depending on which bits are set in
451 <replaceable>num</replaceable>. Only works if the RTS was
452 compiled with the <option>DEBUG</option> option.</para>
453 </listitem>
454 </varlistentry>
455
456 <varlistentry>
457 <term>
458 <option>-r</option><replaceable>file</replaceable>
459 <indexterm><primary><option>-r</option></primary><secondary>RTS option</secondary></indexterm>
460 <indexterm><primary>ticky ticky profiling</primary></indexterm>
461 <indexterm><primary>profiling</primary><secondary>ticky ticky</secondary></indexterm>
462 </term>
463 <listitem>
464 <para>Produce &ldquo;ticky-ticky&rdquo; statistics at the
465 end of the program run. The <replaceable>file</replaceable>
466 business works just like on the <option>-S</option> RTS
467 option (above).</para>
468
469 <para>&ldquo;Ticky-ticky&rdquo; statistics are counts of
470 various program actions (updates, enters, etc.) The program
471 must have been compiled using
472 <option>-ticky</option><indexterm><primary><option>-ticky</option></primary></indexterm>
473 (a.k.a. &ldquo;ticky-ticky profiling&rdquo;), and, for it to
474 be really useful, linked with suitable system libraries.
475 Not a trivial undertaking: consult the installation guide on
476 how to set things up for easy &ldquo;ticky-ticky&rdquo;
477 profiling. For more information, see <xref
478 linkend="ticky-ticky"/>.</para>
479 </listitem>
480 </varlistentry>
481
482 <varlistentry>
483 <term>
484 <option>-xc</option>
485 <indexterm><primary><option>-xc</option></primary><secondary>RTS option</secondary></indexterm>
486 </term>
487 <listitem>
488 <para>(Only available when the program is compiled for
489 profiling.) When an exception is raised in the program,
490 this option causes the current cost-centre-stack to be
491 dumped to <literal>stderr</literal>.</para>
492
493 <para>This can be particularly useful for debugging: if your
494 program is complaining about a <literal>head []</literal>
495 error and you haven't got a clue which bit of code is
496 causing it, compiling with <literal>-prof
497 -auto-all</literal> and running with <literal>+RTS -xc
498 -RTS</literal> will tell you exactly the call stack at the
499 point the error was raised.</para>
500
501 <para>The output contains one line for each exception raised
502 in the program (the program might raise and catch several
503 exceptions during its execution), where each line is of the
504 form:</para>
505
506 <screen>
507 &lt; cc<subscript>1</subscript>, ..., cc<subscript>n</subscript> &gt;
508 </screen>
509 <para>each <literal>cc</literal><subscript>i</subscript> is
510 a cost centre in the program (see <xref
511 linkend="cost-centres"/>), and the sequence represents the
512 &ldquo;call stack&rdquo; at the point the exception was
513 raised. The leftmost item is the innermost function in the
514 call stack, and the rightmost item is the outermost
515 function.</para>
516
517 </listitem>
518 </varlistentry>
519
520 <varlistentry>
521 <term>
522 <option>-Z</option>
523 <indexterm><primary><option>-Z</option></primary><secondary>RTS option</secondary></indexterm>
524 </term>
525 <listitem>
526 <para>Turn <emphasis>off</emphasis> &ldquo;update-frame
527 squeezing&rdquo; at garbage-collection time. (There's no
528 particularly good reason to turn it off, except to ensure
529 the accuracy of certain data collected regarding thunk entry
530 counts.)</para>
531 </listitem>
532 </varlistentry>
533 </variablelist>
534
535 </sect2>
536
537 <sect2 id="rts-hooks">
538 <title>&ldquo;Hooks&rdquo; to change RTS behaviour</title>
539
540 <indexterm><primary>hooks</primary><secondary>RTS</secondary></indexterm>
541 <indexterm><primary>RTS hooks</primary></indexterm>
542 <indexterm><primary>RTS behaviour, changing</primary></indexterm>
543
544 <para>GHC lets you exercise rudimentary control over the RTS
545 settings for any given program, by compiling in a
546 &ldquo;hook&rdquo; that is called by the run-time system. The RTS
547 contains stub definitions for all these hooks, but by writing your
548 own version and linking it on the GHC command line, you can
549 override the defaults.</para>
550
551 <para>Owing to the vagaries of DLL linking, these hooks don't work
552 under Windows when the program is built dynamically.</para>
553
554 <para>The hook <literal>ghc_rts_opts</literal><indexterm><primary><literal>ghc_rts_opts</literal></primary>
555 </indexterm>lets you set RTS
556 options permanently for a given program. A common use for this is
557 to give your program a default heap and/or stack size that is
558 greater than the default. For example, to set <literal>-H128m
559 -K1m</literal>, place the following definition in a C source
560 file:</para>
561
562 <programlisting>
563 char *ghc_rts_opts = "-H128m -K1m";
564 </programlisting>
565
566 <para>Compile the C file, and include the object file on the
567 command line when you link your Haskell program.</para>
568
569 <para>These flags are interpreted first, before any RTS flags from
570 the <literal>GHCRTS</literal> environment variable and any flags
571 on the command line.</para>
572
573 <para>You can also change the messages printed when the runtime
574 system &ldquo;blows up,&rdquo; e.g., on stack overflow. The hooks
575 for these are as follows:</para>
576
577 <variablelist>
578
579 <varlistentry>
580 <term>
581 <function>void OutOfHeapHook (unsigned long, unsigned long)</function>
582 <indexterm><primary><function>OutOfHeapHook</function></primary></indexterm>
583 </term>
584 <listitem>
585 <para>The heap-overflow message.</para>
586 </listitem>
587 </varlistentry>
588
589 <varlistentry>
590 <term>
591 <function>void StackOverflowHook (long int)</function>
592 <indexterm><primary><function>StackOverflowHook</function></primary></indexterm>
593 </term>
594 <listitem>
595 <para>The stack-overflow message.</para>
596 </listitem>
597 </varlistentry>
598
599 <varlistentry>
600 <term>
601 <function>void MallocFailHook (long int)</function>
602 <indexterm><primary><function>MallocFailHook</function></primary></indexterm>
603 </term>
604 <listitem>
605 <para>The message printed if <function>malloc</function>
606 fails.</para>
607 </listitem>
608 </varlistentry>
609 </variablelist>
610
611 <para>For examples of the use of these hooks, see GHC's own
612 versions in the file
613 <filename>ghc/compiler/parser/hschooks.c</filename> in a GHC
614 source tree.</para>
615 </sect2>
616 </sect1>
617
618 <!-- Emacs stuff:
619 ;;; Local Variables: ***
620 ;;; mode: xml ***
621 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***
622 ;;; End: ***
623 -->