1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <sect1 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, profiling, etc.</para>
13 <para>You have some control over the behaviour of the RTS, by giving
14 special command-line arguments to your program.</para>
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>
20 <option>-RTS</option><indexterm><primary><option>-RTS</option></primary></indexterm>
21 as its own. For example:</para>
24 % ./a.out -f +RTS -p -S -RTS -h foo bar
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>
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
37 % hls -ltr /usr/etc +RTS -A5m
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>––RTS</option><indexterm><primary><option>--RTS</option></primary></indexterm>.</para>
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>
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>
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>
64 <sect2 id="rts-optinos-environment">
65 <title>Setting global RTS options</title>
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>
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>
82 <para>RTS options taken from the <envar>GHCRTS</envar> environment
83 variable can be overridden by options given on the command
88 <sect2 id="rts-options-misc">
89 <title>Miscellaneous RTS options</title>
93 <term><option>-V<replaceable>secs</replaceable></option>
94 <indexterm><primary><option>-V</option></primary><secondary>RTS
95 option</secondary></indexterm></term>
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>
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>
112 <para>Using a value of zero disables the RTS clock
113 completetly, 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>
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>
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>
136 <sect2 id="rts-options-gc">
137 <title>RTS options to control the garbage collector</title>
139 <indexterm><primary>garbage collector</primary><secondary>options</secondary></indexterm>
140 <indexterm><primary>RTS options</primary><secondary>garbage collection</secondary></indexterm>
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>
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>
156 <para>[Default: 256k] 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>
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
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>
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>
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>
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 >30%.</para>
195 <para>NOTE: compaction doesn't currently work when a single
196 generation is requested using the <option>-G1</option>
202 <term><option>-c</option><replaceable>n</replaceable></term>
205 <para>[Default: 30] Automatically enable
206 compacting collection when the live data exceeds
207 <replaceable>n</replaceable>% 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>
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>
223 <para>[Default: 2] 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>
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
235 <option>-F</option><replaceable>factor</replaceable>.</para>
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>
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>
251 <para>[Default: 2] 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>
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>
272 <option>-H</option><replaceable>size</replaceable>
273 <indexterm><primary><option>-H</option></primary><secondary>RTS option</secondary></indexterm>
274 <indexterm><primary>heap size, suggested</primary></indexterm>
277 <para>[Default: 0] This option provides a
278 “suggested heap size” for the garbage collector. The
279 garbage collector will use about this much memory until the
280 program residency grows and the heap size needs to be
281 expanded to retain reasonable performance.</para>
283 <para>By default, the heap will start small, and grow and
284 shrink as necessary. This can be bad for performance, so if
285 you have plenty of memory it's worthwhile supplying a big
286 <option>-H</option><replaceable>size</replaceable>. For
287 improving GC performance, using
288 <option>-H</option><replaceable>size</replaceable> is
289 usually a better bet than
290 <option>-A</option><replaceable>size</replaceable>.</para>
296 <option>-I</option><replaceable>seconds</replaceable>
297 <indexterm><primary><option>-I</option></primary>
298 <secondary>RTS option</secondary>
300 <indexterm><primary>idle GC</primary>
304 <para>(default: 0.3) In the threaded and SMP versions of the RTS (see
305 <option>-threaded</option>, <xref linkend="options-linker" />), a
306 major GC is automatically performed if the runtime has been idle
307 (no Haskell computation has been running) for a period of time.
308 The amount of idle time which must pass before a GC is performed is
309 set by the <option>-I</option><replaceable>seconds</replaceable>
310 option. Specifying <option>-I0</option> disables the idle GC.</para>
312 <para>For an interactive application, it is probably a good idea to
313 use the idle GC, because this will allow finalizers to run and
314 deadlocked threads to be detected in the idle time when no Haskell
315 computation is happening. Also, it will mean that a GC is less
316 likely to happen when the application is busy, and so
317 responsiveness may be improved. However, if the amount of live data in
318 the heap is particularly large, then the idle GC can cause a
319 significant delay, and too small an interval could adversely affect
320 interactive responsiveness.</para>
322 <para>This is an experimental feature, please let us know if it
323 causes problems and/or could benefit from further tuning.</para>
329 <option>-k</option><replaceable>size</replaceable>
330 <indexterm><primary><option>-k</option></primary><secondary>RTS option</secondary></indexterm>
331 <indexterm><primary>stack, minimum size</primary></indexterm>
334 <para>[Default: 1k] Set the initial stack size for
335 new threads. Thread stacks (including the main thread's
336 stack) live on the heap, and grow as required. The default
337 value is good for concurrent applications with lots of small
338 threads; if your program doesn't fit this model then
339 increasing this option may help performance.</para>
341 <para>The main thread is normally started with a slightly
342 larger heap to cut down on unnecessary stack growth while
343 the program is starting up.</para>
349 <option>-K</option><replaceable>size</replaceable>
350 <indexterm><primary><option>-K</option></primary><secondary>RTS option</secondary></indexterm>
351 <indexterm><primary>stack, maximum size</primary></indexterm>
354 <para>[Default: 8M] Set the maximum stack size for
355 an individual thread to <replaceable>size</replaceable>
356 bytes. This option is there purely to stop the program
357 eating up all the available memory in the machine if it gets
358 into an infinite loop.</para>
364 <option>-m</option><replaceable>n</replaceable>
365 <indexterm><primary><option>-m</option></primary><secondary>RTS option</secondary></indexterm>
366 <indexterm><primary>heap, minimum free</primary></indexterm>
369 <para>Minimum % <replaceable>n</replaceable> of heap
370 which must be available for allocation. The default is
377 <option>-M</option><replaceable>size</replaceable>
378 <indexterm><primary><option>-M</option></primary><secondary>RTS option</secondary></indexterm>
379 <indexterm><primary>heap size, maximum</primary></indexterm>
382 <para>[Default: unlimited] Set the maximum heap size to
383 <replaceable>size</replaceable> bytes. The heap normally
384 grows and shrinks according to the memory requirements of
385 the program. The only reason for having this option is to
386 stop the heap growing without bound and filling up all the
387 available swap space, which at the least will result in the
388 program being summarily killed by the operating
391 <para>The maximum heap size also affects other garbage
392 collection parameters: when the amount of live data in the
393 heap exceeds a certain fraction of the maximum heap size,
394 compacting collection will be automatically enabled for the
395 oldest generation, and the <option>-F</option> parameter
396 will be reduced in order to avoid exceeding the maximum heap
403 <option>-s</option><replaceable>file</replaceable>
404 <indexterm><primary><option>-s</option></primary><secondary>RTS option</secondary></indexterm>
407 <option>-S</option><replaceable>file</replaceable>
408 <indexterm><primary><option>-S</option></primary><secondary>RTS option</secondary></indexterm>
411 <para>Write modest (<option>-s</option>) or verbose
412 (<option>-S</option>) garbage-collector statistics into file
413 <replaceable>file</replaceable>. The default
414 <replaceable>file</replaceable> is
415 <filename><replaceable>program</replaceable>.stat</filename>. The
416 <replaceable>file</replaceable> <constant>stderr</constant>
417 is treated specially, with the output really being sent to
418 <constant>stderr</constant>.</para>
420 <para>This option is useful for watching how the storage
421 manager adjusts the heap size based on the current amount of
428 <option>-t<replaceable>file</replaceable></option>
429 <indexterm><primary><option>-t</option></primary><secondary>RTS option</secondary></indexterm>
432 <para>Write a one-line GC stats summary after running the
433 program. This output is in the same format as that produced
434 by the <option>-Rghc-timing</option> option.</para>
436 <para>As with <option>-s</option>, the default
437 <replaceable>file</replaceable> is
438 <filename><replaceable>program</replaceable>.stat</filename>. The
439 <replaceable>file</replaceable> <constant>stderr</constant>
440 is treated specially, with the output really being sent to
441 <constant>stderr</constant>.</para>
449 <title>RTS options for profiling and parallelism</title>
451 <para>The RTS options related to profiling are described in <xref
452 linkend="rts-options-heap-prof"/>, those for concurrency in
453 <xref linkend="using-concurrent" />, and those for parallelism in
454 <xref linkend="parallel-options"/>.</para>
457 <sect2 id="rts-options-debugging">
458 <title>RTS options for hackers, debuggers, and over-interested
461 <indexterm><primary>RTS options, hacking/debugging</primary></indexterm>
463 <para>These RTS options might be used (a) to avoid a GHC bug,
464 (b) to see “what's really happening”, or
465 (c) because you feel like it. Not recommended for everyday
473 <indexterm><primary><option>-B</option></primary><secondary>RTS option</secondary></indexterm>
476 <para>Sound the bell at the start of each (major) garbage
479 <para>Oddly enough, people really do use this option! Our
480 pal in Durham (England), Paul Callaghan, writes: “Some
481 people here use it for a variety of
482 purposes—honestly!—e.g., confirmation that the
483 code/machine is doing something, infinite loop detection,
484 gauging cost of recently added code. Certain people can even
485 tell what stage [the program] is in by the beep
486 pattern. But the major use is for annoying others in the
487 same office…”</para>
493 <option>-D</option><replaceable>num</replaceable>
494 <indexterm><primary>-D</primary><secondary>RTS option</secondary></indexterm>
497 <para>An RTS debugging flag; varying quantities of output
498 depending on which bits are set in
499 <replaceable>num</replaceable>. Only works if the RTS was
500 compiled with the <option>DEBUG</option> option.</para>
506 <option>-r</option><replaceable>file</replaceable>
507 <indexterm><primary><option>-r</option></primary><secondary>RTS option</secondary></indexterm>
508 <indexterm><primary>ticky ticky profiling</primary></indexterm>
509 <indexterm><primary>profiling</primary><secondary>ticky ticky</secondary></indexterm>
512 <para>Produce “ticky-ticky” statistics at the
513 end of the program run. The <replaceable>file</replaceable>
514 business works just like on the <option>-S</option> RTS
515 option (above).</para>
517 <para>“Ticky-ticky” statistics are counts of
518 various program actions (updates, enters, etc.) The program
519 must have been compiled using
520 <option>-ticky</option><indexterm><primary><option>-ticky</option></primary></indexterm>
521 (a.k.a. “ticky-ticky profiling”), and, for it to
522 be really useful, linked with suitable system libraries.
523 Not a trivial undertaking: consult the installation guide on
524 how to set things up for easy “ticky-ticky”
525 profiling. For more information, see <xref
526 linkend="ticky-ticky"/>.</para>
533 <indexterm><primary><option>-xc</option></primary><secondary>RTS option</secondary></indexterm>
536 <para>(Only available when the program is compiled for
537 profiling.) When an exception is raised in the program,
538 this option causes the current cost-centre-stack to be
539 dumped to <literal>stderr</literal>.</para>
541 <para>This can be particularly useful for debugging: if your
542 program is complaining about a <literal>head []</literal>
543 error and you haven't got a clue which bit of code is
544 causing it, compiling with <literal>-prof
545 -auto-all</literal> and running with <literal>+RTS -xc
546 -RTS</literal> will tell you exactly the call stack at the
547 point the error was raised.</para>
549 <para>The output contains one line for each exception raised
550 in the program (the program might raise and catch several
551 exceptions during its execution), where each line is of the
555 < cc<subscript>1</subscript>, ..., cc<subscript>n</subscript> >
557 <para>each <literal>cc</literal><subscript>i</subscript> is
558 a cost centre in the program (see <xref
559 linkend="cost-centres"/>), and the sequence represents the
560 “call stack” at the point the exception was
561 raised. The leftmost item is the innermost function in the
562 call stack, and the rightmost item is the outermost
571 <indexterm><primary><option>-Z</option></primary><secondary>RTS option</secondary></indexterm>
574 <para>Turn <emphasis>off</emphasis> “update-frame
575 squeezing” at garbage-collection time. (There's no
576 particularly good reason to turn it off, except to ensure
577 the accuracy of certain data collected regarding thunk entry
585 <sect2 id="rts-hooks">
586 <title>“Hooks” to change RTS behaviour</title>
588 <indexterm><primary>hooks</primary><secondary>RTS</secondary></indexterm>
589 <indexterm><primary>RTS hooks</primary></indexterm>
590 <indexterm><primary>RTS behaviour, changing</primary></indexterm>
592 <para>GHC lets you exercise rudimentary control over the RTS
593 settings for any given program, by compiling in a
594 “hook” that is called by the run-time system. The RTS
595 contains stub definitions for all these hooks, but by writing your
596 own version and linking it on the GHC command line, you can
597 override the defaults.</para>
599 <para>Owing to the vagaries of DLL linking, these hooks don't work
600 under Windows when the program is built dynamically.</para>
602 <para>The hook <literal>ghc_rts_opts</literal><indexterm><primary><literal>ghc_rts_opts</literal></primary>
603 </indexterm>lets you set RTS
604 options permanently for a given program. A common use for this is
605 to give your program a default heap and/or stack size that is
606 greater than the default. For example, to set <literal>-H128m
607 -K1m</literal>, place the following definition in a C source
611 char *ghc_rts_opts = "-H128m -K1m";
614 <para>Compile the C file, and include the object file on the
615 command line when you link your Haskell program.</para>
617 <para>These flags are interpreted first, before any RTS flags from
618 the <literal>GHCRTS</literal> environment variable and any flags
619 on the command line.</para>
621 <para>You can also change the messages printed when the runtime
622 system “blows up,” e.g., on stack overflow. The hooks
623 for these are as follows:</para>
629 <function>void OutOfHeapHook (unsigned long, unsigned long)</function>
630 <indexterm><primary><function>OutOfHeapHook</function></primary></indexterm>
633 <para>The heap-overflow message.</para>
639 <function>void StackOverflowHook (long int)</function>
640 <indexterm><primary><function>StackOverflowHook</function></primary></indexterm>
643 <para>The stack-overflow message.</para>
649 <function>void MallocFailHook (long int)</function>
650 <indexterm><primary><function>MallocFailHook</function></primary></indexterm>
653 <para>The message printed if <function>malloc</function>
659 <para>For examples of the use of these hooks, see GHC's own
661 <filename>ghc/compiler/parser/hschooks.c</filename> in a GHC
666 <title>Getting information about the RTS</title>
668 <indexterm><primary>RTS</primary></indexterm>
670 <para>It is possible to ask the RTS to give some information about
671 itself. To do this, use the <option>--info</option> flag, e.g.</para>
673 $ ./a.out +RTS --info
675 ,("GHC version", "6.7")
676 ,("RTS way", "rts_p")
677 ,("Host platform", "x86_64-unknown-linux")
678 ,("Build platform", "x86_64-unknown-linux")
679 ,("Target platform", "x86_64-unknown-linux")
680 ,("Compiler unregisterised", "NO")
681 ,("Tables next to code", "YES")
684 <para>The information is formatted such that it can be read as a
685 of type <literal>[(String, String)]</literal>.</para>
690 ;;; Local Variables: ***
692 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***