Skip to content

Commit b3d6948

Browse files
authored
feature: dune produces action traces (#13302)
When dune is executed as an action (e.g. in our test suite), the trace file that it produces is discarded. Instead of discarding it, it is now copied to the action trace directory to be included in the host's dune trace. This gives us greater visibility on what is happening in the tests. For example, we can now write the following query to determine the number of times we run `ocamlc -config` in our test suite: ``` $ dune trace cat | jq -s '[ .[] | select(.cat == "process" and .name == "finish" and .args.process_args == ["-config"]) ] | length' ``` The answer is 2872 times if anybody is curious. Yes, that is ridiculous. --------- Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 23d1881 commit b3d6948

File tree

8 files changed

+94
-9
lines changed

8 files changed

+94
-9
lines changed

boot/libs.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ let local_libraries =
105105
; special_builtin_support = None
106106
; root_module = None
107107
}
108+
; { path = "otherlibs/dune-action-trace"
109+
; main_module_name = Some "Dune_action_trace"
110+
; include_subdirs = No
111+
; special_builtin_support = None
112+
; root_module = None
113+
}
108114
; { path = "vendor/bigstringaf"
109115
; main_module_name = Some "Bigstringaf"
110116
; include_subdirs = No
@@ -255,12 +261,6 @@ let local_libraries =
255261
; special_builtin_support = None
256262
; root_module = None
257263
}
258-
; { path = "otherlibs/dune-action-trace"
259-
; main_module_name = Some "Dune_action_trace"
260-
; include_subdirs = No
261-
; special_builtin_support = None
262-
; root_module = None
263-
}
264264
; { path = "src/dune_engine"
265265
; main_module_name = Some "Dune_engine"
266266
; include_subdirs = No

doc/changes/added/13302.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Dune dune produces trace events in `DUNE_ACTION_TRACE_DIR` if this variable
2+
is set. (#13302, @rgrinberg)

otherlibs/stdune/src/temp.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ val clear_dir : Path.t -> unit
3232
safely use any file name there. *)
3333
val temp_file : dir:Path.t -> prefix:string -> suffix:string -> Path.t
3434

35+
val temp_dir : parent_dir:Path.t -> prefix:string -> suffix:string -> Path.t
36+
3537
(** Like [temp_file], but passes the temporary file to the callback [f], and
3638
makes sure the temporary file is deleted when [f] completes. If [f] raises
3739
an exception, the exception is re-raised (and the file is still deleted). *)

src/dune_trace/dune

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@
33
(foreign_stubs
44
(language c)
55
(names dune_trace_stubs))
6-
(libraries stdune csexp bigstringaf threads.posix spawn unix))
6+
(libraries
7+
stdune
8+
dune_action_trace
9+
csexp
10+
bigstringaf
11+
threads.posix
12+
spawn
13+
unix))

src/dune_trace/dune_trace.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@ let () =
1111
| None -> ()
1212
| Some t ->
1313
Out.emit t (Event.exit ());
14-
Out.close t)
14+
Out.close t;
15+
(match Env.(get initial Dune_action_trace.Private.trace_dir_env_var) with
16+
| None -> ()
17+
| Some dir ->
18+
let dir = Path.of_string dir in
19+
Path.mkdir_p dir;
20+
let dst =
21+
Path.relative
22+
(Temp.temp_dir ~parent_dir:dir ~prefix:"dune" ~suffix:"trace")
23+
"trace.csexp"
24+
in
25+
Io.copy_file ~src:t.path ~dst ()))
1526
;;
1627

1728
let set_global t =

src/dune_trace/out.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type t =
2222
; buf : Buffer.t
2323
; cats : Category.Set.t
2424
; mutex : Mutex.t
25+
; path : Path.t
2526
}
2627

2728
(* CR-someday rgrinberg: remove this once we drop support for < 5.2 *)
@@ -70,7 +71,7 @@ let create cats path =
7071
in
7172
let cats = Category.Set.of_list cats in
7273
let buf = Buffer.create (1 lsl 16) in
73-
{ fd; cats; buf; mutex = Mutex.create () }
74+
{ fd; cats; buf; mutex = Mutex.create (); path }
7475
;;
7576

7677
let to_buffer t sexp =

src/dune_trace/out.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ type t =
33
; buf : Buffer.t
44
; cats : Category.Set.t
55
; mutex : Mutex.t
6+
; path : Stdune.Path.t
67
}
78

89
val emit : ?buffered:bool -> t -> Event.t -> unit
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Dune itself produces action traces
2+
3+
$ cat >dune-project<<EOF
4+
> (lang dune 3.22)
5+
> EOF
6+
7+
$ cat >dune <<EOF
8+
> (rule (with-stdout-to foo (echo bar)))
9+
> (rule
10+
> (alias foo)
11+
> (deps (source_tree .))
12+
> (action (run dune build ./foo)))
13+
> EOF
14+
15+
$ dune build @foo
16+
17+
$ dune trace cat | jq 'include "dune";
18+
> select(.name == "init")
19+
> | { args: .args | keys, keys: keys, argv: .args.argv | .[1:] }
20+
> '
21+
{
22+
"args": [
23+
"argv",
24+
"build_dir",
25+
"env",
26+
"initial_cwd",
27+
"pid",
28+
"root"
29+
],
30+
"keys": [
31+
"args",
32+
"cat",
33+
"name",
34+
"ts"
35+
],
36+
"argv": [
37+
"build",
38+
"@foo"
39+
]
40+
}
41+
{
42+
"args": [
43+
"argv",
44+
"build_dir",
45+
"digest",
46+
"env",
47+
"initial_cwd",
48+
"pid",
49+
"root"
50+
],
51+
"keys": [
52+
"args",
53+
"cat",
54+
"name",
55+
"ts"
56+
],
57+
"argv": [
58+
"build",
59+
"./foo"
60+
]
61+
}

0 commit comments

Comments
 (0)