Profiling with landmarks

I am trying to profile a project with landmarks but I cannot get it to work. According to the documentation it seems there are two simple ways to do this with dune. One is to add the lines

(libraries landmarks)
(preprocess (pps landmarks-ppx --auto)

to the dune file. When I do this, I get a “not found” error, even though the landmarks-ppx library is installed:

$ dune build
File "dune", line 4, characters 19-32:
4 |   (preprocess (pps landmarks.ppx --auto)))
Error: Library "landmarks.ppx" not found.
-> required by _build/default/test.exe
-> required by alias all
-> required by alias default

$ opam install landmarks-ppx
[NOTE] Package landmarks-ppx is already installed (current version is 1.4).

The other way is to add the line

(instrumentation (backend landmarks))

and then compile the file with dune build --instrument-with landmarks. When I do this, it compiles successfully, but I cannot figure out how to generate the profiled output. I have tried

$ OCAML_LANDMARKS=output=profile.txt dune exec ./test.exe

and various variations, but no profile output file ever appears anywhere that I can find. What am I doing wrong?

your example code mentions landmarks.ppx but the documentation you quote mentions landmarks-ppx. Does using the latter work?

Hello !

  1. It seems that indeed you need to replace “landmarks.ppx” by “landmarks-ppx”.
  2. The problem with the alternative approach is that you need to add “–instrument-with” to the “dune exec” command. If you do not do that, dune will rebuild without the instrumentation:
OCAML_LANDMARKS=on dune exec --instrument-with landmarks ./main.exe

Also, if you do not use the “–auto” argument:

(instrumentation (backend landmarks --auto))

and if you do not have “[@landmark]”
and if you are not using the compilation flag “-linkall”
then the compiler will not link the landmarks library (because it will appear unused) which is in charge of reading the OCAML_LANDMARKS environment variable and dumping the profiling at exit.

Good luck !

1 Like

Thank you both! Those do both indeed work.

For the first approach, I copied my example code from Profiling OCaml programs the quick and dirty way | Tianyi Song and didn’t notice it was different from the documentation. I guess it has a typo!

For the second approach, I wonder whether the need to pass those two extra flags could be documented better somewhere? There’s nothing about either of them at Instrumentation — Dune documentation.