[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / docs / users_guide / profiling.vsgml
1 <sect>Profiling
2 <label id="profiling">
3 <p>
4 <nidx>profiling, with cost-centres</nidx>
5 <nidx>cost-centre profiling</nidx>
6
7 Glasgow Haskell comes with a time and space profiling system. Its
8 purpose is to help you improve your understanding of your program's
9 execution behaviour, so you can improve it.
10
11 Any comments, suggestions and/or improvements you have are welcome.
12 Recommended ``profiling tricks'' would be especially cool!
13
14 <sect1>How to profile a Haskell program
15 <label id="profiling-intro">
16 <p>
17
18 The GHC approach to profiling is very simple: annotate the expressions
19 you consider ``interesting'' with <em>cost centre</em> labels (strings);
20 so, for example, you might have:
21
22 <tscreen><verb>
23 f x y
24   = let
25         output1 = _scc_ "Pass1" ( pass1 x )
26         output2 = _scc_ "Pass2" ( pass2 output1 y )
27         output3 = _scc_ "Pass3" ( pass3 (output2 `zip` [1 .. ]) )
28     in concat output3
29 </verb></tscreen>
30
31 The costs of the evaluating the expressions bound to @output1@,
32 @output2@ and @output3@ will be attributed to the ``cost
33 centres'' @Pass1@, @Pass2@ and @Pass3@, respectively.
34
35 The costs of evaluating other expressions, e.g., @concat output4@,
36 will be inherited by the scope which referenced the function @f@.
37
38 You can put in cost-centres via @_scc_@ constructs by hand, as in the
39 example above.  Perfectly cool.  That's probably what you
40 <em>would</em> do if your program divided into obvious ``passes'' or
41 ``phases'', or whatever.
42
43 If your program is large or you have no clue what might be gobbling
44 all the time, you can get GHC to mark all functions with @_scc_@
45 constructs, automagically.  Add an @-auto@ compilation flag to the
46 usual @-prof@ option.
47
48 Once you start homing in on the Guilty Suspects, you may well switch
49 from automagically-inserted cost-centres to a few well-chosen ones of
50 your own.
51
52 To use profiling, you must <em>compile</em> and <em>run</em> with special
53 options.  (We usually forget the ``run'' magic!---Do as we say, not as
54 we do...) Details follow.
55
56 If you're serious about this profiling game, you should probably read
57 one or more of the Sansom/Peyton Jones papers about the GHC profiling
58 system.  Just visit the <url name="Glasgow FP group web page"
59 url="http://www.dcs.gla.ac.uk/fp/">...
60
61 <sect1>Compiling programs for profiling
62 <label id="prof-compiler-options">
63 <p>
64 <nidx>profiling options</nidx>
65 <nidx>options, for profiling</nidx>
66
67 To make use of the cost centre profiling system <em>all</em> modules must
68 be compiled and linked with the @-prof@ option.<nidx>-prof option</nidx>
69 Any @_scc_@ constructs you've put in your source will spring to life.
70
71 Without a @-prof@ option, your @_scc_@s are ignored; so you can
72 compiled @_scc_@-laden code without changing it.
73
74 There are a few other profiling-related compilation options.  Use them
75 <em>in addition to</em> @-prof@.  These do not have to be used
76 consistently for all modules in a program.
77
78 <descrip>
79 <tag>@-auto@:</tag>
80 <nidx>-auto option</nidx>
81 <nidx>cost centres, automatically inserting</nidx>
82 GHC will automatically add @_scc_@ constructs for
83 all top-level, exported functions.
84
85 <tag>@-auto-all@:</tag>
86 <nidx>-auto-all option</nidx>
87 <em>All</em> top-level functions, exported or not, will be automatically
88 @_scc_@'d.
89
90 <tag>@-caf-all@:</tag>
91 <nidx>-caf-all option</nidx>
92 The costs of all CAFs in a module are usually attributed to one
93 ``big'' CAF cost-centre. With this option, all CAFs get their own cost-centre.
94 An ``if all else fails'' option...
95
96 %<tag>@-dict-all@:</tag>
97 %<nidx>-dict-all option</nidx>
98 %Similarly, this option means that all ``dictionaries'' (internal
99 %constructs to support Haskell overloading) should get their own
100 %cost-centre.  (Again, the costs are usually attributed to one ``big''
101 %DICT cost-centre.)
102 %
103 %Incidentally, something is probably Bad Wrong (i.e., a GHC bug) if you
104 %see big costs attributed to dictionaries.
105
106 <tag>@-ignore-scc@:</tag>
107 <nidx>-ignore-scc option</nidx>
108 Ignore any @_scc_@ constructs,
109 so a module which already has @_scc_@s can be
110 compiled for profiling with the annotations ignored.
111
112 <tag>@-G<group>@:</tag>
113 <nidx>-G&lt;group&gt; option</nidx>
114 Specifies the @<group>@ to be attached to all the cost-centres
115 declared in the module. If no group is specified it defaults to the
116 module name.
117 </descrip>
118
119 In addition to the @-prof@ option your system might be setup to enable
120 you to compile and link with the @-prof-details@ <nidx>-prof-details
121 option</nidx> option instead. This enables additional detailed counts
122 to be reported with the @-P@ RTS option.  
123
124 %-prof-details should also enable age profiling if we get it going again ...
125
126 <sect1>How to control your profiled program at runtime
127 <label id="prof-rts-options">
128 <p>
129 <nidx>profiling RTS options</nidx>
130 <nidx>RTS options, for profiling</nidx>
131
132 It isn't enough to compile your program for profiling with @-prof@!
133
134 When you <em>run</em> your profiled program, you must tell the runtime
135 system (RTS) what you want to profile (e.g., time and/or space), and
136 how you wish the collected data to be reported.  You also may wish to
137 set the sampling interval used in time profiling.
138
139 Executive summary: @./a.out +RTS -pT@ produces a time profile in
140 @a.out.prof@; @./a.out +RTS -hC@ produces space-profiling
141 info which can be mangled by @hp2ps@ and viewed with @ghostview@
142 (or equivalent).
143
144 Profiling runtime flags are passed to your program between the usual
145 @+RTS@ and @-RTS@ options.
146
147 <descrip>
148 <tag>@-p<sort>@ or @-P<sort>@:</tag>
149 <nidx>-p&lt;sort&gt; RTS option (profiling)</nidx>
150 <nidx>-P&lt;sort&gt; RTS option (profiling)</nidx>
151 <nidx>time profile</nidx>
152 <nidx>serial time profile</nidx>
153 The @-p?@ option produces a standard <em>time profile</em> report.
154 It is written into the file @<program>@@.prof@.
155
156 The @-P?@ option produces a more detailed report containing the
157 actual time and allocation data as well.  (Not used much.)
158
159 %The @-P?@ option also produces <em>serial time-profiling</em>
160 %information, in the file @<program>@@.time@. This can be
161 %converted into a (somewhat unsatisfactory) PostScript graph using
162 %@hp2ps@ (see Section <ref name="hp2ps - heap profile to PostScript" id="hp2ps">).
163
164 %???? -F2s needed for serial time profile??? ToDo
165
166 The @<sort>@ indicates how the cost centres are to be sorted in the
167 report. Valid @<sort>@ options are:
168 <descrip>
169 <tag>@T@:</tag> by time, largest first (the default);
170 <tag>@A@:</tag> by bytes allocated, largest first;
171 <tag>@C@:</tag> alphabetically by group, module and cost centre.
172 </descrip>
173
174 <tag>@-i<secs>@:</tag> <nidx>-i&lt;secs&gt; RTS option
175 (profiling)</nidx> Set the profiling (sampling) interval to @<secs>@
176 seconds (the default is 1~second).  Fractions are allowed: for example
177 @-i0.2@ will get 5 samples per second.
178
179 <tag>@-h<break-down>@:</tag>
180 <nidx>-h&lt;break-down&gt; RTS option (profiling)</nidx>
181 <nidx>heap profile</nidx>
182
183 Produce a detailed <em>space profile</em> of the heap occupied by live
184 closures. The profile is written to the file @<program>@@.hp@ from
185 which a PostScript graph can be produced using @hp2ps@ (see Section
186 <ref name="hp2ps - heap profile to PostScript" id="hp2ps">).
187
188 The heap space profile may be broken down by different criteria:
189 <descrip>
190 <tag>@-hC@:</tag> cost centre which produced the closure (the default).
191 <tag>@-hM@:</tag> cost centre module which produced the closure.
192 <tag>@-hG@:</tag> cost centre group which produced the closure.
193 <tag>@-hD@:</tag> closure description --- a string describing the closure.
194 <tag>@-hY@:</tag> closure type --- a string describing the closure's type.
195 %<tag>@-hT<ints>,<start>@:</tag> the time interval the closure was
196 %created. @<ints>@ specifies the no. of interval bands plotted
197 %(default 18) and @<start>@ the number of seconds after which the
198 %reported intervals start (default 0.0).
199 </descrip>
200 By default all live closures in the heap are profiled, but particular
201 closures of interest can be selected (see below). 
202 </descrip>
203
204
205 Heap (space) profiling uses hash tables. If these tables
206 should fill the run will abort. The
207 @-z<tbl><size>@<nidx>-z&lt;tbl&gt;&lt;size&gt; RTS option (profiling)</nidx> option is used to
208 increase the size of the relevant hash table (@C@, @M@,
209 @G@, @D@ or @Y@, defined as for @<break-down>@ above). The
210 actual size used is the next largest power of 2.
211
212 The heap profile can be restricted to particular closures of interest.
213 The closures of interest can selected by the attached cost centre
214 (module:label, module and group), closure category (description, type,
215 and kind) using the following options:
216
217 <descrip>
218 <tag>@-c{<mod>:<lab>,<mod>:<lab>...@}:</tag>
219 <nidx>-c{&lt;lab&gt;</nidx> RTS option (profiling)}
220 Selects individual cost centre(s).
221 <tag>@-m{<mod>,<mod>...@}:</tag>
222 <nidx>-m{&lt;mod&gt;</nidx> RTS option (profiling)}
223 Selects all cost centres from the module(s) specified.
224 <tag>@-g{<grp>,<grp>...@}:</tag>
225 <nidx>-g{&lt;grp&gt;</nidx> RTS option (profiling)}
226 Selects all cost centres from the groups(s) specified.
227 <tag>@-d{<des>,<des>...@}:</tag>
228 <nidx>-d{&lt;des&gt;</nidx> RTS option (profiling)}
229 Selects closures which have one of the specified descriptions.
230 <tag>@-y{<typ>,<typ>...@}:</tag>
231 <nidx>-y{&lt;typ&gt;</nidx> RTS option (profiling)}
232 Selects closures which have one of the specified type descriptions.
233 <tag>@-k{<knd>,<knd>...@}:</tag>
234 <nidx>-k{&lt;knd&gt;</nidx> RTS option (profiling)}
235 Selects closures which are of one of the specified closure kinds.
236 Valid closure kinds are @CON@ (constructor), @FN@ (manifest
237 function), @PAP@ (partial application), @BH@ (black hole) and
238 @THK@ (thunk).
239 </descrip>
240
241 The space occupied by a closure will be reported in the heap profile
242 if the closure satisfies the following logical expression:
243
244 <quote>
245 ([-c] or [-m] or [-g]) and ([-d] or [-y] or [-k])
246 </quote>
247
248 where a particular option is true if the closure (or its attached cost
249 centre) is selected by the option (or the option is not specified).
250
251 <sect1>What's in a profiling report?
252 <label id="prof-output">
253 <p>
254 <nidx>profiling report, meaning thereof</nidx>
255
256 When you run your profiled program with the @-p@ RTS option <nidx>-p
257 RTS option</nidx>, you get the following information about your ``cost
258 centres'':
259
260 <descrip>
261 %-------------------------------------------------------------
262 <tag>@COST CENTRE@:</tag> The cost-centre's name.
263 %-------------------------------------------------------------
264 <tag>@MODULE@:</tag>
265 The module associated with the cost-centre;
266 important mostly if you have identically-named cost-centres in
267 different modules.
268 %-------------------------------------------------------------
269 <tag>@scc@:</tag>
270 How many times this cost-centre was entered; think
271 of it as ``I got to the @_scc_@ construct this many times...''
272 %-------------------------------------------------------------
273 <tag>@%time@:</tag>
274 What part of the time was spent in this cost-centre (see also ``ticks,''
275 below).
276 %-------------------------------------------------------------
277 <tag>@%alloc@:</tag>
278 What part of the memory allocation was done in this cost-centre
279 (see also ``bytes,'' below).
280 %-------------------------------------------------------------
281 <tag>@inner@:</tag>
282 How many times this cost-centre ``passed control'' to an inner
283 cost-centre; for example, @scc=4@ plus @subscc=8@ means
284 ``This @_scc_@ was entered four times, but went out to
285 other @_scc_s@ eight times.''
286 %-------------------------------------------------------------
287 <tag>@cafs@:</tag>
288 <nidx>CAF, profiling</nidx>
289 How many CAFs this cost centre evaluated.
290 %-------------------------------------------------------------
291 <tag>@dicts@:</tag>
292 <nidx>Dictionaries, profiling</nidx>
293 How many dictionaries this cost centre evaluated.
294 </descrip>
295
296 In addition you can use the @-P@ RTS option <nidx></nidx> to get the following additional information: 
297 <descrip>
298 %-------------------------------------------------------------
299 <tag>@ticks@:</tag>  The raw number of time ``ticks'' which were
300 attributed to this cost-centre; from this, we get the @%time@
301 figure mentioned above.
302 %-------------------------------------------------------------
303 <tag>@bytes@:</tag> Number of bytes allocated in the heap while in
304 this cost-centre; again, this is the raw number from which we
305 get the @%alloc@ figure mentioned above.
306 </descrip>
307
308 Finally if you built your program with @-prof-details@
309 <nidx></nidx> the @-P@ RTS option will also
310 produce the following information:
311 <descrip>
312 %-------------------------------------------------------------
313 <tag>@closures@:</tag>
314 <nidx>closures, profiling</nidx>
315 How many heap objects were allocated; these objects may be of varying
316 size.  If you divide the number of bytes (mentioned below) by this
317 number of ``closures'', then you will get the average object size.
318 (Not too interesting, but still...)
319 %-------------------------------------------------------------
320 <tag>@thunks@:</tag>
321 <nidx>thunks, profiling</nidx>
322 How many times we entered (evaluated) a thunk---an unevaluated
323 object in the heap---while we were in this cost-centre.
324 %-------------------------------------------------------------
325 <tag>@funcs@:</tag>
326 <nidx>functions, profiling</nidx>
327 How many times we entered (evaluated) a function while we we in this
328 cost-centre.  (In Haskell, functions are first-class values and may be
329 passed as arguments, returned as results, evaluated, and generally
330 manipulated just like data values)
331 %-------------------------------------------------------------
332 <tag>@PAPs@:</tag>
333 <nidx>partial applications, profiling</nidx>
334 How many times we entered (evaluated) a partial application (PAP), i.e.,
335 a function applied to fewer arguments than it needs.  For example, @Int@
336 addition applied to one argument would be a PAP.  A PAP is really
337 just a particular form for a function.
338 </descrip>
339
340 <sect1>Producing graphical heap profiles
341 <label id="prof-graphs">
342 <p>
343 <nidx>heap profiles, producing</nidx>
344
345 Utility programs which produce graphical profiles.
346
347 <sect2>@hp2ps@--heap profile to PostScript
348 <label id="hp2ps">
349 <p>
350 <nidx>hp2ps (utility)</nidx>
351 <nidx>heap profiles</nidx>
352 <nidx>PostScript, from heap profiles</nidx>
353
354 Usage:
355
356 <tscreen> <verb>
357 hp2ps [flags] [<file>[.stat]]
358 </verb> </tscreen>
359
360 The program @hp2ps@<nidx>hp2ps program</nidx> converts a heap profile
361 as produced by the @-h<break-down>@<nidx>-h&lt;break-down&gt; RTS
362 option</nidx> runtime option into a PostScript graph of the heap
363 profile. By convention, the file to be processed by @hp2ps@ has a
364 @.hp@ extension. The PostScript output is written to @<file>@@.ps@. If
365 @<file>@ is omitted entirely, then the program behaves as a filter.
366
367 @hp2ps@ is distributed in @ghc/utils/hp2ps@ in a GHC source
368 distribution. It was originally developed by Dave Wakeling as part of
369 the HBC/LML heap profiler.
370
371 The flags are:
372 <descrip>
373 <tag>@-d@</tag>
374 In order to make graphs more readable, @hp2ps@ sorts the shaded
375 bands for each identifier. The default sort ordering is for the bands
376 with the largest area to be stacked on top of the smaller ones.  The
377 @-d@ option causes rougher bands (those representing series of
378 values with the largest standard deviations) to be stacked on top of
379 smoother ones.
380
381 <tag>@-b@</tag> 
382 Normally, @hp2ps@ puts the title of the graph in a small box at the
383 top of the page. However, if the JOB string is too long to fit in a
384 small box (more than 35 characters), then
385 @hp2ps@ will choose to use a big box instead.  The @-b@
386 option forces @hp2ps@ to use a big box.
387
388 <tag>@-e<float>[in|mm|pt]@</tag>
389 Generate encapsulated PostScript suitable for inclusion in LaTeX
390 documents.  Usually, the PostScript graph is drawn in landscape mode
391 in an area 9 inches wide by 6 inches high, and @hp2ps@ arranges
392 for this area to be approximately centred on a sheet of a4 paper.
393 This format is convenient of studying the graph in detail, but it is
394 unsuitable for inclusion in LaTeX documents.  The @-e@ option
395 causes the graph to be drawn in portrait mode, with float specifying
396 the width in inches, millimetres or points (the default).  The
397 resulting PostScript file conforms to the Encapsulated PostScript
398 (EPS) convention, and it can be included in a LaTeX document using
399 Rokicki's dvi-to-PostScript converter @dvips@.
400
401 <tag>@-g@</tag>
402 Create output suitable for the @gs@ PostScript previewer (or
403 similar). In this case the graph is printed in portrait mode without
404 scaling. The output is unsuitable for a laser printer.
405
406 <tag>@-l@</tag>
407 Normally a profile is limited to 20 bands with additional identifiers
408 being grouped into an @OTHER@ band. The @-l@ flag removes this
409 20 band and limit, producing as many bands as necessary. No key is
410 produced as it won't fit!. It is useful for creation time profiles
411 with many bands.
412
413 <tag>@-m<int>@</tag>
414 Normally a profile is limited to 20 bands with additional identifiers
415 being grouped into an @OTHER@ band. The @-m@ flag specifies an
416 alternative band limit (the maximum is 20).
417
418 @-m0@ requests the band limit to be removed. As many bands as
419 necessary are produced. However no key is produced as it won't fit! It
420 is useful for displaying creation time profiles with many bands.
421
422 <tag>@-p@</tag>
423 Use previous parameters. By default, the PostScript graph is
424 automatically scaled both horizontally and vertically so that it fills
425 the page.  However, when preparing a series of graphs for use in a
426 presentation, it is often useful to draw a new graph using the same
427 scale, shading and ordering as a previous one. The @-p@ flag causes
428 the graph to be drawn using the parameters determined by a previous
429 run of @hp2ps@ on @file@. These are extracted from
430 @file@@.aux@.
431
432 <tag>@-s@</tag> Use a small box for the title.
433
434 <tag>@-t<float>@</tag>
435 Normally trace elements which sum to a total of less than 1\% of the
436 profile are removed from the profile. The @-t@ option allows this
437 percentage to be modified (maximum 5\%). 
438
439 @-t0@ requests no trace elements to be removed from the profile,
440 ensuring that all the data will be displayed. 
441
442 <tag>@-?@</tag> Print out usage information.
443 </descrip>
444
445 <sect2>@stat2resid@---residency info from GC stats
446 <label id="stat2resid">
447 <p>
448 <nidx>stat2resid (utility)</nidx>
449 <nidx>GC stats---residency info</nidx>
450 <nidx>residency, from GC stats</nidx>
451
452 Usage:
453
454 <tscreen> <verb>
455 stat2resid [<file>[.stat] [<outfile>]]
456 </verb> </tscreen>
457
458 The program @stat2resid@<nidx>stat2resid</nidx> converts a detailed
459 garbage collection statistics file produced by the 
460 @-S@<nidx>-S RTS option</nidx> runtime option into a PostScript heap
461 residency graph. The garbage collection statistics file can be
462 produced without compiling your program for profiling.
463
464 By convention, the file to be processed by @stat2resid@ has a
465 @.stat@ extension. If the @<outfile>@ is not specified the
466 PostScript will be written to @<file>@@.resid.ps@. If
467 @<file>@ is omitted entirely, then the program behaves as a filter. 
468
469 The plot can not be produced from the statistics file for a
470 generational collector, though a suitable stats file can be produced
471 using the @-F2s@<nidx>-F2s RTS option</nidx> runtime option when the
472 program has been compiled for generational garbage collection (the
473 default).
474
475 @stat2resid@ is distributed in @ghc/utils/stat2resid@ in a GHC source
476 distribution.
477
478 %************************************************************************
479 %*                                                                      *
480 <sect1>Using ``ticky-ticky'' profiling (for implementors)
481 <label id="ticky-ticky">
482 <p>
483 <nidx>ticky-ticky profiling (implementors)</nidx>
484 %*                                                                      *
485 %************************************************************************
486
487 (ToDo: document properly.)
488
489 It is possible to compile Glasgow Haskell programs so that they will
490 count lots and lots of interesting things, e.g., number of updates,
491 number of data constructors entered, etc., etc.  We call this
492 ``ticky-ticky'' profiling,<nidx>ticky-ticky profiling</nidx>%
493 <nidx>profiling, ticky-ticky</nidx> because that's the sound a Sun4 makes
494 when it is running up all those counters (<em>slowly</em>).
495
496 Ticky-ticky profiling is mainly intended for implementors; it is quite
497 separate from the main ``cost-centre'' profiling system, intended for
498 all users everywhere.
499
500 To be able to use ticky-ticky profiling, you will need to have built
501 appropriate libraries and things when you made the system.  See
502 ``Customising what libraries to build,'' in the installation guide.
503
504 To get your compiled program to spit out the ticky-ticky numbers, use
505 a @-r@ RTS option<nidx>-r RTS option</nidx>.