1 <sect1 id="runtime-control">
2 <title>Running a compiled program</title>
4 <indexterm><primary>runtime control of Haskell programs</primary></indexterm>
5 <indexterm><primary>running, compiled program</primary></indexterm>
6 <indexterm><primary>RTS options</primary></indexterm>
8 <para>To make an executable program, the GHC system compiles your
9 code and then links it with a non-trivial runtime system (RTS),
10 which handles storage management, profiling, etc.</para>
12 <para>You have some control over the behaviour of the RTS, by giving
13 special command-line arguments to your program.</para>
15 <para>When your Haskell program starts up, its RTS extracts
16 command-line arguments bracketed between
17 <option>+RTS</option><indexterm><primary><option>+RTS</option></primary></indexterm>
19 <option>-RTS</option><indexterm><primary><option>-RTS</option></primary></indexterm>
20 as its own. For example:</para>
23 % ./a.out -f +RTS -p -S -RTS -h foo bar
26 <para>The RTS will snaffle <option>-p</option> <option>-S</option>
27 for itself, and the remaining arguments <literal>-f -h foo bar</literal>
28 will be handed to your program if/when it calls
29 <function>System.getArgs</function>.</para>
31 <para>No <option>-RTS</option> option is required if the
32 runtime-system options extend to the end of the command line, as in
36 % hls -ltr /usr/etc +RTS -A5m
39 <para>If you absolutely positively want all the rest of the options
40 in a command line to go to the program (and not the RTS), use a
41 <option>--RTS</option><indexterm><primary><option>--RTS</option></primary></indexterm>.</para>
43 <para>As always, for RTS options that take
44 <replaceable>size</replaceable>s: If the last character of
45 <replaceable>size</replaceable> is a K or k, multiply by 1000; if an
46 M or m, by 1,000,000; if a G or G, by 1,000,000,000. (And any
47 wraparound in the counters is <emphasis>your</emphasis>
50 <para>Giving a <literal>+RTS -f</literal>
51 <indexterm><primary><option>-f</option></primary><secondary>RTS option</secondary></indexterm> option
52 will print out the RTS options actually available in your program
53 (which vary, depending on how you compiled).</para>
55 <para>NOTE: since GHC is itself compiled by GHC, you can change RTS
56 options in the compiler using the normal
57 <literal>+RTS ... -RTS</literal>
58 combination. eg. to increase the maximum heap
59 size for a compilation to 128M, you would add
60 <literal>+RTS -M128m -RTS</literal>
61 to the command line.</para>
63 <sect2 id="rts-optinos-environment">
64 <title>Setting global RTS options</title>
66 <indexterm><primary>RTS options</primary><secondary>from the environment</secondary></indexterm>
67 <indexterm><primary>environment variable</primary><secondary>for
68 setting RTS options</secondary></indexterm>
70 <para>RTS options are also taken from the environment variable
71 <envar>GHCRTS</envar><indexterm><primary><envar>GHCRTS</envar></primary>
72 </indexterm>. For example, to set the maximum heap size
73 to 128M for all GHC-compiled programs (using an
74 <literal>sh</literal>-like shell):</para>
81 <para>RTS options taken from the <envar>GHCRTS</envar> environment
82 variable can be overriden by options given on the command
87 <sect2 id="rts-options-gc">
88 <title>RTS options to control the garbage collector</title>
90 <indexterm><primary>garbage collector</primary><secondary>options</secondary></indexterm>
91 <indexterm><primary>RTS options</primary><secondary>garbage collection</secondary></indexterm>
93 <para>There are several options to give you precise control over
94 garbage collection. Hopefully, you won't need any of these in
95 normal operation, but there are several things that can be tweaked
96 for maximum performance.</para>
101 <term><option>-A</option><replaceable>size</replaceable></Term>
102 <indexterm><primary><option>-A</option></primary><secondary>RTS option</secondary></indexterm>
103 <indexterm><primary>allocation area, size</primary></indexterm>
105 <para>[Default: 256k] Set the allocation area size
106 used by the garbage collector. The allocation area
107 (actually generation 0 step 0) is fixed and is never resized
108 (unless you use <option>-H</option>, below).</para>
110 <para>Increasing the allocation area size may or may not
111 give better performance (a bigger allocation area means
112 worse cache behaviour but fewer garbage collections and less
115 <para>With only 1 generation (<option>-G1</option>) the
116 <option>-A</option> option specifies the minimum allocation
117 area, since the actual size of the allocation area will be
118 resized according to the amount of data in the heap (see
119 <option>-F</option>, below).</para>
124 <term><option>-c</option></term>
125 <indexterm><primary><option>-c</option></primary><secondary>RTS option</secondary>
127 <indexterm><primary>garbage collection</primary><secondary>compacting</secondary>
129 <indexterm><primary>compacting garbage collection</primary></indexterm>
132 <para>Use a compacting algorithm for collecting the oldest
133 generation. By default, the oldest generation is collected
134 using a copying algorithm; this option causes it to be
135 compacted in-place instead. The compaction algorithm is
136 slower than the copying algorithm, but the savings in memory
137 use can be considerable.</para>
139 <para>For a given heap size (using the <option>-H</option>
140 option), compaction can in fact reduce the GC cost by
141 allowing fewer GCs to be performed. This is more likely
142 when the ratio of live data to heap size is high, say
143 >30%.</para>
145 <para>NOTE: compaction doesn't currently work when a single
146 generation is requested using the <option>-G1</option>
152 <term><option>-F</option><replaceable>factor</replaceable></Term>
154 <indexterm><primary><option>-F</option></primary><secondary>RTS option</secondary></indexterm>
155 <indexterm><primary>heap size, factor</primary></indexterm>
157 <para>[Default: 2] This option controls the amount
158 of memory reserved for the older generations (and in the
159 case of a two space collector the size of the allocation
160 area) as a factor of the amount of live data. For example,
161 if there was 2M of live data in the oldest generation when
162 we last collected it, then by default we'll wait until it
163 grows to 4M before collecting it again.</para>
165 <para>The default seems to work well here. If you have
166 plenty of memory, it is usually better to use
167 <option>-H</option><replaceable>size</replaceable> than to
169 <option>-F</option><replaceable>factor</replaceable>.</para>
171 <para>The <option>-F</option> setting will be automatically
172 reduced by the garbage collector when the maximum heap size
173 (the <option>-M</option><replaceable>size</replaceable>
174 setting) is approaching.</para>
179 <term><option>-G</option><replaceable>generations</replaceable></Term>
180 <indexterm><primary><option>-G</option></primary><secondary>RTS option</secondary></indexterm>
181 <indexterm><primary>generations, number
182 of</primary></indexterm>
184 <para>[Default: 2] Set the number of generations
185 used by the garbage collector. The default of 2 seems to be
186 good, but the garbage collector can support any number of
187 generations. Anything larger than about 4 is probably not a
188 good idea unless your program runs for a
189 <emphasis>long</emphasis> time, because the oldest
190 generation will hardly ever get collected.</para>
192 <para>Specifying 1 generation with <option>+RTS -G1</option>
193 gives you a simple 2-space collector, as you would expect.
194 In a 2-space collector, the <option>-A</option> option (see
195 above) specifies the <Emphasis>minimum</Emphasis> allocation
196 area size, since the allocation area will grow with the
197 amount of live data in the heap. In a multi-generational
198 collector the allocation area is a fixed size (unless you
199 use the <option>-H</option> option, see below).</para>
204 <term><option>-H</option><replaceable>size</replaceable></term>
205 <indexterm><primary><option>-H</option></primary><secondary>RTS option</secondary></indexterm>
206 <indexterm><primary>heap size, suggested</primary></indexterm>
208 <para>[Default: 0] This option provides a
209 “suggested heap size” for the garbage collector. The
210 garbage collector will use about this much memory until the
211 program residency grows and the heap size needs to be
212 expanded to retain reasonable performance.</para>
214 <para>By default, the heap will start small, and grow and
215 shrink as necessary. This can be bad for performance, so if
216 you have plenty of memory it's worthwhile supplying a big
217 <option>-H</option><replaceable>size</replaceable>. For
218 improving GC performance, using
219 <option>-H</option><replaceable>size</replaceable> is
220 usually a better bet than
221 <option>-A</option><replaceable>size</replaceable>.</para>
226 <term><option>-k</option><replaceable>size</replaceable></term>
227 <indexterm><primary><option>-k</option></primary><secondary>RTS option</secondary></indexterm>
228 <indexterm><primary>stack, minimum size</primary></indexterm>
230 <para>[Default: 1k] Set the initial stack size for
231 new threads. Thread stacks (including the main thread's
232 stack) live on the heap, and grow as required. The default
233 value is good for concurrent applications with lots of small
234 threads; if your program doesn't fit this model then
235 increasing this option may help performance.</para>
237 <para>The main thread is normally started with a slightly
238 larger heap to cut down on unnecessary stack growth while
239 the program is starting up.</para>
244 <term><option>-K</option><replaceable>size</replaceable></term>
245 <indexterm><primary><option>-K</option></primary><secondary>RTS option</secondary></indexterm>
246 <indexterm><primary>stack, maximum size</primary></indexterm>
248 <para>[Default: 1M] Set the maximum stack size for
249 an individual thread to <replaceable>size</replaceable>
250 bytes. This option is there purely to stop the program
251 eating up all the available memory in the machine if it gets
252 into an infinite loop.</para>
257 <term><option>-m</option><replaceable>n</replaceable></term>
258 <indexterm><primary><option>-m</option></primary><secondary>RTS option</secondary></indexterm>
259 <indexterm><primary>heap, minimum free</primary></indexterm>
261 <para>Minimum % <replaceable>n</replaceable> of heap
262 which must be available for allocation. The default is
268 <term><option>-M</option><replaceable>size</replaceable></term>
269 <indexterm><primary><option>-M</option></primary><secondary>RTS option</secondary></indexterm>
270 <indexterm><primary>heap size, maximum</primary></indexterm>
272 <para>[Default: 64M] Set the maximum heap size to
273 <replaceable>size</replaceable> bytes. The heap normally
274 grows and shrinks according to the memory requirements of
275 the program. The only reason for having this option is to
276 stop the heap growing without bound and filling up all the
277 available swap space, which at the least will result in the
278 program being summarily killed by the operating
284 <term><option>-s</option><replaceable>file</replaceable></term>
285 <term><option>-S</option><replaceable>file</replaceable></Term>
286 <indexterm><primary><option>-S</option></primary><secondary>RTS option</secondary></indexterm>
287 <indexterm><primary><option>-s</option></primary><secondary>RTS option</secondary></indexterm>
289 <para>Write modest (<option>-s</option>) or verbose
290 (<option>-S</option>) garbage-collector statistics into file
291 <replaceable>file</replaceable>. The default
292 <replaceable>file</replaceable> is
293 <Filename><replaceable>program</replaceable>.stat</Filename>. The
294 <replaceable>file</replaceable> <constant>stderr</constant>
295 is treated specially, with the output really being sent to
296 <constant>stderr</constant>.</para>
298 <para>This option is useful for watching how the storage
299 manager adjusts the heap size based on the current amount of
305 <term><option>-t</option></term>
306 <indexterm><primary><option>-t</option></primary><secondary>RTS option</secondary></indexterm>
308 <para>Write a one-line GC stats summary after running the
309 program. This output is in the same format as that produced
310 by the <option>-Rghc-timing</option> option.</para>
317 <!-- ---------------------------------------------------------------------- -->
319 <title>RTS options for profiling and Concurrent/Parallel Haskell</title>
321 <para>The RTS options related to profiling are described in <XRef
322 LinkEnd="prof-rts-options">; and those for concurrent/parallel
323 stuff, in <XRef LinkEnd="parallel-rts-opts">.</para>
326 <!-- ---------------------------------------------------------------------- -->
327 <sect2 id="rts-options-debugging">
328 <title>RTS options for hackers, debuggers, and over-interested
331 <indexterm><primary>RTS options, hacking/debugging</primary></indexterm>
333 <para>These RTS options might be used (a) to avoid a GHC bug,
334 (b) to see “what's really happening”, or
335 (c) because you feel like it. Not recommended for everyday
341 <term><option>-B</option></term>
342 <indexterm><primary><option>-B</option></primary><secondary>RTS option</secondary></indexterm>
344 <para>Sound the bell at the start of each (major) garbage
347 <para>Oddly enough, people really do use this option! Our
348 pal in Durham (England), Paul Callaghan, writes: “Some
349 people here use it for a variety of
350 purposes—honestly!—e.g., confirmation that the
351 code/machine is doing something, infinite loop detection,
352 gauging cost of recently added code. Certain people can even
353 tell what stage [the program] is in by the beep
354 pattern. But the major use is for annoying others in the
355 same office…”</para>
360 <term><option>-D</option><replaceable>num</replaceable></term>
361 <indexterm><primary>-D</primary><secondary>RTS option</secondary></indexterm>
363 <para>An RTS debugging flag; varying quantities of output
364 depending on which bits are set in
365 <replaceable>num</replaceable>. Only works if the RTS was
366 compiled with the <option>DEBUG</option> option.</para>
371 <term><option>-r</option><replaceable>file</replaceable></term>
372 <indexterm><primary><option>-r</option></primary><secondary>RTS option</secondary></indexterm>
373 <indexterm><primary>ticky ticky profiling</primary></indexterm>
374 <indexterm><primary>profiling</primary><secondary>ticky ticky</secondary></indexterm>
376 <para>Produce “ticky-ticky” statistics at the
377 end of the program run. The <replaceable>file</replaceable>
378 business works just like on the <option>-S</option> RTS
379 option (above).</para>
381 <para>“Ticky-ticky” statistics are counts of
382 various program actions (updates, enters, etc.) The program
383 must have been compiled using
384 <option>-ticky</option><indexterm><primary><option>-ticky</option></primary></indexterm>
385 (a.k.a. “ticky-ticky profiling”), and, for it to
386 be really useful, linked with suitable system libraries.
387 Not a trivial undertaking: consult the installation guide on
388 how to set things up for easy “ticky-ticky”
389 profiling. For more information, see <XRef
390 LinkEnd="ticky-ticky">.</para>
395 <term><option>-xc</option></term>
396 <indexterm><primary><option>-xc</option></primary><secondary>RTS
397 option</secondary></indexterm>
399 <para>(Only available when the program is compiled for
400 profiling.) When an exception is raised in the program,
401 this option causes the current cost-centre-stack to be
402 dumped to <literal>stderr</literal>.</para>
404 <para>This can be particularly useful for debugging: if your
405 program is complaining about a <literal>head []</literal>
406 error and you haven't got a clue which bit of code is
407 causing it, compiling with <literal>-prof
408 -auto-all</literal> and running with <literal>+RTS -xc
409 -RTS</literal> will tell you exactly the call stack at the
410 point the error was raised.</para>
415 <term><option>-Z</option></term>
416 <indexterm><primary><option>-Z</option></primary><secondary>RTS
417 option</secondary></indexterm>
419 <para>Turn <emphasis>off</emphasis> “update-frame
420 squeezing” at garbage-collection time. (There's no
421 particularly good reason to turn it off, except to ensure
422 the accuracy of certain data collected regarding thunk entry
430 <sect2 id="rts-hooks">
431 <title>“Hooks” to change RTS behaviour</title>
433 <indexterm><primary>hooks</primary><secondary>RTS</secondary></indexterm>
434 <indexterm><primary>RTS hooks</primary></indexterm>
435 <indexterm><primary>RTS behaviour, changing</primary></indexterm>
437 <para>GHC lets you exercise rudimentary control over the RTS
438 settings for any given program, by compiling in a
439 “hook” that is called by the run-time system. The RTS
440 contains stub definitions for all these hooks, but by writing your
441 own version and linking it on the GHC command line, you can
442 override the defaults.</para>
444 <para>Owing to the vagaries of DLL linking, these hooks don't work
445 under Windows when the program is built dynamically.</para>
448 <Function>defaultsHook</Function><indexterm><primary><function>defaultHook</function></primary></indexterm>
449 lets you change various RTS options. The commonest use for this
450 is to give your program a default heap and/or stack size that is
451 greater than the default. For example, to set
452 <literal>-M128m -K1m</literal>:</para>
456 #include "RtsFlags.h"
457 void defaultsHook (void) {
458 RtsFlags.GcFlags.maxStkSize = 1000002 / sizeof(W_);
459 RtsFlags.GcFlags.maxHeapSize = 128*1024*1024 / BLOCK_SIZE_W;
463 <para>Don't use powers of two for heap/stack sizes: these are more
464 likely to interact badly with direct-mapped caches. The full set
465 of flags is defined in <Filename>ghc/rts/RtsFlags.h</Filename> the
466 the GHC source tree.</para>
468 <para>You can also change the messages printed when the runtime
469 system “blows up,” e.g., on stack overflow. The hooks
470 for these are as follows:</para>
475 <term><Function>void ErrorHdrHook (FILE *)</function></term>
476 <indexterm><primary><function>ErrorHdrHook</function></primary></indexterm>
478 <para>What's printed out before the message from
479 <function>error</function>.</para>
484 <term><function>void OutOfHeapHook (unsigned long, unsigned long)</function></term>
485 <indexterm><primary><function>OutOfHeapHook</function></primary></indexterm>
487 <para>The heap-overflow message.</para>
492 <term><function>void StackOverflowHook (long int)</function></term>
493 <indexterm><primary><function>StackOverflowHook</function></primary></indexterm>
495 <para>The stack-overflow message.</para>
500 <term><function>void MallocFailHook (long int)</function></term>
501 <indexterm><primary><function>MallocFailHook</function></primary></indexterm>
503 <para>The message printed if <Function>malloc</Function>
509 <term><function>void PatErrorHdrHook (FILE *)</function></term>
510 <indexterm><primary><function>PatErrorHdrHook</function></primary></indexterm>
512 <para>The message printed if a pattern-match fails (the
513 failures that were not handled by the Haskell
519 <term><function>void PreTraceHook (FILE *)</function></term>
520 <indexterm><primary><function>PreTraceHook</function></primary></indexterm>
522 <para>What's printed out before a <Function>trace</Function>
528 <term><function>void PostTraceHook (FILE *)</function></term>
529 <indexterm><primary><function>PostTraceHook</function></primary></indexterm>
531 <para>What's printed out after a <Function>trace</Function>
537 <para>For example, here is the “hooks” code used by
542 #include "../rts/RtsFlags.h"
548 RtsFlags.GcFlags.heapSizeSuggestion = 6*1024*1024 / BLOCK_SIZE;
549 RtsFlags.GcFlags.maxStkSize = 8*1024*1024 / sizeof(W_);
550 RtsFlags.GcFlags.giveStats = COLLECT_GC_STATS;
551 RtsFlags.GcFlags.statsFile = stderr;
555 ErrorHdrHook (long fd)
562 PatErrorHdrHook (long fd)
564 const char msg[]="\n*** Pattern-matching error within GHC!\n\nThis is a compiler bug; please report it to glasgow-haskell-bugs@haskell.org.\n\nFail:";
565 write(fd,msg,sizeof(msg)-1);
569 PreTraceHook (long fd)
571 const char msg[]="\n";
572 write(fd,msg,sizeof(msg)-1);
576 PostTraceHook (long fd)
579 const char msg[]="\n";
580 write(fd,msg,sizeof(msg)-1);
589 ;;; Local Variables: ***
591 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter" "sect1") ***