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