Skip to content

Commit

Permalink
feature: trace loading/marshalling files
Browse files Browse the repository at this point in the history
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 <[email protected]>

<!-- ps-id: 5cb1b063-6dd5-4cd7-a081-1d56fab2b6d7 -->
  • Loading branch information
rgrinberg committed Jun 17, 2023
1 parent 59b44dc commit a3c7351
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
6 changes: 3 additions & 3 deletions boot/libs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/dune_util/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
(names dune_flock))
(libraries
stdune
dune_stats
chrome_trace
xdg
dyn
ordering
Expand Down
35 changes: 32 additions & 3 deletions src/dune_util/persistent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a3c7351

Please sign in to comment.