1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <chapter id="profiling">
3 <title>Profiling</title>
4 <indexterm><primary>profiling</primary>
6 <indexterm><primary>cost-centre profiling</primary></indexterm>
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>
12 <para> Any comments, suggestions and/or improvements you have are
13 welcome. Recommended “profiling tricks” would be
14 especially cool! </para>
16 <para>Profiling a program is a three-step process:</para>
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>
27 <indexterm><primary><literal>-auto</literal></primary>
29 <indexterm><primary><literal>-auto-all</literal></primary>
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>
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>
49 <sect1 id="cost-centres">
50 <title>Cost centres and cost-centre stacks</title>
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>
61 <para>Let's take a look at an example:</para>
64 main = print (nfib 25)
65 nfib n = if n < 2 then 1 else nfib (n-1) + nfib (n-2)
68 <para>Compile and run this program as follows:</para>
71 $ ghc -prof -auto-all -o Main Main.hs
77 <para>When a GHC-compiled program is run with the
78 <option>-p</option> RTS option, it generates a file called
79 <filename><prog>.prof</filename>. In this case, the file
80 will contain something like this:</para>
83 Fri May 12 14:06 2000 Time and Allocation Profiling Report (Final)
87 total time = 0.14 secs (7 ticks @ 20 ms)
88 total alloc = 8,741,204 bytes (excludes profiling overheads)
90 COST CENTRE MODULE %time %alloc
96 COST CENTRE MODULE entries %time %alloc %time %alloc
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
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>
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%
120 of both the time and allocation costs of the program.</para>
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>
128 <para>The time and allocation incurred by a given part of the
129 program is displayed in two ways: “individual”, which
130 are the costs incurred by the code covered by this cost centre
131 stack alone, and “inherited”, which includes the costs
132 incurred by all the children of this node.</para>
134 <para>The usefulness of cost-centre stacks is better demonstrated
135 by modifying the example slightly:</para>
138 main = print (f 25 + g 25)
140 g n = nfib (n `div` 2)
141 nfib n = if n < 2 then 1 else nfib (n-1) + nfib (n-2)
144 <para>Compile and run this program as before, and take a look at
145 the new profiling results:</para>
148 COST CENTRE MODULE scc %time %alloc %time %alloc
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
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>
166 <para>The actual meaning of the various columns in the output is:</para>
172 <para>The number of times this particular point in the call
173 graph was entered.</para>
178 <term>individual %time</term>
180 <para>The percentage of the total run time of the program
181 spent at this point in the call graph.</para>
186 <term>individual %alloc</term>
188 <para>The percentage of the total memory allocations
189 (excluding profiling overheads) of the program made by this
195 <term>inherited %time</term>
197 <para>The percentage of the total run time of the program
198 spent below this point in the call graph.</para>
203 <term>inherited %alloc</term>
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>
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>
218 <term><literal>ticks</literal></term>
220 <para>The raw number of time “ticks” which were
221 attributed to this cost-centre; from this, we get the
222 <literal>%time</literal> figure mentioned
228 <term><literal>bytes</literal></term>
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>%alloc</literal> figure mentioned
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>
247 <sect2><title>Inserting cost centres by hand</title>
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>
255 <para>The syntax of a cost centre annotation is</para>
258 {-# SCC "name" #-} <expression>
261 <para>where <literal>"name"</literal> is an aribrary string,
262 that will become the name of your cost centre as it appears
263 in the profiling output, and
264 <literal><expression></literal> is any Haskell
265 expression. An <literal>SCC</literal> annotation extends as
266 far to the right as possible when parsing.</para>
270 <sect2 id="prof-rules">
271 <title>Rules for attributing costs</title>
273 <para>The cost of evaluating any expression in your program is
274 attributed to a cost-centre stack using the following rules:</para>
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>
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
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>
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>
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>
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>
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>
330 <indexterm><primary><literal>-caf-all</literal></primary>
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>
340 <sect1 id="prof-compiler-options">
341 <title>Compiler options for profiling</title>
343 <indexterm><primary>profiling</primary><secondary>options</secondary></indexterm>
344 <indexterm><primary>options</primary><secondary>for profiling</secondary></indexterm>
349 <option>-prof</option>:
350 <indexterm><primary><option>-prof</option></primary></indexterm>
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>
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
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>
375 <option>-auto</option>:
376 <indexterm><primary><option>-auto</option></primary></indexterm>
377 <indexterm><primary>cost centres</primary><secondary>automatically inserting</secondary></indexterm>
380 <para> GHC will automatically add
381 <function>_scc_</function> constructs for all
382 top-level, exported functions.</para>
388 <option>-auto-all</option>:
389 <indexterm><primary><option>-auto-all</option></primary></indexterm>
392 <para> <emphasis>All</emphasis> top-level functions,
393 exported or not, will be automatically
394 <function>_scc_</function>'d.</para>
400 <option>-caf-all</option>:
401 <indexterm><primary><option>-caf-all</option></primary></indexterm>
404 <para> The costs of all CAFs in a module are usually
405 attributed to one “big” CAF cost-centre. With
406 this option, all CAFs get their own cost-centre. An
407 “if all else fails” option…</para>
413 <option>-ignore-scc</option>:
414 <indexterm><primary><option>-ignore-scc</option></primary></indexterm>
417 <para>Ignore any <function>_scc_</function>
418 constructs, so a module which already has
419 <function>_scc_</function>s can be compiled
420 for profiling with the annotations ignored.</para>
428 <sect1 id="prof-time-options">
429 <title>Time and allocation profiling</title>
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>
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>
445 <para>The <option>-p</option> option produces a standard
446 <emphasis>time profile</emphasis> report. It is written
448 <filename><replaceable>program</replaceable>.prof</filename>.</para>
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>
458 <option>-px</option>:
459 <indexterm><primary><option>-px</option></primary></indexterm>
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>
471 <indexterm><primary><option>-xc</option></primary><secondary>RTS option</secondary></indexterm>
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>
485 <sect1 id="prof-heap">
486 <title>Profiling memory usage</title>
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 ativity, and may
494 even cause the program to run out of memory altogether.</para>
496 <para>To generate a heap profile from your program:</para>
500 <para>Compile the program for profiling (<xref
501 linkend="prof-compiler-options"/>).</para>
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>
510 <para>Run <command>hp2ps</command> to produce a Postscript
512 <filename><replaceable>prog</replaceable>.ps</filename>. The
513 <command>hp2ps</command> utility is described in detail in
514 <xref linkend="hp2ps"/>.</para>
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>
523 <sect2 id="rts-options-heap-prof">
524 <title>RTS options for heap profiling</title>
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>
536 <indexterm><primary><option>-hc</option></primary><secondary>RTS option</secondary></indexterm>
539 <para>Breaks down the graph by the cost-centre stack which
540 produced the data.</para>
547 <indexterm><primary><option>-hm</option></primary><secondary>RTS option</secondary></indexterm>
550 <para>Break down the live heap by the module containing
551 the code which produced the data.</para>
558 <indexterm><primary><option>-hd</option></primary><secondary>RTS option</secondary></indexterm>
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>
571 <indexterm><primary><option>-hy</option></primary><secondary>RTS option</secondary></indexterm>
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>
584 <indexterm><primary><option>-hr</option></primary><secondary>RTS option</secondary></indexterm>
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>
596 <indexterm><primary><option>-hb</option></primary><secondary>RTS option</secondary></indexterm>
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>
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>
616 <option>-hc</option><replaceable>name</replaceable>,...
617 <indexterm><primary><option>-hc</option></primary><secondary>RTS option</secondary></indexterm>
620 <para>Restrict the profile to closures produced by
621 cost-centre stacks with one of the specified cost centres
628 <option>-hC</option><replaceable>name</replaceable>,...
629 <indexterm><primary><option>-hC</option></primary><secondary>RTS option</secondary></indexterm>
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>
640 <option>-hm</option><replaceable>module</replaceable>,...
641 <indexterm><primary><option>-hm</option></primary><secondary>RTS option</secondary></indexterm>
644 <para>Restrict the profile to closures produced by the
645 specified modules.</para>
651 <option>-hd</option><replaceable>desc</replaceable>,...
652 <indexterm><primary><option>-hd</option></primary><secondary>RTS option</secondary></indexterm>
655 <para>Restrict the profile to closures with the specified
656 description strings.</para>
662 <option>-hy</option><replaceable>type</replaceable>,...
663 <indexterm><primary><option>-hy</option></primary><secondary>RTS option</secondary></indexterm>
666 <para>Restrict the profile to closures with the specified
673 <option>-hr</option><replaceable>cc</replaceable>,...
674 <indexterm><primary><option>-hr</option></primary><secondary>RTS option</secondary></indexterm>
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>
685 <option>-hb</option><replaceable>bio</replaceable>,...
686 <indexterm><primary><option>-hb</option></primary><secondary>RTS option</secondary></indexterm>
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>
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>
703 <replaceable>prog</replaceable> +RTS -hr -hdBranch,Leaf
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>
713 <para>There are two more options which relate to heap
719 <option>-i<replaceable>secs</replaceable></option>:
720 <indexterm><primary><option>-i</option></primary></indexterm>
723 <para>Set the profiling (sampling) interval to
724 <replaceable>secs</replaceable> seconds (the default is
725 0.1 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>
735 <indexterm><primary><option>-xt</option></primary><secondary>RTS option</secondary></indexterm>
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
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>
748 <para>Memory occupied by threads and their stacks is
749 labelled as “TSO” when displaying the profile
750 by closure description or type description.</para>
757 <sect2 id="retainer-prof">
758 <title>Retainer Profiling</title>
760 <para>Retainer profiling is designed to help answer questions
761 like <quote>why is this data being retained?</quote>. We start
762 by defining what we mean by a retainer:</para>
765 <para>A retainer is either the system stack, or an unevaluated
766 closure (thunk).</para>
769 <para>In particular, constructors are <emphasis>not</emphasis>
772 <para>An object A is retained by an object B if object A can be
773 reached by recursively following pointers starting from object
774 B but not meeting any other retainers on the way. Each object
775 has one or more retainers, collectively called its
776 <firstterm>retainer set</firstterm>.</para>
778 <para>When retainer profiling is requested by giving the program
779 the <option>-hr</option> option, a graph is generated which is
780 broken down by retainer set. A retainer set is displayed as a
781 set of cost-centre stacks; because this is usually too large to
782 fit on the profile graph, each retainer set is numbered and
783 shown abbreviated on the graph along with its number, and the
784 full list of retainer sets is dumped into the file
785 <filename><replaceable>prog</replaceable>.prof</filename>.</para>
787 <para>Retainer profiling requires multiple passes over the live
788 heap in order to discover the full retainer set for each
789 object, which can be quite slow. So we set a limit on the
790 maximum size of a retainer set, where all retainer sets larger
791 than the maximum retainer set size are replaced by the special
792 set <literal>MANY</literal>. The maximum set size defaults to 8
793 and can be altered with the <option>-R</option> RTS
798 <term><option>-R</option><replaceable>size</replaceable></term>
800 <para>Restrict the number of elements in a retainer set to
801 <replaceable>size</replaceable> (default 8).</para>
807 <title>Hints for using retainer profiling</title>
809 <para>The definition of retainers is designed to reflect a
810 common cause of space leaks: a large structure is retained by
811 an unevaluated computation, and will be released once the
812 compuation is forced. A good example is looking up a value in
813 a finite map, where unless the lookup is forced in a timely
814 manner the unevaluated lookup will cause the whole mapping to
815 be retained. These kind of space leaks can often be
816 eliminated by forcing the relevant computations to be
817 performed eagerly, using <literal>seq</literal> or strictness
818 annotations on data constructor fields.</para>
820 <para>Often a particular data structure is being retained by a
821 chain of unevaluated closures, only the nearest of which will
822 be reported by retainer profiling - for example A retains B, B
823 retains C, and C retains a large structure. There might be a
824 large number of Bs but only a single A, so A is really the one
825 we're interested in eliminating. However, retainer profiling
826 will in this case report B as the retainer of the large
827 structure. To move further up the chain of retainers, we can
828 ask for another retainer profile but this time restrict the
829 profile to B objects, so we get a profile of the retainers of
833 <replaceable>prog</replaceable> +RTS -hr -hcB
836 <para>This trick isn't foolproof, because there might be other
837 B closures in the heap which aren't the retainers we are
838 interested in, but we've found this to be a useful technique
839 in most cases.</para>
843 <sect2 id="biography-prof">
844 <title>Biographical Profiling</title>
846 <para>A typical heap object may be in one of the following four
847 states at each point in its lifetime:</para>
851 <para>The <firstterm>lag</firstterm> stage, which is the
852 time between creation and the first use of the
856 <para>the <firstterm>use</firstterm> stage, which lasts from
857 the first use until the last use of the object, and</para>
860 <para>The <firstterm>drag</firstterm> stage, which lasts
861 from the final use until the last reference to the object
865 <para>An object which is never used is said to be in the
866 <firstterm>void</firstterm> state for its whole
871 <para>A biographical heap profile displays the portion of the
872 live heap in each of the four states listed above. Usually the
873 most interesting states are the void and drag states: live heap
874 in these states is more likely to be wasted space than heap in
875 the lag or use states.</para>
877 <para>It is also possible to break down the heap in one or more
878 of these states by a different criteria, by restricting a
879 profile by biography. For example, to show the portion of the
880 heap in the drag or void state by producer: </para>
883 <replaceable>prog</replaceable> +RTS -hc -hbdrag,void
886 <para>Once you know the producer or the type of the heap in the
887 drag or void states, the next step is usually to find the
891 <replaceable>prog</replaceable> +RTS -hr -hc<replaceable>cc</replaceable>...
894 <para>NOTE: this two stage process is required because GHC
895 cannot currently profile using both biographical and retainer
896 information simultaneously.</para>
905 <sect1 id="prof-xml-tool">
906 <title>Graphical time/allocation profile</title>
908 <para>You can view the time and allocation profiling graph of your
909 program graphically, using <command>ghcprof</command>. This is a
910 new tool with GHC 4.08, and will eventually be the de-facto
911 standard way of viewing GHC profiles<footnote><para>Actually this
912 isn't true any more, we are working on a new tool for
913 displaying heap profiles using Gtk+HS, so
914 <command>ghcprof</command> may go away at some point in the future.</para>
917 <para>To run <command>ghcprof</command>, you need
918 <productname>daVinci</productname> installed, which can be
920 url="http://www.informatik.uni-bremen.de/daVinci/"><citetitle>The Graph
921 Visualisation Tool daVinci</citetitle></ulink>. Install one of
923 distributions<footnote><para><productname>daVinci</productname> is
924 sadly not open-source :-(.</para></footnote>, and set your
925 <envar>DAVINCIHOME</envar> environment variable to point to the
926 installation directory.</para>
928 <para><command>ghcprof</command> uses an XML-based profiling log
929 format, and you therefore need to run your program with a
930 different option: <option>-px</option>. The file generated is
931 still called <filename><prog>.prof</filename>. To see the
932 profile, run <command>ghcprof</command> like this:</para>
934 <indexterm><primary><option>-px</option></primary></indexterm>
937 $ ghcprof <prog>.prof
940 <para>which should pop up a window showing the call-graph of your
941 program in glorious detail. More information on using
942 <command>ghcprof</command> can be found at <ulink
943 url="http://www.dcs.warwick.ac.uk/people/academic/Stephen.Jarvis/profiler/index.html"><citetitle>The
944 Cost-Centre Stack Profiling Tool for
945 GHC</citetitle></ulink>.</para>
950 <title><command>hp2ps</command>––heap profile to PostScript</title>
952 <indexterm><primary><command>hp2ps</command></primary></indexterm>
953 <indexterm><primary>heap profiles</primary></indexterm>
954 <indexterm><primary>postscript, from heap profiles</primary></indexterm>
955 <indexterm><primary><option>-h<break-down></option></primary></indexterm>
960 hp2ps [flags] [<file>[.hp]]
964 <command>hp2ps</command><indexterm><primary>hp2ps
965 program</primary></indexterm> converts a heap profile as produced
966 by the <option>-h<break-down></option> runtime option into a
967 PostScript graph of the heap profile. By convention, the file to
968 be processed by <command>hp2ps</command> has a
969 <filename>.hp</filename> extension. The PostScript output is
970 written to <filename><file>@.ps</filename>. If
971 <filename><file></filename> is omitted entirely, then the
972 program behaves as a filter.</para>
974 <para><command>hp2ps</command> is distributed in
975 <filename>ghc/utils/hp2ps</filename> in a GHC source
976 distribution. It was originally developed by Dave Wakeling as part
977 of the HBC/LML heap profiler.</para>
979 <para>The flags are:</para>
984 <term><option>-d</option></term>
986 <para>In order to make graphs more readable,
987 <command>hp2ps</command> sorts the shaded bands for each
988 identifier. The default sort ordering is for the bands with
989 the largest area to be stacked on top of the smaller ones.
990 The <option>-d</option> option causes rougher bands (those
991 representing series of values with the largest standard
992 deviations) to be stacked on top of smoother ones.</para>
997 <term><option>-b</option></term>
999 <para>Normally, <command>hp2ps</command> puts the title of
1000 the graph in a small box at the top of the page. However, if
1001 the JOB string is too long to fit in a small box (more than
1002 35 characters), then <command>hp2ps</command> will choose to
1003 use a big box instead. The <option>-b</option> option
1004 forces <command>hp2ps</command> to use a big box.</para>
1009 <term><option>-e<float>[in|mm|pt]</option></term>
1011 <para>Generate encapsulated PostScript suitable for
1012 inclusion in LaTeX documents. Usually, the PostScript graph
1013 is drawn in landscape mode in an area 9 inches wide by 6
1014 inches high, and <command>hp2ps</command> arranges for this
1015 area to be approximately centred on a sheet of a4 paper.
1016 This format is convenient of studying the graph in detail,
1017 but it is unsuitable for inclusion in LaTeX documents. The
1018 <option>-e</option> option causes the graph to be drawn in
1019 portrait mode, with float specifying the width in inches,
1020 millimetres or points (the default). The resulting
1021 PostScript file conforms to the Encapsulated PostScript
1022 (EPS) convention, and it can be included in a LaTeX document
1023 using Rokicki's dvi-to-PostScript converter
1024 <command>dvips</command>.</para>
1029 <term><option>-g</option></term>
1031 <para>Create output suitable for the <command>gs</command>
1032 PostScript previewer (or similar). In this case the graph is
1033 printed in portrait mode without scaling. The output is
1034 unsuitable for a laser printer.</para>
1039 <term><option>-l</option></term>
1041 <para>Normally a profile is limited to 20 bands with
1042 additional identifiers being grouped into an
1043 <literal>OTHER</literal> band. The <option>-l</option> flag
1044 removes this 20 band and limit, producing as many bands as
1045 necessary. No key is produced as it won't fit!. It is useful
1046 for creation time profiles with many bands.</para>
1051 <term><option>-m<int></option></term>
1053 <para>Normally a profile is limited to 20 bands with
1054 additional identifiers being grouped into an
1055 <literal>OTHER</literal> band. The <option>-m</option> flag
1056 specifies an alternative band limit (the maximum is
1059 <para><option>-m0</option> requests the band limit to be
1060 removed. As many bands as necessary are produced. However no
1061 key is produced as it won't fit! It is useful for displaying
1062 creation time profiles with many bands.</para>
1067 <term><option>-p</option></term>
1069 <para>Use previous parameters. By default, the PostScript
1070 graph is automatically scaled both horizontally and
1071 vertically so that it fills the page. However, when
1072 preparing a series of graphs for use in a presentation, it
1073 is often useful to draw a new graph using the same scale,
1074 shading and ordering as a previous one. The
1075 <option>-p</option> flag causes the graph to be drawn using
1076 the parameters determined by a previous run of
1077 <command>hp2ps</command> on <filename>file</filename>. These
1078 are extracted from <filename>file@.aux</filename>.</para>
1083 <term><option>-s</option></term>
1085 <para>Use a small box for the title.</para>
1090 <term><option>-t<float></option></term>
1092 <para>Normally trace elements which sum to a total of less
1093 than 1% of the profile are removed from the
1094 profile. The <option>-t</option> option allows this
1095 percentage to be modified (maximum 5%).</para>
1097 <para><option>-t0</option> requests no trace elements to be
1098 removed from the profile, ensuring that all the data will be
1104 <term><option>-c</option></term>
1106 <para>Generate colour output.</para>
1111 <term><option>-y</option></term>
1113 <para>Ignore marks.</para>
1118 <term><option>-?</option></term>
1120 <para>Print out usage information.</para>
1126 <sect2 id="manipulating-hp">
1127 <title>Manipulating the hp file</title>
1129 <para>(Notes kindly offered by Jan-Willhem Maessen.)</para>
1132 The <filename>FOO.hp</filename> file produced when you ask for the
1133 heap profile of a program <filename>FOO</filename> is a text file with a particularly
1134 simple structure. Here's a representative example, with much of the
1135 actual data omitted:
1138 DATE "Thu Dec 26 18:17 2002"
1139 SAMPLE_UNIT "seconds"
1150 BEGIN_SAMPLE 11695.47
1153 The first four lines (<literal>JOB</literal>, <literal>DATE</literal>, <literal>SAMPLE_UNIT</literal>, <literal>VALUE_UNIT</literal>) form a
1154 header. Each block of lines starting with <literal>BEGIN_SAMPLE</literal> and ending
1155 with <literal>END_SAMPLE</literal> forms a single sample (you can think of this as a
1156 vertical slice of your heap profile). The hp2ps utility should accept
1157 any input with a properly-formatted header followed by a series of
1163 <title>Zooming in on regions of your profile</title>
1166 You can look at particular regions of your profile simply by loading a
1167 copy of the <filename>.hp</filename> file into a text editor and deleting the unwanted
1168 samples. The resulting <filename>.hp</filename> file can be run through <command>hp2ps</command> and viewed
1174 <title>Viewing the heap profile of a running program</title>
1177 The <filename>.hp</filename> file is generated incrementally as your
1178 program runs. In principle, running <command>hp2ps</command> on the incomplete file
1179 should produce a snapshot of your program's heap usage. However, the
1180 last sample in the file may be incomplete, causing <command>hp2ps</command> to fail. If
1181 you are using a machine with UNIX utilities installed, it's not too
1182 hard to work around this problem (though the resulting command line
1183 looks rather Byzantine):
1185 head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1189 The command <command>fgrep -n END_SAMPLE FOO.hp</command> finds the
1190 end of every complete sample in <filename>FOO.hp</filename>, and labels each sample with
1191 its ending line number. We then select the line number of the last
1192 complete sample using <command>tail</command> and <command>cut</command>. This is used as a
1193 parameter to <command>head</command>; the result is as if we deleted the final
1194 incomplete sample from <filename>FOO.hp</filename>. This results in a properly-formatted
1195 .hp file which we feed directly to <command>hp2ps</command>.
1199 <title>Viewing a heap profile in real time</title>
1202 The <command>gv</command> and <command>ghostview</command> programs
1203 have a "watch file" option can be used to view an up-to-date heap
1204 profile of your program as it runs. Simply generate an incremental
1205 heap profile as described in the previous section. Run <command>gv</command> on your
1208 gv -watch -seascape FOO.ps
1210 If you forget the <literal>-watch</literal> flag you can still select
1211 "Watch file" from the "State" menu. Now each time you generate a new
1212 profile <filename>FOO.ps</filename> the view will update automatically.
1216 This can all be encapsulated in a little script:
1219 head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1221 gv -watch -seascape FOO.ps &
1223 sleep 10 # We generate a new profile every 10 seconds.
1224 head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1228 Occasionally <command>gv</command> will choke as it tries to read an incomplete copy of
1229 <filename>FOO.ps</filename> (because <command>hp2ps</command> is still running as an update
1230 occurs). A slightly more complicated script works around this
1231 problem, by using the fact that sending a SIGHUP to gv will cause it
1232 to re-read its input file:
1235 head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1241 head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
1252 <sect1 id="ticky-ticky">
1253 <title>Using “ticky-ticky” profiling (for implementors)</title>
1254 <indexterm><primary>ticky-ticky profiling</primary></indexterm>
1256 <para>(ToDo: document properly.)</para>
1258 <para>It is possible to compile Glasgow Haskell programs so that
1259 they will count lots and lots of interesting things, e.g., number
1260 of updates, number of data constructors entered, etc., etc. We
1261 call this “ticky-ticky”
1262 profiling,<indexterm><primary>ticky-ticky
1263 profiling</primary></indexterm> <indexterm><primary>profiling,
1264 ticky-ticky</primary></indexterm> because that's the sound a Sun4
1265 makes when it is running up all those counters
1266 (<emphasis>slowly</emphasis>).</para>
1268 <para>Ticky-ticky profiling is mainly intended for implementors;
1269 it is quite separate from the main “cost-centre”
1270 profiling system, intended for all users everywhere.</para>
1272 <para>To be able to use ticky-ticky profiling, you will need to
1273 have built appropriate libraries and things when you made the
1274 system. See “Customising what libraries to build,” in
1275 the installation guide.</para>
1277 <para>To get your compiled program to spit out the ticky-ticky
1278 numbers, use a <option>-r</option> RTS
1279 option<indexterm><primary>-r RTS option</primary></indexterm>.
1280 See <xref linkend="runtime-control"/>.</para>
1282 <para>Compiling your program with the <option>-ticky</option>
1283 switch yields an executable that performs these counts. Here is a
1284 sample ticky-ticky statistics file, generated by the invocation
1285 <command>foo +RTS -rfoo.ticky</command>.</para>
1288 foo +RTS -rfoo.ticky
1291 ALLOCATIONS: 3964631 (11330900 words total: 3999476 admin, 6098829 goods, 1232595 slop)
1292 total words: 2 3 4 5 6+
1293 69647 ( 1.8%) function values 50.0 50.0 0.0 0.0 0.0
1294 2382937 ( 60.1%) thunks 0.0 83.9 16.1 0.0 0.0
1295 1477218 ( 37.3%) data values 66.8 33.2 0.0 0.0 0.0
1296 0 ( 0.0%) big tuples
1297 2 ( 0.0%) black holes 0.0 100.0 0.0 0.0 0.0
1298 0 ( 0.0%) prim things
1299 34825 ( 0.9%) partial applications 0.0 0.0 0.0 100.0 0.0
1300 2 ( 0.0%) thread state objects 0.0 0.0 0.0 0.0 100.0
1302 Total storage-manager allocations: 3647137 (11882004 words)
1303 [551104 words lost to speculative heap-checks]
1307 ENTERS: 9400092 of which 2005772 (21.3%) direct to the entry code
1308 [the rest indirected via Node's info ptr]
1309 1860318 ( 19.8%) thunks
1310 3733184 ( 39.7%) data values
1311 3149544 ( 33.5%) function values
1312 [of which 1999880 (63.5%) bypassed arg-satisfaction chk]
1313 348140 ( 3.7%) partial applications
1314 308906 ( 3.3%) normal indirections
1315 0 ( 0.0%) permanent indirections
1318 2137257 ( 36.4%) from entering a new constructor
1319 [the rest from entering an existing constructor]
1320 2349219 ( 40.0%) vectored [the rest unvectored]
1322 RET_NEW: 2137257: 32.5% 46.2% 21.3% 0.0% 0.0% 0.0% 0.0% 0.0% 0.0%
1323 RET_OLD: 3733184: 2.8% 67.9% 29.3% 0.0% 0.0% 0.0% 0.0% 0.0% 0.0%
1324 RET_UNBOXED_TUP: 2: 0.0% 0.0%100.0% 0.0% 0.0% 0.0% 0.0% 0.0% 0.0%
1326 RET_VEC_RETURN : 2349219: 0.0% 0.0%100.0% 0.0% 0.0% 0.0% 0.0% 0.0% 0.0%
1328 UPDATE FRAMES: 2241725 (0 omitted from thunks)
1332 0 ( 0.0%) data values
1333 34827 ( 1.6%) partial applications
1334 [2 in place, 34825 allocated new space]
1335 2206898 ( 98.4%) updates to existing heap objects (46 by squeezing)
1336 UPD_CON_IN_NEW: 0: 0 0 0 0 0 0 0 0 0
1337 UPD_PAP_IN_NEW: 34825: 0 0 0 34825 0 0 0 0 0
1339 NEW GEN UPDATES: 2274700 ( 99.9%)
1341 OLD GEN UPDATES: 1852 ( 0.1%)
1343 Total bytes copied during GC: 190096
1345 **************************************************
1346 3647137 ALLOC_HEAP_ctr
1347 11882004 ALLOC_HEAP_tot
1352 34831 ALLOC_FUN_hst_0
1353 34816 ALLOC_FUN_hst_1
1357 2382937 ALLOC_UP_THK_ctr
1360 0 E!NT_PERM_IND_ctr requires +RTS -Z
1361 [... lots more info omitted ...]
1362 0 GC_SEL_ABANDONED_ctr
1365 0 GC_FAILED_PROMOTION_ctr
1366 47524 GC_WORDS_COPIED_ctr
1369 <para>The formatting of the information above the row of asterisks
1370 is subject to change, but hopefully provides a useful
1371 human-readable summary. Below the asterisks <emphasis>all
1372 counters</emphasis> maintained by the ticky-ticky system are
1373 dumped, in a format intended to be machine-readable: zero or more
1374 spaces, an integer, a space, the counter name, and a newline.</para>
1376 <para>In fact, not <emphasis>all</emphasis> counters are
1377 necessarily dumped; compile- or run-time flags can render certain
1378 counters invalid. In this case, either the counter will simply
1379 not appear, or it will appear with a modified counter name,
1380 possibly along with an explanation for the omission (notice
1381 <literal>ENT_PERM_IND_ctr</literal> appears
1382 with an inserted <literal>!</literal> above). Software analysing
1383 this output should always check that it has the counters it
1384 expects. Also, beware: some of the counters can have
1385 <emphasis>large</emphasis> values!</para>
1392 ;;; Local Variables: ***
1394 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***