1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <section id="runtime-control">
3 <title>Running a compiled program</title>
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>
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, thread scheduling, profiling, and
15 The RTS has a lot of options to control its behaviour. For
16 example, you can change the context-switch interval, the default
17 size of the heap, and enable heap profiling. These options can be
18 passed to the runtime system in a variety of different ways; the
19 next section (<xref linkend="setting-rts-options" />) describes
20 the various methods, and the following sections describe the RTS
24 <section id="setting-rts-options">
25 <title>Setting RTS options</title>
26 <indexterm><primary>RTS options, setting</primary></indexterm>
29 There are four ways to set RTS options:
34 on the command line between <literal>+RTS ... -RTS</literal>, when running the program
35 (<xref linkend="rts-opts-cmdline" />)
39 <para>at compile-time, using <option>--with-rtsopts</option>
40 (<xref linkend="rts-opts-compile-time" />)
44 <para>with the environment variable <envar>GHCRTS</envar>
45 (<xref linkend="rts-options-environment" />)
49 <para>by overriding “hooks” in the runtime system
50 (<xref linkend="rts-hooks" />)
56 <section id="rts-opts-cmdline">
57 <title>Setting RTS options on the command line</title>
60 If you set the <literal>-rtsopts</literal> flag appropriately
61 when linking (see <xref linkend="options-linker" />), you can
62 give RTS options on the command line when running your
67 When your Haskell program starts up, the RTS extracts
68 command-line arguments bracketed between
69 <option>+RTS</option><indexterm><primary><option>+RTS</option></primary></indexterm>
71 <option>-RTS</option><indexterm><primary><option>-RTS</option></primary></indexterm>
72 as its own. For example:
76 $ ghc prog.hs -rtsopts
77 [1 of 1] Compiling Main ( prog.hs, prog.o )
79 $ ./prog -f +RTS -H32m -S -RTS -h foo bar
84 snaffle <option>-H32m</option> <option>-S</option> for itself,
85 and the remaining arguments <literal>-f -h foo bar</literal>
86 will be available to your program if/when it calls
87 <function>System.Environment.getArgs</function>.
91 No <option>-RTS</option> option is required if the
92 runtime-system options extend to the end of the command line, as in
97 % hls -ltr /usr/etc +RTS -A5m
101 If you absolutely positively want all the rest of the options
102 in a command line to go to the program (and not the RTS), use a
103 <option>––RTS</option><indexterm><primary><option>--RTS</option></primary></indexterm>.
107 As always, for RTS options that take
108 <replaceable>size</replaceable>s: If the last character of
109 <replaceable>size</replaceable> is a K or k, multiply by 1000; if an
110 M or m, by 1,000,000; if a G or G, by 1,000,000,000. (And any
111 wraparound in the counters is <emphasis>your</emphasis>
116 Giving a <literal>+RTS -?</literal>
117 <indexterm><primary><option>-?</option></primary><secondary>RTS option</secondary></indexterm> option
118 will print out the RTS options actually available in your program
119 (which vary, depending on how you compiled).</para>
122 NOTE: since GHC is itself compiled by GHC, you can change RTS
123 options in the compiler using the normal
124 <literal>+RTS ... -RTS</literal>
125 combination. eg. to set the maximum heap
126 size for a compilation to 128M, you would add
127 <literal>+RTS -M128m -RTS</literal>
132 <section id="rts-opts-compile-time">
133 <title>Setting RTS options at compile time</title>
136 GHC lets you change the default RTS options for a program at
137 compile time, using the <literal>-with-rtsopts</literal>
138 flag (<xref linkend="options-linker" />). For example, to
139 set <literal>-H128m -K64m</literal>, link
140 with <literal>-with-rtsopts="-H128m -K64m"</literal>.
144 <section id="rts-options-environment">
145 <title>Setting RTS options with the <envar>GHCRTS</envar>
146 environment variable</title>
148 <indexterm><primary>RTS options</primary><secondary>from the environment</secondary></indexterm>
149 <indexterm><primary>environment variable</primary><secondary>for
150 setting RTS options</secondary></indexterm>
153 If the <literal>-rtsopts</literal> flag is set to
154 something other than <literal>none</literal> when linking,
155 RTS options are also taken from the environment variable
156 <envar>GHCRTS</envar><indexterm><primary><envar>GHCRTS</envar></primary>
157 </indexterm>. For example, to set the maximum heap size
158 to 2G for all GHC-compiled programs (using an
159 <literal>sh</literal>-like shell):
168 RTS options taken from the <envar>GHCRTS</envar> environment
169 variable can be overridden by options given on the command
174 Tip: setting something like <literal>GHCRTS=-M2G</literal>
175 in your environment is a handy way to avoid Haskell programs
176 growing beyond the real memory in your machine, which is
177 easy to do by accident and can cause the machine to slow to
178 a crawl until the OS decides to kill the process (and you
179 hope it kills the right one).
183 <section id="rts-hooks">
184 <title>“Hooks” to change RTS behaviour</title>
186 <indexterm><primary>hooks</primary><secondary>RTS</secondary></indexterm>
187 <indexterm><primary>RTS hooks</primary></indexterm>
188 <indexterm><primary>RTS behaviour, changing</primary></indexterm>
190 <para>GHC lets you exercise rudimentary control over the RTS
191 settings for any given program, by compiling in a
192 “hook” that is called by the run-time system. The RTS
193 contains stub definitions for all these hooks, but by writing your
194 own version and linking it on the GHC command line, you can
195 override the defaults.</para>
197 <para>Owing to the vagaries of DLL linking, these hooks don't work
198 under Windows when the program is built dynamically.</para>
200 <para>The hook <literal>ghc_rts_opts</literal><indexterm><primary><literal>ghc_rts_opts</literal></primary>
201 </indexterm>lets you set RTS
202 options permanently for a given program, in the same way as the
203 newer <option>-with-rtsopts</option> linker option does. A common use for this is
204 to give your program a default heap and/or stack size that is
205 greater than the default. For example, to set <literal>-H128m
206 -K1m</literal>, place the following definition in a C source
210 char *ghc_rts_opts = "-H128m -K1m";
213 <para>Compile the C file, and include the object file on the
214 command line when you link your Haskell program.</para>
216 <para>These flags are interpreted first, before any RTS flags from
217 the <literal>GHCRTS</literal> environment variable and any flags
218 on the command line.</para>
220 <para>You can also change the messages printed when the runtime
221 system “blows up,” e.g., on stack overflow. The hooks
222 for these are as follows:</para>
228 <function>void OutOfHeapHook (unsigned long, unsigned long)</function>
229 <indexterm><primary><function>OutOfHeapHook</function></primary></indexterm>
232 <para>The heap-overflow message.</para>
238 <function>void StackOverflowHook (long int)</function>
239 <indexterm><primary><function>StackOverflowHook</function></primary></indexterm>
242 <para>The stack-overflow message.</para>
248 <function>void MallocFailHook (long int)</function>
249 <indexterm><primary><function>MallocFailHook</function></primary></indexterm>
252 <para>The message printed if <function>malloc</function>
258 <para>For examples of the use of these hooks, see GHC's own
260 <filename>ghc/compiler/parser/hschooks.c</filename> in a GHC
266 <section id="rts-options-misc">
267 <title>Miscellaneous RTS options</title>
271 <term><option>-V<replaceable>secs</replaceable></option>
272 <indexterm><primary><option>-V</option></primary><secondary>RTS
273 option</secondary></indexterm></term>
275 <para>Sets the interval that the RTS clock ticks at. The
276 runtime uses a single timer signal to count ticks; this timer
277 signal is used to control the context switch timer (<xref
278 linkend="using-concurrent" />) and the heap profiling
279 timer <xref linkend="rts-options-heap-prof" />. Also, the
280 time profiler uses the RTS timer signal directly to record
281 time profiling samples.</para>
283 <para>Normally, setting the <option>-V</option> option
284 directly is not necessary: the resolution of the RTS timer is
285 adjusted automatically if a short interval is requested with
286 the <option>-C</option> or <option>-i</option> options.
287 However, setting <option>-V</option> is required in order to
288 increase the resolution of the time profiler.</para>
290 <para>Using a value of zero disables the RTS clock
291 completely, and has the effect of disabling timers that
292 depend on it: the context switch timer and the heap profiling
293 timer. Context switches will still happen, but
294 deterministically and at a rate much faster than normal.
295 Disabling the interval timer is useful for debugging, because
296 it eliminates a source of non-determinism at runtime.</para>
301 <term><option>--install-signal-handlers=<replaceable>yes|no</replaceable></option>
302 <indexterm><primary><option>--install-signal-handlers</option></primary><secondary>RTS
303 option</secondary></indexterm></term>
305 <para>If yes (the default), the RTS installs signal handlers to catch
306 things like ctrl-C. This option is primarily useful for when
307 you are using the Haskell code as a DLL, and want to set your
308 own signal handlers.</para>
311 with <option>--install-signal-handlers=no</option>, the RTS
312 interval timer signal is still enabled. The timer signal
313 is either SIGVTALRM or SIGALRM, depending on the RTS
314 configuration and OS capabilities. To disable the timer
315 signal, use the <literal>-V0</literal> RTS option (see
322 <term><option>-xm<replaceable>address</replaceable></option>
323 <indexterm><primary><option>-xm</option></primary><secondary>RTS
324 option</secondary></indexterm></term>
327 WARNING: this option is for working around memory
328 allocation problems only. Do not use unless GHCi fails
329 with a message like “<literal>failed to mmap() memory below 2Gb</literal>”. If you need to use this option to get GHCi working
330 on your machine, please file a bug.
334 On 64-bit machines, the RTS needs to allocate memory in the
335 low 2Gb of the address space. Support for this across
336 different operating systems is patchy, and sometimes fails.
337 This option is there to give the RTS a hint about where it
338 should be able to allocate memory in the low 2Gb of the
339 address space. For example, <literal>+RTS -xm20000000
340 -RTS</literal> would hint that the RTS should allocate
341 starting at the 0.5Gb mark. The default is to use the OS's
342 built-in support for allocating memory in the low 2Gb if
343 available (e.g. <literal>mmap</literal>
344 with <literal>MAP_32BIT</literal> on Linux), or
345 otherwise <literal>-xm40000000</literal>.
352 <section id="rts-options-gc">
353 <title>RTS options to control the garbage collector</title>
355 <indexterm><primary>garbage collector</primary><secondary>options</secondary></indexterm>
356 <indexterm><primary>RTS options</primary><secondary>garbage collection</secondary></indexterm>
358 <para>There are several options to give you precise control over
359 garbage collection. Hopefully, you won't need any of these in
360 normal operation, but there are several things that can be tweaked
361 for maximum performance.</para>
367 <option>-A</option><replaceable>size</replaceable>
368 <indexterm><primary><option>-A</option></primary><secondary>RTS option</secondary></indexterm>
369 <indexterm><primary>allocation area, size</primary></indexterm>
372 <para>[Default: 512k] Set the allocation area size
373 used by the garbage collector. The allocation area
374 (actually generation 0 step 0) is fixed and is never resized
375 (unless you use <option>-H</option>, below).</para>
377 <para>Increasing the allocation area size may or may not
378 give better performance (a bigger allocation area means
379 worse cache behaviour but fewer garbage collections and less
382 <para>With only 1 generation (<option>-G1</option>) the
383 <option>-A</option> option specifies the minimum allocation
384 area, since the actual size of the allocation area will be
385 resized according to the amount of data in the heap (see
386 <option>-F</option>, below).</para>
393 <indexterm><primary><option>-c</option></primary><secondary>RTS option</secondary></indexterm>
394 <indexterm><primary>garbage collection</primary><secondary>compacting</secondary></indexterm>
395 <indexterm><primary>compacting garbage collection</primary></indexterm>
398 <para>Use a compacting algorithm for collecting the oldest
399 generation. By default, the oldest generation is collected
400 using a copying algorithm; this option causes it to be
401 compacted in-place instead. The compaction algorithm is
402 slower than the copying algorithm, but the savings in memory
403 use can be considerable.</para>
405 <para>For a given heap size (using the <option>-H</option>
406 option), compaction can in fact reduce the GC cost by
407 allowing fewer GCs to be performed. This is more likely
408 when the ratio of live data to heap size is high, say
409 >30%.</para>
411 <para>NOTE: compaction doesn't currently work when a single
412 generation is requested using the <option>-G1</option>
418 <term><option>-c</option><replaceable>n</replaceable></term>
421 <para>[Default: 30] Automatically enable
422 compacting collection when the live data exceeds
423 <replaceable>n</replaceable>% of the maximum heap size
424 (see the <option>-M</option> option). Note that the maximum
425 heap size is unlimited by default, so this option has no
426 effect unless the maximum heap size is set with
427 <option>-M</option><replaceable>size</replaceable>. </para>
433 <option>-F</option><replaceable>factor</replaceable>
434 <indexterm><primary><option>-F</option></primary><secondary>RTS option</secondary></indexterm>
435 <indexterm><primary>heap size, factor</primary></indexterm>
439 <para>[Default: 2] This option controls the amount
440 of memory reserved for the older generations (and in the
441 case of a two space collector the size of the allocation
442 area) as a factor of the amount of live data. For example,
443 if there was 2M of live data in the oldest generation when
444 we last collected it, then by default we'll wait until it
445 grows to 4M before collecting it again.</para>
447 <para>The default seems to work well here. If you have
448 plenty of memory, it is usually better to use
449 <option>-H</option><replaceable>size</replaceable> than to
451 <option>-F</option><replaceable>factor</replaceable>.</para>
453 <para>The <option>-F</option> setting will be automatically
454 reduced by the garbage collector when the maximum heap size
455 (the <option>-M</option><replaceable>size</replaceable>
456 setting) is approaching.</para>
462 <option>-G</option><replaceable>generations</replaceable>
463 <indexterm><primary><option>-G</option></primary><secondary>RTS option</secondary></indexterm>
464 <indexterm><primary>generations, number of</primary></indexterm>
467 <para>[Default: 2] Set the number of generations
468 used by the garbage collector. The default of 2 seems to be
469 good, but the garbage collector can support any number of
470 generations. Anything larger than about 4 is probably not a
471 good idea unless your program runs for a
472 <emphasis>long</emphasis> time, because the oldest
473 generation will hardly ever get collected.</para>
475 <para>Specifying 1 generation with <option>+RTS -G1</option>
476 gives you a simple 2-space collector, as you would expect.
477 In a 2-space collector, the <option>-A</option> option (see
478 above) specifies the <emphasis>minimum</emphasis> allocation
479 area size, since the allocation area will grow with the
480 amount of live data in the heap. In a multi-generational
481 collector the allocation area is a fixed size (unless you
482 use the <option>-H</option> option, see below).</para>
488 <option>-qg<optional><replaceable>gen</replaceable></optional></option>
489 <indexterm><primary><option>-qg</option><secondary>RTS
490 option</secondary></primary></indexterm>
493 <para>[New in GHC 6.12.1] [Default: 0]
495 generation <replaceable>gen</replaceable> and higher.
496 Omitting <replaceable>gen</replaceable> turns off the
497 parallel GC completely, reverting to sequential GC.</para>
499 <para>The default parallel GC settings are usually suitable
500 for parallel programs (i.e. those
501 using <literal>par</literal>, Strategies, or with multiple
502 threads). However, it is sometimes beneficial to enable
503 the parallel GC for a single-threaded sequential program
504 too, especially if the program has a large amount of heap
505 data and GC is a significant fraction of runtime. To use
506 the parallel GC in a sequential program, enable the
507 parallel runtime with a suitable <literal>-N</literal>
508 option, and additionally it might be beneficial to
509 restrict parallel GC to the old generation
510 with <literal>-qg1</literal>.</para>
516 <option>-qb<optional><replaceable>gen</replaceable></optional></option>
517 <indexterm><primary><option>-qb</option><secondary>RTS
518 option</secondary></primary></indexterm>
522 [New in GHC 6.12.1] [Default: 1] Use
523 load-balancing in the parallel GC in
524 generation <replaceable>gen</replaceable> and higher.
525 Omitting <replaceable>gen</replaceable> disables
526 load-balancing entirely.</para>
529 Load-balancing shares out the work of GC between the
530 available cores. This is a good idea when the heap is
531 large and we need to parallelise the GC work, however it
532 is also pessimal for the short young-generation
533 collections in a parallel program, because it can harm
534 locality by moving data from the cache of the CPU where is
535 it being used to the cache of another CPU. Hence the
536 default is to do load-balancing only in the
537 old-generation. In fact, for a parallel program it is
538 sometimes beneficial to disable load-balancing entirely
539 with <literal>-qb</literal>.
546 <option>-H</option><replaceable>size</replaceable>
547 <indexterm><primary><option>-H</option></primary><secondary>RTS option</secondary></indexterm>
548 <indexterm><primary>heap size, suggested</primary></indexterm>
551 <para>[Default: 0] This option provides a
552 “suggested heap size” for the garbage collector. The
553 garbage collector will use about this much memory until the
554 program residency grows and the heap size needs to be
555 expanded to retain reasonable performance.</para>
557 <para>By default, the heap will start small, and grow and
558 shrink as necessary. This can be bad for performance, so if
559 you have plenty of memory it's worthwhile supplying a big
560 <option>-H</option><replaceable>size</replaceable>. For
561 improving GC performance, using
562 <option>-H</option><replaceable>size</replaceable> is
563 usually a better bet than
564 <option>-A</option><replaceable>size</replaceable>.</para>
570 <option>-I</option><replaceable>seconds</replaceable>
571 <indexterm><primary><option>-I</option></primary>
572 <secondary>RTS option</secondary>
574 <indexterm><primary>idle GC</primary>
578 <para>(default: 0.3) In the threaded and SMP versions of the RTS (see
579 <option>-threaded</option>, <xref linkend="options-linker" />), a
580 major GC is automatically performed if the runtime has been idle
581 (no Haskell computation has been running) for a period of time.
582 The amount of idle time which must pass before a GC is performed is
583 set by the <option>-I</option><replaceable>seconds</replaceable>
584 option. Specifying <option>-I0</option> disables the idle GC.</para>
586 <para>For an interactive application, it is probably a good idea to
587 use the idle GC, because this will allow finalizers to run and
588 deadlocked threads to be detected in the idle time when no Haskell
589 computation is happening. Also, it will mean that a GC is less
590 likely to happen when the application is busy, and so
591 responsiveness may be improved. However, if the amount of live data in
592 the heap is particularly large, then the idle GC can cause a
593 significant delay, and too small an interval could adversely affect
594 interactive responsiveness.</para>
596 <para>This is an experimental feature, please let us know if it
597 causes problems and/or could benefit from further tuning.</para>
603 <option>-ki</option><replaceable>size</replaceable>
604 <indexterm><primary><option>-k</option></primary><secondary>RTS option</secondary></indexterm>
605 <indexterm><primary>stack, initial size</primary></indexterm>
609 [Default: 1k] Set the initial stack size for new
610 threads. (Note: this flag used to be
611 simply <option>-k</option>, but was renamed
612 to <option>-ki</option> in GHC 7.2.1. The old name is
613 still accepted for backwards compatibility, but that may
614 be removed in a future version).
618 Thread stacks (including the main thread's stack) live on
619 the heap. As the stack grows, new stack chunks are added
620 as required; if the stack shrinks again, these extra stack
621 chunks are reclaimed by the garbage collector. The
622 default initial stack size is deliberately small, in order
623 to keep the time and space overhead for thread creation to
624 a minimum, and to make it practical to spawn threads for
625 even tiny pieces of work.
632 <option>-kc</option><replaceable>size</replaceable>
633 <indexterm><primary><option>-kc</option></primary><secondary>RTS
634 option</secondary></indexterm>
635 <indexterm><primary>stack</primary><secondary>chunk size</secondary></indexterm>
639 [Default: 32k] Set the size of “stack
640 chunks”. When a thread's current stack overflows, a
641 new stack chunk is created and added to the thread's
642 stack, until the limit set by <option>-K</option> is
647 The advantage of smaller stack chunks is that the garbage
648 collector can avoid traversing stack chunks if they are
649 known to be unmodified since the last collection, so
650 reducing the chunk size means that the garbage collector
651 can identify more stack as unmodified, and the GC overhead
652 might be reduced. On the other hand, making stack chunks
653 too small adds some overhead as there will be more
654 overflow/underflow between chunks. The default setting of
655 32k appears to be a reasonable compromise in most cases.
662 <option>-kb</option><replaceable>size</replaceable>
663 <indexterm><primary><option>-kc</option></primary><secondary>RTS
664 option</secondary></indexterm>
665 <indexterm><primary>stack</primary><secondary>chunk buffer size</secondary></indexterm>
669 [Default: 1k] Sets the stack chunk buffer size.
670 When a stack chunk overflows and a new stack chunk is
671 created, some of the data from the previous stack chunk is
672 moved into the new chunk, to avoid an immediate underflow
673 and repeated overflow/underflow at the boundary. The
674 amount of stack moved is set by the <option>-kb</option>
678 Note that to avoid wasting space, this value should
679 typically be less than 10% of the size of a stack
680 chunk (<option>-kc</option>), because in a chain of stack
681 chunks, each chunk will have a gap of unused space of this
689 <option>-K</option><replaceable>size</replaceable>
690 <indexterm><primary><option>-K</option></primary><secondary>RTS option</secondary></indexterm>
691 <indexterm><primary>stack, maximum size</primary></indexterm>
694 <para>[Default: 8M] Set the maximum stack size for
695 an individual thread to <replaceable>size</replaceable>
696 bytes. If the thread attempts to exceed this limit, it will
697 be send the <literal>StackOverflow</literal> exception.
700 This option is there mainly to stop the program eating up
701 all the available memory in the machine if it gets into an
709 <option>-m</option><replaceable>n</replaceable>
710 <indexterm><primary><option>-m</option></primary><secondary>RTS option</secondary></indexterm>
711 <indexterm><primary>heap, minimum free</primary></indexterm>
714 <para>Minimum % <replaceable>n</replaceable> of heap
715 which must be available for allocation. The default is
722 <option>-M</option><replaceable>size</replaceable>
723 <indexterm><primary><option>-M</option></primary><secondary>RTS option</secondary></indexterm>
724 <indexterm><primary>heap size, maximum</primary></indexterm>
727 <para>[Default: unlimited] Set the maximum heap size to
728 <replaceable>size</replaceable> bytes. The heap normally
729 grows and shrinks according to the memory requirements of
730 the program. The only reason for having this option is to
731 stop the heap growing without bound and filling up all the
732 available swap space, which at the least will result in the
733 program being summarily killed by the operating
736 <para>The maximum heap size also affects other garbage
737 collection parameters: when the amount of live data in the
738 heap exceeds a certain fraction of the maximum heap size,
739 compacting collection will be automatically enabled for the
740 oldest generation, and the <option>-F</option> parameter
741 will be reduced in order to avoid exceeding the maximum heap
748 <option>-t</option><optional><replaceable>file</replaceable></optional>
749 <indexterm><primary><option>-t</option></primary><secondary>RTS option</secondary></indexterm>
752 <option>-s</option><optional><replaceable>file</replaceable></optional>
753 <indexterm><primary><option>-s</option></primary><secondary>RTS option</secondary></indexterm>
756 <option>-S</option><optional><replaceable>file</replaceable></optional>
757 <indexterm><primary><option>-S</option></primary><secondary>RTS option</secondary></indexterm>
760 <option>--machine-readable</option>
761 <indexterm><primary><option>--machine-readable</option></primary><secondary>RTS option</secondary></indexterm>
764 <para>These options produce runtime-system statistics, such
765 as the amount of time spent executing the program and in the
766 garbage collector, the amount of memory allocated, the
767 maximum size of the heap, and so on. The three
768 variants give different levels of detail:
769 <option>-t</option> produces a single line of output in the
770 same format as GHC's <option>-Rghc-timing</option> option,
771 <option>-s</option> produces a more detailed summary at the
772 end of the program, and <option>-S</option> additionally
773 produces information about each and every garbage
776 <para>The output is placed in
777 <replaceable>file</replaceable>. If
778 <replaceable>file</replaceable> is omitted, then the output
779 is sent to <constant>stderr</constant>.</para>
782 If you use the <literal>-t</literal> flag then, when your
783 program finishes, you will see something like this:
787 <<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>>
797 The total number of bytes allocated by the program over the
803 The total number of garbage collections performed.
808 The average and maximum "residency", which is the amount of
809 live data in bytes. The runtime can only determine the
810 amount of live data during a major GC, which is why the
811 number of samples corresponds to the number of major GCs
812 (and is usually relatively small). To get a better picture
813 of the heap profile of your program, use
814 the <option>-hT</option> RTS option
815 (<xref linkend="rts-profiling" />).
820 The peak memory the RTS has allocated from the OS.
825 The amount of CPU time and elapsed wall clock time while
826 initialising the runtime system (INIT), running the program
827 itself (MUT, the mutator), and garbage collecting (GC).
833 You can also get this in a more future-proof, machine readable
834 format, with <literal>-t --machine-readable</literal>:
838 [("bytes allocated", "36169392")
840 ,("average_bytes_used", "603392")
841 ,("max_bytes_used", "1065272")
842 ,("num_byte_usage_samples", "2")
843 ,("peak_megabytes_allocated", "3")
844 ,("init_cpu_seconds", "0.00")
845 ,("init_wall_seconds", "0.00")
846 ,("mutator_cpu_seconds", "0.02")
847 ,("mutator_wall_seconds", "0.02")
848 ,("GC_cpu_seconds", "0.07")
849 ,("GC_wall_seconds", "0.07")
854 If you use the <literal>-s</literal> flag then, when your
855 program finishes, you will see something like this (the exact
856 details will vary depending on what sort of RTS you have, e.g.
857 you will only see profiling data if your RTS is compiled for
862 36,169,392 bytes allocated in the heap
863 4,057,632 bytes copied during GC
864 1,065,272 bytes maximum residency (2 sample(s))
865 54,312 bytes maximum slop
866 3 MB total memory in use (0 MB lost due to fragmentation)
868 Generation 0: 67 collections, 0 parallel, 0.04s, 0.03s elapsed
869 Generation 1: 2 collections, 0 parallel, 0.03s, 0.04s elapsed
871 SPARKS: 359207 (557 converted, 149591 pruned)
873 INIT time 0.00s ( 0.00s elapsed)
874 MUT time 0.01s ( 0.02s elapsed)
875 GC time 0.07s ( 0.07s elapsed)
876 EXIT time 0.00s ( 0.00s elapsed)
877 Total time 0.08s ( 0.09s elapsed)
879 %GC time 89.5% (75.3% elapsed)
881 Alloc rate 4,520,608,923 bytes per MUT second
883 Productivity 10.5% of total user, 9.1% of total elapsed
889 The "bytes allocated in the heap" is the total bytes allocated
890 by the program over the whole run.
895 GHC uses a copying garbage collector by default. "bytes copied
896 during GC" tells you how many bytes it had to copy during
902 The maximum space actually used by your program is the
903 "bytes maximum residency" figure. This is only checked during
904 major garbage collections, so it is only an approximation;
905 the number of samples tells you how many times it is checked.
910 The "bytes maximum slop" tells you the most space that is ever
911 wasted due to the way GHC allocates memory in blocks. Slop is
912 memory at the end of a block that was wasted. There's no way
913 to control this; we just like to see how much memory is being
919 The "total memory in use" tells you the peak memory the RTS has
920 allocated from the OS.
925 Next there is information about the garbage collections done.
926 For each generation it says how many garbage collections were
927 done, how many of those collections were done in parallel,
928 the total CPU time used for garbage collecting that generation,
929 and the total wall clock time elapsed while garbage collecting
934 <para>The <literal>SPARKS</literal> statistic refers to the
935 use of <literal>Control.Parallel.par</literal> and related
936 functionality in the program. Each spark represents a call
937 to <literal>par</literal>; a spark is "converted" when it is
938 executed in parallel; and a spark is "pruned" when it is
939 found to be already evaluated and is discarded from the pool
940 by the garbage collector. Any remaining sparks are
941 discarded at the end of execution, so "converted" plus
942 "pruned" does not necessarily add up to the total.</para>
946 Next there is the CPU time and wall clock time elapsed broken
947 down by what the runtime system was doing at the time.
948 INIT is the runtime system initialisation.
949 MUT is the mutator time, i.e. the time spent actually running
951 GC is the time spent doing garbage collection.
952 RP is the time spent doing retainer profiling.
953 PROF is the time spent doing other profiling.
954 EXIT is the runtime system shutdown time.
955 And finally, Total is, of course, the total.
958 %GC time tells you what percentage GC is of Total.
959 "Alloc rate" tells you the "bytes allocated in the heap" divided
961 "Productivity" tells you what percentage of the Total CPU and wall
962 clock elapsed times are spent in the mutator (MUT).
968 The <literal>-S</literal> flag, as well as giving the same
969 output as the <literal>-s</literal> flag, prints information
970 about each GC as it happens:
974 Alloc Copied Live GC GC TOT TOT Page Flts
975 bytes bytes bytes user elap user elap
976 528496 47728 141512 0.01 0.02 0.02 0.02 0 0 (Gen: 1)
978 524944 175944 1726384 0.00 0.00 0.08 0.11 0 0 (Gen: 0)
982 For each garbage collection, we print:
988 How many bytes we allocated this garbage collection.
993 How many bytes we copied this garbage collection.
998 How many bytes are currently live.
1003 How long this garbage collection took (CPU time and elapsed
1009 How long the program has been running (CPU time and elapsed
1015 How many page faults occured this garbage collection.
1020 How many page faults occured since the end of the last garbage
1026 Which generation is being garbage collected.
1038 <title>RTS options for concurrency and parallelism</title>
1040 <para>The RTS options related to concurrency are described in
1041 <xref linkend="using-concurrent" />, and those for parallelism in
1042 <xref linkend="parallel-options"/>.</para>
1045 <section id="rts-profiling">
1046 <title>RTS options for profiling</title>
1048 <para>Most profiling runtime options are only available when you
1049 compile your program for profiling (see
1050 <xref linkend="prof-compiler-options" />, and
1051 <xref linkend="rts-options-heap-prof" /> for the runtime options).
1052 However, there is one profiling option that is available
1053 for ordinary non-profiled executables:</para>
1058 <option>-hT</option>
1059 <indexterm><primary><option>-hT</option></primary><secondary>RTS
1060 option</secondary></indexterm>
1063 <para>Generates a basic heap profile, in the
1064 file <literal><replaceable>prog</replaceable>.hp</literal>.
1065 To produce the heap profile graph,
1066 use <command>hp2ps</command> (see <xref linkend="hp2ps"
1067 />). The basic heap profile is broken down by data
1068 constructor, with other types of closures (functions, thunks,
1069 etc.) grouped into broad categories
1070 (e.g. <literal>FUN</literal>, <literal>THUNK</literal>). To
1071 get a more detailed profile, use the full profiling
1072 support (<xref linkend="profiling" />).</para>
1078 <section id="rts-eventlog">
1079 <title>Tracing</title>
1081 <indexterm><primary>tracing</primary></indexterm>
1082 <indexterm><primary>events</primary></indexterm>
1083 <indexterm><primary>eventlog files</primary></indexterm>
1086 When the program is linked with the <option>-eventlog</option>
1087 option (<xref linkend="options-linker" />), runtime events can
1088 be logged in two ways:
1094 In binary format to a file for later analysis by a
1095 variety of tools. One such tool
1096 is <ulink url="http://hackage.haskell.org/package/ThreadScope">ThreadScope</ulink><indexterm><primary>ThreadScope</primary></indexterm>,
1097 which interprets the event log to produce a visual parallel
1098 execution profile of the program.
1103 As text to standard output, for debugging purposes.
1111 <option>-l<optional><replaceable>flags</replaceable></optional></option>
1112 <indexterm><primary><option>-l</option></primary><secondary>RTS option</secondary></indexterm>
1116 Log events in binary format to the
1117 file <filename><replaceable>program</replaceable>.eventlog</filename>,
1118 where <replaceable>flags</replaceable> is a sequence of
1119 zero or more characters indicating which kinds of events
1120 to log. Currently there is only one type
1121 supported: <literal>-ls</literal>, for scheduler events.
1125 The format of the log file is described by the header
1126 <filename>EventLogFormat.h</filename> that comes with
1127 GHC, and it can be parsed in Haskell using
1128 the <ulink url="http://hackage.haskell.org/package/ghc-events">ghc-events</ulink>
1129 library. To dump the contents of
1130 a <literal>.eventlog</literal> file as text, use the
1131 tool <literal>show-ghc-events</literal> that comes with
1132 the <ulink url="http://hackage.haskell.org/package/ghc-events">ghc-events</ulink>
1140 <option>-v</option><optional><replaceable>flags</replaceable></optional>
1141 <indexterm><primary><option>-v</option></primary><secondary>RTS option</secondary></indexterm>
1145 Log events as text to standard output, instead of to
1146 the <literal>.eventlog</literal> file.
1147 The <replaceable>flags</replaceable> are the same as
1148 for <option>-l</option>, with the additional
1149 option <literal>t</literal> which indicates that the
1150 each event printed should be preceded by a timestamp value
1151 (in the binary <literal>.eventlog</literal> file, all
1152 events are automatically associated with a timestamp).
1161 options <option>-D<replaceable>x</replaceable></option> also
1162 generate events which are logged using the tracing framework.
1163 By default those events are dumped as text to stdout
1164 (<option>-D<replaceable>x</replaceable></option>
1165 implies <option>-v</option>), but they may instead be stored in
1166 the binary eventlog file by using the <option>-l</option>
1171 <section id="rts-options-debugging">
1172 <title>RTS options for hackers, debuggers, and over-interested
1175 <indexterm><primary>RTS options, hacking/debugging</primary></indexterm>
1177 <para>These RTS options might be used (a) to avoid a GHC bug,
1178 (b) to see “what's really happening”, or
1179 (c) because you feel like it. Not recommended for everyday
1187 <indexterm><primary><option>-B</option></primary><secondary>RTS option</secondary></indexterm>
1190 <para>Sound the bell at the start of each (major) garbage
1193 <para>Oddly enough, people really do use this option! Our
1194 pal in Durham (England), Paul Callaghan, writes: “Some
1195 people here use it for a variety of
1196 purposes—honestly!—e.g., confirmation that the
1197 code/machine is doing something, infinite loop detection,
1198 gauging cost of recently added code. Certain people can even
1199 tell what stage [the program] is in by the beep
1200 pattern. But the major use is for annoying others in the
1201 same office…”</para>
1207 <option>-D</option><replaceable>x</replaceable>
1208 <indexterm><primary>-D</primary><secondary>RTS option</secondary></indexterm>
1212 An RTS debugging flag; only availble if the program was
1213 linked with the <option>-debug</option> option. Various
1214 values of <replaceable>x</replaceable> are provided to
1215 enable debug messages and additional runtime sanity checks
1216 in different subsystems in the RTS, for
1217 example <literal>+RTS -Ds -RTS</literal> enables debug
1218 messages from the scheduler.
1219 Use <literal>+RTS -?</literal> to find out which
1220 debug flags are supported.
1224 Debug messages will be sent to the binary event log file
1225 instead of stdout if the <option>-l</option> option is
1226 added. This might be useful for reducing the overhead of
1234 <option>-r</option><replaceable>file</replaceable>
1235 <indexterm><primary><option>-r</option></primary><secondary>RTS option</secondary></indexterm>
1236 <indexterm><primary>ticky ticky profiling</primary></indexterm>
1237 <indexterm><primary>profiling</primary><secondary>ticky ticky</secondary></indexterm>
1240 <para>Produce “ticky-ticky” statistics at the
1241 end of the program run (only available if the program was
1242 linked with <option>-debug</option>).
1243 The <replaceable>file</replaceable> business works just like
1244 on the <option>-S</option> RTS option, above.</para>
1246 <para>For more information on ticky-ticky profiling, see
1247 <xref linkend="ticky-ticky"/>.</para>
1253 <option>-xc</option>
1254 <indexterm><primary><option>-xc</option></primary><secondary>RTS option</secondary></indexterm>
1257 <para>(Only available when the program is compiled for
1258 profiling.) When an exception is raised in the program,
1259 this option causes the current cost-centre-stack to be
1260 dumped to <literal>stderr</literal>.</para>
1262 <para>This can be particularly useful for debugging: if your
1263 program is complaining about a <literal>head []</literal>
1264 error and you haven't got a clue which bit of code is
1265 causing it, compiling with <literal>-prof
1266 -auto-all</literal> and running with <literal>+RTS -xc
1267 -RTS</literal> will tell you exactly the call stack at the
1268 point the error was raised.</para>
1270 <para>The output contains one line for each exception raised
1271 in the program (the program might raise and catch several
1272 exceptions during its execution), where each line is of the
1276 < cc<subscript>1</subscript>, ..., cc<subscript>n</subscript> >
1278 <para>each <literal>cc</literal><subscript>i</subscript> is
1279 a cost centre in the program (see <xref
1280 linkend="cost-centres"/>), and the sequence represents the
1281 “call stack” at the point the exception was
1282 raised. The leftmost item is the innermost function in the
1283 call stack, and the rightmost item is the outermost
1292 <indexterm><primary><option>-Z</option></primary><secondary>RTS option</secondary></indexterm>
1295 <para>Turn <emphasis>off</emphasis> “update-frame
1296 squeezing” at garbage-collection time. (There's no
1297 particularly good reason to turn it off, except to ensure
1298 the accuracy of certain data collected regarding thunk entry
1307 <title>Getting information about the RTS</title>
1309 <indexterm><primary>RTS</primary></indexterm>
1311 <para>It is possible to ask the RTS to give some information about
1312 itself. To do this, use the <option>--info</option> flag, e.g.</para>
1314 $ ./a.out +RTS --info
1316 ,("GHC version", "6.7")
1317 ,("RTS way", "rts_p")
1318 ,("Host platform", "x86_64-unknown-linux")
1319 ,("Host architecture", "x86_64")
1320 ,("Host OS", "linux")
1321 ,("Host vendor", "unknown")
1322 ,("Build platform", "x86_64-unknown-linux")
1323 ,("Build architecture", "x86_64")
1324 ,("Build OS", "linux")
1325 ,("Build vendor", "unknown")
1326 ,("Target platform", "x86_64-unknown-linux")
1327 ,("Target architecture", "x86_64")
1328 ,("Target OS", "linux")
1329 ,("Target vendor", "unknown")
1330 ,("Word size", "64")
1331 ,("Compiler unregisterised", "NO")
1332 ,("Tables next to code", "YES")
1335 <para>The information is formatted such that it can be read as a
1336 of type <literal>[(String, String)]</literal>. Currently the following
1337 fields are present:</para>
1342 <term><literal>GHC RTS</literal></term>
1344 <para>Is this program linked against the GHC RTS? (always
1350 <term><literal>GHC version</literal></term>
1352 <para>The version of GHC used to compile this program.</para>
1357 <term><literal>RTS way</literal></term>
1359 <para>The variant (“way”) of the runtime. The
1360 most common values are <literal>rts</literal> (vanilla),
1361 <literal>rts_thr</literal> (threaded runtime, i.e. linked using the
1362 <literal>-threaded</literal> option) and <literal>rts_p</literal>
1363 (profiling runtime, i.e. linked using the <literal>-prof</literal>
1364 option). Other variants include <literal>debug</literal>
1365 (linked using <literal>-debug</literal>),
1366 <literal>t</literal> (ticky-ticky profiling) and
1367 <literal>dyn</literal> (the RTS is
1368 linked in dynamically, i.e. a shared library, rather than statically
1369 linked into the executable itself). These can be combined,
1370 e.g. you might have <literal>rts_thr_debug_p</literal>.</para>
1376 <literal>Target platform</literal>,
1377 <literal>Target architecture</literal>,
1378 <literal>Target OS</literal>,
1379 <literal>Target vendor</literal>
1382 <para>These are the platform the program is compiled to run on.</para>
1388 <literal>Build platform</literal>,
1389 <literal>Build architecture</literal>,
1390 <literal>Build OS</literal>,
1391 <literal>Build vendor</literal>
1394 <para>These are the platform where the program was built
1395 on. (That is, the target platform of GHC itself.) Ordinarily
1396 this is identical to the target platform. (It could potentially
1397 be different if cross-compiling.)</para>
1403 <literal>Host platform</literal>,
1404 <literal>Host architecture</literal>
1405 <literal>Host OS</literal>
1406 <literal>Host vendor</literal>
1409 <para>These are the platform where GHC itself was compiled.
1410 Again, this would normally be identical to the build and
1411 target platforms.</para>
1416 <term><literal>Word size</literal></term>
1418 <para>Either <literal>"32"</literal> or <literal>"64"</literal>,
1419 reflecting the word size of the target platform.</para>
1424 <term><literal>Compiler unregistered</literal></term>
1426 <para>Was this program compiled with an “unregistered”
1427 version of GHC? (I.e., a version of GHC that has no platform-specific
1428 optimisations compiled in, usually because this is a currently
1429 unsupported platform.) This value will usually be no, unless you're
1430 using an experimental build of GHC.</para>
1435 <term><literal>Tables next to code</literal></term>
1437 <para>Putting info tables directly next to entry code is a useful
1438 performance optimisation that is not available on all platforms.
1439 This field tells you whether the program has been compiled with
1440 this optimisation. (Usually yes, except on unusual platforms.)</para>
1450 ;;; Local Variables: ***
1451 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***