Skip to content

Commit 24b9fed

Browse files
committed
Allow sharing traces with -share flag
Signed-off-by: Michael Jarrett <[email protected]>
1 parent e882fb3 commit 24b9fed

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

src/env_vars.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ open Async
77
files around. *)
88
let perfetto_dir = Unix.getenv "MAGIC_TRACE_PERFETTO_DIR"
99

10+
(* Documentation string for [-share] *)
11+
let share_doc = Unix.getenv "MAGIC_TRACE_SHARE_DOC"
12+
13+
(* Points to a filesystem path that will be invoked with a trace filename to implement
14+
[-share]. *)
15+
let share_command_filename = Unix.getenv "MAGIC_TRACE_SHARE_COMMAND"
16+
1017
(* Override which [perf] to use. If this isn't set, magic-trace will use whatever's first
1118
in $PATH. *)
1219
let perf_path = Option.value ~default:"perf" (Unix.getenv "MAGIC_TRACE_PERF_PATH")

src/env_vars.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ val perf_path : string
66
val perf_is_privileged : bool
77
val perf_no_kcore : bool
88
val perfetto_dir : string option
9+
val share_doc : string option
10+
val share_command_filename : string option
911
val no_dlfilter : bool
1012
val fzf_demangle_symbols : bool
1113
val no_ocaml_exception_debug_info : bool

src/tracing_tool_output.ml

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@ open! Core
22
open! Async
33

44
module Serve = struct
5-
type enabled =
5+
type t =
66
{ port : int
77
; perfetto_ui_base_directory : string
88
}
99

10-
type t =
11-
| Disabled
12-
| Enabled of enabled
13-
14-
let param =
15-
match Env_vars.perfetto_dir with
16-
| None -> Command.Param.return Disabled
17-
| Some perfetto_ui_base_directory ->
18-
let%map_open.Command serve =
19-
flag "serve" no_arg ~doc:" Host the magic-trace UI locally."
10+
let maybe_param =
11+
Option.map Env_vars.perfetto_dir ~f:(fun perfetto_ui_base_directory ->
12+
let%map_open.Command () =
13+
flag "serve" (no_arg_required ()) ~doc:" Host the magic-trace UI locally."
2014
and port =
2115
let default = 8080 in
2216
flag
@@ -27,7 +21,7 @@ module Serve = struct
2721
"PORT Chooses the port that the local copy of the magic-trace UI will be \
2822
served on if [-serve] is specified. (default: %{default#Int})"]
2923
in
30-
if serve then Enabled { port; perfetto_ui_base_directory } else Disabled
24+
{ port; perfetto_ui_base_directory })
3125
;;
3226

3327
let url t =
@@ -112,6 +106,24 @@ module Serve = struct
112106
;;
113107
end
114108

109+
module Share = struct
110+
type t = { share_command_filename : string }
111+
112+
let maybe_param =
113+
Option.both Env_vars.share_doc Env_vars.share_command_filename
114+
|> Option.map ~f:(fun (doc, share_command_filename) ->
115+
let%map_open.Command () = flag "share" (no_arg_required ()) ~doc in
116+
{ share_command_filename })
117+
;;
118+
119+
let share_trace_file t ~filename =
120+
Process.run_forwarding
121+
~prog:t.share_command_filename
122+
~args:[ Filename_unix.realpath filename ]
123+
()
124+
;;
125+
end
126+
115127
type events_output_format =
116128
| Sexp
117129
| Binio
@@ -122,8 +134,13 @@ type events_writer =
122134
; callstack_compression_state : Callstack_compression.t
123135
}
124136

137+
type display_mode =
138+
| Disabled
139+
| Serve of Serve.t
140+
| Share of Share.t
141+
125142
type t =
126-
{ serve : Serve.t
143+
{ display_mode : display_mode
127144
; output_path : string
128145
}
129146

@@ -138,8 +155,14 @@ let param =
138155
[%string
139156
"FILE File to output the trace to. File format depends on suffix [*.sexp \
140157
*.binio *.fxt] (default: '%{default}')"]
141-
and serve = Serve.param in
142-
{ serve; output_path }
158+
and display_mode =
159+
[ Serve.maybe_param |> Option.map ~f:(map ~f:(fun s -> Serve s))
160+
; Share.maybe_param |> Option.map ~f:(map ~f:(fun s -> Share s))
161+
]
162+
|> List.filter_opt
163+
|> choose_one_non_optional ~if_nothing_chosen:(Default_to Disabled)
164+
in
165+
{ display_mode; output_path }
143166
;;
144167

145168
let notify_trace ~store_path =
@@ -165,7 +188,7 @@ let write_and_maybe_serve
165188
=
166189
let open Deferred.Or_error.Let_syntax in
167190
maybe_stash_old_trace ~filename;
168-
let { serve; output_path } = t in
191+
let { display_mode; output_path } = t in
169192
let matches_sexp = String.is_suffix ~suffix:".sexp" output_path in
170193
let matches_binio = String.is_suffix ~suffix:".binio" output_path in
171194
match matches_sexp || matches_binio with
@@ -191,17 +214,18 @@ let write_and_maybe_serve
191214
~filename:indirect_store_path
192215
()
193216
in
194-
let%bind.Deferred.Or_error res = f ~events_writer:None ~writer:(Some writer) () in
195-
let%map () =
196-
match serve with
217+
let%bind res = f ~events_writer:None ~writer:(Some writer) () in
218+
let%bind () =
219+
match display_mode with
197220
| Disabled -> notify_trace ~store_path:output_path
198-
| Enabled serve ->
221+
| Share share -> Share.share_trace_file share ~filename
222+
| Serve serve ->
199223
Serve.serve_trace_file serve ~filename ~store_path:indirect_store_path
200224
in
201225
Core_unix.close fd;
202-
res
226+
return res
203227
| true ->
204-
let%map.Deferred.Or_error res =
228+
let%map res =
205229
let format =
206230
match matches_sexp with
207231
| true -> Sexp

0 commit comments

Comments
 (0)