17

I wish to profile my program written in Haskell.

On compilation, I am told that I do not have profiling libraries for certain dependencies (e.g., criterion) installed and cabal aborts.

I have no interest in profiling parts of those dependencies; code called from main doesn't even use them.

How can I profile my application without installing profiling libraries I don't need and without removing all those dependencies?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
nh2
  • 24,526
  • 11
  • 79
  • 128
  • You can't. Just install them, what's the problem? – Daniel Wagner Aug 31 '12 at 01:24
  • 8
    1) I don't need them, 2) Installing them is incredibly tedious: http://stackoverflow.com/questions/1704421/cabal-not-installing-dependencies-when-needing-profiling-libraries – nh2 Aug 31 '12 at 02:29
  • 3
    While I agree that it's tedious, your claim that you don't need them is clearly false. – Daniel Wagner Aug 31 '12 at 02:36
  • I will accept this as an answer if you post it. It doesn't solve my problem, but the anwswer seems to be "not yet possible". – nh2 Aug 31 '12 at 02:56
  • Why not use use -prof rather than criterion? [Chapter 5](http://lambda.haskell.org/platform/doc/current/ghc-doc/users_guide/index.html) of the Platform docs. Wait, what Haskell compiler are you using? Haskell Platform? What version? – ja. Aug 31 '12 at 03:37

3 Answers3

13

A good way to circumvent having to compile everything with profiling is to use cabal sandbox. It allows you to set up a sandbox for one application only, and thereby you won't have to re-install your entire ~/.cabal prefix. You'll need a recent version of Cabal, so run cabal update && cabal install cabal-install first.

Once you initialise a sandbox, create a file cabal.config to include the necessary directives (in your case library-profiling: True; executable-profiling: True may also be handy.)

A side-effect of this is that you can test your code with dependencies that need not be installed globally, for example, experimental versions, or outdated versions.

EDIT: btw, I don't think you need to have profiling enabled for criterion to work. In any case, it works for me without profiling being enabled. Just write a Main module that contains main = defaultMain benchmarks where benchmarks has type [Benchmark], i.e. a list of benchmarks that you've written.

You then compile that file (say, we call it benchmarks.hs with ghc --make -o bench benchmarks.hs, and run the program, ./bench with the appropriate arguments (consult the criterion documentation for details. A good default argument is, say ./bench -o benchmarks.html which will generate a nifty report similar to this one)

Edward Z. Yang
  • 26,325
  • 16
  • 80
  • 110
Aleksandar Dimitrov
  • 9,275
  • 3
  • 41
  • 48
  • I guess naming `criterion` was implying the wrong thing: it was just an example of a dependency I have which I don't want to profile. I should rather have said something like *"My application uses `hunit` for testing, but I do not wish to profile the testing code itself and there fore would like to avoid installing that with profiling enabled"*. – nh2 Aug 31 '12 at 16:11
  • Criterion is only good if you want to compare two functions *time-wise*. It doesn't tell you much about *why* your code is slow/fast. In order to find memory leaks or lazy thunks, you will still need heap profiling, which needs the `-prof` switch. – Aleksandar Dimitrov Aug 31 '12 at 21:07
  • For anybody who reads this now: `cabal-dev` has largely been superseeded by *sandboxes* in Cabal itself. – nh2 Feb 07 '14 at 18:30
  • Good call! [Here's the documentation](http://www.haskell.org/cabal/users-guide/installing-packages.html#developing-with-sandboxes) on sandboxes within the new Cabal. Make sure you're using at least version 1.18 – Aleksandar Dimitrov Feb 12 '14 at 08:20
8

I had the same problem this week, and although I had recompiled everything by hand, I was instructed in the IRC channel to do the following:

  1. Go to your cabal config file (in case you don't know where)
  2. Edit the line for enable library profiling (and while you are at it, enable documentation)
  3. Run Cabal Install World
MdxBhmt
  • 1,310
  • 8
  • 16
6

As mentioned in the question you refer to in your comment, a good way to solve this problem in the future is to enable profiling in the cabal configuration. This way all libraries are installed with profiling support. This might not be a satisfying solution but I guess many are opting for it.

If you are only interested in getting an impression of the memory usage of your program you can generate a heap profile of your program using -hT. More precisely, you have to compile the program with -rtsopts to enable RTS options then execute it using +RTS -hT. The compiler generates a file with the extension hp. You can convert the hp file into a postscript file with a heap profile using hp2ps. This should work without any profiling support, but note that I am too lazy to verify it as I have installed all libraries with profiling support ; )

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Jan Christiansen
  • 3,153
  • 1
  • 19
  • 16
  • Wow, you are right! Heap profiles don't need `-prof`, so my above problem does not arise if I only need space profiling. Nice. – nh2 Aug 31 '12 at 16:07
  • 3
    Please note that this is only the case for `-hT`, that is type-based heap profiling. There are other profiling types like retainer profiling that still need `-prof`. – Jan Christiansen Aug 31 '12 at 16:16