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

fix(metrics): output correct process metrics #6892

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Unreleased
----------

- Fix `--trace-file` output. Dune now emits a single *complete* event for every
executed process. Unterminated *async* events are no longer written. (#6892,
@rgrinberg)

- Fix preprocessing with `staged_pps` (#6748, fixes #6644, @rgrinberg)

- Make `dune describe workspace` return consistent dependencies for
Expand Down
47 changes: 18 additions & 29 deletions src/dune_engine/process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -577,29 +577,22 @@ end = struct
fail ~loc ~annots paragraphs
end

let report_process_start stats ~metadata ~id ~pid ~prog ~args ~now =
let report_process_finished stats ~metadata ~prog ~pid ~args ~started_at
(times : Proc.Times.t) =
let common =
let name =
match metadata.name with
| Some n -> n
| None -> Filename.basename prog
in
let ts = Timestamp.of_float_seconds now in
let ts = Timestamp.of_float_seconds started_at in
Event.common_fields ~cat:("process" :: metadata.categories) ~name ~ts ()
in
let args =
[ ("process_args", `List (List.map args ~f:(fun arg -> `String arg)))
; ("pid", `Int (Pid.to_int pid))
]
in
let event =
Event.async (Chrome_trace.Id.create (`Int id)) ~args Start common
in
Dune_stats.emit stats event;
(common, args)

let report_process_end stats (common, args) ~now (times : Proc.Times.t) =
let common = Event.set_ts common (Timestamp.of_float_seconds now) in
let dur = Event.Timestamp.of_float_seconds times.elapsed_time in
let event = Event.complete ~args ~dur common in
Dune_stats.emit stats event
Expand Down Expand Up @@ -692,7 +685,7 @@ let run_internal ?dir ?(stdout_to = Io.stdout) ?(stderr_to = Io.stderr)
(stdout, stderr)
| _ -> ((`No_capture, stdout_to), (`No_capture, stderr_to))
in
let event_common, started_at, pid =
let started_at, pid =
(* Output.fd might create the file with Unix.openfile. We need to make
sure to call it before doing the chdir as the path might be
relative. *)
Expand All @@ -705,26 +698,21 @@ let run_internal ?dir ?(stdout_to = Io.stdout) ?(stderr_to = Io.stderr)
| false -> env
in
let env = Env.to_unix env |> Spawn.Env.of_list in
let started_at, pid =
let started_at =
(* jeremiedimino: I think we should do this just before the [execve]
in the stub for [Spawn.spawn] to be as precise as possible *)
let now = Unix.gettimeofday () in
( now
, Spawn.spawn () ~prog:prog_str ~argv ~env ~stdout ~stderr ~stdin
~setpgid:Spawn.Pgid.new_process_group
~cwd:
(match dir with
| None -> Inherit
| Some dir -> Path (Path.to_string dir))
|> Pid.of_int )
Unix.gettimeofday ()
in
let event_common =
Option.map config.stats ~f:(fun stats ->
( stats
, report_process_start stats ~metadata ~id ~pid ~prog:prog_str
~args ~now:started_at ))
let pid =
Spawn.spawn () ~prog:prog_str ~argv ~env ~stdout ~stderr ~stdin
~setpgid:Spawn.Pgid.new_process_group
~cwd:
(match dir with
| None -> Inherit
| Some dir -> Path (Path.to_string dir))
|> Pid.of_int
in
(event_common, started_at, pid)
(started_at, pid)
in
Io.release stdout_to;
Io.release stderr_to;
Expand All @@ -736,8 +724,9 @@ let run_internal ?dir ?(stdout_to = Io.stdout) ?(stderr_to = Io.stderr)
; resource_usage = process_info.resource_usage
}
in
Option.iter event_common ~f:(fun (stats, common) ->
report_process_end stats common ~now:process_info.end_time times);
Option.iter config.stats ~f:(fun stats ->
report_process_finished stats ~metadata ~prog:prog_str ~pid ~args
~started_at times);
Option.iter response_file ~f:Path.unlink;
let actual_stdout =
match stdout_capture with
Expand Down