From a3c7351276ded4da5d78045471c6a7e1acd6137b Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sun, 11 Jun 2023 11:41:10 +0100 Subject: [PATCH] feature: trace loading/marshalling files Loading/Marshalling can be slow if the files being loaded are huge. We record the overhead when --trace-file is enabled. Signed-off-by: Rudi Grinberg --- boot/libs.ml | 6 +++--- src/dune_util/dune | 2 ++ src/dune_util/persistent.ml | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/boot/libs.ml b/boot/libs.ml index c9f1b57fc15b..0980f10c0bbe 100644 --- a/boot/libs.ml +++ b/boot/libs.ml @@ -25,6 +25,9 @@ let local_libraries = ; ("otherlibs/dune-glob/src", Some "Dune_glob", false, None) ; ("otherlibs/xdg", Some "Xdg", false, None) ; ("otherlibs/dune-rpc/private", Some "Dune_rpc_private", false, None) + ; ("otherlibs/chrome-trace/src", Some "Chrome_trace", false, None) + ; ("vendor/spawn/src", Some "Spawn", false, None) + ; ("src/dune_stats", Some "Dune_stats", false, None) ; ("vendor/build_path_prefix_map/src", Some "Build_path_prefix_map", false, None) ; ("src/dune_config", Some "Dune_config", false, None) @@ -33,13 +36,10 @@ let local_libraries = ; ("otherlibs/dune-private-libs/section", Some "Dune_section", false, None) ; ("src/dune_lang", Some "Dune_lang", false, None) ; ("vendor/opam-file-format", None, false, None) - ; ("otherlibs/chrome-trace/src", Some "Chrome_trace", false, None) ; ("src/dune_async_io", Some "Dune_async_io", false, None) ; ("src/fiber_util", Some "Fiber_util", false, None) ; ("src/dune_cache_storage", Some "Dune_cache_storage", false, None) ; ("src/dune_cache", Some "Dune_cache", false, None) - ; ("vendor/spawn/src", Some "Spawn", false, None) - ; ("src/dune_stats", Some "Dune_stats", false, None) ; ("otherlibs/dune-action-plugin/src", Some "Dune_action_plugin", false, None) ; ("src/csexp_rpc", Some "Csexp_rpc", false, None) diff --git a/src/dune_util/dune b/src/dune_util/dune index 9bd5b06a6a54..9197a0b7d5dd 100644 --- a/src/dune_util/dune +++ b/src/dune_util/dune @@ -5,6 +5,8 @@ (names dune_flock)) (libraries stdune + dune_stats + chrome_trace xdg dyn ordering diff --git a/src/dune_util/persistent.ml b/src/dune_util/persistent.ml index 138a45849711..397b88059321 100644 --- a/src/dune_util/persistent.ml +++ b/src/dune_util/persistent.ml @@ -54,8 +54,8 @@ module Make (D : Desc) = struct output_string oc magic; Marshal.to_channel oc v []) - let load file = - if Path.exists file then + let load = + let read_file file = Io.with_file_in file ~f:(fun ic -> match really_input_string ic (String.length magic) with | exception End_of_file -> None @@ -72,7 +72,36 @@ module Make (D : Desc) = struct None | d -> Some d else None) - else None + in + let read_file = + lazy + (match Dune_stats.global () with + | None -> read_file + | Some stats -> + fun file -> + let start = Unix.gettimeofday () in + let res = Result.try_with (fun () -> read_file file) in + let event = + let stop = Unix.gettimeofday () in + let module Event = Chrome_trace.Event in + let module Timestamp = Event.Timestamp in + let dur = Timestamp.of_float_seconds (stop -. start) in + let common = + Event.common_fields ~name:"Loading Persistent State" + ~ts:(Timestamp.of_float_seconds start) + () + in + let args = + [ ("path", `String (Path.to_string file)) + ; ("module", `String D.name) + ] + in + Event.complete common ~args ~dur + in + Dune_stats.emit stats event; + Result.ok_exn res) + in + fun file -> if Path.exists file then (Lazy.force read_file) file else None end type t = T : (module Desc with type t = 'a) * 'a -> t