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