Profiling and why it is not here

What profiling needs

A profile attributes time and allocation to the functions of the program, and to do that GHC compiles a second version of every library with cost centres in it. The flag is -prof, and it needs:

A normal GHC installation ships the normal libraries. The profiling ones are a separate package, and this container has only the first kind, which is why the next command fails before it reads the program.

The real error

module Main (main) where double :: Int -> Int double x = x + x main :: IO () main = print (double 21)

ghc -prof -fno-code tiny.hs

[1 of 1] Compiling Main ( tiny.hs, nothing )
tiny.hs:1:8: error: [GHC-88719]
    Could not load module ‘Prelude’.
    Perhaps you haven't installed the profiling libraries for package ‘base-4.20.2.0’?
    Use -v to see a list of the files searched for.
  |
1 | module Main (main) where
  |        ^^^^

The error names the cause exactly: the compiler wants the profiling build of base and cannot find the module. It is not a problem of the program, and no flag or pragma fixes it; the libraries have to be installed on the machine. The same program builds without -prof:

ghc -Wall -O1 -o tiny tiny.hs
./tiny

[1 of 2] Compiling Main ( tiny.hs, tiny.o )
[2 of 2] Linking tiny
42

The package that would fix it

On Alpine, the profiling libraries of the boot packages come in packages whose names end in -prof, and the RTS is in ghc itself:

apk add ghc-prof # on Debian and Ubuntu: apt install ghc-prof # the GHC user guide: -prof, -fprof-auto, -fprof-cafs

The size is the reason they are not in every image: the profiling build of base alone is tens of megabytes, and every library of a project adds its own. A machine that profiles installs them once and pays the disk, and a build server that only builds and tests does not.

With the libraries in place the workflow is three commands: build with -prof -fprof-auto, run with +RTS -p, and read the .prof file that names the functions with the individual and inherited cost. The -hc and -hy variants produce a heap profile by cost centre or by type, which is the tool for a space leak.

What to use in this container

Two tools that need no extra packages answer most of the questions a profile would, and both were used earlier in the course:

A profile adds the per-function attribution, which is what you need when the total is too slow and you do not know where. Until then, the two tools above and a careful reading of the Core are the cheaper path, and this course proves everything with them. The next lesson goes back to the interactive debugger, which is installed and needs nothing.

Exercises

  1. Run ghc -prof on a program of your own and copy the first error line.
  2. Explain why a profiled program cannot use a normal library.
  3. Read the +RTS -s report of the program above and say which line a leak would inflate.
  4. Compare the -ddump-timings of -O0 and -O1 builds.
  5. Find the names of the profiling packages for your distribution.
  6. Explain what -fprof-auto adds and why it slows the program.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does -prof need besides the flag?

    Answer

    Profiling builds of the libraries, including base and the RTS

    The profiled program links against profiled libraries, which are a separate installation.

  2. What is the error in this container when -prof is used?

    Answer

    GHC cannot load the Prelude because the profiling libraries are missing

    GHC-88719 says it could not load the module and suggests the profiling libraries of base.

  3. Which RTS flag writes a profile file?

    Answer

    +RTS -p

    -p writes the .prof report; -s is the summary that needs no profiling build.

  4. What does this program print? module Main (main) where main :: IO () main = print (21 * 2)

    Answer

    42

    The product is 42 and print shows it without quotes.

  5. Which tool in this container gives allocation and residency?

    Answer

    +RTS -s

    The RTS summary is always available and needs no profiling libraries.

  6. What does this program print? module Main (main) where main :: IO () main = print (length [1 .. 5 :: Int])

    Answer

    5

    The range has five elements.

  7. Why do distribution images leave the profiling libraries out?

    Answer

    They are large and most builds do not use them

    Tens of megabytes per package for a tool used only when profiling.

  8. What does this program print? module Main (main) where main :: IO () main = print (sum [1 .. 10 :: Int])

    Answer

    55

    The sum of 1 to 10 is 55.

  9. What does -fprof-auto add?

    Answer

    A cost centre to every binding, so the report names your functions

    Without it only the cost centres you annotate appear, and the report is mostly library functions.

  10. What is the cheapest way to find which function allocates?

    Answer

    Compare +RTS -s reports and read the Core of the suspects

    The RTS summary plus the Core answers many questions before a profile is worth installing.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image