document -eventlog and the associated runtime options
[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: 512k&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>-qg<optional><replaceable>gen</replaceable></optional></option>
302           <indexterm><primary><option>-qg</option><secondary>RTS
303           option</secondary></primary></indexterm>
304         </term>
305         <listitem>
306           <para>&lsqb;New in GHC 6.12.1&rsqb; &lsqb;Default: 0&rsqb;
307             Use parallel GC in
308             generation <replaceable>gen</replaceable> and higher.
309             Omitting <replaceable>gen</replaceable> turns off the
310             parallel GC completely, reverting to sequential GC.</para>
311           
312           <para>The default parallel GC settings are usually suitable
313             for parallel programs (i.e. those
314             using <literal>par</literal>, Strategies, or with multiple
315             threads).  However, it is sometimes beneficial to enable
316             the parallel GC for a single-threaded sequential program
317             too, especially if the program has a large amount of heap
318             data and GC is a significant fraction of runtime.  To use
319             the parallel GC in a sequential program, enable the
320             parallel runtime with a suitable <literal>-N</literal>
321             option, and additionally it might be beneficial to
322             restrict parallel GC to the old generation
323             with <literal>-qg1</literal>.</para>
324         </listitem>
325       </varlistentry>        
326
327       <varlistentry>
328         <term>
329           <option>-qb<optional><replaceable>gen</replaceable></optional></option>
330           <indexterm><primary><option>-qb</option><secondary>RTS
331           option</secondary></primary></indexterm>
332         </term>
333         <listitem>
334           <para>
335             &lsqb;New in GHC 6.12.1&rsqb; &lsqb;Default: 1&rsqb; Use
336             load-balancing in the parallel GC in
337             generation <replaceable>gen</replaceable> and higher.
338             Omitting <replaceable>gen</replaceable> disables
339             load-balancing entirely.</para>
340           
341           <para>
342             Load-balancing shares out the work of GC between the
343             available cores.  This is a good idea when the heap is
344             large and we need to parallelise the GC work, however it
345             is also pessimal for the short young-generation
346             collections in a parallel program, because it can harm
347             locality by moving data from the cache of the CPU where is
348             it being used to the cache of another CPU.  Hence the
349             default is to do load-balancing only in the
350             old-generation.  In fact, for a parallel program it is
351             sometimes beneficial to disable load-balancing entirely
352             with <literal>-qb</literal>.
353           </para>
354         </listitem>
355       </varlistentry>
356
357       <varlistentry>
358         <term>
359           <option>-H</option><replaceable>size</replaceable>
360           <indexterm><primary><option>-H</option></primary><secondary>RTS option</secondary></indexterm>
361           <indexterm><primary>heap size, suggested</primary></indexterm>
362         </term>
363         <listitem>
364           <para>&lsqb;Default: 0&rsqb; This option provides a
365           &ldquo;suggested heap size&rdquo; for the garbage collector.  The
366           garbage collector will use about this much memory until the
367           program residency grows and the heap size needs to be
368           expanded to retain reasonable performance.</para>
369
370           <para>By default, the heap will start small, and grow and
371           shrink as necessary.  This can be bad for performance, so if
372           you have plenty of memory it's worthwhile supplying a big
373           <option>-H</option><replaceable>size</replaceable>.  For
374           improving GC performance, using
375           <option>-H</option><replaceable>size</replaceable> is
376           usually a better bet than
377           <option>-A</option><replaceable>size</replaceable>.</para>
378         </listitem>
379       </varlistentry>
380
381       <varlistentry>
382         <term>
383           <option>-I</option><replaceable>seconds</replaceable>
384           <indexterm><primary><option>-I</option></primary>
385             <secondary>RTS option</secondary>
386           </indexterm>
387           <indexterm><primary>idle GC</primary>
388           </indexterm>
389           </term>
390         <listitem>
391           <para>(default: 0.3) In the threaded and SMP versions of the RTS (see
392             <option>-threaded</option>, <xref linkend="options-linker" />), a
393             major GC is automatically performed if the runtime has been idle
394             (no Haskell computation has been running) for a period of time.
395             The amount of idle time which must pass before a GC is performed is
396             set by the <option>-I</option><replaceable>seconds</replaceable>
397             option.  Specifying <option>-I0</option> disables the idle GC.</para>
398
399           <para>For an interactive application, it is probably a good idea to
400             use the idle GC, because this will allow finalizers to run and
401             deadlocked threads to be detected in the idle time when no Haskell
402             computation is happening.  Also, it will mean that a GC is less
403             likely to happen when the application is busy, and so
404             responsiveness may be improved.   However, if the amount of live data in
405             the heap is particularly large, then the idle GC can cause a
406             significant delay, and too small an interval could adversely affect
407             interactive responsiveness.</para>
408
409           <para>This is an experimental feature, please let us know if it
410             causes problems and/or could benefit from further tuning.</para>
411         </listitem>
412       </varlistentry>
413
414       <varlistentry>
415         <term>
416          <option>-k</option><replaceable>size</replaceable>
417          <indexterm><primary><option>-k</option></primary><secondary>RTS option</secondary></indexterm>
418          <indexterm><primary>stack, minimum size</primary></indexterm>
419         </term>
420         <listitem>
421           <para>&lsqb;Default: 1k&rsqb; Set the initial stack size for
422           new threads.  Thread stacks (including the main thread's
423           stack) live on the heap, and grow as required.  The default
424           value is good for concurrent applications with lots of small
425           threads; if your program doesn't fit this model then
426           increasing this option may help performance.</para>
427
428           <para>The main thread is normally started with a slightly
429           larger heap to cut down on unnecessary stack growth while
430           the program is starting up.</para>
431         </listitem>
432       </varlistentry>
433
434       <varlistentry>
435         <term>
436           <option>-K</option><replaceable>size</replaceable>
437           <indexterm><primary><option>-K</option></primary><secondary>RTS option</secondary></indexterm>
438           <indexterm><primary>stack, maximum size</primary></indexterm>
439         </term>
440         <listitem>
441           <para>&lsqb;Default: 8M&rsqb; Set the maximum stack size for
442           an individual thread to <replaceable>size</replaceable>
443           bytes.  This option is there purely to stop the program
444           eating up all the available memory in the machine if it gets
445           into an infinite loop.</para>
446         </listitem>
447       </varlistentry>
448
449       <varlistentry>
450         <term>
451           <option>-m</option><replaceable>n</replaceable>
452           <indexterm><primary><option>-m</option></primary><secondary>RTS option</secondary></indexterm>
453           <indexterm><primary>heap, minimum free</primary></indexterm>
454         </term>
455         <listitem>
456           <para>Minimum &percnt; <replaceable>n</replaceable> of heap
457           which must be available for allocation.  The default is
458           3&percnt;.</para>
459         </listitem>
460       </varlistentry>
461
462       <varlistentry>
463         <term>
464           <option>-M</option><replaceable>size</replaceable>
465           <indexterm><primary><option>-M</option></primary><secondary>RTS option</secondary></indexterm>
466           <indexterm><primary>heap size, maximum</primary></indexterm>
467         </term>
468         <listitem>
469           <para>&lsqb;Default: unlimited&rsqb; Set the maximum heap size to
470           <replaceable>size</replaceable> bytes.  The heap normally
471           grows and shrinks according to the memory requirements of
472           the program.  The only reason for having this option is to
473           stop the heap growing without bound and filling up all the
474           available swap space, which at the least will result in the
475           program being summarily killed by the operating
476           system.</para>
477
478           <para>The maximum heap size also affects other garbage
479           collection parameters: when the amount of live data in the
480           heap exceeds a certain fraction of the maximum heap size,
481           compacting collection will be automatically enabled for the
482           oldest generation, and the <option>-F</option> parameter
483           will be reduced in order to avoid exceeding the maximum heap
484           size.</para>
485         </listitem>
486       </varlistentry>
487
488       <varlistentry>
489         <term>
490           <option>-t</option><optional><replaceable>file</replaceable></optional>
491           <indexterm><primary><option>-t</option></primary><secondary>RTS option</secondary></indexterm>
492         </term>
493         <term>
494           <option>-s</option><optional><replaceable>file</replaceable></optional>
495           <indexterm><primary><option>-s</option></primary><secondary>RTS option</secondary></indexterm>
496         </term>
497         <term>
498           <option>-S</option><optional><replaceable>file</replaceable></optional>
499           <indexterm><primary><option>-S</option></primary><secondary>RTS option</secondary></indexterm>
500         </term>
501         <term>
502           <option>--machine-readable</option>
503           <indexterm><primary><option>--machine-readable</option></primary><secondary>RTS option</secondary></indexterm>
504         </term>
505         <listitem>
506           <para>These options produce runtime-system statistics, such
507           as the amount of time spent executing the program and in the
508           garbage collector, the amount of memory allocated, the
509           maximum size of the heap, and so on.  The three
510           variants give different levels of detail:
511           <option>-t</option> produces a single line of output in the
512           same format as GHC's <option>-Rghc-timing</option> option,
513           <option>-s</option> produces a more detailed summary at the
514           end of the program, and <option>-S</option> additionally
515           produces information about each and every garbage
516           collection.</para>
517
518           <para>The output is placed in
519           <replaceable>file</replaceable>.  If
520           <replaceable>file</replaceable> is omitted, then the output
521           is sent to <constant>stderr</constant>.</para>
522
523     <para>
524         If you use the <literal>-t</literal> flag then, when your
525         program finishes, you will see something like this:
526     </para>
527
528 <programlisting>
529 &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;
530 </programlisting>
531
532     <para>
533         This tells you:
534     </para>
535
536     <itemizedlist>
537       <listitem>
538         <para>
539           The total number of bytes allocated by the program over the
540           whole run.
541         </para>
542       </listitem>
543       <listitem>
544         <para>
545           The total number of garbage collections performed.
546         </para>
547       </listitem>
548       <listitem>
549         <para>
550           The average and maximum "residency", which is the amount of
551           live data in bytes.  The runtime can only determine the
552           amount of live data during a major GC, which is why the
553           number of samples corresponds to the number of major GCs
554           (and is usually relatively small).  To get a better picture
555           of the heap profile of your program, use
556           the <option>-hT</option> RTS option
557           (<xref linkend="rts-profiling" />).
558         </para>
559       </listitem>
560       <listitem>
561         <para>
562           The peak memory the RTS has allocated from the OS. 
563         </para>
564       </listitem>
565       <listitem>
566         <para>
567           The amount of CPU time and elapsed wall clock time while
568           initialising the runtime system (INIT), running the program
569           itself (MUT, the mutator), and garbage collecting (GC).
570         </para>
571       </listitem>
572     </itemizedlist>
573
574     <para>
575         You can also get this in a more future-proof, machine readable
576         format, with <literal>-t --machine-readable</literal>:
577     </para>
578
579 <programlisting>
580  [("bytes allocated", "36169392")
581  ,("num_GCs", "69")
582  ,("average_bytes_used", "603392")
583  ,("max_bytes_used", "1065272")
584  ,("num_byte_usage_samples", "2")
585  ,("peak_megabytes_allocated", "3")
586  ,("init_cpu_seconds", "0.00")
587  ,("init_wall_seconds", "0.00")
588  ,("mutator_cpu_seconds", "0.02")
589  ,("mutator_wall_seconds", "0.02")
590  ,("GC_cpu_seconds", "0.07")
591  ,("GC_wall_seconds", "0.07")
592  ]
593 </programlisting>
594
595     <para>
596         If you use the <literal>-s</literal> flag then, when your
597         program finishes, you will see something like this (the exact
598         details will vary depending on what sort of RTS you have, e.g.
599         you will only see profiling data if your RTS is compiled for
600         profiling):
601     </para>
602
603 <programlisting>
604       36,169,392 bytes allocated in the heap
605        4,057,632 bytes copied during GC
606        1,065,272 bytes maximum residency (2 sample(s))
607           54,312 bytes maximum slop
608                3 MB total memory in use (0 MB lost due to fragmentation)
609
610   Generation 0:    67 collections,     0 parallel,  0.04s,  0.03s elapsed
611   Generation 1:     2 collections,     0 parallel,  0.03s,  0.04s elapsed
612
613   SPARKS: 359207 (557 converted, 149591 pruned)
614
615   INIT  time    0.00s  (  0.00s elapsed)
616   MUT   time    0.01s  (  0.02s elapsed)
617   GC    time    0.07s  (  0.07s elapsed)
618   EXIT  time    0.00s  (  0.00s elapsed)
619   Total time    0.08s  (  0.09s elapsed)
620
621   %GC time      89.5%  (75.3% elapsed)
622
623   Alloc rate    4,520,608,923 bytes per MUT second
624
625   Productivity  10.5% of total user, 9.1% of total elapsed
626 </programlisting>
627
628     <itemizedlist>
629       <listitem>
630         <para>
631         The "bytes allocated in the heap" is the total bytes allocated
632         by the program over the whole run.
633         </para>
634       </listitem>
635       <listitem>
636         <para>
637         GHC uses a copying garbage collector by default. "bytes copied
638         during GC" tells you how many bytes it had to copy during
639         garbage collection.
640         </para>
641       </listitem>
642       <listitem>
643         <para>
644         The maximum space actually used by your program is the
645         "bytes maximum residency" figure. This is only checked during
646         major garbage collections, so it is only an approximation;
647         the number of samples tells you how many times it is checked.
648         </para>
649       </listitem>
650       <listitem>
651         <para>
652         The "bytes maximum slop" tells you the most space that is ever
653         wasted due to the way GHC allocates memory in blocks.  Slop is
654         memory at the end of a block that was wasted.  There's no way
655         to control this; we just like to see how much memory is being
656         lost this way.
657         </para>
658       </listitem>
659       <listitem>
660         <para>
661         The "total memory in use" tells you the peak memory the RTS has
662         allocated from the OS.
663         </para>
664       </listitem>
665       <listitem>
666         <para>
667         Next there is information about the garbage collections done.
668         For each generation it says how many garbage collections were
669         done, how many of those collections were done in parallel,
670         the total CPU time used for garbage collecting that generation,
671         and the total wall clock time elapsed while garbage collecting
672         that generation.
673         </para>
674       </listitem>
675       <listitem>
676         <para>The <literal>SPARKS</literal> statistic refers to the
677           use of <literal>Control.Parallel.par</literal> and related
678           functionality in the program.  Each spark represents a call
679           to <literal>par</literal>; a spark is "converted" when it is
680           executed in parallel; and a spark is "pruned" when it is
681           found to be already evaluated and is discarded from the pool
682           by the garbage collector.  Any remaining sparks are
683           discarded at the end of execution, so "converted" plus
684           "pruned" does not necessarily add up to the total.</para>
685       </listitem>
686       <listitem>
687         <para>
688         Next there is the CPU time and wall clock time elapsed broken
689         down by what the runtime system was doing at the time.
690         INIT is the runtime system initialisation.
691         MUT is the mutator time, i.e. the time spent actually running
692         your code.
693         GC is the time spent doing garbage collection.
694         RP is the time spent doing retainer profiling.
695         PROF is the time spent doing other profiling.
696         EXIT is the runtime system shutdown time.
697         And finally, Total is, of course, the total.
698         </para>
699         <para>
700         %GC time tells you what percentage GC is of Total.
701         "Alloc rate" tells you the "bytes allocated in the heap" divided
702         by the MUT CPU time.
703         "Productivity" tells you what percentage of the Total CPU and wall
704         clock elapsed times are spent in the mutator (MUT).
705         </para>
706       </listitem>
707     </itemizedlist>
708
709     <para>
710         The <literal>-S</literal> flag, as well as giving the same
711         output as the <literal>-s</literal> flag, prints information
712         about each GC as it happens:
713     </para>
714
715 <programlisting>
716     Alloc    Copied     Live    GC    GC     TOT     TOT  Page Flts
717     bytes     bytes     bytes  user  elap    user    elap
718    528496     47728    141512  0.01  0.02    0.02    0.02    0    0  (Gen:  1)
719 [...]
720    524944    175944   1726384  0.00  0.00    0.08    0.11    0    0  (Gen:  0)
721 </programlisting>
722
723     <para>
724         For each garbage collection, we print:
725     </para>
726
727     <itemizedlist>
728       <listitem>
729         <para>
730           How many bytes we allocated this garbage collection.
731         </para>
732       </listitem>
733       <listitem>
734         <para>
735           How many bytes we copied this garbage collection.
736         </para>
737       </listitem>
738       <listitem>
739         <para>
740           How many bytes are currently live.
741         </para>
742       </listitem>
743       <listitem>
744         <para>
745           How long this garbage collection took (CPU time and elapsed
746           wall clock time).
747         </para>
748       </listitem>
749       <listitem>
750         <para>
751           How long the program has been running (CPU time and elapsed
752           wall clock time).
753         </para>
754       </listitem>
755       <listitem>
756         <para>
757           How many page faults occured this garbage collection.
758         </para>
759       </listitem>
760       <listitem>
761         <para>
762           How many page faults occured since the end of the last garbage
763           collection.
764         </para>
765       </listitem>
766       <listitem>
767         <para>
768           Which generation is being garbage collected.
769         </para>
770       </listitem>
771     </itemizedlist>
772
773         </listitem>
774       </varlistentry>
775     </variablelist>
776
777   </sect2>
778
779   <sect2>
780     <title>RTS options for concurrency and parallelism</title>
781
782     <para>The RTS options related to concurrency are described in
783       <xref linkend="using-concurrent" />, and those for parallelism in
784       <xref linkend="parallel-options"/>.</para>
785   </sect2>
786
787   <sect2 id="rts-profiling">
788     <title>RTS options for profiling</title>
789
790     <para>Most profiling runtime options are only available when you
791     compile your program for profiling (see
792     <xref linkend="prof-compiler-options" />, and
793     <xref linkend="rts-options-heap-prof" /> for the runtime options).
794     However, there is one profiling option that is available
795     for ordinary non-profiled executables:</para>
796
797     <variablelist>
798       <varlistentry>
799         <term>
800           <option>-hT</option>
801           <indexterm><primary><option>-hT</option></primary><secondary>RTS
802               option</secondary></indexterm>
803         </term>
804         <listitem>
805           <para>Generates a basic heap profile, in the
806             file <literal><replaceable>prog</replaceable>.hp</literal>.
807             To produce the heap profile graph,
808             use <command>hp2ps</command> (see <xref linkend="hp2ps"
809                                                     />).  The basic heap profile is broken down by data
810             constructor, with other types of closures (functions, thunks,
811             etc.) grouped into broad categories
812             (e.g. <literal>FUN</literal>, <literal>THUNK</literal>).  To
813             get a more detailed profile, use the full profiling
814             support (<xref linkend="profiling" />).</para>
815         </listitem>
816       </varlistentry>
817     </variablelist>
818   </sect2>
819
820   <sect2 id="rts-eventlog">
821     <title>Tracing</title>
822
823     <indexterm><primary>tracing</primary></indexterm>
824     <indexterm><primary>events</primary></indexterm>
825     <indexterm><primary>eventlog files</primary></indexterm>
826
827     <para>
828       When the program is linked with the <option>-eventlog</option>
829       option (<xref linkend="options-linker" />), runtime events can
830       be logged in two ways:
831     </para>
832
833     <itemizedlist>
834       <listitem>
835         <para>
836           In binary format to a file for later analysis by a
837           variety of tools.  One such tool
838           is <ulink url="http://hackage.haskell.org/package/ThreadScope">ThreadScope</ulink><indexterm><primary>ThreadScope</primary></indexterm>,
839           which interprets the event log to produce a visual parallel
840           execution profile of the program.
841         </para>
842       </listitem>
843       <listitem>
844         <para>
845           As text to standard output, for debugging purposes.
846         </para>
847       </listitem>
848     </itemizedlist>
849
850     <variablelist>
851       <varlistentry>
852         <term>
853           <option>-l<optional><replaceable>type</replaceable></optional></option>
854           <indexterm><primary><option>-l</option></primary><secondary>RTS option</secondary></indexterm>
855         </term>
856         <listitem>
857           <para>
858             Log events in binary format to the
859             file <filename><replaceable>program</replaceable>.eventlog</filename>,
860             where <replaceable>type</replaceable> indicates the type
861             of events to log.  Currently there is only one type
862             supported: <literal>-ls</literal>, for scheduler events.
863           </para>
864
865           <para>
866             The format of the log file is described by the header
867             <filename>EventLogFormat.h</filename> that comes with
868             GHC, and it can be parsed in Haskell using
869             the <ulink url="http://hackage.haskell.org/package/ghc-events">ghc-events</ulink>
870             library.  To dump the contents of
871             a <literal>.eventlog</literal> file as text, use the
872             tool <literal>show-ghc-events</literal> that comes with
873             the <ulink url="http://hackage.haskell.org/package/ghc-events">ghc-events</ulink>
874             package.
875           </para>
876         </listitem>
877       </varlistentry>
878
879       <varlistentry>
880         <term>
881           <option>-v</option>
882           <indexterm><primary><option>-v</option></primary><secondary>RTS option</secondary></indexterm>
883         </term>
884         <listitem>
885           <para>
886             Log events as text to standard output, instead of to
887             the <literal>.eventlog</literal> file.
888           </para>
889         </listitem>
890       </varlistentry>
891
892     </variablelist>
893
894     <para>
895       The debugging
896       options <option>-D<replaceable>x</replaceable></option> also
897       generate events which are logged using the tracing framework.
898       By default those events are dumped as text to stdout
899       (<option>-D<replaceable>x</replaceable></option>
900       implies <option>-v</option>), but they may instead be stored in
901       the binary eventlog file by using the <option>-l</option>
902       option.
903     </para>
904   </sect2>
905
906   <sect2 id="rts-options-debugging">
907     <title>RTS options for hackers, debuggers, and over-interested
908     souls</title>
909
910     <indexterm><primary>RTS options, hacking/debugging</primary></indexterm>
911
912     <para>These RTS options might be used (a)&nbsp;to avoid a GHC bug,
913     (b)&nbsp;to see &ldquo;what's really happening&rdquo;, or
914     (c)&nbsp;because you feel like it.  Not recommended for everyday
915     use!</para>
916
917     <variablelist>
918
919       <varlistentry>
920         <term>
921           <option>-B</option>
922           <indexterm><primary><option>-B</option></primary><secondary>RTS option</secondary></indexterm>
923         </term>
924         <listitem>
925           <para>Sound the bell at the start of each (major) garbage
926           collection.</para>
927
928           <para>Oddly enough, people really do use this option!  Our
929           pal in Durham (England), Paul Callaghan, writes: &ldquo;Some
930           people here use it for a variety of
931           purposes&mdash;honestly!&mdash;e.g., confirmation that the
932           code/machine is doing something, infinite loop detection,
933           gauging cost of recently added code. Certain people can even
934           tell what stage &lsqb;the program&rsqb; is in by the beep
935           pattern. But the major use is for annoying others in the
936           same office&hellip;&rdquo;</para>
937         </listitem>
938       </varlistentry>
939
940       <varlistentry>
941         <term>
942           <option>-D</option><replaceable>x</replaceable>
943           <indexterm><primary>-D</primary><secondary>RTS option</secondary></indexterm>
944         </term>
945         <listitem>
946           <para>
947             An RTS debugging flag; only availble if the program was
948             linked with the <option>-debug</option> option.  Various
949             values of <replaceable>x</replaceable> are provided to
950             enable debug messages and additional runtime sanity checks
951             in different subsystems in the RTS, for
952             example <literal>+RTS -Ds -RTS</literal> enables debug
953             messages from the scheduler.
954             Use <literal>+RTS&nbsp;-?</literal> to find out which
955             debug flags are supported.
956           </para>
957
958           <para>
959             Debug messages will be sent to the binary event log file
960             instead of stdout if the <option>-l</option> option is
961             added.  This might be useful for reducing the overhead of
962             debug tracing.
963           </para>
964         </listitem>
965       </varlistentry>
966
967       <varlistentry>
968         <term>
969           <option>-r</option><replaceable>file</replaceable>
970           <indexterm><primary><option>-r</option></primary><secondary>RTS option</secondary></indexterm>
971           <indexterm><primary>ticky ticky profiling</primary></indexterm>
972           <indexterm><primary>profiling</primary><secondary>ticky ticky</secondary></indexterm>
973         </term>
974         <listitem>
975           <para>Produce &ldquo;ticky-ticky&rdquo; statistics at the
976           end of the program run.  The <replaceable>file</replaceable>
977           business works just like on the <option>-S</option> RTS
978           option (above).</para>
979
980           <para>&ldquo;Ticky-ticky&rdquo; statistics are counts of
981           various program actions (updates, enters, etc.)  The program
982           must have been compiled using
983           <option>-ticky</option><indexterm><primary><option>-ticky</option></primary></indexterm>
984           (a.k.a. &ldquo;ticky-ticky profiling&rdquo;), and, for it to
985           be really useful, linked with suitable system libraries.
986           Not a trivial undertaking: consult the installation guide on
987           how to set things up for easy &ldquo;ticky-ticky&rdquo;
988           profiling.  For more information, see <xref
989           linkend="ticky-ticky"/>.</para>
990         </listitem>
991       </varlistentry>
992
993       <varlistentry>
994         <term>
995           <option>-xc</option>
996           <indexterm><primary><option>-xc</option></primary><secondary>RTS option</secondary></indexterm>
997         </term>
998         <listitem>
999           <para>(Only available when the program is compiled for
1000           profiling.)  When an exception is raised in the program,
1001           this option causes the current cost-centre-stack to be
1002           dumped to <literal>stderr</literal>.</para>
1003
1004           <para>This can be particularly useful for debugging: if your
1005           program is complaining about a <literal>head []</literal>
1006           error and you haven't got a clue which bit of code is
1007           causing it, compiling with <literal>-prof
1008           -auto-all</literal> and running with <literal>+RTS -xc
1009           -RTS</literal> will tell you exactly the call stack at the
1010           point the error was raised.</para>
1011
1012           <para>The output contains one line for each exception raised
1013           in the program (the program might raise and catch several
1014           exceptions during its execution), where each line is of the
1015           form:</para>
1016
1017 <screen>
1018 &lt; cc<subscript>1</subscript>, ..., cc<subscript>n</subscript> &gt;
1019 </screen>
1020           <para>each <literal>cc</literal><subscript>i</subscript> is
1021           a cost centre in the program (see <xref
1022           linkend="cost-centres"/>), and the sequence represents the
1023           &ldquo;call stack&rdquo; at the point the exception was
1024           raised.  The leftmost item is the innermost function in the
1025           call stack, and the rightmost item is the outermost
1026           function.</para>
1027
1028         </listitem>
1029       </varlistentry>
1030
1031       <varlistentry>
1032         <term>
1033           <option>-Z</option>
1034           <indexterm><primary><option>-Z</option></primary><secondary>RTS option</secondary></indexterm>
1035         </term>
1036         <listitem>
1037           <para>Turn <emphasis>off</emphasis> &ldquo;update-frame
1038           squeezing&rdquo; at garbage-collection time.  (There's no
1039           particularly good reason to turn it off, except to ensure
1040           the accuracy of certain data collected regarding thunk entry
1041           counts.)</para>
1042         </listitem>
1043       </varlistentry>
1044     </variablelist>
1045
1046   </sect2>
1047
1048   <sect2 id="rts-hooks">
1049     <title>&ldquo;Hooks&rdquo; to change RTS behaviour</title>
1050
1051     <indexterm><primary>hooks</primary><secondary>RTS</secondary></indexterm>
1052     <indexterm><primary>RTS hooks</primary></indexterm>
1053     <indexterm><primary>RTS behaviour, changing</primary></indexterm>
1054
1055     <para>GHC lets you exercise rudimentary control over the RTS
1056     settings for any given program, by compiling in a
1057     &ldquo;hook&rdquo; that is called by the run-time system.  The RTS
1058     contains stub definitions for all these hooks, but by writing your
1059     own version and linking it on the GHC command line, you can
1060     override the defaults.</para>
1061
1062     <para>Owing to the vagaries of DLL linking, these hooks don't work
1063     under Windows when the program is built dynamically.</para>
1064
1065     <para>The hook <literal>ghc_rts_opts</literal><indexterm><primary><literal>ghc_rts_opts</literal></primary>
1066       </indexterm>lets you set RTS
1067     options permanently for a given program.  A common use for this is
1068     to give your program a default heap and/or stack size that is
1069     greater than the default.  For example, to set <literal>-H128m
1070     -K1m</literal>, place the following definition in a C source
1071     file:</para>
1072
1073 <programlisting>
1074 char *ghc_rts_opts = "-H128m -K1m";
1075 </programlisting>
1076
1077     <para>Compile the C file, and include the object file on the
1078     command line when you link your Haskell program.</para>
1079
1080     <para>These flags are interpreted first, before any RTS flags from
1081     the <literal>GHCRTS</literal> environment variable and any flags
1082     on the command line.</para>
1083
1084     <para>You can also change the messages printed when the runtime
1085     system &ldquo;blows up,&rdquo; e.g., on stack overflow.  The hooks
1086     for these are as follows:</para>
1087
1088     <variablelist>
1089
1090       <varlistentry>
1091         <term>
1092           <function>void OutOfHeapHook (unsigned long, unsigned long)</function>
1093           <indexterm><primary><function>OutOfHeapHook</function></primary></indexterm>
1094         </term>
1095         <listitem>
1096           <para>The heap-overflow message.</para>
1097         </listitem>
1098       </varlistentry>
1099
1100       <varlistentry>
1101         <term>
1102           <function>void StackOverflowHook (long int)</function>
1103           <indexterm><primary><function>StackOverflowHook</function></primary></indexterm>
1104         </term>
1105         <listitem>
1106           <para>The stack-overflow message.</para>
1107         </listitem>
1108       </varlistentry>
1109
1110       <varlistentry>
1111         <term>
1112           <function>void MallocFailHook (long int)</function>
1113           <indexterm><primary><function>MallocFailHook</function></primary></indexterm>
1114         </term>
1115         <listitem>
1116           <para>The message printed if <function>malloc</function>
1117           fails.</para>
1118         </listitem>
1119       </varlistentry>
1120     </variablelist>
1121
1122     <para>For examples of the use of these hooks, see GHC's own
1123     versions in the file
1124     <filename>ghc/compiler/parser/hschooks.c</filename> in a GHC
1125     source tree.</para>
1126   </sect2>
1127
1128   <sect2>
1129     <title>Getting information about the RTS</title>
1130
1131     <indexterm><primary>RTS</primary></indexterm>
1132
1133     <para>It is possible to ask the RTS to give some information about
1134     itself. To do this, use the <option>--info</option> flag, e.g.</para>
1135 <screen>
1136 $ ./a.out +RTS --info
1137  [("GHC RTS", "YES")
1138  ,("GHC version", "6.7")
1139  ,("RTS way", "rts_p")
1140  ,("Host platform", "x86_64-unknown-linux")
1141  ,("Host architecture", "x86_64")
1142  ,("Host OS", "linux")
1143  ,("Host vendor", "unknown")
1144  ,("Build platform", "x86_64-unknown-linux")
1145  ,("Build architecture", "x86_64")
1146  ,("Build OS", "linux")
1147  ,("Build vendor", "unknown")
1148  ,("Target platform", "x86_64-unknown-linux")
1149  ,("Target architecture", "x86_64")
1150  ,("Target OS", "linux")
1151  ,("Target vendor", "unknown")
1152  ,("Word size", "64")
1153  ,("Compiler unregisterised", "NO")
1154  ,("Tables next to code", "YES")
1155  ]
1156 </screen>
1157     <para>The information is formatted such that it can be read as a
1158     of type <literal>[(String, String)]</literal>. Currently the following
1159     fields are present:</para>
1160
1161     <variablelist>
1162
1163       <varlistentry>
1164         <term><literal>GHC RTS</literal></term>
1165         <listitem>
1166           <para>Is this program linked against the GHC RTS? (always
1167           "YES").</para>
1168         </listitem>
1169       </varlistentry>
1170
1171       <varlistentry>
1172         <term><literal>GHC version</literal></term>
1173         <listitem>
1174           <para>The version of GHC used to compile this program.</para>
1175         </listitem>
1176       </varlistentry>
1177
1178       <varlistentry>
1179         <term><literal>RTS way</literal></term>
1180         <listitem>
1181           <para>The variant (&ldquo;way&rdquo;) of the runtime. The
1182           most common values are <literal>rts</literal> (vanilla),
1183           <literal>rts_thr</literal> (threaded runtime, i.e. linked using the
1184           <literal>-threaded</literal> option) and <literal>rts_p</literal>
1185           (profiling runtime, i.e. linked using the <literal>-prof</literal>
1186           option). Other variants include <literal>debug</literal>
1187           (linked using <literal>-debug</literal>),
1188           <literal>t</literal> (ticky-ticky profiling) and
1189           <literal>dyn</literal> (the RTS is
1190           linked in dynamically, i.e. a shared library, rather than statically
1191           linked into the executable itself). These can be combined,
1192           e.g. you might have <literal>rts_thr_debug_p</literal>.</para>
1193         </listitem>
1194       </varlistentry>
1195
1196       <varlistentry>
1197         <term>
1198             <literal>Target platform</literal>,
1199             <literal>Target architecture</literal>,
1200             <literal>Target OS</literal>,
1201             <literal>Target vendor</literal>
1202         </term>
1203         <listitem>
1204           <para>These are the platform the program is compiled to run on.</para>
1205         </listitem>
1206       </varlistentry>
1207
1208       <varlistentry>
1209         <term>
1210             <literal>Build platform</literal>,
1211             <literal>Build architecture</literal>,
1212             <literal>Build OS</literal>,
1213             <literal>Build vendor</literal>
1214         </term>
1215         <listitem>
1216           <para>These are the platform where the program was built
1217           on. (That is, the target platform of GHC itself.) Ordinarily
1218           this is identical to the target platform. (It could potentially
1219           be different if cross-compiling.)</para>
1220         </listitem>
1221       </varlistentry>
1222
1223       <varlistentry>
1224         <term>
1225             <literal>Host platform</literal>,
1226             <literal>Host architecture</literal>
1227             <literal>Host OS</literal>
1228             <literal>Host vendor</literal>
1229         </term>
1230         <listitem>
1231           <para>These are the platform where GHC itself was compiled.
1232           Again, this would normally be identical to the build and
1233           target platforms.</para>
1234         </listitem>
1235       </varlistentry>
1236
1237       <varlistentry>
1238         <term><literal>Word size</literal></term>
1239         <listitem>
1240           <para>Either <literal>"32"</literal> or <literal>"64"</literal>,
1241           reflecting the word size of the target platform.</para>
1242         </listitem>
1243       </varlistentry>
1244
1245       <varlistentry>
1246         <term><literal>Compiler unregistered</literal></term>
1247         <listitem>
1248           <para>Was this program compiled with an &ldquo;unregistered&rdquo;
1249           version of GHC? (I.e., a version of GHC that has no platform-specific
1250           optimisations compiled in, usually because this is a currently
1251           unsupported platform.) This value will usually be no, unless you're
1252           using an experimental build of GHC.</para>
1253         </listitem>
1254       </varlistentry>
1255
1256       <varlistentry>
1257         <term><literal>Tables next to code</literal></term>
1258         <listitem>
1259           <para>Putting info tables directly next to entry code is a useful
1260           performance optimisation that is not available on all platforms.
1261           This field tells you whether the program has been compiled with
1262           this optimisation. (Usually yes, except on unusual platforms.)</para>
1263         </listitem>
1264       </varlistentry>
1265
1266     </variablelist>
1267
1268   </sect2>
1269 </sect1>
1270
1271 <!-- Emacs stuff:
1272      ;;; Local Variables: ***
1273      ;;; mode: xml ***
1274      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***
1275      ;;; End: ***
1276  -->