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

Added --output option to redirect olly printing #5

Merged
merged 3 commits into from
Sep 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions bin/olly.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module H = Hdr_histogram
module Ts = Runtime_events.Timestamp
open Cmdliner

let print_percentiles name json hist =
let print_percentiles json output hist =
let ms ns = ns /. 1000000. in
let mean_latency = H.mean hist |> ms
and max_latency = float_of_int (H.max hist) |> ms in
Expand All @@ -28,31 +28,30 @@ let print_percentiles name json hist =
100.0;
|]
in
let oc = match output with Some s -> open_out s | None -> stderr in
if json then
let distribs =
List.init (Array.length percentiles) (fun i ->
H.value_at_percentile hist percentiles.(i)
|> float_of_int |> ms |> string_of_float)
|> String.concat ","
in
Printf.printf
{|{"name": "%s", "mean_latency": %d, "max_latency: %d, "distr_latency": [%s]}
|}
name
Printf.fprintf oc
{|{"mean_latency": %d, "max_latency": %d, "distr_latency": [%s]}|}
(int_of_float mean_latency)
(int_of_float max_latency) distribs
else (
Printf.eprintf "\n";
Printf.eprintf "GC latency profile:\n";
Printf.eprintf "#[Mean (ms):\t%.2f,\t Stddev (ms):\t%.2f]\n" mean_latency
Printf.fprintf oc "\n";
Printf.fprintf oc "GC latency profile:\n";
Printf.fprintf oc "#[Mean (ms):\t%.2f,\t Stddev (ms):\t%.2f]\n" mean_latency
(H.stddev hist |> ms);
Printf.eprintf "#[Min (ms):\t%.2f,\t max (ms):\t%.2f]\n"
Printf.fprintf oc "#[Min (ms):\t%.2f,\t max (ms):\t%.2f]\n"
(float_of_int (H.min hist) |> ms)
max_latency;
Printf.eprintf "\n";
Printf.eprintf "Percentile \t Latency (ms)\n";
Printf.fprintf oc "\n";
Printf.fprintf oc "Percentile \t Latency (ms)\n";
Fun.flip Array.iter percentiles (fun p ->
Printf.eprintf "%.4f \t %.2f\n" p
Printf.fprintf oc "%.4f \t %.2f\n" p
(float_of_int (H.value_at_percentile hist p) |> ms)))

let lost_events ring_id num =
Expand Down Expand Up @@ -133,7 +132,7 @@ let trace trace_filename exec_args =
let cleanup () = close_out trace_file in
olly ~runtime_begin ~runtime_end ~init ~cleanup exec_args

let latency json exec_args =
let latency json output exec_args =
let current_event = Hashtbl.create 13 in
let hist =
H.init ~lowest_discernible_value:10 ~highest_trackable_value:10_000_000_000
Expand All @@ -153,9 +152,7 @@ let latency json exec_args =
| _ -> ()
in
let init = Fun.id in
let cleanup () =
print_percentiles (List.hd @@ String.split_on_char ' ' exec_args) json hist
in
let cleanup () = print_percentiles json output hist in
olly ~runtime_begin ~runtime_end ~init ~cleanup exec_args

let help man_format cmds topic =
Expand Down Expand Up @@ -225,6 +222,17 @@ let () =
Arg.(value & flag & info [ "json" ] ~docv:"json" ~doc)
in

let output_option =
let doc =
"Redirect the output of `olly` to specified file. The output of the \
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does "output of the command is not redirected" mean?

Copy link
Author

Choose a reason for hiding this comment

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

If prog.exe prints on stdout and/or stderr, then olly latency -o some_file prog.exe will print the latency info in some_file, but the original prints will still go to stdout/stderr

Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be useful to add a description in the PR message to indicate how this command works like ^^.

command is not redirected."
in
Arg.(
value
& opt (some string) None
& info [ "o"; "output" ] ~docv:"output" ~doc)
in

let latency_cmd =
let man =
[
Expand All @@ -235,7 +243,7 @@ let () =
in
let doc = "Report the GC latency profile." in
let info = Cmd.info "latency" ~doc ~sdocs ~man in
Cmd.v info Term.(const latency $ json_option $ exec_args 0)
Cmd.v info Term.(const latency $ json_option $ output_option $ exec_args 0)
in

let help_cmd =
Expand Down