this repo has no description
at main 116 lines 3.5 kB view raw
1let instrument = false 2 3open Bos 4 5let instrument_dir = 6 lazy 7 (let dir = Fpath.v "landmarks" in 8 OS.Dir.delete dir |> Result.get_ok; 9 OS.Dir.create dir |> Result.get_ok |> ignore; 10 dir) 11 12type t = { 13 cmd : string list; 14 time : float; (** Running time in seconds. *) 15 output_file : Fpath.t option; 16 output : string; 17 errors : string; 18 status : [ `Exited of int | `Signaled of int ]; 19} 20 21(* Environment variables passed to commands. *) 22 23(* Record the commands executed, their running time and optionally the path to 24 the produced file. *) 25let commands = ref [] 26let n = Atomic.make 0 27 28(** Return the list of executed commands where the first argument was [cmd]. *) 29let run env cmd output_file = 30 let cmd = Bos.Cmd.to_list cmd in 31 let myn = Atomic.fetch_and_add n 1 in 32 Logs.debug (fun m -> m "%d - Executing: %s" myn (String.concat " " cmd)); 33 let proc_mgr = Eio.Stdenv.process_mgr env in 34 let t_start = Unix.gettimeofday () in 35 let env = 36 let env = OS.Env.current () |> Result.get_ok in 37 env 38 in 39 let env = 40 Astring.String.Map.fold 41 (fun k v env -> Astring.String.concat [ k; "="; v ] :: env) 42 env [] 43 |> Array.of_list 44 in 45 (* Logs.debug (fun m -> m "Running cmd %a" Fmt.(list ~sep:sp string) cmd); *) 46 let output, errors, status = 47 Eio.Switch.run ~name:"Process.parse_out" @@ fun sw -> 48 let r, w = Eio.Process.pipe proc_mgr ~sw in 49 let re, we = Eio.Process.pipe proc_mgr ~sw in 50 try 51 let child = 52 Eio.Process.spawn ~sw proc_mgr ~stdout:w ~stderr:we ~env cmd 53 in 54 Eio.Flow.close w; 55 Eio.Flow.close we; 56 let output, err = 57 Eio.Fiber.pair 58 (fun () -> 59 Eio.Buf_read.parse_exn Eio.Buf_read.take_all r ~max_size:max_int) 60 (fun () -> 61 Eio.Buf_read.parse_exn Eio.Buf_read.take_all re ~max_size:max_int) 62 in 63 Eio.Flow.close r; 64 Eio.Flow.close re; 65 let status = Eio.Process.await child in 66 (output, err, status) 67 with Eio.Exn.Io _ as ex -> 68 let bt = Printexc.get_raw_backtrace () in 69 Eio.Exn.reraise_with_context ex bt "%d - running command: %a" myn 70 Eio.Process.pp_args cmd 71 in 72 (* Logs.debug (fun m -> 73 m "Finished running cmd %a" Fmt.(list ~sep:sp string) cmd); *) 74 let t_end = Unix.gettimeofday () in 75 let time = t_end -. t_start in 76 let result = { cmd; time; output_file; output; errors; status } in 77 commands := result :: !commands; 78 (match result.status with 79 | `Exited 0 -> () 80 | _ -> 81 let verb, n = 82 match result.status with 83 | `Exited n -> ("exited", n) 84 | `Signaled n -> ("signaled", n) 85 in 86 Logs.err (fun m -> 87 m 88 "@[<2>Process %s with %d:@ '@[%a'@]@]@\n\n\ 89 Stdout:\n\ 90 %s\n\n\ 91 Stderr:\n\ 92 %s" 93 verb n 94 Fmt.(list ~sep:sp string) 95 result.cmd result.output result.errors)); 96 result 97 98(** Print an executed command and its time. *) 99 100let filter_commands cmd = 101 match 102 List.filter 103 (fun c -> match c.cmd with _ :: cmd' :: _ -> cmd = cmd' | _ -> false) 104 !commands 105 with 106 | [] -> [] 107 | _ :: _ as cmds -> cmds 108 109let print_cmd c = 110 Printf.printf "[%4.2f] $ %s\n" c.time (String.concat " " c.cmd) 111 112(** Returns the [k] commands that took the most time for a given subcommand. *) 113let k_longest_commands cmd k = 114 filter_commands cmd 115 |> List.sort (fun a b -> Float.compare b.time a.time) 116 |> List.filteri (fun i _ -> i < k)