Skip to content

Instantly share code, notes, and snippets.

@potomak
Last active March 20, 2016 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potomak/9161444c65f9d42d8311 to your computer and use it in GitHub Desktop.
Save potomak/9161444c65f9d42d8311 to your computer and use it in GitHub Desktop.
Profiling a Haskell executable memory usage

Profiling a Haskell executable memory usage

To generate a heap profile from your program:

  1. Compile the program for profiling.
  2. Run it with one of the heap profiling options (eg. -h for a basic producer profile). This generates the file prog.hp.
  3. Run hp2ps to produce a Postscript file, prog.ps.

Notes about the compilation for profiling

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.

Notes about running the executable with the profiling options

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.

Notes about producing a Postscript file

The only option that I used was -c to generate a color output.

hp2ps -c prog.trunc.hp

References

  1. https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/prof-heap.html
  2. https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/hp2ps.html
  3. http://lambdor.net/?p=258
  4. https://downloads.haskell.org/~ghc/6.10.2/docs/html/users_guide/template-haskell.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment