Document new GC options -q1 and -qg<n>
[ghc-hetmet.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-misc">
89     <title>Miscellaneous RTS options</title>
90
91     <variablelist>
92      <varlistentry>
93        <term><option>-V<replaceable>secs</replaceable></option>
94        <indexterm><primary><option>-V</option></primary><secondary>RTS
95        option</secondary></indexterm></term>
96        <listitem>
97          <para>Sets the interval that the RTS clock ticks at.  The
98          runtime uses a single timer signal to count ticks; this timer
99          signal is used to control the context switch timer (<xref
100          linkend="using-concurrent" />) and the heap profiling
101          timer <xref linkend="rts-options-heap-prof" />.  Also, the
102          time profiler uses the RTS timer signal directly to record
103          time profiling samples.</para>
104
105          <para>Normally, setting the <option>-V</option> option
106          directly is not necessary: the resolution of the RTS timer is
107          adjusted automatically if a short interval is requested with
108          the <option>-C</option> or <option>-i</option> options.
109          However, setting <option>-V</option> is required in order to
110          increase the resolution of the time profiler.</para>
111
112          <para>Using a value of zero disables the RTS clock
113          completely, and has the effect of disabling timers that
114          depend on it: the context switch timer and the heap profiling
115          timer.  Context switches will still happen, but
116          deterministically and at a rate much faster than normal.
117          Disabling the interval timer is useful for debugging, because
118          it eliminates a source of non-determinism at runtime.</para>
119        </listitem>
120      </varlistentry>
121
122      <varlistentry>
123        <term><option>--install-signal-handlers=<replaceable>yes|no</replaceable></option>
124        <indexterm><primary><option>--install-signal-handlers</option></primary><secondary>RTS
125        option</secondary></indexterm></term>
126        <listitem>
127          <para>If yes (the default), the RTS installs signal handlers to catch
128          things like ctrl-C. This option is primarily useful for when
129          you are using the Haskell code as a DLL, and want to set your
130          own signal handlers.</para>
131        </listitem>
132      </varlistentry>
133
134      <varlistentry>
135        <term><option>-xm<replaceable>address</replaceable></option>
136        <indexterm><primary><option>-xm</option></primary><secondary>RTS
137        option</secondary></indexterm></term>
138        <listitem>
139          <para>
140            WARNING: this option is for working around memory
141            allocation problems only.  Do not use unless GHCi fails
142            with a message like &ldquo;<literal>failed to mmap() memory below 2Gb</literal>&rdquo;.  If you need to use this option to get GHCi working
143            on your machine, please file a bug.
144          </para>
145          
146          <para>
147            On 64-bit machines, the RTS needs to allocate memory in the
148            low 2Gb of the address space.  Support for this across
149            different operating systems is patchy, and sometimes fails.
150            This option is there to give the RTS a hint about where it
151            should be able to allocate memory in the low 2Gb of the
152            address space.  For example, <literal>+RTS -xm20000000
153            -RTS</literal> would hint that the RTS should allocate
154            starting at the 0.5Gb mark.  The default is to use the OS's
155            built-in support for allocating memory in the low 2Gb if
156            available (e.g. <literal>mmap</literal>
157            with <literal>MAP_32BIT</literal> on Linux), or
158            otherwise <literal>-xm40000000</literal>.
159          </para>
160        </listitem>
161      </varlistentry>
162     </variablelist>
163   </sect2>
164
165   <sect2 id="rts-options-gc">
166     <title>RTS options to control the garbage collector</title>
167
168     <indexterm><primary>garbage collector</primary><secondary>options</secondary></indexterm>
169     <indexterm><primary>RTS options</primary><secondary>garbage collection</secondary></indexterm>
170
171     <para>There are several options to give you precise control over
172     garbage collection.  Hopefully, you won't need any of these in
173     normal operation, but there are several things that can be tweaked
174     for maximum performance.</para>
175
176     <variablelist>
177
178       <varlistentry>
179         <term>
180           <option>-A</option><replaceable>size</replaceable>
181           <indexterm><primary><option>-A</option></primary><secondary>RTS option</secondary></indexterm>
182           <indexterm><primary>allocation area, size</primary></indexterm>
183         </term>
184         <listitem>
185           <para>&lsqb;Default: 256k&rsqb; Set the allocation area size
186           used by the garbage collector.  The allocation area
187           (actually generation 0 step 0) is fixed and is never resized
188           (unless you use <option>-H</option>, below).</para>
189
190           <para>Increasing the allocation area size may or may not
191           give better performance (a bigger allocation area means
192           worse cache behaviour but fewer garbage collections and less
193           promotion).</para>
194
195           <para>With only 1 generation (<option>-G1</option>) the
196           <option>-A</option> option specifies the minimum allocation
197           area, since the actual size of the allocation area will be
198           resized according to the amount of data in the heap (see
199           <option>-F</option>, below).</para>
200         </listitem>
201       </varlistentry>
202
203       <varlistentry>
204         <term>
205           <option>-c</option>
206           <indexterm><primary><option>-c</option></primary><secondary>RTS option</secondary></indexterm>
207           <indexterm><primary>garbage collection</primary><secondary>compacting</secondary></indexterm>
208           <indexterm><primary>compacting garbage collection</primary></indexterm>
209         </term>
210         <listitem>
211           <para>Use a compacting algorithm for collecting the oldest
212           generation.  By default, the oldest generation is collected
213           using a copying algorithm; this option causes it to be
214           compacted in-place instead.  The compaction algorithm is
215           slower than the copying algorithm, but the savings in memory
216           use can be considerable.</para>
217
218           <para>For a given heap size (using the <option>-H</option>
219           option), compaction can in fact reduce the GC cost by
220           allowing fewer GCs to be performed.  This is more likely
221           when the ratio of live data to heap size is high, say
222           &gt;30&percnt;.</para>
223
224           <para>NOTE: compaction doesn't currently work when a single
225           generation is requested using the <option>-G1</option>
226           option.</para>
227         </listitem>
228       </varlistentry>
229
230       <varlistentry>
231         <term><option>-c</option><replaceable>n</replaceable></term>
232
233         <listitem>
234           <para>&lsqb;Default: 30&rsqb; Automatically enable
235           compacting collection when the live data exceeds
236           <replaceable>n</replaceable>&percnt; of the maximum heap size
237           (see the <option>-M</option> option).  Note that the maximum
238           heap size is unlimited by default, so this option has no
239           effect unless the maximum heap size is set with
240           <option>-M</option><replaceable>size</replaceable>. </para>
241         </listitem>
242       </varlistentry>
243
244       <varlistentry>
245         <term>
246           <option>-F</option><replaceable>factor</replaceable>
247           <indexterm><primary><option>-F</option></primary><secondary>RTS option</secondary></indexterm>
248           <indexterm><primary>heap size, factor</primary></indexterm>
249         </term>
250         <listitem>
251
252           <para>&lsqb;Default: 2&rsqb; This option controls the amount
253           of memory reserved for the older generations (and in the
254           case of a two space collector the size of the allocation
255           area) as a factor of the amount of live data.  For example,
256           if there was 2M of live data in the oldest generation when
257           we last collected it, then by default we'll wait until it
258           grows to 4M before collecting it again.</para>
259
260           <para>The default seems to work well here.  If you have
261           plenty of memory, it is usually better to use
262           <option>-H</option><replaceable>size</replaceable> than to
263           increase
264           <option>-F</option><replaceable>factor</replaceable>.</para>
265
266           <para>The <option>-F</option> setting will be automatically
267           reduced by the garbage collector when the maximum heap size
268           (the <option>-M</option><replaceable>size</replaceable>
269           setting) is approaching.</para>
270         </listitem>
271       </varlistentry>
272
273       <varlistentry>
274         <term>
275           <option>-G</option><replaceable>generations</replaceable>
276           <indexterm><primary><option>-G</option></primary><secondary>RTS option</secondary></indexterm>
277           <indexterm><primary>generations, number of</primary></indexterm>
278         </term>
279         <listitem>
280           <para>&lsqb;Default: 2&rsqb; Set the number of generations
281           used by the garbage collector.  The default of 2 seems to be
282           good, but the garbage collector can support any number of
283           generations.  Anything larger than about 4 is probably not a
284           good idea unless your program runs for a
285           <emphasis>long</emphasis> time, because the oldest
286           generation will hardly ever get collected.</para>
287
288           <para>Specifying 1 generation with <option>+RTS -G1</option>
289           gives you a simple 2-space collector, as you would expect.
290           In a 2-space collector, the <option>-A</option> option (see
291           above) specifies the <emphasis>minimum</emphasis> allocation
292           area size, since the allocation area will grow with the
293           amount of live data in the heap.  In a multi-generational
294           collector the allocation area is a fixed size (unless you
295           use the <option>-H</option> option, see below).</para>
296         </listitem>
297       </varlistentry>
298
299       <varlistentry>
300         <term>
301           <option>-q1</option>
302           <indexterm><primary><option>-q1</option><secondary>RTS
303           option</secondary></primary></indexterm>
304         </term>
305         <listitem>
306           <para>&lsqb;New in GHC 6.12.1&rsqb; Disable the parallel GC.
307             The parallel GC is turned on automatically when parallel
308             execution is enabled with the <option>-N</option> option;
309             this option is available to turn it off if
310             necessary.</para>
311           
312           <para>Experiments have shown that parallel GC usually
313             results in a performance improvement given 3 cores or
314             more; with 2 cores it may or may not be beneficial,
315             depending on the workload.  Bigger heaps work better with
316             parallel GC, so set your <option>-H</option> value high (3
317             or more times the maximum residency).  Look at the timing
318             stats with <option>+RTS -s</option> to see whether you're
319             getting any benefit from parallel GC or not.  If you find
320             parallel GC is significantly <emphasis>slower</emphasis>
321             (in elapsed time) than sequential GC, please report it as
322             a bug.</para>
323
324           <para>In GHC 6.10.1 it was possible to use a different
325             number of threads for GC than for execution, because the GC
326             used its own pool of threads.  Now, the GC uses the same
327             threads as the mutator (for executing the program).</para>
328         </listitem>
329       </varlistentry>        
330
331       <varlistentry>
332         <term>
333           <option>-qg<replaceable>n</replaceable></option>
334           <indexterm><primary><option>-qg</option><secondary>RTS
335           option</secondary></primary></indexterm>
336         </term>
337         <listitem>
338           <para>
339             &lsqb;Default: 1&rsqb; &lsqb;New in GHC 6.12.1&rsqb;
340             Enable the parallel GC only in
341             generation <replaceable>n</replaceable> and greater.
342             Parallel GC is often not worthwhile for collections in
343             generation 0 (the young generation), so it is enabled by
344             default only for collections in generation 1 (and higher,
345             if applicable).
346           </para>
347         </listitem>
348       </varlistentry>
349
350       <varlistentry>
351         <term>
352           <option>-H</option><replaceable>size</replaceable>
353           <indexterm><primary><option>-H</option></primary><secondary>RTS option</secondary></indexterm>
354           <indexterm><primary>heap size, suggested</primary></indexterm>
355         </term>
356         <listitem>
357           <para>&lsqb;Default: 0&rsqb; This option provides a
358           &ldquo;suggested heap size&rdquo; for the garbage collector.  The
359           garbage collector will use about this much memory until the
360           program residency grows and the heap size needs to be
361           expanded to retain reasonable performance.</para>
362
363           <para>By default, the heap will start small, and grow and
364           shrink as necessary.  This can be bad for performance, so if
365           you have plenty of memory it's worthwhile supplying a big
366           <option>-H</option><replaceable>size</replaceable>.  For
367           improving GC performance, using
368           <option>-H</option><replaceable>size</replaceable> is
369           usually a better bet than
370           <option>-A</option><replaceable>size</replaceable>.</para>
371         </listitem>
372       </varlistentry>
373
374       <varlistentry>
375         <term>
376           <option>-I</option><replaceable>seconds</replaceable>
377           <indexterm><primary><option>-I</option></primary>
378             <secondary>RTS option</secondary>
379           </indexterm>
380           <indexterm><primary>idle GC</primary>
381           </indexterm>
382           </term>
383         <listitem>
384           <para>(default: 0.3) In the threaded and SMP versions of the RTS (see
385             <option>-threaded</option>, <xref linkend="options-linker" />), a
386             major GC is automatically performed if the runtime has been idle
387             (no Haskell computation has been running) for a period of time.
388             The amount of idle time which must pass before a GC is performed is
389             set by the <option>-I</option><replaceable>seconds</replaceable>
390             option.  Specifying <option>-I0</option> disables the idle GC.</para>
391
392           <para>For an interactive application, it is probably a good idea to
393             use the idle GC, because this will allow finalizers to run and
394             deadlocked threads to be detected in the idle time when no Haskell
395             computation is happening.  Also, it will mean that a GC is less
396             likely to happen when the application is busy, and so
397             responsiveness may be improved.   However, if the amount of live data in
398             the heap is particularly large, then the idle GC can cause a
399             significant delay, and too small an interval could adversely affect
400             interactive responsiveness.</para>
401
402           <para>This is an experimental feature, please let us know if it
403             causes problems and/or could benefit from further tuning.</para>
404         </listitem>
405       </varlistentry>
406
407       <varlistentry>
408         <term>
409          <option>-k</option><replaceable>size</replaceable>
410          <indexterm><primary><option>-k</option></primary><secondary>RTS option</secondary></indexterm>
411          <indexterm><primary>stack, minimum size</primary></indexterm>
412         </term>
413         <listitem>
414           <para>&lsqb;Default: 1k&rsqb; Set the initial stack size for
415           new threads.  Thread stacks (including the main thread's
416           stack) live on the heap, and grow as required.  The default
417           value is good for concurrent applications with lots of small
418           threads; if your program doesn't fit this model then
419           increasing this option may help performance.</para>
420
421           <para>The main thread is normally started with a slightly
422           larger heap to cut down on unnecessary stack growth while
423           the program is starting up.</para>
424         </listitem>
425       </varlistentry>
426
427       <varlistentry>
428         <term>
429           <option>-K</option><replaceable>size</replaceable>
430           <indexterm><primary><option>-K</option></primary><secondary>RTS option</secondary></indexterm>
431           <indexterm><primary>stack, maximum size</primary></indexterm>
432         </term>
433         <listitem>
434           <para>&lsqb;Default: 8M&rsqb; Set the maximum stack size for
435           an individual thread to <replaceable>size</replaceable>
436           bytes.  This option is there purely to stop the program
437           eating up all the available memory in the machine if it gets
438           into an infinite loop.</para>
439         </listitem>
440       </varlistentry>
441
442       <varlistentry>
443         <term>
444           <option>-m</option><replaceable>n</replaceable>
445           <indexterm><primary><option>-m</option></primary><secondary>RTS option</secondary></indexterm>
446           <indexterm><primary>heap, minimum free</primary></indexterm>
447         </term>
448         <listitem>
449           <para>Minimum &percnt; <replaceable>n</replaceable> of heap
450           which must be available for allocation.  The default is
451           3&percnt;.</para>
452         </listitem>
453       </varlistentry>
454
455       <varlistentry>
456         <term>
457           <option>-M</option><replaceable>size</replaceable>
458           <indexterm><primary><option>-M</option></primary><secondary>RTS option</secondary></indexterm>
459           <indexterm><primary>heap size, maximum</primary></indexterm>
460         </term>
461         <listitem>
462           <para>&lsqb;Default: unlimited&rsqb; Set the maximum heap size to
463           <replaceable>size</replaceable> bytes.  The heap normally
464           grows and shrinks according to the memory requirements of
465           the program.  The only reason for having this option is to
466           stop the heap growing without bound and filling up all the
467           available swap space, which at the least will result in the
468           program being summarily killed by the operating
469           system.</para>
470
471           <para>The maximum heap size also affects other garbage
472           collection parameters: when the amount of live data in the
473           heap exceeds a certain fraction of the maximum heap size,
474           compacting collection will be automatically enabled for the
475           oldest generation, and the <option>-F</option> parameter
476           will be reduced in order to avoid exceeding the maximum heap
477           size.</para>
478         </listitem>
479       </varlistentry>
480
481       <varlistentry>
482         <term>
483           <option>-t</option><optional><replaceable>file</replaceable></optional>
484           <indexterm><primary><option>-t</option></primary><secondary>RTS option</secondary></indexterm>
485         </term>
486         <term>
487           <option>-s</option><optional><replaceable>file</replaceable></optional>
488           <indexterm><primary><option>-s</option></primary><secondary>RTS option</secondary></indexterm>
489         </term>
490         <term>
491           <option>-S</option><optional><replaceable>file</replaceable></optional>
492           <indexterm><primary><option>-S</option></primary><secondary>RTS option</secondary></indexterm>
493         </term>
494         <term>
495           <option>--machine-readable</option>
496           <indexterm><primary><option>--machine-readable</option></primary><secondary>RTS option</secondary></indexterm>
497         </term>
498         <listitem>
499           <para>These options produce runtime-system statistics, such
500           as the amount of time spent executing the program and in the
501           garbage collector, the amount of memory allocated, the
502           maximum size of the heap, and so on.  The three
503           variants give different levels of detail:
504           <option>-t</option> produces a single line of output in the
505           same format as GHC's <option>-Rghc-timing</option> option,
506           <option>-s</option> produces a more detailed summary at the
507           end of the program, and <option>-S</option> additionally
508           produces information about each and every garbage
509           collection.</para>
510
511           <para>The output is placed in
512           <replaceable>file</replaceable>.  If
513           <replaceable>file</replaceable> is omitted, then the output
514           is sent to <constant>stderr</constant>.</para>
515
516     <para>
517         If you use the <literal>-t</literal> flag then, when your
518         program finishes, you will see something like this:
519     </para>
520
521 <programlisting>
522 &lt;&lt;ghc: 36169392 bytes, 69 GCs, 603392/1065272 avg/max bytes residency (2 samples), 3M in use, 0.00 INIT (0.00 elapsed), 0.02 MUT (0.02 elapsed), 0.07 GC (0.07 elapsed) :ghc&gt;&gt;
523 </programlisting>
524
525     <para>
526         This tells you:
527     </para>
528
529     <itemizedlist>
530       <listitem>
531         <para>
532           The total bytes allocated by the program. This may be less
533           than the peak memory use, as some may be freed. 
534         </para>
535       </listitem>
536       <listitem>
537         <para>
538           The total number of garbage collections that occurred.
539         </para>
540       </listitem>
541       <listitem>
542         <para>
543           The average and maximum space used by your program.
544           This is only checked during major garbage collections, so it
545           is only an approximation; the number of samples tells you how
546           many times it is checked.
547         </para>
548       </listitem>
549       <listitem>
550         <para>
551           The peak memory the RTS has allocated from the OS. 
552         </para>
553       </listitem>
554       <listitem>
555         <para>
556           The amount of CPU time and elapsed wall clock time while
557           initialising the runtime system (INIT), running the program
558           itself (MUT, the mutator), and garbage collecting (GC).
559         </para>
560       </listitem>
561     </itemizedlist>
562
563     <para>
564         You can also get this in a more future-proof, machine readable
565         format, with <literal>-t --machine-readable</literal>:
566     </para>
567
568 <programlisting>
569  [("bytes allocated", "36169392")
570  ,("num_GCs", "69")
571  ,("average_bytes_used", "603392")
572  ,("max_bytes_used", "1065272")
573  ,("num_byte_usage_samples", "2")
574  ,("peak_megabytes_allocated", "3")
575  ,("init_cpu_seconds", "0.00")
576  ,("init_wall_seconds", "0.00")
577  ,("mutator_cpu_seconds", "0.02")
578  ,("mutator_wall_seconds", "0.02")
579  ,("GC_cpu_seconds", "0.07")
580  ,("GC_wall_seconds", "0.07")
581  ]
582 </programlisting>
583
584     <para>
585         If you use the <literal>-s</literal> flag then, when your
586         program finishes, you will see something like this (the exact
587         details will vary depending on what sort of RTS you have, e.g.
588         you will only see profiling data if your RTS is compiled for
589         profiling):
590     </para>
591
592 <programlisting>
593       36,169,392 bytes allocated in the heap
594        4,057,632 bytes copied during GC
595        1,065,272 bytes maximum residency (2 sample(s))
596           54,312 bytes maximum slop
597                3 MB total memory in use (0 MB lost due to fragmentation)
598
599   Generation 0:    67 collections,     0 parallel,  0.04s,  0.03s elapsed
600   Generation 1:     2 collections,     0 parallel,  0.03s,  0.04s elapsed
601
602   SPARKS: 359207 (557 converted, 149591 pruned)
603
604   INIT  time    0.00s  (  0.00s elapsed)
605   MUT   time    0.01s  (  0.02s elapsed)
606   GC    time    0.07s  (  0.07s elapsed)
607   EXIT  time    0.00s  (  0.00s elapsed)
608   Total time    0.08s  (  0.09s elapsed)
609
610   %GC time      89.5%  (75.3% elapsed)
611
612   Alloc rate    4,520,608,923 bytes per MUT second
613
614   Productivity  10.5% of total user, 9.1% of total elapsed
615 </programlisting>
616
617     <itemizedlist>
618       <listitem>
619         <para>
620         The "bytes allocated in the heap" is the total bytes allocated
621         by the program. This may be less than the peak memory use, as
622         some may be freed.
623         </para>
624       </listitem>
625       <listitem>
626         <para>
627         GHC uses a copying garbage collector. "bytes copied during GC" 
628         tells you how many bytes it had to copy during garbage collection.
629         </para>
630       </listitem>
631       <listitem>
632         <para>
633         The maximum space actually used by your program is the
634         "bytes maximum residency" figure. This is only checked during
635         major garbage collections, so it is only an approximation;
636         the number of samples tells you how many times it is checked.
637         </para>
638       </listitem>
639       <listitem>
640         <para>
641         The "bytes maximum slop" tells you the most space that is ever
642         wasted due to the way GHC packs data into so-called "megablocks".
643         </para>
644       </listitem>
645       <listitem>
646         <para>
647         The "total memory in use" tells you the peak memory the RTS has
648         allocated from the OS.
649         </para>
650       </listitem>
651       <listitem>
652         <para>
653         Next there is information about the garbage collections done.
654         For each generation it says how many garbage collections were
655         done, how many of those collections used multiple threads,
656         the total CPU time used for garbage collecting that generation,
657         and the total wall clock time elapsed while garbage collecting
658         that generation.
659         </para>
660       </listitem>
661       <listitem>
662         <para>The <literal>SPARKS</literal> statistic refers to the
663           use of <literal>Control.Parallel.par</literal> and related
664           functionality in the program.  Each spark represents a call
665           to <literal>par</literal>; a spark is "converted" when it is
666           executed in parallel; and a spark is "pruned" when it is
667           found to be already evaluated and is discarded from the pool
668           by the garbage collector.  Any remaining sparks are
669           discarded at the end of execution, so "converted" plus
670           "pruned" does not necessarily add up to the total.</para>
671       </listitem>
672       <listitem>
673         <para>
674         Next there is the CPU time and wall clock time elapsedm broken
675         down by what the runtiem system was doing at the time.
676         INIT is the runtime system initialisation.
677         MUT is the mutator time, i.e. the time spent actually running
678         your code.
679         GC is the time spent doing garbage collection.
680         RP is the time spent doing retainer profiling.
681         PROF is the time spent doing other profiling.
682         EXIT is the runtime system shutdown time.
683         And finally, Total is, of course, the total.
684         </para>
685         <para>
686         %GC time tells you what percentage GC is of Total.
687         "Alloc rate" tells you the "bytes allocated in the heap" divided
688         by the MUT CPU time.
689         "Productivity" tells you what percentage of the Total CPU and wall
690         clock elapsed times are spent in the mutator (MUT).
691         </para>
692       </listitem>
693     </itemizedlist>
694
695     <para>
696         The <literal>-S</literal> flag, as well as giving the same
697         output as the <literal>-s</literal> flag, prints information
698         about each GC as it happens:
699     </para>
700
701 <programlisting>
702     Alloc    Copied     Live    GC    GC     TOT     TOT  Page Flts
703     bytes     bytes     bytes  user  elap    user    elap
704    528496     47728    141512  0.01  0.02    0.02    0.02    0    0  (Gen:  1)
705 [...]
706    524944    175944   1726384  0.00  0.00    0.08    0.11    0    0  (Gen:  0)
707 </programlisting>
708
709     <para>
710         For each garbage collection, we print:
711     </para>
712
713     <itemizedlist>
714       <listitem>
715         <para>
716           How many bytes we allocated this garbage collection.
717         </para>
718       </listitem>
719       <listitem>
720         <para>
721           How many bytes we copied this garbage collection.
722         </para>
723       </listitem>
724       <listitem>
725         <para>
726           How many bytes are currently live.
727         </para>
728       </listitem>
729       <listitem>
730         <para>
731           How long this garbage collection took (CPU time and elapsed
732           wall clock time).
733         </para>
734       </listitem>
735       <listitem>
736         <para>
737           How long the program has been running (CPU time and elapsed
738           wall clock time).
739         </para>
740       </listitem>
741       <listitem>
742         <para>
743           How many page faults occured this garbage collection.
744         </para>
745       </listitem>
746       <listitem>
747         <para>
748           How many page faults occured since the end of the last garbage
749           collection.
750         </para>
751       </listitem>
752       <listitem>
753         <para>
754           Which generation is being garbage collected.
755         </para>
756       </listitem>
757     </itemizedlist>
758
759         </listitem>
760       </varlistentry>
761     </variablelist>
762
763   </sect2>
764
765   <sect2>
766     <title>RTS options for concurrency and parallelism</title>
767
768     <para>The RTS options related to concurrency are described in
769       <xref linkend="using-concurrent" />, and those for parallelism in
770       <xref linkend="parallel-options"/>.</para>
771   </sect2>
772
773   <sect2 id="rts-profiling">
774     <title>RTS options for profiling</title>
775
776     <para>Most profiling runtime options are only available when you
777     compile your program for profiling (see
778     <xref linkend="prof-compiler-options" />, and
779     <xref linkend="rts-options-heap-prof" /> for the runtime options).
780     However, there is one profiling option that is available
781     for ordinary non-profiled executables:</para>
782
783     <variablelist>
784       <varlistentry>
785         <term>
786           <option>-hT</option>
787           <indexterm><primary><option>-hT</option></primary><secondary>RTS
788               option</secondary></indexterm>
789         </term>
790         <listitem>
791           <para>Generates a basic heap profile, in the
792             file <literal><replaceable>prog</replaceable>.hp</literal>.
793             To produce the heap profile graph,
794             use <command>hp2ps</command> (see <xref linkend="hp2ps"
795                                                     />).  The basic heap profile is broken down by data
796             constructor, with other types of closures (functions, thunks,
797             etc.) grouped into broad categories
798             (e.g. <literal>FUN</literal>, <literal>THUNK</literal>).  To
799             get a more detailed profile, use the full profiling
800             support (<xref linkend="profiling" />).</para>
801         </listitem>
802       </varlistentry>
803     </variablelist>
804   </sect2>
805
806   <sect2 id="rts-options-debugging">
807     <title>RTS options for hackers, debuggers, and over-interested
808     souls</title>
809
810     <indexterm><primary>RTS options, hacking/debugging</primary></indexterm>
811
812     <para>These RTS options might be used (a)&nbsp;to avoid a GHC bug,
813     (b)&nbsp;to see &ldquo;what's really happening&rdquo;, or
814     (c)&nbsp;because you feel like it.  Not recommended for everyday
815     use!</para>
816
817     <variablelist>
818
819       <varlistentry>
820         <term>
821           <option>-B</option>
822           <indexterm><primary><option>-B</option></primary><secondary>RTS option</secondary></indexterm>
823         </term>
824         <listitem>
825           <para>Sound the bell at the start of each (major) garbage
826           collection.</para>
827
828           <para>Oddly enough, people really do use this option!  Our
829           pal in Durham (England), Paul Callaghan, writes: &ldquo;Some
830           people here use it for a variety of
831           purposes&mdash;honestly!&mdash;e.g., confirmation that the
832           code/machine is doing something, infinite loop detection,
833           gauging cost of recently added code. Certain people can even
834           tell what stage &lsqb;the program&rsqb; is in by the beep
835           pattern. But the major use is for annoying others in the
836           same office&hellip;&rdquo;</para>
837         </listitem>
838       </varlistentry>
839
840       <varlistentry>
841         <term>
842           <option>-D</option><replaceable>num</replaceable>
843           <indexterm><primary>-D</primary><secondary>RTS option</secondary></indexterm>
844         </term>
845         <listitem>
846           <para>An RTS debugging flag; varying quantities of output
847           depending on which bits are set in
848           <replaceable>num</replaceable>.  Only works if the RTS was
849           compiled with the <option>DEBUG</option> option.</para>
850         </listitem>
851       </varlistentry>
852
853       <varlistentry>
854         <term>
855           <option>-r</option><replaceable>file</replaceable>
856           <indexterm><primary><option>-r</option></primary><secondary>RTS option</secondary></indexterm>
857           <indexterm><primary>ticky ticky profiling</primary></indexterm>
858           <indexterm><primary>profiling</primary><secondary>ticky ticky</secondary></indexterm>
859         </term>
860         <listitem>
861           <para>Produce &ldquo;ticky-ticky&rdquo; statistics at the
862           end of the program run.  The <replaceable>file</replaceable>
863           business works just like on the <option>-S</option> RTS
864           option (above).</para>
865
866           <para>&ldquo;Ticky-ticky&rdquo; statistics are counts of
867           various program actions (updates, enters, etc.)  The program
868           must have been compiled using
869           <option>-ticky</option><indexterm><primary><option>-ticky</option></primary></indexterm>
870           (a.k.a. &ldquo;ticky-ticky profiling&rdquo;), and, for it to
871           be really useful, linked with suitable system libraries.
872           Not a trivial undertaking: consult the installation guide on
873           how to set things up for easy &ldquo;ticky-ticky&rdquo;
874           profiling.  For more information, see <xref
875           linkend="ticky-ticky"/>.</para>
876         </listitem>
877       </varlistentry>
878
879       <varlistentry>
880         <term>
881           <option>-xc</option>
882           <indexterm><primary><option>-xc</option></primary><secondary>RTS option</secondary></indexterm>
883         </term>
884         <listitem>
885           <para>(Only available when the program is compiled for
886           profiling.)  When an exception is raised in the program,
887           this option causes the current cost-centre-stack to be
888           dumped to <literal>stderr</literal>.</para>
889
890           <para>This can be particularly useful for debugging: if your
891           program is complaining about a <literal>head []</literal>
892           error and you haven't got a clue which bit of code is
893           causing it, compiling with <literal>-prof
894           -auto-all</literal> and running with <literal>+RTS -xc
895           -RTS</literal> will tell you exactly the call stack at the
896           point the error was raised.</para>
897
898           <para>The output contains one line for each exception raised
899           in the program (the program might raise and catch several
900           exceptions during its execution), where each line is of the
901           form:</para>
902
903 <screen>
904 &lt; cc<subscript>1</subscript>, ..., cc<subscript>n</subscript> &gt;
905 </screen>
906           <para>each <literal>cc</literal><subscript>i</subscript> is
907           a cost centre in the program (see <xref
908           linkend="cost-centres"/>), and the sequence represents the
909           &ldquo;call stack&rdquo; at the point the exception was
910           raised.  The leftmost item is the innermost function in the
911           call stack, and the rightmost item is the outermost
912           function.</para>
913
914         </listitem>
915       </varlistentry>
916
917       <varlistentry>
918         <term>
919           <option>-Z</option>
920           <indexterm><primary><option>-Z</option></primary><secondary>RTS option</secondary></indexterm>
921         </term>
922         <listitem>
923           <para>Turn <emphasis>off</emphasis> &ldquo;update-frame
924           squeezing&rdquo; at garbage-collection time.  (There's no
925           particularly good reason to turn it off, except to ensure
926           the accuracy of certain data collected regarding thunk entry
927           counts.)</para>
928         </listitem>
929       </varlistentry>
930     </variablelist>
931
932   </sect2>
933
934   <sect2 id="rts-hooks">
935     <title>&ldquo;Hooks&rdquo; to change RTS behaviour</title>
936
937     <indexterm><primary>hooks</primary><secondary>RTS</secondary></indexterm>
938     <indexterm><primary>RTS hooks</primary></indexterm>
939     <indexterm><primary>RTS behaviour, changing</primary></indexterm>
940
941     <para>GHC lets you exercise rudimentary control over the RTS
942     settings for any given program, by compiling in a
943     &ldquo;hook&rdquo; that is called by the run-time system.  The RTS
944     contains stub definitions for all these hooks, but by writing your
945     own version and linking it on the GHC command line, you can
946     override the defaults.</para>
947
948     <para>Owing to the vagaries of DLL linking, these hooks don't work
949     under Windows when the program is built dynamically.</para>
950
951     <para>The hook <literal>ghc_rts_opts</literal><indexterm><primary><literal>ghc_rts_opts</literal></primary>
952       </indexterm>lets you set RTS
953     options permanently for a given program.  A common use for this is
954     to give your program a default heap and/or stack size that is
955     greater than the default.  For example, to set <literal>-H128m
956     -K1m</literal>, place the following definition in a C source
957     file:</para>
958
959 <programlisting>
960 char *ghc_rts_opts = "-H128m -K1m";
961 </programlisting>
962
963     <para>Compile the C file, and include the object file on the
964     command line when you link your Haskell program.</para>
965
966     <para>These flags are interpreted first, before any RTS flags from
967     the <literal>GHCRTS</literal> environment variable and any flags
968     on the command line.</para>
969
970     <para>You can also change the messages printed when the runtime
971     system &ldquo;blows up,&rdquo; e.g., on stack overflow.  The hooks
972     for these are as follows:</para>
973
974     <variablelist>
975
976       <varlistentry>
977         <term>
978           <function>void OutOfHeapHook (unsigned long, unsigned long)</function>
979           <indexterm><primary><function>OutOfHeapHook</function></primary></indexterm>
980         </term>
981         <listitem>
982           <para>The heap-overflow message.</para>
983         </listitem>
984       </varlistentry>
985
986       <varlistentry>
987         <term>
988           <function>void StackOverflowHook (long int)</function>
989           <indexterm><primary><function>StackOverflowHook</function></primary></indexterm>
990         </term>
991         <listitem>
992           <para>The stack-overflow message.</para>
993         </listitem>
994       </varlistentry>
995
996       <varlistentry>
997         <term>
998           <function>void MallocFailHook (long int)</function>
999           <indexterm><primary><function>MallocFailHook</function></primary></indexterm>
1000         </term>
1001         <listitem>
1002           <para>The message printed if <function>malloc</function>
1003           fails.</para>
1004         </listitem>
1005       </varlistentry>
1006     </variablelist>
1007
1008     <para>For examples of the use of these hooks, see GHC's own
1009     versions in the file
1010     <filename>ghc/compiler/parser/hschooks.c</filename> in a GHC
1011     source tree.</para>
1012   </sect2>
1013
1014   <sect2>
1015     <title>Getting information about the RTS</title>
1016
1017     <indexterm><primary>RTS</primary></indexterm>
1018
1019     <para>It is possible to ask the RTS to give some information about
1020     itself. To do this, use the <option>--info</option> flag, e.g.</para>
1021 <screen>
1022 $ ./a.out +RTS --info
1023  [("GHC RTS", "Yes")
1024  ,("GHC version", "6.7")
1025  ,("RTS way", "rts_p")
1026  ,("Host platform", "x86_64-unknown-linux")
1027  ,("Build platform", "x86_64-unknown-linux")
1028  ,("Target platform", "x86_64-unknown-linux")
1029  ,("Compiler unregisterised", "NO")
1030  ,("Tables next to code", "YES")
1031  ]
1032 </screen>
1033     <para>The information is formatted such that it can be read as a
1034     of type <literal>[(String, String)]</literal>.</para>
1035   </sect2>
1036 </sect1>
1037
1038 <!-- Emacs stuff:
1039      ;;; Local Variables: ***
1040      ;;; mode: xml ***
1041      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***
1042      ;;; End: ***
1043  -->