Skip to content

Commit

Permalink
Add a simple way to override what perf to use
Browse files Browse the repository at this point in the history
This makes it easier to test out newer versions of `perf` without
needing to mess around with `$PATH`.

Signed-off-by: Tudor Brindus <[email protected]>
  • Loading branch information
Xyene committed Jun 4, 2024
1 parent 5c45ca3 commit 78aa314
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/env_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ open Async
files around. *)
let perfetto_dir = Unix.getenv "MAGIC_TRACE_PERFETTO_DIR"

(** Override which [perf] to use. If this isn't set, magic-trace will use whatever's first
in $PATH. *)
let perf_path = Option.value ~default:"perf" (Unix.getenv "MAGIC_TRACE_PERF_PATH")

(* Whether [perf] should be considered privileged when running as not-root. Bypasses error
checks around kernel tracing when not root. *)
let perf_is_privileged = Option.is_some (Unix.getenv "MAGIC_TRACE_PERF_IS_PRIVILEGED")
Expand Down
1 change: 1 addition & 0 deletions src/env_vars.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open! Core

val debug : bool
val experimental : bool
val perf_path : string
val perf_is_privileged : bool
val perfetto_dir : string option
val no_dlfilter : bool
Expand Down
4 changes: 3 additions & 1 deletion src/perf_capabilities.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ let supports_snapshot_on_exit = kernel_version_at_least ~major:5 ~minor:4
let supports_dlfilter = kernel_version_at_least ~major:5 ~minor:14

let detect_exn () =
let%bind perf_version_proc = Process.create_exn ~prog:"perf" ~args:[ "--version" ] () in
let%bind perf_version_proc =
Process.create_exn ~prog:Env_vars.perf_path ~args:[ "--version" ] ()
in
let%map version_string = Reader.contents (Process.stdout perf_version_proc) in
let version = Version.of_perf_version_string_exn version_string in
let set_if bool flag cap = cap + if bool then flag else empty in
Expand Down
9 changes: 5 additions & 4 deletions src/perf_tool_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open! Async
parent process alive even though it just failed. That, in turn, makes magic-trace stop
responding to Ctrl+C. *)
let perf_env = `Extend [ "PAGER", "cat" ]
let perf = Env_vars.perf_path

module Record_opts = struct
type t =
Expand Down Expand Up @@ -377,7 +378,7 @@ module Recording = struct
in
let argv =
List.concat
[ [ "perf"; "record"; "-o"; record_dir ^/ "perf.data"; "--timestamp" ]
[ [ perf; "record"; "-o"; record_dir ^/ "perf.data"; "--timestamp" ]
; event_opts
; overwrite_opts
; switch_opts
Expand All @@ -391,7 +392,7 @@ module Recording = struct
in
if debug_print_perf_commands then Core.printf "%s\n%!" (String.concat ~sep:" " argv);
(* Perf prints output we don't care about and --quiet doesn't work for some reason *)
let perf_pid = perf_fork_exec ~env:perf_env ~prog:"perf" ~argv () in
let perf_pid = perf_fork_exec ~env:perf_env ~prog:perf ~argv () in
(* This detaches the perf process from our "process group" but not our session. This
makes it so that when Ctrl-C is sent to magic_trace in the terminal to end an attach
session, it doesn't also send SIGINT to the perf process, allowing us to send it a
Expand Down Expand Up @@ -508,11 +509,11 @@ let decode_events
]
in
if debug_print_perf_commands
then Core.printf "perf %s\n%!" (String.concat ~sep:" " args);
then Core.printf "%s %s\n%!" perf (String.concat ~sep:" " args);
(* CR-someday tbrindus: this should be switched over to using
[perf_fork_exec] to avoid the [perf script] process from outliving
the parent. *)
let%map perf_script_proc = Process.create_exn ~env:perf_env ~prog:"perf" ~args () in
let%map perf_script_proc = Process.create_exn ~env:perf_env ~prog:perf ~args () in
let line_pipe = Process.stdout perf_script_proc |> Reader.lines in
don't_wait_for
(Reader.transfer
Expand Down
2 changes: 1 addition & 1 deletion src/trace.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let supports_command command =
;;

let supports_fzf = supports_command "fzf"
let supports_perf = supports_command "perf"
let supports_perf = supports_command Env_vars.perf_path

let check_for_perf () =
if force supports_perf
Expand Down

0 comments on commit 78aa314

Please sign in to comment.