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