To generate a heap profile from your program:
- Compile the program for profiling.
- Run it with one of the heap profiling options (eg.
-h
for a basic producer profile). This generates the fileprog.hp
. - Run
hp2ps
to produce a Postscript file,prog.ps
.
To compile a program for profiling add -prof
to the ghc-options
section of
the project's cabal file. You'll get a warning:
Warning: 'ghc-options: -prof' is not necessary and will lead to problems when
used on a library. Use the configure flag --enable-library-profiling and/or
--enable-executable-profiling.
that you can ignore. I didn't investigate this warning deeper.
If cabal complains because it can't find the profiling version of some
libraries you need to reinstall them with the profile option. What I did was to
remove the current sandbox cabal sandbox delete
, create a new one cabal sandbox init
, and install all the dependencies for profiling cabal install -p --only-dependencies
If you get the error
Dynamic linking required, but this is a non-standard build (eg. prof).
You need to build the program twice: once the dynamic way, and then
in the desired way using -osuf to set the object file suffix.
it's because you're using TemplateHaskell extension and splice expressions are
trying to load and use object code compiled for profiling that is not
compatible with the profiling version of the runtime. To solve the issue
compile the executable without the option -prof
and then compile it again
with both the options -prof
and -osuf p_o
to name the object files
differently. See more about this issue at Using Template Haskell with
Profiling.
To run the executable with the heap profiling options:
cabal run -- +RTS -T -N -hc -RTS
You'll find that cost-centre stack names will have a default length of 25
characters. I wanted more informations about these stacks so I run the
executable again with the option -Lnum
that sets the maximum length of a
cost-centre stack name in a heap profile.
cabal run -- +RTS -T -N -hc -L10000 -RTS
With such long cost-centre stack names you'll end up with an incomprehensible chart, so what I did was to open the heap profile file with vim and run a regexp to trim all the cost-centre stack names to a lower length with the command
:%s/\(.\{1,35}\).*\t/\1\t/gc
and by saving the file with a new name with :w prog.trunc.hp
.
The only option that I used was -c
to generate a color output.
hp2ps -c prog.trunc.hp