30175aed34d08c7315145a30fe1a80a0630f1376
[ghc-hetmet.git] / ghc / docs / users_guide / profiling.sgml
1 <chapter id="profiling">
2   <title>Profiling</Title>
3   <indexterm><primary>profiling</primary>
4   </indexterm>
5   <indexterm><primary>cost-centre profiling</primary></indexterm>
6
7   <Para> Glasgow Haskell comes with a time and space profiling
8   system. Its purpose is to help you improve your understanding of
9   your program's execution behaviour, so you can improve it.</Para>
10   
11   <Para> Any comments, suggestions and/or improvements you have are
12   welcome.  Recommended &ldquo;profiling tricks&rdquo; would be
13   especially cool! </Para>
14
15   <para>Profiling a program is a three-step process:</para>
16
17   <orderedlist>
18     <listitem>
19       <para> Re-compile your program for profiling with the
20       <literal>-prof</literal> option, and probably one of the
21       <literal>-auto</literal> or <literal>-auto-all</literal>
22       options.  These options are described in more detail in <xref
23       linkend="prof-compiler-options"> </para>
24       <indexterm><primary><literal>-prof</literal></primary>
25       </indexterm>
26       <indexterm><primary><literal>-auto</literal></primary>
27       </indexterm>
28       <indexterm><primary><literal>-auto-all</literal></primary>
29       </indexterm>
30     </listitem>
31
32     <listitem>
33       <para> Run your program with one of the profiling options
34       <literal>-p</literal> or <literal>-h</literal>.  This generates
35       a file of profiling information.</para>
36       <indexterm><primary><literal>-p</literal></primary><secondary>RTS
37       option</secondary></indexterm>
38       <indexterm><primary><literal>-h</literal></primary><secondary>RTS
39       option</secondary></indexterm>
40     </listitem>
41       
42     <listitem>
43       <para> Examine the generated profiling information, using one of
44       GHC's profiling tools.  The tool to use will depend on the kind
45       of profiling information generated.</para>
46     </listitem>
47     
48   </orderedlist>
49   
50   <sect1>
51     <title>Cost centres and cost-centre stacks</title>
52     
53     <para>GHC's profiling system assigns <firstterm>costs</firstterm>
54     to <firstterm>cost centres</firstterm>.  A cost is simply the time
55     or space required to evaluate an expression.  Cost centres are
56     program annotations around expressions; all costs incurred by the
57     annotated expression are assigned to the enclosing cost centre.
58     Furthermore, GHC will remember the stack of enclosing cost centres
59     for any given expression at run-time and generate a call-graph of
60     cost attributions.</para>
61
62     <para>Let's take a look at an example:</para>
63
64     <programlisting>
65 main = print (nfib 25)
66 nfib n = if n < 2 then 1 else nfib (n-1) + nfib (n-2)
67 </programlisting>
68
69     <para>Compile and run this program as follows:</para>
70
71     <screen>
72 $ ghc -prof -auto-all -o Main Main.hs
73 $ ./Main +RTS -p
74 121393
75 $
76 </screen>
77
78     <para>When a GHC-compiled program is run with the
79     <option>-p</option> RTS option, it generates a file called
80     <filename>&lt;prog&gt;.prof</filename>.  In this case, the file
81     will contain something like this:</para>
82
83 <screen>
84        Tue Apr 18 12:52 2000 Time and Allocation Profiling Report  (Final)
85
86            Main +RTS -p -RTS
87
88         total time  =        0.14 secs   (7 ticks @ 20 ms)
89         total alloc =   8,741,204 bytes  (excludes profiling overheads)
90
91 COST CENTRE          MODULE     %time %alloc
92
93 nfib                 Main       100.0  100.0
94
95
96 COST CENTRE              MODULE         scc %time %alloc    inner  cafs
97
98 MAIN                     MAIN             0   0.0   0.0        0     1
99  main                    Main             0   0.0   0.0        0     1
100  CAF                     PrelHandle       3   0.0   0.0        0     3
101  CAF                     PrelAddr         1   0.0   0.0        0     0
102  CAF                     Main             6   0.0   0.0        1     0
103   main                   Main             1   0.0   0.0        1     1
104    nfib                  Main        242785 100.0 100.0   242784     4
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 usefulness of cost-centre stacks is better demonstrated
129     by  modifying the example slightly:</para>
130
131     <programlisting>
132 main = print (f 25 + g 25)
133 f n  = nfib n
134 g n  = nfib (n `div` 2)
135 nfib n = if n < 2 then 1 else nfib (n-1) + nfib (n-2)
136 </programlisting>
137
138     <para>Compile and run this program as before, and take a look at
139     the new profiling results:</para>
140
141 <screen>
142 COST CENTRE              MODULE         scc %time %alloc    inner  cafs
143
144 MAIN                     MAIN             0   0.0   0.0        0     1
145  main                    Main             0   0.0   0.0        0     1
146  CAF                     PrelHandle       3   0.0   0.0        0     3
147  CAF                     PrelAddr         1   0.0   0.0        0     0
148  CAF                     Main             9   0.0   0.0        1     1
149   main                   Main             1   0.0   0.0        2     2
150    g                     Main             1   0.0   0.0        1     3
151     nfib                 Main           465   0.0   0.2      464     0
152    f                     Main             1   0.0   0.0        1     1
153     nfib                 Main        242785 100.0  99.8   242784     1
154 </screen>
155
156     <para>Now although we had two calls to <function>nfib</function>
157     in the program, it is immediately clear that it was the call from
158     <function>f</function> which took all the time.</para>
159
160     <para>The actual meaning of the various columns in the output is:</para>
161
162     <variablelist>
163       <varlistentry>
164         <term>scc</term>
165         <listitem>
166           <para>The number of times this particular point in the call
167           graph was entered.</para>
168         </listitem>
169       </varlistentry>
170
171       <varlistentry>
172         <term>&percnt;time</term>
173         <listitem>
174           <para>The percentage of the total run time of the program
175           spent at this point in the call graph.</para>
176         </listitem>
177       </varlistentry>
178
179       <varlistentry>
180         <term>&percnt;alloc</term>
181         <listitem>
182           <para>The percentage of the total memory allocations
183           (excluding profiling overheads) of the program made by this
184           call.</para>
185         </listitem>
186       </varlistentry>
187
188       <varlistentry>
189         <term>inner</term>
190         <listitem>
191           <para>The number of times an inner call-graph context was
192           entered from here (including recursive calls).</para>
193         </listitem>
194       </varlistentry>
195
196       <varlistentry>
197         <term>cafs</term>
198         <listitem>
199           <para>The number of times a CAF context was entered from
200           here.  CAFs are described in <xref
201           linkend="prof-rules">.</para>
202         </listitem>
203       </varlistentry>
204     </variablelist>
205
206     <para>In addition you can use the <Option>-P</Option> RTS option
207     <indexterm><primary><option>-P</option></primary></indexterm> to
208     get the following additional information:</para>
209
210     <variablelist>
211       <varlistentry>
212         <term><literal>ticks</literal></term>
213         <listitem>
214           <Para>The raw number of time &ldquo;ticks&rdquo; which were
215           attributed to this cost-centre; from this, we get the
216           <literal>&percnt;time</literal> figure mentioned
217           above.</Para>
218         </listitem>
219       </varlistentry>
220
221       <varlistentry>
222         <term><literal>bytes</literal></term>
223         <listItem>
224           <Para>Number of bytes allocated in the heap while in this
225           cost-centre; again, this is the raw number from which we get
226           the <literal>&percnt;alloc</literal> figure mentioned
227           above.</Para>
228         </listItem>
229       </varListEntry>
230     </variablelist>
231
232     <para>What about recursive functions, and mutually recursive
233     groups of functions?  Where are the costs attributed?  Well,
234     although GHC does keep information about which groups of functions
235     called each other recursively, this information isn't displayed in
236     the basic time and allocation profile, instead the call-graph is
237     flattened into a tree.  The XML profiling tool (described in <xref
238     linkend="prof-xml-tool">) will be able to display real loops in
239     the call-graph.</para>
240
241     <sect2><title>Inserting cost centres by hand</title>
242
243       <para>Cost centres are just program annotations.  When you say
244       <option>-auto-all</option> to the compiler, it automatically
245       inserts a cost centre annotation around every top-level function
246       in your program, but you are entirely free to add the cost
247       centre annotations yourself.</para>
248
249       <para>The syntax of a cost centre annotation is</para>
250
251       <programlisting>
252      _scc_ "name" &lt;expression&gt;
253 </programlisting>
254
255       <para>where <literal>"name"</literal> is an aribrary string,
256       that will become the name of your cost centre as it appears
257       in the profiling output, and
258       <literal>&lt;expression&gt;</literal> is any Haskell
259       expression.  An <literal>_scc_</literal> annotation extends as
260       far to the right as possible when parsing.</para>
261
262     </sect2>
263
264     <sect2 id="prof-rules">
265       <title>Rules for attributing costs</title>
266
267       <para>The cost of evaluating any expression in your program is
268       attributed to a cost-centre stack using the following rules:</para>
269
270       <itemizedlist>
271         <listitem>
272           <para>If the expression is part of the
273           <firstterm>one-off</firstterm> costs of evaluating the
274           enclosing top-level definition, then costs are attributed to
275           the stack of lexically enclosing <literal>_scc_</literal>
276           annotations on top of the special <literal>CAF</literal>
277           cost-centre. </para>
278         </listitem>
279
280         <listitem>
281           <para>Otherwise, costs are attributed to the stack of
282           lexically-enclosing <literal>_scc_</literal> annotations,
283           appended to the cost-centre stack in effect at the
284           <firstterm>call site</firstterm> of the current top-level
285           definition<footnote> <para>The call-site is just the place
286           in the source code which mentions the particular function or
287           variable.</para></footnote>.  Notice that this is a recursive
288           definition.</para>
289         </listitem>
290       </itemizedlist>
291
292       <para>What do we mean by one-off costs?  Well, Haskell is a lazy
293       language, and certain expressions are only ever evaluated once.
294       For example, if we write:</para>
295
296       <programlisting>
297 x = nfib 25
298 </programlisting>
299
300       <para>then <varname>x</varname> will only be evaluated once (if
301       at all), and subsequent demands for <varname>x</varname> will
302       immediately get to see the cached result.  The definition
303       <varname>x</varname> is called a CAF (Constant Applicative
304       Form), because it has no arguments.</para>
305
306       <para>For the purposes of profiling, we say that the expression
307       <literal>nfib 25</literal> belongs to the one-off costs of
308       evaluating <varname>x</varname>.</para>
309
310       <para>Since one-off costs aren't strictly speaking part of the
311       call-graph of the program, they are attributed to a special
312       top-level cost centre, <literal>CAF</literal>.  There may be one
313       <literal>CAF</literal> cost centre for each module (the
314       default), or one for each top-level definition with any one-off
315       costs (this behaviour can be selected by giving GHC the
316       <option>-caf-all</option> flag).</para>
317
318       <indexterm><primary><literal>-caf-all</literal></primary>
319       </indexterm>
320
321       <para>If you think you have a weird profile, or the call-graph
322       doesn't look like you expect it to, feel free to send it (and
323       your program) to us at
324       <email>glasgow-haskell-bugs@haskell.org</email>.</para>
325
326     </sect2>
327   </sect1>
328
329   <sect1 id="prof-heap">
330     <title>Profiling memory usage</title>
331
332     <para>In addition to profiling the time and allocation behaviour
333     of your program, you can also generate a graph of its memory usage
334     over time.  This is useful for detecting the causes of
335     <firstterm>space leaks</firstterm>, when your program holds on to
336     more memory at run-time that it needs to.  Space leaks lead to
337     longer run-times due to heavy garbage collector ativity, and may
338     even cause the program to run out of memory altogether.</para>
339
340     <para>To generate a heap profile from your program, compile it as
341     before, but this time run it with the <option>-h</option> runtime
342     option.  This generates a file
343     <filename>&lt;prog&gt;.hp</filename> file, which you then process
344     with <command>hp2ps</command> to produce a Postscript file
345     <filename>&lt;prog&gt;.ps</filename>.  The Postscript file can be
346     viewed with something like <command>ghostview</command>, or
347     printed out on a Postscript-compatible printer.</para>
348
349     <para>For the RTS options that control the kind of heap profile
350     generated, see <xref linkend="prof-rts-options">.  Details on the
351     usage of the <command>hp2ps</command> program are given in <xref
352     linkend="hp2ps"></para>
353
354   </sect1>
355
356   <sect1 id="prof-xml-tool">
357     <title>Graphical time/allocation profile</title>
358
359     <para>You can view the time and allocation profiling graph of your
360     program graphically, using <command>ghcprof</command>.  This is a
361     new tool with GHC 4.07, and will eventually be the de-facto
362     standard way of viewing GHC profiles.</para>
363
364     <para>To run <command>ghcprof</command>, you need
365     <productname>daVinci</productname> installed, which can be
366     obtained from <ulink
367     url="http://www.tzi.de/~davinci/"><citetitle>The Graph
368     Visualisation Tool daVinci</citetitle></ulink>.  Install one of
369     the binary
370     distributions<footnote><para><productname>daVinci</productname> is
371     sadly not open-source :-(.</para></footnote>, and set your
372     <envar>DAVINCIHOME</envar> environment variable to point to the
373     installation directory.</para>
374
375     <para><command>ghcprof</command> uses an XML-based profiling log
376     format, and you therefore need to run your program with a
377     different option: <option>-px</option>.  The file generated is
378     still called <filename>&lt;prog&gt;.prof</filename>.  To see the
379     profile, run <command>ghcprof</command> like this:</para>
380
381     <indexterm><primary><option>-px</option></primary></indexterm>
382
383 <screen>
384 $ ghcprof &lt;prog&gt;.prof
385 </screen>
386
387     <para>which should pop up a window showing the call-graph of your
388     program in glorious detail.  More information on using
389     <command>ghcprof</command> can be found at <ulink
390     url="http://www.dcs.warwick.ac.uk/people/academic/Stephen.Jarvis/profiler/index.html"><citetitle>The
391     Cost-Centre Stack Profiling Tool for
392     GHC</citetitle></ulink>.</para>
393
394   </sect1>
395
396   <sect1 id="prof-compiler-options">
397     <title>Compiler options for profiling</title>
398
399     <indexterm><primary>profiling</primary><secondary>options</secondary></indexterm>
400     <indexterm><primary>options</primary><secondary>for profiling</secondary></indexterm>
401
402     <Para> To make use of the cost centre profiling system
403     <Emphasis>all</Emphasis> modules must be compiled and linked with
404     the <Option>-prof</Option> option. Any
405     <Function>&lowbar;scc&lowbar;</Function> constructs you've put in
406     your source will spring to life.</Para> 
407
408     <indexterm><primary><literal>-prof</literal></primary></indexterm>
409
410     <Para> Without a <Option>-prof</Option> option, your
411     <Function>&lowbar;scc&lowbar;</Function>s are ignored; so you can
412     compiled <Function>&lowbar;scc&lowbar;</Function>-laden code
413     without changing it.</Para>
414     
415     <Para>There are a few other profiling-related compilation options.
416     Use them <Emphasis>in addition to</Emphasis>
417     <Option>-prof</Option>.  These do not have to be used consistently
418     for all modules in a program.</Para>
419
420     <variableList>
421
422       <varListEntry>
423         <term><Option>-auto</Option>:</Term>
424         <indexterm><primary><literal>-auto</literal></primary></indexterm>
425         <indexterm><primary>cost centres</primary><secondary>automatically inserting</secondary></indexterm>
426         <listItem>
427           <Para> GHC will automatically add
428           <Function>&lowbar;scc&lowbar;</Function> constructs for all
429           top-level, exported functions.</Para>
430         </listItem>
431       </varListEntry>
432       
433       <varListEntry>
434         <term><Option>-auto-all</Option>:</Term>
435         <indexterm><primary><literal>-auto-all</literal></primary></indexterm>
436         <listItem>
437           <Para> <Emphasis>All</Emphasis> top-level functions,
438           exported or not, will be automatically
439           <Function>&lowbar;scc&lowbar;</Function>'d.</Para>
440         </listItem>
441       </varListEntry>
442
443       <varListEntry>
444         <term><Option>-caf-all</Option>:</Term>
445         <indexterm><primary><literal>-caf-all</literal></primary></indexterm>
446         <listItem>
447           <Para> The costs of all CAFs in a module are usually
448           attributed to one &ldquo;big&rdquo; CAF cost-centre. With
449           this option, all CAFs get their own cost-centre.  An
450           &ldquo;if all else fails&rdquo; option&hellip;</Para>
451         </listItem>
452       </varListEntry>
453
454       <varListEntry>
455         <term><Option>-ignore-scc</Option>:</Term>
456         <indexterm><primary><literal>-ignore-scc</literal></primary></indexterm>
457         <listItem>
458           <Para>Ignore any <Function>&lowbar;scc&lowbar;</Function>
459           constructs, so a module which already has
460           <Function>&lowbar;scc&lowbar;</Function>s can be compiled
461           for profiling with the annotations ignored.</Para>
462         </listItem>
463       </varListEntry>
464
465     </variableList>
466
467   </sect1>
468
469   <sect1 id="prof-rts-options">
470     <title>Runtime options for profiling</Title>
471
472     <indexterm><primary>profiling RTS options</primary></indexterm>
473     <indexterm><primary>RTS options, for profiling</primary></indexterm>
474
475     <Para>It isn't enough to compile your program for profiling with
476     <Option>-prof</Option>!</Para>
477
478     <Para>When you <Emphasis>run</Emphasis> your profiled program, you
479     must tell the runtime system (RTS) what you want to profile (e.g.,
480     time and/or space), and how you wish the collected data to be
481     reported.  You also may wish to set the sampling interval used in
482     time profiling.</Para>
483
484     <Para>Executive summary: <command>./a.out +RTS -pT</command>
485     produces a time profile in <Filename>a.out.prof</Filename>;
486     <command>./a.out +RTS -hC</command> produces space-profiling info
487     which can be mangled by <command>hp2ps</command> and viewed with
488     <command>ghostview</command> (or equivalent).</Para>
489
490     <Para>Profiling runtime flags are passed to your program between
491     the usual <Option>+RTS</Option> and <Option>-RTS</Option>
492     options.</Para>
493
494     <variableList>
495       
496       <varListEntry>
497         <term><Option>-p</Option> or <Option>-P</Option>:</Term>
498         <indexterm><primary><option>-p</option></primary></indexterm>
499         <indexterm><primary><option>-P</option></primary></indexterm>
500         <indexterm><primary>time profile</primary></indexterm>
501         <listItem>
502           <Para>The <Option>-p</Option> option produces a standard
503           <Emphasis>time profile</Emphasis> report.  It is written
504           into the file
505           <Filename>&lt;program&gt;.prof</Filename>.</Para>
506
507           <Para>The <Option>-P</Option> option produces a more
508           detailed report containing the actual time and allocation
509           data as well.  (Not used much.)</Para>
510         </listitem>
511       </varlistentry>
512
513       <varlistentry>
514         <term><option>-px</option>:</term>
515         <indexterm><primary><option>-px</option></primary></indexterm>
516         <listitem>
517           <para>The <option>-px</option> option generates profiling
518           information in the XML format understood by our new
519           profiling tool, see <xref linkend="prof-xml-tool">.</para>
520         </listitem>
521       </varlistentry>
522
523       <varlistentry>
524         <term><Option>-i&lt;secs&gt;</Option>:</Term>
525         <indexterm><primary><option>-i</option></primary></indexterm>
526         <listItem>
527           <Para> Set the profiling (sampling) interval to
528           <literal>&lt;secs&gt;</literal> seconds (the default is
529           1&nbsp;second).  Fractions are allowed: for example
530           <Option>-i0.2</Option> will get 5 samples per second.  This
531           only affects heap profiling; time profiles are always
532           sampled on a 1/50 second frequency.</Para>
533         </listItem>
534       </varlistentry>
535
536       <varlistentry>
537         <term><Option>-h&lt;break-down&gt;</Option>:</Term>
538         <indexterm><primary><option>-h&lt;break-down&gt</option></primary></indexterm>
539         <indexterm><primary>heap profile</primary></indexterm>
540         <listItem>
541           <Para>Produce a detailed <Emphasis>heap profile</Emphasis>
542           of the heap occupied by live closures. The profile is
543           written to the file <Filename>&lt;program&gt;.hp</Filename>
544           from which a PostScript graph can be produced using
545           <command>hp2ps</command> (see <XRef
546           LinkEnd="hp2ps">).</Para>
547
548           <Para>The heap space profile may be broken down by different
549           criteria:</para>
550
551           <variableList>
552
553             <varListEntry>
554               <term><Option>-hC</Option>:</Term>
555               <listItem>
556                 <Para>cost centre which produced the closure (the
557                 default).</Para>
558               </listItem>
559             </varListEntry>
560
561             <varListEntry>
562               <term><Option>-hM</Option>:</Term>
563               <listItem>
564                 <Para>cost centre module which produced the
565                 closure.</Para>
566               </listItem>
567             </varListEntry>
568
569             <varListEntry>
570               <term><Option>-hD</Option>:</Term>
571               <listItem>
572                 <Para>closure description&mdash;a string describing
573                 the closure.</Para>
574               </listItem>
575             </varListEntry>
576
577             <varListEntry>
578               <term><Option>-hY</Option>:</Term>
579               <listItem>
580                 <Para>closure type&mdash;a string describing the
581                 closure's type.</Para>
582               </listItem>
583             </varListEntry>
584           </variableList>
585
586         </listItem>
587       </varListEntry>
588
589       <varlistentry>
590         <term><option>-hx</option>:</term>
591         <indexterm><primary><option>-hx</option></primary></indexterm>
592         <listitem>
593           <para>The <option>-hx</option> option generates heap
594           profiling information in the XML format understood by our
595           new profiling tool (NOTE: heap profiling with the new tool
596           is not yet working!  Use <command>hp2ps</command>-style heap
597           profiling for the time being).</para>
598         </listitem>
599       </varlistentry>
600
601     </variableList>
602     
603   </sect1>
604
605   <sect1 id="hp2ps">
606     <title><command>hp2ps</command>--heap profile to PostScript</title>
607
608     <indexterm><primary><command>hp2ps</command></primary></indexterm>
609     <indexterm><primary>heap profiles</primary></indexterm>
610     <indexterm><primary>postscript, from heap profiles</primary></indexterm>
611     <indexterm><primary><option>-h&lt;break-down&gt;</option></primary></indexterm>
612     
613     <para>Usage:</para>
614     
615 <screen>
616 hp2ps [flags] [&lt;file&gt;[.hp]]
617 </screen>
618
619     <para>The program
620     <command>hp2ps</command><indexterm><primary>hp2ps
621     program</primary></indexterm> converts a heap profile as produced
622     by the <Option>-h&lt;break-down&gt;</Option> runtime option into a
623     PostScript graph of the heap profile. By convention, the file to
624     be processed by <command>hp2ps</command> has a
625     <filename>.hp</filename> extension. The PostScript output is
626     written to <filename>&lt;file&gt;@.ps</filename>. If
627     <filename>&lt;file&gt;</filename> is omitted entirely, then the
628     program behaves as a filter.</para>
629
630     <para><command>hp2ps</command> is distributed in
631     <filename>ghc/utils/hp2ps</filename> in a GHC source
632     distribution. It was originally developed by Dave Wakeling as part
633     of the HBC/LML heap profiler.</para>
634
635     <para>The flags are:</para>
636
637     <variableList>
638       
639       <varListEntry>
640         <term><Option>-d</Option></Term>
641         <listItem>
642           <para>In order to make graphs more readable,
643           <command>hp2ps</command> sorts the shaded bands for each
644           identifier. The default sort ordering is for the bands with
645           the largest area to be stacked on top of the smaller ones.
646           The <Option>-d</Option> option causes rougher bands (those
647           representing series of values with the largest standard
648           deviations) to be stacked on top of smoother ones.</para>
649         </listItem>
650       </varListEntry>
651
652       <varListEntry>
653         <term><Option>-b</Option></Term>
654         <listItem>
655           <para>Normally, <command>hp2ps</command> puts the title of
656           the graph in a small box at the top of the page. However, if
657           the JOB string is too long to fit in a small box (more than
658           35 characters), then <command>hp2ps</command> will choose to
659           use a big box instead.  The <Option>-b</Option> option
660           forces <command>hp2ps</command> to use a big box.</para>
661         </listItem>
662       </varListEntry>
663
664       <varListEntry>
665         <term><Option>-e&lt;float&gt;[in&verbar;mm&verbar;pt]</Option></Term>
666         <listItem>
667           <para>Generate encapsulated PostScript suitable for
668           inclusion in LaTeX documents.  Usually, the PostScript graph
669           is drawn in landscape mode in an area 9 inches wide by 6
670           inches high, and <command>hp2ps</command> arranges for this
671           area to be approximately centred on a sheet of a4 paper.
672           This format is convenient of studying the graph in detail,
673           but it is unsuitable for inclusion in LaTeX documents.  The
674           <Option>-e</Option> option causes the graph to be drawn in
675           portrait mode, with float specifying the width in inches,
676           millimetres or points (the default).  The resulting
677           PostScript file conforms to the Encapsulated PostScript
678           (EPS) convention, and it can be included in a LaTeX document
679           using Rokicki's dvi-to-PostScript converter
680           <command>dvips</command>.</para>
681         </listItem>
682       </varListEntry>
683
684       <varListEntry>
685         <term><Option>-g</Option></Term>
686         <listItem>
687           <para>Create output suitable for the <command>gs</command>
688           PostScript previewer (or similar). In this case the graph is
689           printed in portrait mode without scaling. The output is
690           unsuitable for a laser printer.</para>
691         </listItem>
692       </varListEntry>
693
694       <varListEntry>
695         <term><Option>-l</Option></Term>
696         <listItem>
697           <para>Normally a profile is limited to 20 bands with
698           additional identifiers being grouped into an
699           <literal>OTHER</literal> band. The <Option>-l</Option> flag
700           removes this 20 band and limit, producing as many bands as
701           necessary. No key is produced as it won't fit!. It is useful
702           for creation time profiles with many bands.</para>
703         </listItem>
704       </varListEntry>
705
706       <varListEntry>
707         <term><Option>-m&lt;int&gt;</Option></Term>
708         <listItem>
709           <para>Normally a profile is limited to 20 bands with
710           additional identifiers being grouped into an
711           <literal>OTHER</literal> band. The <Option>-m</Option> flag
712           specifies an alternative band limit (the maximum is
713           20).</para>
714
715           <para><Option>-m0</Option> requests the band limit to be
716           removed. As many bands as necessary are produced. However no
717           key is produced as it won't fit! It is useful for displaying
718           creation time profiles with many bands.</para>
719         </listItem>
720       </varListEntry>
721
722       <varListEntry>
723         <term><Option>-p</Option></Term>
724         <listItem>
725           <para>Use previous parameters. By default, the PostScript
726           graph is automatically scaled both horizontally and
727           vertically so that it fills the page.  However, when
728           preparing a series of graphs for use in a presentation, it
729           is often useful to draw a new graph using the same scale,
730           shading and ordering as a previous one. The
731           <Option>-p</Option> flag causes the graph to be drawn using
732           the parameters determined by a previous run of
733           <command>hp2ps</command> on <filename>file</filename>. These
734           are extracted from <filename>file@.aux</filename>.</para>
735         </listItem>
736       </varListEntry>
737
738       <varListEntry>
739         <term><Option>-s</Option></Term>
740         <listItem>
741           <para>Use a small box for the title.</para>
742         </listItem>
743       </varListEntry>
744       
745       <varListEntry>
746         <term><Option>-t&lt;float&gt;</Option></Term>
747         <listItem>
748           <para>Normally trace elements which sum to a total of less
749           than 1&percnt; of the profile are removed from the
750           profile. The <option>-t</option> option allows this
751           percentage to be modified (maximum 5&percnt;).</para>
752
753           <para><Option>-t0</Option> requests no trace elements to be
754           removed from the profile, ensuring that all the data will be
755           displayed.</para>
756         </listItem>
757       </varListEntry>
758
759       <varListEntry>
760         <term><Option>-c</Option></Term>
761         <listItem>
762           <para>Generate colour output.</para>
763         </listItem>
764       </varListEntry>
765       
766       <varListEntry>
767         <term><Option>-y</Option></Term>
768         <listItem>
769           <para>Ignore marks.</para>
770         </listItem>
771       </varListEntry>
772       
773       <varListEntry>
774         <term><Option>-?</Option></Term>
775         <listItem>
776           <para>Print out usage information.</para>
777         </listItem>
778       </varListEntry>
779     </variableList>
780   </sect1>
781
782   <sect1 id="ticky-ticky">
783     <title>Using &ldquo;ticky-ticky&rdquo; profiling (for implementors)</Title>
784     <indexterm><primary>ticky-ticky profiling</primary></indexterm>
785
786     <para>(ToDo: document properly.)</para>
787
788     <para>It is possible to compile Glasgow Haskell programs so that
789     they will count lots and lots of interesting things, e.g., number
790     of updates, number of data constructors entered, etc., etc.  We
791     call this &ldquo;ticky-ticky&rdquo;
792     profiling,<indexterm><primary>ticky-ticky
793     profiling</primary></indexterm> <indexterm><primary>profiling,
794     ticky-ticky</primary></indexterm> because that's the sound a Sun4
795     makes when it is running up all those counters
796     (<Emphasis>slowly</Emphasis>).</para>
797
798     <para>Ticky-ticky profiling is mainly intended for implementors;
799     it is quite separate from the main &ldquo;cost-centre&rdquo;
800     profiling system, intended for all users everywhere.</para>
801
802     <para>To be able to use ticky-ticky profiling, you will need to
803     have built appropriate libraries and things when you made the
804     system.  See &ldquo;Customising what libraries to build,&rdquo; in
805     the installation guide.</para>
806
807     <para>To get your compiled program to spit out the ticky-ticky
808     numbers, use a <Option>-r</Option> RTS
809     option<indexterm><primary>-r RTS option</primary></indexterm>.
810     See <XRef LinkEnd="runtime-control">.</para>
811
812     <para>Compiling your program with the <Option>-ticky</Option>
813     switch yields an executable that performs these counts.  Here is a
814     sample ticky-ticky statistics file, generated by the invocation
815     <command>foo +RTS -rfoo.ticky</command>.</para>
816
817 <screen>
818  foo +RTS -rfoo.ticky
819
820
821 ALLOCATIONS: 3964631 (11330900 words total: 3999476 admin, 6098829 goods, 1232595 slop)
822                                 total words:        2     3     4     5    6+
823   69647 (  1.8%) function values                 50.0  50.0   0.0   0.0   0.0
824 2382937 ( 60.1%) thunks                           0.0  83.9  16.1   0.0   0.0
825 1477218 ( 37.3%) data values                     66.8  33.2   0.0   0.0   0.0
826       0 (  0.0%) big tuples
827       2 (  0.0%) black holes                      0.0 100.0   0.0   0.0   0.0
828       0 (  0.0%) prim things
829   34825 (  0.9%) partial applications             0.0   0.0   0.0 100.0   0.0
830       2 (  0.0%) thread state objects             0.0   0.0   0.0   0.0 100.0
831
832 Total storage-manager allocations: 3647137 (11882004 words)
833         [551104 words lost to speculative heap-checks]
834
835 STACK USAGE:
836
837 ENTERS: 9400092  of which 2005772 (21.3%) direct to the entry code
838                   [the rest indirected via Node's info ptr]
839 1860318 ( 19.8%) thunks
840 3733184 ( 39.7%) data values
841 3149544 ( 33.5%) function values
842                   [of which 1999880 (63.5%) bypassed arg-satisfaction chk]
843  348140 (  3.7%) partial applications
844  308906 (  3.3%) normal indirections
845       0 (  0.0%) permanent indirections
846
847 RETURNS: 5870443
848 2137257 ( 36.4%) from entering a new constructor
849                   [the rest from entering an existing constructor]
850 2349219 ( 40.0%) vectored [the rest unvectored]
851
852 RET_NEW:         2137257:  32.5% 46.2% 21.3%  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
853 RET_OLD:         3733184:   2.8% 67.9% 29.3%  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
854 RET_UNBOXED_TUP:       2:   0.0%  0.0%100.0%  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
855
856 RET_VEC_RETURN : 2349219:   0.0%  0.0%100.0%  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
857
858 UPDATE FRAMES: 2241725 (0 omitted from thunks)
859 SEQ FRAMES:    1
860 CATCH FRAMES:  1
861 UPDATES: 2241725
862       0 (  0.0%) data values
863   34827 (  1.6%) partial applications
864                   [2 in place, 34825 allocated new space]
865 2206898 ( 98.4%) updates to existing heap objects (46 by squeezing)
866 UPD_CON_IN_NEW:         0:       0      0      0      0      0      0      0      0      0
867 UPD_PAP_IN_NEW:     34825:       0      0      0  34825      0      0      0      0      0
868
869 NEW GEN UPDATES: 2274700 ( 99.9%)
870
871 OLD GEN UPDATES: 1852 (  0.1%)
872
873 Total bytes copied during GC: 190096
874
875 **************************************************
876 3647137 ALLOC_HEAP_ctr
877 11882004 ALLOC_HEAP_tot
878   69647 ALLOC_FUN_ctr
879   69647 ALLOC_FUN_adm
880   69644 ALLOC_FUN_gds
881   34819 ALLOC_FUN_slp
882   34831 ALLOC_FUN_hst_0
883   34816 ALLOC_FUN_hst_1
884       0 ALLOC_FUN_hst_2
885       0 ALLOC_FUN_hst_3
886       0 ALLOC_FUN_hst_4
887 2382937 ALLOC_UP_THK_ctr
888       0 ALLOC_SE_THK_ctr
889  308906 ENT_IND_ctr
890       0 E!NT_PERM_IND_ctr requires +RTS -Z
891 [... lots more info omitted ...]
892       0 GC_SEL_ABANDONED_ctr
893       0 GC_SEL_MINOR_ctr
894       0 GC_SEL_MAJOR_ctr
895       0 GC_FAILED_PROMOTION_ctr
896   47524 GC_WORDS_COPIED_ctr
897 </screen>
898
899     <para>The formatting of the information above the row of asterisks
900     is subject to change, but hopefully provides a useful
901     human-readable summary.  Below the asterisks <Emphasis>all
902     counters</Emphasis> maintained by the ticky-ticky system are
903     dumped, in a format intended to be machine-readable: zero or more
904     spaces, an integer, a space, the counter name, and a newline.</para>
905
906     <para>In fact, not <Emphasis>all</Emphasis> counters are
907     necessarily dumped; compile- or run-time flags can render certain
908     counters invalid.  In this case, either the counter will simply
909     not appear, or it will appear with a modified counter name,
910     possibly along with an explanation for the omission (notice
911     <literal>ENT&lowbar;PERM&lowbar;IND&lowbar;ctr</literal> appears
912     with an inserted <literal>!</literal> above).  Software analysing
913     this output should always check that it has the counters it
914     expects.  Also, beware: some of the counters can have
915     <Emphasis>large</Emphasis> values!</para>
916
917   </sect1>
918
919 </chapter>
920
921 <!-- Emacs stuff:
922      ;;; Local Variables: ***
923      ;;; mode: sgml ***
924      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***
925      ;;; End: ***
926  -->