Skip to content

Commit 64edaa7

Browse files
committed
refactor: extract common sandbox tracing code
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent 152cb7b commit 64edaa7

File tree

1 file changed

+61
-55
lines changed

1 file changed

+61
-55
lines changed

src/dune_engine/sandbox.ml

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,22 @@ let maybe_async f =
77
type of this function would need to be polymorphic which is forbidden by the
88
relaxed value restriction. *)
99
match Config.(get background_sandboxes) with
10-
| `Disabled -> Fiber.return (f ())
11-
| `Enabled -> Scheduler.async_exn f
10+
| `Disabled ->
11+
let start = Time.now () in
12+
f ();
13+
let finish = Time.now () in
14+
Fiber.return (start, finish, None)
15+
| `Enabled ->
16+
let queue_start = Time.now () in
17+
let open Fiber.O in
18+
let+ start, finish =
19+
Scheduler.async_exn (fun () ->
20+
let start = Time.now () in
21+
f ();
22+
let finish = Time.now () in
23+
start, finish)
24+
in
25+
start, finish, Some (Time.diff start queue_start)
1226
;;
1327

1428
let init =
@@ -177,21 +191,16 @@ let create ~mode ~rule_loc ~dirs ~deps ~rule_dir ~rule_digest =
177191
in
178192
let t = { dir = sandbox_dir; snapshot = None; loc = rule_loc } in
179193
let open Fiber.O in
180-
let queue_start = Time.now () in
181-
let+ start, stop =
194+
let+ start, stop, queued =
182195
maybe_async (fun () ->
183-
let start = Time.now () in
184196
Path.rm_rf (Path.build sandbox_dir);
185197
create_dirs t ~dirs ~rule_dir;
186198
(* CR-someday amokhov: Note that this doesn't link dynamic dependencies, so
187199
targets produced dynamically will be unavailable. *)
188-
link_deps t ~mode ~deps;
189-
let stop = Time.now () in
190-
start, stop)
200+
link_deps t ~mode ~deps)
191201
in
192202
Dune_trace.emit ~buffered:true Sandbox (fun () ->
193-
let queued = Time.diff start queue_start in
194-
Dune_trace.Event.sandbox `Create ~start ~stop ~queued:(Some queued) t.loc ~dir:t.dir);
203+
Dune_trace.Event.sandbox `Create ~start ~stop ~queued t.loc ~dir:t.dir);
195204
match mode with
196205
| Patch_back_source_tree -> { t with snapshot = Some (snapshot t) }
197206
| _ -> t
@@ -264,38 +273,42 @@ let hint_delete_dir =
264273
let move_targets_to_build_dir t ~should_be_skipped ~(targets : Targets.Validated.t)
265274
: unit Fiber.t
266275
=
267-
maybe_async (fun () ->
268-
Option.iter t.snapshot ~f:(fun old_snapshot ->
269-
register_snapshot_promotion t targets ~old_snapshot);
270-
Targets.Validated.iter
271-
targets
272-
~file:(fun target ->
273-
if not (should_be_skipped target)
274-
then rename_optional_file ~src:(map_path t target) ~dst:target)
275-
~dir:(fun target ->
276-
let src_dir = map_path t target in
277-
(match Path.Untracked.stat (Path.build target) with
278-
| Error (Unix.ENOENT, _, _) -> ()
279-
| Error e ->
280-
User_error.raise
281-
~hints:hint_delete_dir
282-
[ Pp.textf "unable to stat %s" (Path.Build.to_string_maybe_quoted target)
283-
; Pp.text "reason:"
284-
; Pp.text (Unix_error.Detailed.to_string_hum e)
285-
]
286-
| Ok { Unix.st_kind; _ } ->
287-
(* We clean up all targets (including directory targets) before
276+
let open Fiber.O in
277+
let+ _start, _finish, _queued =
278+
maybe_async (fun () ->
279+
Option.iter t.snapshot ~f:(fun old_snapshot ->
280+
register_snapshot_promotion t targets ~old_snapshot);
281+
Targets.Validated.iter
282+
targets
283+
~file:(fun target ->
284+
if not (should_be_skipped target)
285+
then rename_optional_file ~src:(map_path t target) ~dst:target)
286+
~dir:(fun target ->
287+
let src_dir = map_path t target in
288+
(match Path.Untracked.stat (Path.build target) with
289+
| Error (Unix.ENOENT, _, _) -> ()
290+
| Error e ->
291+
User_error.raise
292+
~hints:hint_delete_dir
293+
[ Pp.textf "unable to stat %s" (Path.Build.to_string_maybe_quoted target)
294+
; Pp.text "reason:"
295+
; Pp.text (Unix_error.Detailed.to_string_hum e)
296+
]
297+
| Ok { Unix.st_kind; _ } ->
298+
(* We clean up all targets (including directory targets) before
288299
running an action, so this branch should be unreachable unless
289300
the rule somehow escaped the sandbox *)
290-
User_error.raise
291-
~hints:hint_delete_dir
292-
[ Pp.textf
293-
"Target %s of kind %S already exists in the build directory"
294-
(Path.Build.to_string_maybe_quoted target)
295-
(File_kind.to_string_hum st_kind)
296-
]);
297-
if Path.Untracked.exists (Path.build src_dir)
298-
then Unix.rename (Path.Build.to_string src_dir) (Path.Build.to_string target)))
301+
User_error.raise
302+
~hints:hint_delete_dir
303+
[ Pp.textf
304+
"Target %s of kind %S already exists in the build directory"
305+
(Path.Build.to_string_maybe_quoted target)
306+
(File_kind.to_string_hum st_kind)
307+
]);
308+
if Path.Untracked.exists (Path.build src_dir)
309+
then Unix.rename (Path.Build.to_string src_dir) (Path.Build.to_string target)))
310+
in
311+
()
299312
;;
300313

301314
let failed_to_delete_sandbox dir reason =
@@ -306,23 +319,16 @@ let failed_to_delete_sandbox dir reason =
306319
;;
307320

308321
let destroy t =
309-
let queue_start = Time.now () in
310322
let open Fiber.O in
311-
let+ start, stop =
323+
let+ start, stop, queued =
312324
maybe_async (fun () ->
313-
let start = Time.now () in
314-
let () =
315-
try Path.rm_rf ~chmod:true (Path.build t.dir) with
316-
| Sys_error e -> failed_to_delete_sandbox t.dir (Pp.verbatim e)
317-
| Unix.Unix_error (error, syscall, arg) ->
318-
failed_to_delete_sandbox
319-
t.dir
320-
(Unix_error.Detailed.pp (Unix_error.Detailed.create error ~syscall ~arg))
321-
in
322-
let stop = Time.now () in
323-
start, stop)
325+
try Path.rm_rf ~chmod:true (Path.build t.dir) with
326+
| Sys_error e -> failed_to_delete_sandbox t.dir (Pp.verbatim e)
327+
| Unix.Unix_error (error, syscall, arg) ->
328+
failed_to_delete_sandbox
329+
t.dir
330+
(Unix_error.Detailed.pp (Unix_error.Detailed.create error ~syscall ~arg)))
324331
in
325332
Dune_trace.emit ~buffered:true Sandbox (fun () ->
326-
let queued = Time.diff start queue_start in
327-
Dune_trace.Event.sandbox `Destroy ~start ~stop ~queued:(Some queued) t.loc ~dir:t.dir)
333+
Dune_trace.Event.sandbox `Destroy ~start ~stop ~queued t.loc ~dir:t.dir)
328334
;;

0 commit comments

Comments
 (0)