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 build that a distribution may leave out, and this image is one that leaves them out, 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

Why no package fixes it

The obvious next step is to install the profiling libraries, and this Alpine release has no package for them. A search for ghc returns three packages of the compiler and one unrelated name, and none of them is a profiling package: ghc-dev is the development files.

apk search ghc

ghc-9.10.3-r2
ghc-dev-9.10.3-r2
ghc-doc-9.10.3-r2
uusi-0.4.3-r1

Nor does any package name end in -prof: a search over every name that mentions prof, kept to the lines that name GHC, prints nothing. The status that follows the pipeline is the exit code of grep, and it is 1 when grep found no line:

apk search -v "*prof*" | grep -i ghc; echo "status $?"

status 1

The libraries are absent from the filesystem itself, not only from the package names: not one profiling interface file is there. The first command prints no path, and its status belongs to head, which reports success even on empty input, so the count on the second line is the result to read:

find / -name "*.p_hi" 2>/dev/null | head -1; echo "status $?"
find / -name "*.p_hi" 2>/dev/null | wc -l

status 0
0

So ghc -prof cannot work in this image, and no apk add changes that. What the image can measure is the library folder of the compiler it has, which already takes 883 MB without a single profiling build in it:

du -sh $(ghc --print-libdir)

883M    /usr/lib/ghc-9.10.3/lib

The profiling libraries themselves get no number here, because this image does not hold them and a figure for them would be a guess. Two routes lead to a compiler that can profile. Neither was run here, so this page shows no output for either of them.

With such a compiler at hand 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 out whether the GHC package of a distribution you know ships the profiling libraries, and name the package that carries them. Nothing to install in this container.
  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

    A profiled program links against profiled libraries, a separate installation that this image does not carry.

  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 is a profile impossible in this container?

    Answer

    This Alpine release ships no profiling libraries for GHC

    apk search ghc lists ghc, ghc-dev and ghc-doc only, none of them has a .p_hi file, and no package adds one.

  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 outputs on this page come from GHC 9.10.3 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev