Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General instrumentation framework #3526

Closed
ghost opened this issue Jun 4, 2020 · 6 comments
Closed

General instrumentation framework #3526

ghost opened this issue Jun 4, 2020 · 6 comments
Milestone

Comments

@ghost
Copy link

ghost commented Jun 4, 2020

This RFC generalises the support for bisect_ppx to allow Dune to support other instrumentation tools such as landmarks. With this proposal, Dune will no longer have specific knowledge of bisect_ppx and will instead offer a more general and open instrumentation framework.

The usability of the system will also be improved. Indeed, it will become possible to enable instrumentation via the command line directly.

Declaring an instrumentation backend

Instrumentation backends are OCaml libraries with the special field (instrumentation.backend). This field instructs Dune that the library can be used as an intrumentation backend and also provides the parameters that are specific to this backend.

Initially, Dune will only support ppx instrumentation tools, and the instrumentation library must specify the ppx rewriters that instruments the code. This will is as follow:

(library
 (name ...)
 ...
 (instrumentation.backend
  (ppx <ppx-rewriter-name>)))

For instance:

(library
 (name bisect_ppx)
 (instrumentation.backend
  (ppx bisect_ppx)))

(library
 (name landmarks)
 (instrumentation.backend
  (ppx landmarks.ppx)))

When such an instrumentation backend is activated, Dune will implicitly add the mentioned ppx rewriter to the list of ppx rewriters for libraries and executables that support this instrumentation backend.

Marking code that should be instrumented

When an instrumentation backend is activated, Dune will only instrument libraries and executables for which the user has requested instrumentation.

To request instrumentation, one must add the following field to a library or executable stanza:

(library
 (name ...)
 (instrumentation
  (backend <name>)))

This field can be repeated multiple times in order to support various backends. For instance:

(library
 (name foo)
 (instrumentation
  (backend bisect_ppx))
 (instrumentation
  (backend landmarks)))

This will instruct Dune that when either the bisect_ppx or landmarks instrumentation is activated, the library should be instrumented with this backend.

By default, these fields are simply ignored. However, when the corresponding instrumentation backend is activated, Dune will implicitly add the relevant ppx rewriter to the list of ppx rewriters.

At the moment, it is not possible to instrument code that is preprocessed via an action preprocessors. These preprocessors are quite rare now, so it doesn't seem too bad.

Activating an instrumentation backend

Activating an instrumentation backend can be done via the command line or the dune-workspace file.

Via the command line:

$ dune build --instrument-with <name>

For instance:

$ dune build --instrument-with bisect_ppx
$ dune build --instrument-with landmarks

This will instruct Dune to activate the given backend globally, i.e. in all defined build contexts.

It is also possible to do it via the dune-workspace file, either globally:

(lang dune 2.7)
(instrument_with bisect_ppx)

or for each context individually:

(lang dune 2.7)
(context default)
(context (default (name coverage) (instrument_with bisect_ppx)))
(context (default (name profiling) (instrument_with landmarks)))

If both the global and local fields are present, the precedence is the same as for the profile field:

  • the local field as precedence over the command line flag
  • the command line flag has precedence over the global field
@rgrinberg rgrinberg added this to the 2.6 milestone Jun 4, 2020
@nojb
Copy link
Collaborator

nojb commented Jun 4, 2020

Do I understand well that the instrumentation.backend field in library exists solely to make the association between the names of the library and that of the ppx rewriter? Does the library itself play any role otherwise?

@ghost
Copy link
Author

ghost commented Jun 4, 2020

@nojb that's right. And it is also to get a better error message if we pass a library that is not meant to be used for instrumentation.

FTR, we do something very similar for inline tests.

@nojb
Copy link
Collaborator

nojb commented Jun 6, 2020

When a library or an executable declares that it wants to use and instrumentation with (instrumentation (backend <lib>)), shouldn't we automatically add <lib> to the list of library dependencies? It seems a bit strange to go through the trouble of making the association between the library name and the ppx rewriter explicit in the syntax and then to still require the user to manually add the library to the set of dependencies.

On the other hand, I wonder if it is the case that adding the instrumentation library to the set of dependencies of the buildable being instrumented is always the right thing to do.

@nojb
Copy link
Collaborator

nojb commented Jun 7, 2020

When a library or an executable declares that it wants to use and instrumentation with (instrumentation (backend <lib>)), shouldn't we automatically add <lib> to the list of library dependencies?

Answering to myself: this is already handled by the ppx_runtime_libraries field, but it wasn't being set by landmarks.

@nojb nojb mentioned this issue Jun 7, 2020
5 tasks
@ghost
Copy link
Author

ghost commented Jun 8, 2020

Indeed, I did wondered about this myself when writing the proposal and came to the same conclusion.

@rgrinberg
Copy link
Member

This was done in 2.7

@rgrinberg rgrinberg modified the milestones: 2.6, 2.7 Aug 13, 2020
rgrinberg added a commit to rgrinberg/opam-repository that referenced this issue Aug 14, 2020
…lugin, dune-private-libs and dune-glob (2.7.0)

CHANGES:

- Write intermediate files in a `.mdx` folder for each `mdx` stanza
  to prevent the corresponding actions to be executed as part of the `@all`
  alias (ocaml/dune#3659, @NathanReb)

- Read Coq flags from `env` (ocaml/dune#3547 , fixes ocaml/dune#3486, @gares)

- Add instrumentation framework to toggle instrumentation by `bisect_ppx`,
  `landmarks`, etc, via dune-workspace and/or the command-line. (ocaml/dune#3404, ocaml/dune#3526
  @stephanieyou, @nojb)

- Formatting of dune files is now done in the executing dune process instead of
  in a separate process. (ocaml/dune#3536, @nojb)

- Add a `--debug-artifact-substution` flag to help debug problem with
  version not being captured by `dune-build-info` (ocaml/dune#3589,
  @jeremiedimino)

- Allow the use of the `context_name` variable in the `enabled_if` fields of
  executable(s) and install stanzas. (ocaml/dune#3568, fixes ocaml/dune#3566, @voodoos)

- Fix compatibility with OCaml 4.12.0 when compiling empty archives; no .a file
  is generated. (ocaml/dune#3576, @dra27)

- `$ dune utop` no longer tries to load optional libraries that are unavailable
  (ocaml/dune#3612, fixes ocaml/dune#3188, @anuragsoni)

- Fix dune-build-info on 4.10.0+flambda (ocaml/dune#3599, @emillon, @jeremiedimino).

- Allow multiple libraries with `inline_tests` to be defined in the same
  directory (ocaml/dune#3621, @rgrinberg)

- Run exit hooks in jsoo separate compilation mode (ocaml/dune#3626, fixes ocaml/dune#3622,
  @rgrinberg)

- Add (alias ...), (mode ...) fields to (copy_fields ...) stanza (ocaml/dune#3631, @nojb)

- (copy_files ...) now supports copying files from outside the workspace using
  absolute file names (ocaml/dune#3639, @nojb)

- Dune does not use `ocamlc` as an intermediary to call C compiler anymore.
  Configuration flags `ocamlc_cflags` and `ocamlc_cppflags` are always prepended
  to the compiler arguments. (ocaml/dune#3565, fixes ocaml/dune#3346, @voodoos)

- Revert the build optimization in ocaml/dune#2268. This optimization slows down building
  individual executables when they're part of an `executables` stanza group
  (ocaml/dune#3644, @rgrinberg)

- Use `{dev}` rather than `{pinned}` in the generated `.opam` file. (ocaml/dune#3647,
  @kit-ty-kate)

- Insert correct extension name when editing `dune-project` files. Previously,
  dune would just insert the stanza name. (ocaml/dune#3649, fixes ocaml/dune#3624, @rgrinberg)

- Fix crash when evaluating an `mdx` stanza that depends on unavailable
  packages. (ocaml/dune#3650, @craigfe)

- Fix typo in `cache-check-probablity` field in dune config files. This field
  now requires 2.7 as it wasn't usable before this version. (ocaml/dune#3652, @edwintorok)

- Add `"odoc" {with-doc}` to the dependencies in the generated `.opam` files.
  (ocaml/dune#3667, @kit-ty-kate)

- Do not allow user actions to capture dune's stdin (ocaml/dune#3677, fixes ocaml/dune#3672,
  @rgrinberg)

- `(subdir ...)` stanzas can now appear in dune files used via `(include ...)`.
  (ocaml/dune#3676, @nojb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants