Skip to content

Commit c47783a

Browse files
committed
Allow sharing traces via MAGIC_TRACE_SHARE_COMMAND
Tested with `MAGIC_TRACE_SHARE_COMMAND=/usr/bin/echo`. Also fixed a bug where we were unconditionally serving the UI if the Perfetto base dir was specified. We lose port configurability, but it's not a big deal and we can add it back as a separate flag if we want to.
1 parent 8c543a6 commit c47783a

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

src/env_vars.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ open Async
66
a local copy of Perfetto if you don't want to copy trace files around. *)
77
let perfetto_ui_base_directory = Unix.getenv "MAGIC_TRACE_PERFETTO_DIR"
88

9+
(* Points to a filesystem path that will be invoked with a trace filename to implement
10+
[-share]. *)
11+
let share_command_filename = Unix.getenv "MAGIC_TRACE_SHARE_COMMAND"
12+
913
(* Turns on hidden command line options. These options are to help magic-trace developers debug
1014
magic-trace, they're not generally useful. *)
1115
let enable_debug_options = Option.is_some (Unix.getenv "MAGIC_TRACE_DEBUG")

src/env_vars.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ open! Core
22

33
val enable_debug_options : bool
44
val perfetto_ui_base_directory : string option
5+
val share_command_filename : string option

src/tracing_tool_output.ml

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,21 @@ 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_ui_base_directory with
16-
| None -> Command.Param.return Disabled
17-
| Some perfetto_ui_base_directory ->
18-
let%map_open.Command port =
19-
let default = 8080 in
20-
flag
21-
"serve"
22-
(optional_with_default default int)
23-
~doc:
24-
[%string
25-
"PORT Chooses the port that the local copy of Perfetto will be served on. \
26-
(default: %{default#Int})"]
27-
in
28-
Enabled { port; perfetto_ui_base_directory }
10+
let maybe_param =
11+
Option.map Env_vars.perfetto_ui_base_directory ~f:(fun perfetto_ui_base_directory ->
12+
let port = 8080 in
13+
let%map_open.Command serve =
14+
flag
15+
"serve"
16+
no_arg
17+
~doc:[%string " Serves the magic-trace UI on port %{port#Int})"]
18+
in
19+
if serve then Some { port; perfetto_ui_base_directory } else None)
2920
;;
3021

3122
let url t =
@@ -110,8 +101,35 @@ module Serve = struct
110101
;;
111102
end
112103

104+
module Share = struct
105+
type t = { share_command_filename : string }
106+
107+
let maybe_param =
108+
Option.map Env_vars.share_command_filename ~f:(fun share_command_filename ->
109+
let%map_open.Command share = flag "share" no_arg ~doc:"Share trace." in
110+
if share then Some { share_command_filename } else None)
111+
;;
112+
113+
let share_trace_file { share_command_filename } ~store_path =
114+
let%map.Deferred.Or_error output =
115+
Process.run
116+
~prog:share_command_filename
117+
~args:[ Filename_unix.realpath store_path ]
118+
()
119+
in
120+
Core.printf "%s%!" output
121+
;;
122+
end
123+
124+
module Display_mode = struct
125+
type t =
126+
| Serve of Serve.t
127+
| Share of Share.t
128+
| Disabled
129+
end
130+
113131
type t =
114-
{ serve : Serve.t
132+
{ display_mode : Display_mode.t
115133
; store_path : string
116134
}
117135

@@ -121,9 +139,18 @@ let param =
121139
flag
122140
"output"
123141
(optional_with_default default string)
142+
~aliases:[ "o" ]
124143
~doc:[%string "FILE Where to output the trace. (default: '%{default}')"]
125-
and serve = Serve.param in
126-
{ serve; store_path }
144+
and display_mode =
145+
[ Serve.maybe_param
146+
|> Option.map ~f:(map ~f:(Option.map ~f:(fun serve -> Display_mode.Serve serve)))
147+
; Share.maybe_param
148+
|> Option.map ~f:(map ~f:(Option.map ~f:(fun share -> Display_mode.Share share)))
149+
]
150+
|> List.filter_opt
151+
|> choose_one ~if_nothing_chosen:(Default_to Display_mode.Disabled)
152+
in
153+
{ display_mode; store_path }
127154
;;
128155

129156
let notify_trace ~store_path =
@@ -132,9 +159,10 @@ let notify_trace ~store_path =
132159
;;
133160

134161
let maybe_serve t ~filename =
135-
match t.serve with
162+
match t.display_mode with
136163
| Disabled -> notify_trace ~store_path:t.store_path
137-
| Enabled serve -> Serve.serve_trace_file serve ~filename ~store_path:t.store_path
164+
| Serve serve -> Serve.serve_trace_file serve ~filename ~store_path:t.store_path
165+
| Share share -> Share.share_trace_file share ~store_path:t.store_path
138166
;;
139167

140168
let maybe_stash_old_trace ~filename =

0 commit comments

Comments
 (0)