Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add use-trace-processor-shell flag to automate large trace processing #248

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

salaast
Copy link

@salaast salaast commented Jul 18, 2022

  • Add use-processor-exe and open-url flags to run_command in trace.ml to automate large trace file processing and ui url opening

Signed-off-by: Abena Laast <salaast@gmail.com>
… to allow large trace processing and url opening automation

Signed-off-by: Abena Laast <salaast@gmail.com>
Signed-off-by: Abena Laast <salaast@gmail.com>
@salaast salaast changed the title Abena mega trace Add use-processor-exe and open-url flags to run_command Jul 18, 2022
src/trace.ml Outdated
@@ -561,6 +561,32 @@ module Make_commands (Backend : Backend_intf.S) = struct
{ Decode_opts.output_config; decode_opts; print_events }
;;

let open_url_flags =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should allow configuration of this through envvars in env_vars.ml, rather than as commandline parameters. These choices tend to be sticky, so it's nicer to not have users have to specify them over and over.

I'd say a MAGIC_TRACE_WEB_UI_URL that can be any string, and which defaults to https://magic-trace.org, would be sufficient here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once I implement the Env_vars configuration, should I use the commandline flag to indicate whether the user wants to display the ui, or should it always be displayed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now, we should have a separate flag for if the trace processor shell should be used, if available. Maybe in the future we can make the selection automatic using some heuristic based on the size of the trace file (and whether we expect a browser to safely open it), but let's hold off on that bit for now.

src/trace.ml Outdated
| _ -> raise_s [%message "unkown user input" (user_choice : string)]))
;;

let use_processor_flag =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should take the path via an envvar, e.g. MAGIC_TRACE_PROCESSOR_SHELL_PATH; then we can have an optional -use-trace-processor-shell flag if that envvar is set. See

match Env_vars.perfetto_dir with
for inspiration.

We may also want to consider just using the trace processor shell automatically, past a certain filesize, but I haven't given that much thought.

src/trace.ml Outdated
| None ->
print_endline "warning: must use local processor on large trace files";
Deferred.return ()
| Some processor_path -> Async_shell.run processor_path [ "-D"; output_file ]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use something like

let perf_fork_exec ?env ~prog ~argv () =
here, to avoid the trace processor shell outliving magic-trace, in case magic-trace is Ctrl+C'd. May want to extract it into a helper module like process.ml or something.

src/trace.ml Outdated
decode_opts))
decode_opts)
in
let%bind.Deferred () =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think we should do this. Oftentimes we'll run magic-trace attach ... -serve through SSH, so there'd be no browser to open and this'd fail.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you recommend catching an error here or something else? I was unable to find a way to detect from inside the function whether there is actually a browser available.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just shouldn't be opening the user's browser. We already print a link to the terminal for where the user should go to view their trace, and many terminals just allow clicking into it directly.

remove open_url flag,
add processor_path env var and remove arg for use_processor_shell_path flag,
create Enable module for processor_path param

Signed-off-by: Abena Laast <salaast@gmail.com>
Signed-off-by: Abena Laast <salaast@gmail.com>
@salaast salaast changed the title Add use-processor-exe and open-url flags to run_command Add use-trace-processor-shell flag to automate large trace processing Aug 2, 2022
Signed-off-by: Abena Laast <salaast@gmail.com>
src/env_vars.ml Outdated
(* Points to a filesystem path with a trace processor executable.
If provided, the flag can be indicated to automatically run the processor
at the path once the trace file is created *)
let processor_path = Unix.getenv "MAGIC_TRACE_PROCESSOR_SHELL_PATH"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let processor_path = Unix.getenv "MAGIC_TRACE_PROCESSOR_SHELL_PATH"
let processor_path = Unix.getenv "MAGIC_TRACE_TRACE_PROCESSOR_SHELL_PATH"

src/env_vars.mli Outdated
@@ -4,3 +4,4 @@ val debug : bool
val perf_is_privileged : bool
val perfetto_dir : string option
val no_dlfilter : bool
val processor_path : string option
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val processor_path : string option
val trace_processor_shell_path : string option

to be explicit

@@ -8,8 +8,10 @@ let supports_command command =
Lazy.from_fun (fun () ->
match Core_unix.fork () with
| `In_the_child ->
Core_unix.close Core_unix.stdout;
Core_unix.close Core_unix.stderr;
(* gracefully hide perf outputs *)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should leave a comment here about what versions of perf we observed breakages when close-ing and under what circumstances, because it was surprising.

src/trace.ml Outdated
@@ -561,6 +563,41 @@ module Make_commands (Backend : Backend_intf.S) = struct
{ Decode_opts.output_config; decode_opts; print_events }
;;

module Enable = struct
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module Enable = struct
module Enable = struct

Nit: we should give this module a more descriptive name.

src/trace.ml Outdated

type t =
| Disabled
| Enabled of enabled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| Enabled of enabled
| Enabled of { value : string }

Let's inline this type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also give value a better name, like trace_processor_shell_path.

(* Same as [Caml.exit] but does not run at_exit handlers *)
external sys_exit : int -> 'a = "caml_sys_exit"

let call_trace_processor ?env ~prog ~argv () =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's either generalize this with the perf version, or leave a CR-someday to do this later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure which part of this function is perf-specific.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of it is, so ideally this'd be the same function as

let perf_fork_exec ?env ~prog ~argv () =
so we don't have two copies.

You don't need to do this in this PR, but it would be good to leave a note for the future.

magic-trace run -full-execution ./program\n")
magic-trace run -full-execution ./program\n\
# Run a process that generates a large trace file, magic-trace run ./program \
-use-trace-processor-shell\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should leave this out of the documentation for now, or maybe make the -use-trace-processor-shell part of it conditional on the environment variable being present. Definitely don't want to give help text for a flag that doesn't exist.

change processor_path to trace_processor_shell_path,
add comment about closing stdout and stderr breaking things,
better name for Enable module,
inline Enabled,
better name for value,
CR-someday for generalizing call_trace_processor with the perf version,
update processor flag doc to clarify conditional existence

Signed-off-by: Abena Laast <salaast@gmail.com>
match Env_vars.trace_processor_shell_path with
| None -> Command.Param.return Disabled
| Some processor_shell_path ->
let%map_open.Command processor =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let%map_open.Command processor =
let%map_open.Command use_trace_processor_shell =

Nit: descriptive variable names.

let param =
match Env_vars.trace_processor_shell_path with
| None -> Command.Param.return Disabled
| Some processor_shell_path ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| Some processor_shell_path ->
| Some trace_processor_shell_path ->

Nit: avoid having multiple variable names for the semantically-same thing. It's best to shadow the previously-declared name in such cases.

flag
"use-trace-processor-shell"
no_arg
~doc:[%string "use the trace processor set in environment variables"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
~doc:[%string "use the trace processor set in environment variables"]
~doc:[%string "use trace processor shell to view trace"]

@Xyene
Copy link
Member

Xyene commented Aug 5, 2022

There seem to be some conflicts with the base branch, could you please try rebasing to resolve them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants