Lightweight ticky-ticky profiling
[ghc-hetmet.git] / docs / users_guide / profiling.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <chapter id="profiling">
3   <title>Profiling</title>
4   <indexterm><primary>profiling</primary>
5   </indexterm>
6   <indexterm><primary>cost-centre profiling</primary></indexterm>
7
8   <para> Glasgow Haskell comes with a time and space profiling
9   system. Its purpose is to help you improve your understanding of
10   your program's execution behaviour, so you can improve it.</para>
11   
12   <para> Any comments, suggestions and/or improvements you have are
13   welcome.  Recommended &ldquo;profiling tricks&rdquo; would be
14   especially cool! </para>
15
16   <para>Profiling a program is a three-step process:</para>
17
18   <orderedlist>
19     <listitem>
20       <para> Re-compile your program for profiling with the
21       <literal>-prof</literal> option, and probably one of the
22       <literal>-auto</literal> or <literal>-auto-all</literal>
23       options.  These options are described in more detail in <xref
24       linkend="prof-compiler-options"/> </para>
25       <indexterm><primary><literal>-prof</literal></primary>
26       </indexterm>
27       <indexterm><primary><literal>-auto</literal></primary>
28       </indexterm>
29       <indexterm><primary><literal>-auto-all</literal></primary>
30       </indexterm>
31     </listitem>
32
33     <listitem>
34       <para> Run your program with one of the profiling options, eg.
35       <literal>+RTS -p -RTS</literal>.  This generates a file of
36       profiling information.</para>
37       <indexterm><primary><option>-p</option></primary><secondary>RTS
38       option</secondary></indexterm>
39     </listitem>
40       
41     <listitem>
42       <para> Examine the generated profiling information, using one of
43       GHC's profiling tools.  The tool to use will depend on the kind
44       of profiling information generated.</para>
45     </listitem>
46     
47   </orderedlist>
48   
49   <sect1 id="cost-centres">
50     <title>Cost centres and cost-centre stacks</title>
51     
52     <para>GHC's profiling system assigns <firstterm>costs</firstterm>
53     to <firstterm>cost centres</firstterm>.  A cost is simply the time
54     or space required to evaluate an expression.  Cost centres are
55     program annotations around expressions; all costs incurred by the
56     annotated expression are assigned to the enclosing cost centre.
57     Furthermore, GHC will remember the stack of enclosing cost centres
58     for any given expression at run-time and generate a call-graph of
59     cost attributions.</para>
60
61     <para>Let's take a look at an example:</para>
62
63     <programlisting>
64 main = print (nfib 25)
65 nfib n = if n &lt; 2 then 1 else nfib (n-1) + nfib (n-2)
66 </programlisting>
67
68     <para>Compile and run this program as follows:</para>
69
70     <screen>
71 $ ghc -prof -auto-all -o Main Main.hs
72 $ ./Main +RTS -p
73 121393
74 $
75 </screen>
76
77     <para>When a GHC-compiled program is run with the
78     <option>-p</option> RTS option, it generates a file called
79     <filename>&lt;prog&gt;.prof</filename>.  In this case, the file
80     will contain something like this:</para>
81
82 <screen>
83           Fri May 12 14:06 2000 Time and Allocation Profiling Report  (Final)
84
85            Main +RTS -p -RTS
86
87         total time  =        0.14 secs   (7 ticks @ 20 ms)
88         total alloc =   8,741,204 bytes  (excludes profiling overheads)
89
90 COST CENTRE          MODULE     %time %alloc
91
92 nfib                 Main       100.0  100.0
93
94
95                                               individual     inherited
96 COST CENTRE              MODULE      entries %time %alloc   %time %alloc
97
98 MAIN                     MAIN             0    0.0   0.0    100.0 100.0
99  main                    Main             0    0.0   0.0      0.0   0.0
100  CAF                     PrelHandle       3    0.0   0.0      0.0   0.0
101  CAF                     PrelAddr         1    0.0   0.0      0.0   0.0
102  CAF                     Main             6    0.0   0.0    100.0 100.0
103   main                   Main             1    0.0   0.0    100.0 100.0
104    nfib                  Main        242785  100.0 100.0    100.0 100.0
105 </screen>
106
107
108     <para>The first part of the file gives the program name and
109     options, and the total time and total memory allocation measured
110     during the run of the program (note that the total memory
111     allocation figure isn't the same as the amount of
112     <emphasis>live</emphasis> memory needed by the program at any one
113     time; the latter can be determined using heap profiling, which we
114     will describe shortly).</para>
115
116     <para>The second part of the file is a break-down by cost centre
117     of the most costly functions in the program.  In this case, there
118     was only one significant function in the program, namely
119     <function>nfib</function>, and it was responsible for 100&percnt;
120     of both the time and allocation costs of the program.</para>
121
122     <para>The third and final section of the file gives a profile
123     break-down by cost-centre stack.  This is roughly a call-graph
124     profile of the program.  In the example above, it is clear that
125     the costly call to <function>nfib</function> came from
126     <function>main</function>.</para>
127
128     <para>The time and allocation incurred by a given part of the
129     program is displayed in two ways: &ldquo;individual&rdquo;, which
130     are the costs incurred by the code covered by this cost centre
131     stack alone, and &ldquo;inherited&rdquo;, which includes the costs
132     incurred by all the children of this node.</para>
133
134     <para>The usefulness of cost-centre stacks is better demonstrated
135     by  modifying the example slightly:</para>
136
137     <programlisting>
138 main = print (f 25 + g 25)
139 f n  = nfib n
140 g n  = nfib (n `div` 2)
141 nfib n = if n &lt; 2 then 1 else nfib (n-1) + nfib (n-2)
142 </programlisting>
143
144     <para>Compile and run this program as before, and take a look at
145     the new profiling results:</para>
146
147 <screen>
148 COST CENTRE              MODULE         scc  %time %alloc   %time %alloc
149
150 MAIN                     MAIN             0    0.0   0.0    100.0 100.0
151  main                    Main             0    0.0   0.0      0.0   0.0
152  CAF                     PrelHandle       3    0.0   0.0      0.0   0.0
153  CAF                     PrelAddr         1    0.0   0.0      0.0   0.0
154  CAF                     Main             9    0.0   0.0    100.0 100.0
155   main                   Main             1    0.0   0.0    100.0 100.0
156    g                     Main             1    0.0   0.0      0.0   0.2
157     nfib                 Main           465    0.0   0.2      0.0   0.2
158    f                     Main             1    0.0   0.0    100.0  99.8
159     nfib                 Main        242785  100.0  99.8    100.0  99.8
160 </screen>
161
162     <para>Now although we had two calls to <function>nfib</function>
163     in the program, it is immediately clear that it was the call from
164     <function>f</function> which took all the time.</para>
165
166     <para>The actual meaning of the various columns in the output is:</para>
167
168     <variablelist>
169       <varlistentry>
170         <term>entries</term>
171         <listitem>
172           <para>The number of times this particular point in the call
173           graph was entered.</para>
174         </listitem>
175       </varlistentry>
176
177       <varlistentry>
178         <term>individual &percnt;time</term>
179         <listitem>
180           <para>The percentage of the total run time of the program
181           spent at this point in the call graph.</para>
182         </listitem>
183       </varlistentry>
184
185       <varlistentry>
186         <term>individual &percnt;alloc</term>
187         <listitem>
188           <para>The percentage of the total memory allocations
189           (excluding profiling overheads) of the program made by this
190           call.</para>
191         </listitem>
192       </varlistentry>
193
194       <varlistentry>
195         <term>inherited &percnt;time</term>
196         <listitem>
197           <para>The percentage of the total run time of the program
198           spent below this point in the call graph.</para>
199         </listitem>
200       </varlistentry>
201
202       <varlistentry>
203         <term>inherited &percnt;alloc</term>
204         <listitem>
205           <para>The percentage of the total memory allocations
206           (excluding profiling overheads) of the program made by this
207           call and all of its sub-calls.</para>
208         </listitem>
209       </varlistentry>
210     </variablelist>
211
212     <para>In addition you can use the <option>-P</option> RTS option
213     <indexterm><primary><option>-P</option></primary></indexterm> to
214     get the following additional information:</para>
215
216     <variablelist>
217       <varlistentry>
218         <term><literal>ticks</literal></term>
219         <listitem>
220           <para>The raw number of time &ldquo;ticks&rdquo; which were
221           attributed to this cost-centre; from this, we get the
222           <literal>&percnt;time</literal> figure mentioned
223           above.</para>
224         </listitem>
225       </varlistentry>
226
227       <varlistentry>
228         <term><literal>bytes</literal></term>
229         <listitem>
230           <para>Number of bytes allocated in the heap while in this
231           cost-centre; again, this is the raw number from which we get
232           the <literal>&percnt;alloc</literal> figure mentioned
233           above.</para>
234         </listitem>
235       </varlistentry>
236     </variablelist>
237
238     <para>What about recursive functions, and mutually recursive
239     groups of functions?  Where are the costs attributed?  Well,
240     although GHC does keep information about which groups of functions
241     called each other recursively, this information isn't displayed in
242     the basic time and allocation profile, instead the call-graph is
243     flattened into a tree.  The XML profiling tool (described in <xref
244     linkend="prof-xml-tool"/>) will be able to display real loops in
245     the call-graph.</para>
246
247     <sect2><title>Inserting cost centres by hand</title>
248
249       <para>Cost centres are just program annotations.  When you say
250       <option>-auto-all</option> to the compiler, it automatically
251       inserts a cost centre annotation around every top-level function
252       in your program, but you are entirely free to add the cost
253       centre annotations yourself.</para>
254
255       <para>The syntax of a cost centre annotation is</para>
256
257       <programlisting>
258      {-# SCC "name" #-} &lt;expression&gt;
259 </programlisting>
260
261       <para>where <literal>"name"</literal> is an arbitrary string,
262       that will become the name of your cost centre as it appears
263       in the profiling output, and
264       <literal>&lt;expression&gt;</literal> is any Haskell
265       expression.  An <literal>SCC</literal> annotation extends as
266       far to the right as possible when parsing.</para>
267
268     </sect2>
269
270     <sect2 id="prof-rules">
271       <title>Rules for attributing costs</title>
272
273       <para>The cost of evaluating any expression in your program is
274       attributed to a cost-centre stack using the following rules:</para>
275
276       <itemizedlist>
277         <listitem>
278           <para>If the expression is part of the
279           <firstterm>one-off</firstterm> costs of evaluating the
280           enclosing top-level definition, then costs are attributed to
281           the stack of lexically enclosing <literal>SCC</literal>
282           annotations on top of the special <literal>CAF</literal>
283           cost-centre. </para>
284         </listitem>
285
286         <listitem>
287           <para>Otherwise, costs are attributed to the stack of
288           lexically-enclosing <literal>SCC</literal> annotations,
289           appended to the cost-centre stack in effect at the
290           <firstterm>call site</firstterm> of the current top-level
291           definition<footnote> <para>The call-site is just the place
292           in the source code which mentions the particular function or
293           variable.</para></footnote>.  Notice that this is a recursive
294           definition.</para>
295         </listitem>
296
297         <listitem>
298           <para>Time spent in foreign code (see <xref linkend="ffi"/>)
299           is always attributed to the cost centre in force at the
300           Haskell call-site of the foreign function.</para>
301         </listitem>
302       </itemizedlist>
303
304       <para>What do we mean by one-off costs?  Well, Haskell is a lazy
305       language, and certain expressions are only ever evaluated once.
306       For example, if we write:</para>
307
308       <programlisting>
309 x = nfib 25
310 </programlisting>
311
312       <para>then <varname>x</varname> will only be evaluated once (if
313       at all), and subsequent demands for <varname>x</varname> will
314       immediately get to see the cached result.  The definition
315       <varname>x</varname> is called a CAF (Constant Applicative
316       Form), because it has no arguments.</para>
317
318       <para>For the purposes of profiling, we say that the expression
319       <literal>nfib 25</literal> belongs to the one-off costs of
320       evaluating <varname>x</varname>.</para>
321
322       <para>Since one-off costs aren't strictly speaking part of the
323       call-graph of the program, they are attributed to a special
324       top-level cost centre, <literal>CAF</literal>.  There may be one
325       <literal>CAF</literal> cost centre for each module (the
326       default), or one for each top-level definition with any one-off
327       costs (this behaviour can be selected by giving GHC the
328       <option>-caf-all</option> flag).</para>
329
330       <indexterm><primary><literal>-caf-all</literal></primary>
331       </indexterm>
332
333       <para>If you think you have a weird profile, or the call-graph
334       doesn't look like you expect it to, feel free to send it (and
335       your program) to us at
336       <email>glasgow-haskell-bugs@haskell.org</email>.</para>
337     </sect2>
338   </sect1>
339
340   <sect1 id="prof-compiler-options">
341     <title>Compiler options for profiling</title>
342
343     <indexterm><primary>profiling</primary><secondary>options</secondary></indexterm>
344     <indexterm><primary>options</primary><secondary>for profiling</secondary></indexterm>
345
346     <variablelist>
347       <varlistentry>
348         <term>
349           <option>-prof</option>:
350           <indexterm><primary><option>-prof</option></primary></indexterm>
351         </term>
352         <listitem>
353           <para> To make use of the profiling system
354           <emphasis>all</emphasis> modules must be compiled and linked
355           with the <option>-prof</option> option. Any
356           <literal>SCC</literal> annotations you've put in your source
357           will spring to life.</para>
358
359           <para> Without a <option>-prof</option> option, your
360           <literal>SCC</literal>s are ignored; so you can compile
361           <literal>SCC</literal>-laden code without changing
362           it.</para>
363         </listitem>
364       </varlistentry>
365     </variablelist>
366       
367     <para>There are a few other profiling-related compilation options.
368     Use them <emphasis>in addition to</emphasis>
369     <option>-prof</option>.  These do not have to be used consistently
370     for all modules in a program.</para>
371
372     <variablelist>
373       <varlistentry>
374         <term>
375           <option>-auto</option>:
376           <indexterm><primary><option>-auto</option></primary></indexterm>
377           <indexterm><primary>cost centres</primary><secondary>automatically inserting</secondary></indexterm>
378         </term>
379         <listitem>
380           <para> GHC will automatically add
381           <function>&lowbar;scc&lowbar;</function> constructs for all
382           top-level, exported functions.</para>
383         </listitem>
384       </varlistentry>
385       
386       <varlistentry>
387         <term>
388           <option>-auto-all</option>:
389           <indexterm><primary><option>-auto-all</option></primary></indexterm>
390         </term>
391         <listitem>
392           <para> <emphasis>All</emphasis> top-level functions,
393           exported or not, will be automatically
394           <function>&lowbar;scc&lowbar;</function>'d.</para>
395         </listitem>
396       </varlistentry>
397
398       <varlistentry>
399         <term>
400           <option>-caf-all</option>:
401           <indexterm><primary><option>-caf-all</option></primary></indexterm>
402         </term>
403         <listitem>
404           <para> The costs of all CAFs in a module are usually
405           attributed to one &ldquo;big&rdquo; CAF cost-centre. With
406           this option, all CAFs get their own cost-centre.  An
407           &ldquo;if all else fails&rdquo; option&hellip;</para>
408         </listitem>
409       </varlistentry>
410
411       <varlistentry>
412         <term>
413           <option>-ignore-scc</option>:
414           <indexterm><primary><option>-ignore-scc</option></primary></indexterm>
415         </term>
416         <listitem>
417           <para>Ignore any <function>&lowbar;scc&lowbar;</function>
418           constructs, so a module which already has
419           <function>&lowbar;scc&lowbar;</function>s can be compiled
420           for profiling with the annotations ignored.</para>
421         </listitem>
422       </varlistentry>
423
424     </variablelist>
425
426   </sect1>
427
428   <sect1 id="prof-time-options">
429     <title>Time and allocation profiling</title>
430
431     <para>To generate a time and allocation profile, give one of the
432     following RTS options to the compiled program when you run it (RTS
433     options should be enclosed between <literal>+RTS...-RTS</literal>
434     as usual):</para>
435
436     <variablelist>
437       <varlistentry>
438         <term>
439           <option>-p</option> or <option>-P</option>:
440           <indexterm><primary><option>-p</option></primary></indexterm>
441           <indexterm><primary><option>-P</option></primary></indexterm>
442           <indexterm><primary>time profile</primary></indexterm>
443         </term>
444         <listitem>
445           <para>The <option>-p</option> option produces a standard
446           <emphasis>time profile</emphasis> report.  It is written
447           into the file
448           <filename><replaceable>program</replaceable>.prof</filename>.</para>
449
450           <para>The <option>-P</option> option produces a more
451           detailed report containing the actual time and allocation
452           data as well.  (Not used much.)</para>
453         </listitem>
454       </varlistentry>
455
456       <varlistentry>
457         <term>
458           <option>-px</option>:
459           <indexterm><primary><option>-px</option></primary></indexterm>
460         </term>
461         <listitem>
462           <para>The <option>-px</option> option generates profiling
463           information in the XML format understood by our new
464           profiling tool, see <xref linkend="prof-xml-tool"/>.</para>
465         </listitem>
466       </varlistentry>
467
468       <varlistentry>
469         <term>
470           <option>-xc</option>
471           <indexterm><primary><option>-xc</option></primary><secondary>RTS option</secondary></indexterm>
472         </term>
473         <listitem>
474           <para>This option makes use of the extra information
475           maintained by the cost-centre-stack profiler to provide
476           useful information about the location of runtime errors.
477           See <xref linkend="rts-options-debugging"/>.</para>
478         </listitem>
479       </varlistentry>
480
481     </variablelist>
482     
483   </sect1>
484
485   <sect1 id="prof-heap">
486     <title>Profiling memory usage</title>
487
488     <para>In addition to profiling the time and allocation behaviour
489     of your program, you can also generate a graph of its memory usage
490     over time.  This is useful for detecting the causes of
491     <firstterm>space leaks</firstterm>, when your program holds on to
492     more memory at run-time that it needs to.  Space leaks lead to
493     longer run-times due to heavy garbage collector activity, and may
494     even cause the program to run out of memory altogether.</para>
495
496     <para>To generate a heap profile from your program:</para>
497
498     <orderedlist>
499       <listitem>
500         <para>Compile the program for profiling (<xref
501         linkend="prof-compiler-options"/>).</para>
502       </listitem>
503       <listitem>
504         <para>Run it with one of the heap profiling options described
505         below (eg. <option>-hc</option> for a basic producer profile).
506         This generates the file
507         <filename><replaceable>prog</replaceable>.hp</filename>.</para>
508       </listitem>
509       <listitem>
510         <para>Run <command>hp2ps</command> to produce a Postscript
511         file,
512         <filename><replaceable>prog</replaceable>.ps</filename>.  The
513         <command>hp2ps</command> utility is described in detail in
514         <xref linkend="hp2ps"/>.</para> 
515       </listitem>
516       <listitem>
517         <para>Display the heap profile using a postscript viewer such
518         as <application>Ghostview</application>, or print it out on a
519         Postscript-capable printer.</para>
520       </listitem>
521     </orderedlist>
522
523     <sect2 id="rts-options-heap-prof">
524       <title>RTS options for heap profiling</title>
525
526       <para>There are several different kinds of heap profile that can
527       be generated.  All the different profile types yield a graph of
528       live heap against time, but they differ in how the live heap is
529       broken down into bands.  The following RTS options select which
530       break-down to use:</para>
531
532       <variablelist>
533         <varlistentry>
534           <term>
535             <option>-hc</option>
536             <indexterm><primary><option>-hc</option></primary><secondary>RTS option</secondary></indexterm>
537           </term>
538           <listitem>
539             <para>Breaks down the graph by the cost-centre stack which
540             produced the data.</para>
541           </listitem>
542         </varlistentry>
543
544         <varlistentry>
545           <term>
546             <option>-hm</option>
547             <indexterm><primary><option>-hm</option></primary><secondary>RTS option</secondary></indexterm>
548           </term>
549           <listitem>
550             <para>Break down the live heap by the module containing
551             the code which produced the data.</para>
552           </listitem>
553         </varlistentry>
554
555         <varlistentry>
556           <term>
557             <option>-hd</option>
558             <indexterm><primary><option>-hd</option></primary><secondary>RTS option</secondary></indexterm>
559           </term>
560           <listitem>
561             <para>Breaks down the graph by <firstterm>closure
562             description</firstterm>.  For actual data, the description
563             is just the constructor name, for other closures it is a
564             compiler-generated string identifying the closure.</para>
565           </listitem>
566         </varlistentry>
567
568         <varlistentry>
569           <term>
570             <option>-hy</option>
571             <indexterm><primary><option>-hy</option></primary><secondary>RTS option</secondary></indexterm>
572           </term>
573           <listitem>
574             <para>Breaks down the graph by
575             <firstterm>type</firstterm>.  For closures which have
576             function type or unknown/polymorphic type, the string will
577             represent an approximation to the actual type.</para>
578           </listitem>
579         </varlistentry>
580         
581         <varlistentry>
582           <term>
583             <option>-hr</option>
584             <indexterm><primary><option>-hr</option></primary><secondary>RTS option</secondary></indexterm>
585           </term>
586           <listitem>
587             <para>Break down the graph by <firstterm>retainer
588             set</firstterm>.  Retainer profiling is described in more
589             detail below (<xref linkend="retainer-prof"/>).</para>
590           </listitem>
591         </varlistentry>
592
593         <varlistentry>
594           <term>
595             <option>-hb</option>
596             <indexterm><primary><option>-hb</option></primary><secondary>RTS option</secondary></indexterm>
597           </term>
598           <listitem>
599             <para>Break down the graph by
600             <firstterm>biography</firstterm>.  Biographical profiling
601             is described in more detail below (<xref
602             linkend="biography-prof"/>).</para>
603           </listitem>
604         </varlistentry>
605       </variablelist>
606
607       <para>In addition, the profile can be restricted to heap data
608       which satisfies certain criteria - for example, you might want
609       to display a profile by type but only for data produced by a
610       certain module, or a profile by retainer for a certain type of
611       data.  Restrictions are specified as follows:</para>
612       
613       <variablelist>
614         <varlistentry>
615           <term>
616             <option>-hc</option><replaceable>name</replaceable>,...
617             <indexterm><primary><option>-hc</option></primary><secondary>RTS option</secondary></indexterm>
618           </term>
619           <listitem>
620             <para>Restrict the profile to closures produced by
621             cost-centre stacks with one of the specified cost centres
622             at the top.</para>
623           </listitem>
624         </varlistentry>
625
626         <varlistentry>
627           <term>
628             <option>-hC</option><replaceable>name</replaceable>,...
629             <indexterm><primary><option>-hC</option></primary><secondary>RTS option</secondary></indexterm>
630           </term>
631           <listitem>
632             <para>Restrict the profile to closures produced by
633             cost-centre stacks with one of the specified cost centres
634             anywhere in the stack.</para>
635           </listitem>
636         </varlistentry>
637
638         <varlistentry>
639           <term>
640             <option>-hm</option><replaceable>module</replaceable>,...
641             <indexterm><primary><option>-hm</option></primary><secondary>RTS option</secondary></indexterm>
642           </term>
643           <listitem>
644             <para>Restrict the profile to closures produced by the
645             specified modules.</para>
646           </listitem>
647         </varlistentry>
648
649         <varlistentry>
650           <term>
651             <option>-hd</option><replaceable>desc</replaceable>,...
652             <indexterm><primary><option>-hd</option></primary><secondary>RTS option</secondary></indexterm>
653           </term>
654           <listitem>
655             <para>Restrict the profile to closures with the specified
656             description strings.</para>
657           </listitem>
658         </varlistentry>
659
660         <varlistentry>
661           <term>
662             <option>-hy</option><replaceable>type</replaceable>,...
663             <indexterm><primary><option>-hy</option></primary><secondary>RTS option</secondary></indexterm>
664           </term>
665           <listitem>
666             <para>Restrict the profile to closures with the specified
667             types.</para>
668           </listitem>
669         </varlistentry>
670         
671         <varlistentry>
672           <term>
673             <option>-hr</option><replaceable>cc</replaceable>,...
674             <indexterm><primary><option>-hr</option></primary><secondary>RTS option</secondary></indexterm>
675           </term>
676           <listitem>
677             <para>Restrict the profile to closures with retainer sets
678             containing cost-centre stacks with one of the specified
679             cost centres at the top.</para>
680           </listitem>
681         </varlistentry>
682
683         <varlistentry>
684           <term>
685             <option>-hb</option><replaceable>bio</replaceable>,...
686             <indexterm><primary><option>-hb</option></primary><secondary>RTS option</secondary></indexterm>
687           </term>
688           <listitem>
689             <para>Restrict the profile to closures with one of the
690             specified biographies, where
691             <replaceable>bio</replaceable> is one of
692             <literal>lag</literal>, <literal>drag</literal>,
693             <literal>void</literal>, or <literal>use</literal>.</para>
694           </listitem>
695         </varlistentry>
696       </variablelist>
697
698       <para>For example, the following options will generate a
699       retainer profile restricted to <literal>Branch</literal> and
700       <literal>Leaf</literal> constructors:</para>
701
702 <screen>
703 <replaceable>prog</replaceable> +RTS -hr -hdBranch,Leaf
704 </screen>
705
706       <para>There can only be one "break-down" option
707       (eg. <option>-hr</option> in the example above), but there is no
708       limit on the number of further restrictions that may be applied.
709       All the options may be combined, with one exception: GHC doesn't
710       currently support mixing the <option>-hr</option> and
711       <option>-hb</option> options.</para>
712
713       <para>There are three more options which relate to heap
714       profiling:</para>
715
716       <variablelist>
717         <varlistentry>
718           <term>
719             <option>-i<replaceable>secs</replaceable></option>:
720             <indexterm><primary><option>-i</option></primary></indexterm>
721           </term>
722           <listitem>
723             <para>Set the profiling (sampling) interval to
724             <replaceable>secs</replaceable> seconds (the default is
725             0.1&nbsp;second).  Fractions are allowed: for example
726             <option>-i0.2</option> will get 5 samples per second.
727             This only affects heap profiling; time profiles are always
728             sampled on a 1/50 second frequency.</para>
729           </listitem>
730         </varlistentry>
731
732         <varlistentry>
733           <term>
734             <option>-xt</option>
735             <indexterm><primary><option>-xt</option></primary><secondary>RTS option</secondary></indexterm>
736           </term>
737           <listitem>
738             <para>Include the memory occupied by threads in a heap
739             profile.  Each thread takes up a small area for its thread
740             state in addition to the space allocated for its stack
741             (stacks normally start small and then grow as
742             necessary).</para>
743             
744             <para>This includes the main thread, so using
745             <option>-xt</option> is a good way to see how much stack
746             space the program is using.</para>
747
748             <para>Memory occupied by threads and their stacks is
749             labelled as &ldquo;TSO&rdquo; when displaying the profile
750             by closure description or type description.</para>
751           </listitem>
752         </varlistentry>
753
754         <varlistentry>
755           <term>
756             <option>-L<replaceable>num</replaceable></option>
757             <indexterm><primary><option>-L</option></primary><secondary>RTS option</secondary></indexterm>
758           </term>
759           <listitem>
760             <para>
761           Sets the maximum length of a cost-centre stack name in a
762           heap profile. Defaults to 25.
763             </para>
764           </listitem>
765         </varlistentry>
766       </variablelist>
767
768     </sect2>
769     
770     <sect2 id="retainer-prof">
771       <title>Retainer Profiling</title>
772
773       <para>Retainer profiling is designed to help answer questions
774       like <quote>why is this data being retained?</quote>.  We start
775       by defining what we mean by a retainer:</para>
776
777       <blockquote>
778         <para>A retainer is either the system stack, or an unevaluated
779         closure (thunk).</para>
780       </blockquote>
781
782       <para>In particular, constructors are <emphasis>not</emphasis>
783       retainers.</para>
784
785       <para>An object B retains object A if (i) B is a retainer object and
786      (ii) object A can be reached by recursively following pointers
787      starting from object B, but not meeting any other retainer
788      objects on the way. Each live object is retained by one or more
789      retainer objects, collectively called its retainer set, or its
790       <firstterm>retainer set</firstterm>, or its
791       <firstterm>retainers</firstterm>.</para>
792
793       <para>When retainer profiling is requested by giving the program
794       the <option>-hr</option> option, a graph is generated which is
795       broken down by retainer set.  A retainer set is displayed as a
796       set of cost-centre stacks; because this is usually too large to
797       fit on the profile graph, each retainer set is numbered and
798       shown abbreviated on the graph along with its number, and the
799       full list of retainer sets is dumped into the file
800       <filename><replaceable>prog</replaceable>.prof</filename>.</para>
801
802       <para>Retainer profiling requires multiple passes over the live
803       heap in order to discover the full retainer set for each
804       object, which can be quite slow.  So we set a limit on the
805       maximum size of a retainer set, where all retainer sets larger
806       than the maximum retainer set size are replaced by the special
807       set <literal>MANY</literal>.  The maximum set size defaults to 8
808       and can be altered with the <option>-R</option> RTS
809       option:</para>
810       
811       <variablelist>
812         <varlistentry>
813           <term><option>-R</option><replaceable>size</replaceable></term>
814           <listitem>
815             <para>Restrict the number of elements in a retainer set to
816             <replaceable>size</replaceable> (default 8).</para>
817           </listitem>
818         </varlistentry>
819       </variablelist>
820
821       <sect3>
822         <title>Hints for using retainer profiling</title>
823
824         <para>The definition of retainers is designed to reflect a
825         common cause of space leaks: a large structure is retained by
826         an unevaluated computation, and will be released once the
827         computation is forced.  A good example is looking up a value in
828         a finite map, where unless the lookup is forced in a timely
829         manner the unevaluated lookup will cause the whole mapping to
830         be retained.  These kind of space leaks can often be
831         eliminated by forcing the relevant computations to be
832         performed eagerly, using <literal>seq</literal> or strictness
833         annotations on data constructor fields.</para>
834
835         <para>Often a particular data structure is being retained by a
836         chain of unevaluated closures, only the nearest of which will
837         be reported by retainer profiling - for example A retains B, B
838         retains C, and C retains a large structure.  There might be a
839         large number of Bs but only a single A, so A is really the one
840         we're interested in eliminating.  However, retainer profiling
841         will in this case report B as the retainer of the large
842         structure.  To move further up the chain of retainers, we can
843         ask for another retainer profile but this time restrict the
844         profile to B objects, so we get a profile of the retainers of
845         B:</para>
846
847 <screen>
848 <replaceable>prog</replaceable> +RTS -hr -hcB
849 </screen>
850         
851         <para>This trick isn't foolproof, because there might be other
852         B closures in the heap which aren't the retainers we are
853         interested in, but we've found this to be a useful technique
854         in most cases.</para>
855       </sect3>
856     </sect2>
857
858     <sect2 id="biography-prof">
859       <title>Biographical Profiling</title>
860
861       <para>A typical heap object may be in one of the following four
862       states at each point in its lifetime:</para>
863
864       <itemizedlist>
865         <listitem>
866           <para>The <firstterm>lag</firstterm> stage, which is the
867           time between creation and the first use of the
868           object,</para>
869         </listitem>
870         <listitem>
871           <para>the <firstterm>use</firstterm> stage, which lasts from
872           the first use until the last use of the object, and</para>
873         </listitem>
874         <listitem>
875           <para>The <firstterm>drag</firstterm> stage, which lasts
876           from the final use until the last reference to the object
877           is dropped.</para>
878         </listitem>
879         <listitem>
880           <para>An object which is never used is said to be in the
881           <firstterm>void</firstterm> state for its whole
882           lifetime.</para>
883         </listitem>
884       </itemizedlist>
885
886       <para>A biographical heap profile displays the portion of the
887       live heap in each of the four states listed above.  Usually the
888       most interesting states are the void and drag states: live heap
889       in these states is more likely to be wasted space than heap in
890       the lag or use states.</para>
891
892       <para>It is also possible to break down the heap in one or more
893       of these states by a different criteria, by restricting a
894       profile by biography.  For example, to show the portion of the
895       heap in the drag or void state by producer: </para>
896
897 <screen>
898 <replaceable>prog</replaceable> +RTS -hc -hbdrag,void
899 </screen>
900
901       <para>Once you know the producer or the type of the heap in the
902       drag or void states, the next step is usually to find the
903       retainer(s):</para>
904
905 <screen>
906 <replaceable>prog</replaceable> +RTS -hr -hc<replaceable>cc</replaceable>...
907 </screen>
908
909       <para>NOTE: this two stage process is required because GHC
910       cannot currently profile using both biographical and retainer
911       information simultaneously.</para>
912     </sect2>
913
914     <sect2 id="mem-residency">
915       <title>Actual memory residency</title>
916
917       <para>How does the heap residency reported by the heap profiler relate to
918         the actual memory residency of your program when you run it?  You might
919         see a large discrepancy between the residency reported by the heap
920         profiler, and the residency reported by tools on your system
921         (eg. <literal>ps</literal> or <literal>top</literal> on Unix, or the
922         Task Manager on Windows).  There are several reasons for this:</para>
923
924       <itemizedlist>
925         <listitem>
926           <para>There is an overhead of profiling itself, which is subtracted
927             from the residency figures by the profiler.  This overhead goes
928             away when compiling without profiling support, of course.  The
929             space overhead is currently 2 extra
930             words per heap object, which probably results in
931             about a 30% overhead.</para>
932         </listitem>
933
934         <listitem>
935           <para>Garbage collection requires more memory than the actual
936             residency.  The factor depends on the kind of garbage collection
937             algorithm in use:  a major GC in the standard
938             generation copying collector will usually require 3L bytes of
939             memory, where L is the amount of live data.  This is because by
940             default (see the <option>+RTS -F</option> option) we allow the old
941             generation to grow to twice its size (2L) before collecting it, and
942             we require additionally L bytes to copy the live data into.  When
943             using compacting collection (see the <option>+RTS -c</option>
944             option), this is reduced to 2L, and can further be reduced by
945             tweaking the <option>-F</option> option.  Also add the size of the
946             allocation area (currently a fixed 512Kb).</para>
947         </listitem>
948
949         <listitem>
950           <para>The stack isn't counted in the heap profile by default.  See the
951     <option>+RTS -xt</option> option.</para>
952         </listitem>
953
954         <listitem>
955           <para>The program text itself, the C stack, any non-heap data (eg. data
956             allocated by foreign libraries, and data allocated by the RTS), and
957             <literal>mmap()</literal>'d memory are not counted in the heap profile.</para>
958         </listitem>
959       </itemizedlist>
960     </sect2>
961
962   </sect1>
963
964   <sect1 id="prof-xml-tool">
965     <title>Graphical time/allocation profile</title>
966
967     <para>You can view the time and allocation profiling graph of your
968     program graphically, using <command>ghcprof</command>.  This is a
969     new tool with GHC 4.08, and will eventually be the de-facto
970     standard way of viewing GHC profiles<footnote><para>Actually this
971     isn't true any more, we are working on a new tool for
972     displaying heap profiles using Gtk+HS, so
973     <command>ghcprof</command> may go away at some point in the future.</para>
974       </footnote></para>
975
976     <para>To run <command>ghcprof</command>, you need
977     <productname>uDraw(Graph)</productname> installed, which can be
978     obtained from <ulink
979     url="http://www.informatik.uni-bremen.de/uDrawGraph/en/uDrawGraph/uDrawGraph.html"><citetitle>uDraw(Graph)</citetitle></ulink>.  Install one of
980     the binary
981     distributions, and set your
982     <envar>UDG_HOME</envar> environment variable to point to the
983     installation directory.</para>
984
985     <para><command>ghcprof</command> uses an XML-based profiling log
986     format, and you therefore need to run your program with a
987     different option: <option>-px</option>.  The file generated is
988     still called <filename>&lt;prog&gt;.prof</filename>.  To see the
989     profile, run <command>ghcprof</command> like this:</para>
990
991     <indexterm><primary><option>-px</option></primary></indexterm>
992
993 <screen>
994 $ ghcprof &lt;prog&gt;.prof
995 </screen>
996
997     <para>which should pop up a window showing the call-graph of your
998     program in glorious detail.  More information on using
999     <command>ghcprof</command> can be found at <ulink
1000     url="http://www.dcs.warwick.ac.uk/people/academic/Stephen.Jarvis/profiler/index.html"><citetitle>The
1001     Cost-Centre Stack Profiling Tool for
1002     GHC</citetitle></ulink>.</para>
1003
1004   </sect1>
1005
1006   <sect1 id="hp2ps">
1007     <title><command>hp2ps</command>&ndash;&ndash;heap profile to PostScript</title>
1008
1009     <indexterm><primary><command>hp2ps</command></primary></indexterm>
1010     <indexterm><primary>heap profiles</primary></indexterm>
1011     <indexterm><primary>postscript, from heap profiles</primary></indexterm>
1012     <indexterm><primary><option>-h&lt;break-down&gt;</option></primary></indexterm>
1013     
1014     <para>Usage:</para>
1015     
1016 <screen>
1017 hp2ps [flags] [&lt;file&gt;[.hp]]
1018 </screen>
1019
1020     <para>The program
1021     <command>hp2ps</command><indexterm><primary>hp2ps
1022     program</primary></indexterm> converts a heap profile as produced
1023     by the <option>-h&lt;break-down&gt;</option> runtime option into a
1024     PostScript graph of the heap profile. By convention, the file to
1025     be processed by <command>hp2ps</command> has a
1026     <filename>.hp</filename> extension. The PostScript output is
1027     written to <filename>&lt;file&gt;@.ps</filename>. If
1028     <filename>&lt;file&gt;</filename> is omitted entirely, then the
1029     program behaves as a filter.</para>
1030
1031     <para><command>hp2ps</command> is distributed in
1032     <filename>ghc/utils/hp2ps</filename> in a GHC source
1033     distribution. It was originally developed by Dave Wakeling as part
1034     of the HBC/LML heap profiler.</para>
1035
1036     <para>The flags are:</para>
1037
1038     <variablelist>
1039       
1040       <varlistentry>
1041         <term><option>-d</option></term>
1042         <listitem>
1043           <para>In order to make graphs more readable,
1044           <command>hp2ps</command> sorts the shaded bands for each
1045           identifier. The default sort ordering is for the bands with
1046           the largest area to be stacked on top of the smaller ones.
1047           The <option>-d</option> option causes rougher bands (those
1048           representing series of values with the largest standard
1049           deviations) to be stacked on top of smoother ones.</para>
1050         </listitem>
1051       </varlistentry>
1052
1053       <varlistentry>
1054         <term><option>-b</option></term>
1055         <listitem>
1056           <para>Normally, <command>hp2ps</command> puts the title of
1057           the graph in a small box at the top of the page. However, if
1058           the JOB string is too long to fit in a small box (more than
1059           35 characters), then <command>hp2ps</command> will choose to
1060           use a big box instead.  The <option>-b</option> option
1061           forces <command>hp2ps</command> to use a big box.</para>
1062         </listitem>
1063       </varlistentry>
1064
1065       <varlistentry>
1066         <term><option>-e&lt;float&gt;[in&verbar;mm&verbar;pt]</option></term>
1067         <listitem>
1068           <para>Generate encapsulated PostScript suitable for
1069           inclusion in LaTeX documents.  Usually, the PostScript graph
1070           is drawn in landscape mode in an area 9 inches wide by 6
1071           inches high, and <command>hp2ps</command> arranges for this
1072           area to be approximately centred on a sheet of a4 paper.
1073           This format is convenient of studying the graph in detail,
1074           but it is unsuitable for inclusion in LaTeX documents.  The
1075           <option>-e</option> option causes the graph to be drawn in
1076           portrait mode, with float specifying the width in inches,
1077           millimetres or points (the default).  The resulting
1078           PostScript file conforms to the Encapsulated PostScript
1079           (EPS) convention, and it can be included in a LaTeX document
1080           using Rokicki's dvi-to-PostScript converter
1081           <command>dvips</command>.</para>
1082         </listitem>
1083       </varlistentry>
1084
1085       <varlistentry>
1086         <term><option>-g</option></term>
1087         <listitem>
1088           <para>Create output suitable for the <command>gs</command>
1089           PostScript previewer (or similar). In this case the graph is
1090           printed in portrait mode without scaling. The output is
1091           unsuitable for a laser printer.</para>
1092         </listitem>
1093       </varlistentry>
1094
1095       <varlistentry>
1096         <term><option>-l</option></term>
1097         <listitem>
1098           <para>Normally a profile is limited to 20 bands with
1099           additional identifiers being grouped into an
1100           <literal>OTHER</literal> band. The <option>-l</option> flag
1101           removes this 20 band and limit, producing as many bands as
1102           necessary. No key is produced as it won't fit!. It is useful
1103           for creation time profiles with many bands.</para>
1104         </listitem>
1105       </varlistentry>
1106
1107       <varlistentry>
1108         <term><option>-m&lt;int&gt;</option></term>
1109         <listitem>
1110           <para>Normally a profile is limited to 20 bands with
1111           additional identifiers being grouped into an
1112           <literal>OTHER</literal> band. The <option>-m</option> flag
1113           specifies an alternative band limit (the maximum is
1114           20).</para>
1115
1116           <para><option>-m0</option> requests the band limit to be
1117           removed. As many bands as necessary are produced. However no
1118           key is produced as it won't fit! It is useful for displaying
1119           creation time profiles with many bands.</para>
1120         </listitem>
1121       </varlistentry>
1122
1123       <varlistentry>
1124         <term><option>-p</option></term>
1125         <listitem>
1126           <para>Use previous parameters. By default, the PostScript
1127           graph is automatically scaled both horizontally and
1128           vertically so that it fills the page.  However, when
1129           preparing a series of graphs for use in a presentation, it
1130           is often useful to draw a new graph using the same scale,
1131           shading and ordering as a previous one. The
1132           <option>-p</option> flag causes the graph to be drawn using
1133           the parameters determined by a previous run of
1134           <command>hp2ps</command> on <filename>file</filename>. These
1135           are extracted from <filename>file@.aux</filename>.</para>
1136         </listitem>
1137       </varlistentry>
1138
1139       <varlistentry>
1140         <term><option>-s</option></term>
1141         <listitem>
1142           <para>Use a small box for the title.</para>
1143         </listitem>
1144       </varlistentry>
1145       
1146       <varlistentry>
1147         <term><option>-t&lt;float&gt;</option></term>
1148         <listitem>
1149           <para>Normally trace elements which sum to a total of less
1150           than 1&percnt; of the profile are removed from the
1151           profile. The <option>-t</option> option allows this
1152           percentage to be modified (maximum 5&percnt;).</para>
1153
1154           <para><option>-t0</option> requests no trace elements to be
1155           removed from the profile, ensuring that all the data will be
1156           displayed.</para>
1157         </listitem>
1158       </varlistentry>
1159
1160       <varlistentry>
1161         <term><option>-c</option></term>
1162         <listitem>
1163           <para>Generate colour output.</para>
1164         </listitem>
1165       </varlistentry>
1166       
1167       <varlistentry>
1168         <term><option>-y</option></term>
1169         <listitem>
1170           <para>Ignore marks.</para>
1171         </listitem>
1172       </varlistentry>
1173       
1174       <varlistentry>
1175         <term><option>-?</option></term>
1176         <listitem>
1177           <para>Print out usage information.</para>
1178         </listitem>
1179       </varlistentry>
1180     </variablelist>
1181
1182
1183     <sect2 id="manipulating-hp">
1184       <title>Manipulating the hp file</title>
1185
1186 <para>(Notes kindly offered by Jan-Willhem Maessen.)</para>
1187
1188 <para>
1189 The <filename>FOO.hp</filename> file produced when you ask for the
1190 heap profile of a program <filename>FOO</filename> is a text file with a particularly
1191 simple structure. Here's a representative example, with much of the
1192 actual data omitted:
1193 <screen>
1194 JOB "FOO -hC"
1195 DATE "Thu Dec 26 18:17 2002"
1196 SAMPLE_UNIT "seconds"
1197 VALUE_UNIT "bytes"
1198 BEGIN_SAMPLE 0.00
1199 END_SAMPLE 0.00
1200 BEGIN_SAMPLE 15.07
1201   ... sample data ...
1202 END_SAMPLE 15.07
1203 BEGIN_SAMPLE 30.23
1204   ... sample data ...
1205 END_SAMPLE 30.23
1206 ... etc.
1207 BEGIN_SAMPLE 11695.47
1208 END_SAMPLE 11695.47
1209 </screen>
1210 The first four lines (<literal>JOB</literal>, <literal>DATE</literal>, <literal>SAMPLE_UNIT</literal>, <literal>VALUE_UNIT</literal>) form a
1211 header.  Each block of lines starting with <literal>BEGIN_SAMPLE</literal> and ending
1212 with <literal>END_SAMPLE</literal> forms a single sample (you can think of this as a
1213 vertical slice of your heap profile).  The hp2ps utility should accept
1214 any input with a properly-formatted header followed by a series of
1215 *complete* samples.
1216 </para>
1217 </sect2>
1218
1219     <sect2>
1220       <title>Zooming in on regions of your profile</title>
1221
1222 <para>
1223 You can look at particular regions of your profile simply by loading a
1224 copy of the <filename>.hp</filename> file into a text editor and deleting the unwanted
1225 samples.  The resulting <filename>.hp</filename> file can be run through <command>hp2ps</command> and viewed
1226 or printed.
1227 </para>
1228 </sect2>
1229
1230     <sect2>
1231       <title>Viewing the heap profile of a running program</title>
1232
1233 <para>
1234 The <filename>.hp</filename> file is generated incrementally as your
1235 program runs.  In principle, running <command>hp2ps</command> on the incomplete file
1236 should produce a snapshot of your program's heap usage.  However, the
1237 last sample in the file may be incomplete, causing <command>hp2ps</command> to fail.  If
1238 you are using a machine with UNIX utilities installed, it's not too
1239 hard to work around this problem (though the resulting command line
1240 looks rather Byzantine):
1241 <screen>
1242   head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1243     | hp2ps > FOO.ps
1244 </screen>
1245
1246 The command <command>fgrep -n END_SAMPLE FOO.hp</command> finds the
1247 end of every complete sample in <filename>FOO.hp</filename>, and labels each sample with
1248 its ending line number.  We then select the line number of the last
1249 complete sample using <command>tail</command> and <command>cut</command>.  This is used as a
1250 parameter to <command>head</command>; the result is as if we deleted the final
1251 incomplete sample from <filename>FOO.hp</filename>.  This results in a properly-formatted
1252 .hp file which we feed directly to <command>hp2ps</command>.
1253 </para>
1254 </sect2>
1255     <sect2>
1256       <title>Viewing a heap profile in real time</title>
1257
1258 <para>
1259 The <command>gv</command> and <command>ghostview</command> programs
1260 have a "watch file" option can be used to view an up-to-date heap
1261 profile of your program as it runs.  Simply generate an incremental
1262 heap profile as described in the previous section.  Run <command>gv</command> on your
1263 profile:
1264 <screen>
1265   gv -watch -seascape FOO.ps 
1266 </screen>
1267 If you forget the <literal>-watch</literal> flag you can still select
1268 "Watch file" from the "State" menu.  Now each time you generate a new
1269 profile <filename>FOO.ps</filename> the view will update automatically.
1270 </para>
1271
1272 <para>
1273 This can all be encapsulated in a little script:
1274 <screen>
1275   #!/bin/sh
1276   head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1277     | hp2ps > FOO.ps
1278   gv -watch -seascape FOO.ps &amp;
1279   while [ 1 ] ; do
1280     sleep 10 # We generate a new profile every 10 seconds.
1281     head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1282       | hp2ps > FOO.ps
1283   done
1284 </screen>
1285 Occasionally <command>gv</command> will choke as it tries to read an incomplete copy of
1286 <filename>FOO.ps</filename> (because <command>hp2ps</command> is still running as an update
1287 occurs).  A slightly more complicated script works around this
1288 problem, by using the fact that sending a SIGHUP to gv will cause it
1289 to re-read its input file:
1290 <screen>
1291   #!/bin/sh
1292   head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1293     | hp2ps > FOO.ps
1294   gv FOO.ps &amp;
1295   gvpsnum=$!
1296   while [ 1 ] ; do
1297     sleep 10
1298     head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1299       | hp2ps > FOO.ps
1300     kill -HUP $gvpsnum
1301   done    
1302 </screen>
1303 </para>
1304 </sect2>
1305
1306
1307   </sect1>
1308
1309   <sect1 id="ticky-ticky">
1310     <title>Using &ldquo;ticky-ticky&rdquo; profiling (for implementors)</title>
1311     <indexterm><primary>ticky-ticky profiling</primary></indexterm>
1312
1313     <para>(ToDo: document properly.)</para>
1314
1315     <para>It is possible to compile Glasgow Haskell programs so that
1316     they will count lots and lots of interesting things, e.g., number
1317     of updates, number of data constructors entered, etc., etc.  We
1318     call this &ldquo;ticky-ticky&rdquo;
1319     profiling,<indexterm><primary>ticky-ticky
1320     profiling</primary></indexterm> <indexterm><primary>profiling,
1321     ticky-ticky</primary></indexterm> because that's the sound a Sun4
1322     makes when it is running up all those counters
1323     (<emphasis>slowly</emphasis>).</para>
1324
1325     <para>Ticky-ticky profiling is mainly intended for implementors;
1326     it is quite separate from the main &ldquo;cost-centre&rdquo;
1327     profiling system, intended for all users everywhere.</para>
1328
1329     <para>To be able to use ticky-ticky profiling, you will need to
1330     have built appropriate libraries and things when you made the
1331     system.  See &ldquo;Customising what libraries to build,&rdquo; in
1332     the installation guide.</para>
1333
1334     <para>To get your compiled program to spit out the ticky-ticky
1335     numbers, use a <option>-r</option> RTS
1336     option<indexterm><primary>-r RTS option</primary></indexterm>.
1337     See <xref linkend="runtime-control"/>.</para>
1338
1339     <para>Compiling your program with the <option>-ticky</option>
1340     switch yields an executable that performs these counts.  Here is a
1341     sample ticky-ticky statistics file, generated by the invocation
1342     <command>foo +RTS -rfoo.ticky</command>.</para>
1343
1344 <screen>
1345  foo +RTS -rfoo.ticky
1346
1347
1348 ALLOCATIONS: 3964631 (11330900 words total: 3999476 admin, 6098829 goods, 1232595 slop)
1349                                 total words:        2     3     4     5    6+
1350   69647 (  1.8%) function values                 50.0  50.0   0.0   0.0   0.0
1351 2382937 ( 60.1%) thunks                           0.0  83.9  16.1   0.0   0.0
1352 1477218 ( 37.3%) data values                     66.8  33.2   0.0   0.0   0.0
1353       0 (  0.0%) big tuples
1354       2 (  0.0%) black holes                      0.0 100.0   0.0   0.0   0.0
1355       0 (  0.0%) prim things
1356   34825 (  0.9%) partial applications             0.0   0.0   0.0 100.0   0.0
1357       2 (  0.0%) thread state objects             0.0   0.0   0.0   0.0 100.0
1358
1359 Total storage-manager allocations: 3647137 (11882004 words)
1360         [551104 words lost to speculative heap-checks]
1361
1362 STACK USAGE:
1363
1364 ENTERS: 9400092  of which 2005772 (21.3%) direct to the entry code
1365                   [the rest indirected via Node's info ptr]
1366 1860318 ( 19.8%) thunks
1367 3733184 ( 39.7%) data values
1368 3149544 ( 33.5%) function values
1369                   [of which 1999880 (63.5%) bypassed arg-satisfaction chk]
1370  348140 (  3.7%) partial applications
1371  308906 (  3.3%) normal indirections
1372       0 (  0.0%) permanent indirections
1373
1374 RETURNS: 5870443
1375 2137257 ( 36.4%) from entering a new constructor
1376                   [the rest from entering an existing constructor]
1377 2349219 ( 40.0%) vectored [the rest unvectored]
1378
1379 RET_NEW:         2137257:  32.5% 46.2% 21.3%  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
1380 RET_OLD:         3733184:   2.8% 67.9% 29.3%  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
1381 RET_UNBOXED_TUP:       2:   0.0%  0.0%100.0%  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
1382
1383 RET_VEC_RETURN : 2349219:   0.0%  0.0%100.0%  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
1384
1385 UPDATE FRAMES: 2241725 (0 omitted from thunks)
1386 SEQ FRAMES:    1
1387 CATCH FRAMES:  1
1388 UPDATES: 2241725
1389       0 (  0.0%) data values
1390   34827 (  1.6%) partial applications
1391                   [2 in place, 34825 allocated new space]
1392 2206898 ( 98.4%) updates to existing heap objects (46 by squeezing)
1393 UPD_CON_IN_NEW:         0:       0      0      0      0      0      0      0      0      0
1394 UPD_PAP_IN_NEW:     34825:       0      0      0  34825      0      0      0      0      0
1395
1396 NEW GEN UPDATES: 2274700 ( 99.9%)
1397
1398 OLD GEN UPDATES: 1852 (  0.1%)
1399
1400 Total bytes copied during GC: 190096
1401
1402 **************************************************
1403 3647137 ALLOC_HEAP_ctr
1404 11882004 ALLOC_HEAP_tot
1405   69647 ALLOC_FUN_ctr
1406   69647 ALLOC_FUN_adm
1407   69644 ALLOC_FUN_gds
1408   34819 ALLOC_FUN_slp
1409   34831 ALLOC_FUN_hst_0
1410   34816 ALLOC_FUN_hst_1
1411       0 ALLOC_FUN_hst_2
1412       0 ALLOC_FUN_hst_3
1413       0 ALLOC_FUN_hst_4
1414 2382937 ALLOC_UP_THK_ctr
1415       0 ALLOC_SE_THK_ctr
1416  308906 ENT_IND_ctr
1417       0 E!NT_PERM_IND_ctr requires +RTS -Z
1418 [... lots more info omitted ...]
1419       0 GC_SEL_ABANDONED_ctr
1420       0 GC_SEL_MINOR_ctr
1421       0 GC_SEL_MAJOR_ctr
1422       0 GC_FAILED_PROMOTION_ctr
1423   47524 GC_WORDS_COPIED_ctr
1424 </screen>
1425
1426     <para>The formatting of the information above the row of asterisks
1427     is subject to change, but hopefully provides a useful
1428     human-readable summary.  Below the asterisks <emphasis>all
1429     counters</emphasis> maintained by the ticky-ticky system are
1430     dumped, in a format intended to be machine-readable: zero or more
1431     spaces, an integer, a space, the counter name, and a newline.</para>
1432
1433     <para>In fact, not <emphasis>all</emphasis> counters are
1434     necessarily dumped; compile- or run-time flags can render certain
1435     counters invalid.  In this case, either the counter will simply
1436     not appear, or it will appear with a modified counter name,
1437     possibly along with an explanation for the omission (notice
1438     <literal>ENT&lowbar;PERM&lowbar;IND&lowbar;ctr</literal> appears
1439     with an inserted <literal>!</literal> above).  Software analysing
1440     this output should always check that it has the counters it
1441     expects.  Also, beware: some of the counters can have
1442     <emphasis>large</emphasis> values!</para>
1443
1444   </sect1>
1445
1446 </chapter>
1447
1448 <!-- Emacs stuff:
1449      ;;; Local Variables: ***
1450      ;;; mode: xml ***
1451      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
1452      ;;; End: ***
1453  -->