Skip to content

Commit 0b970a0

Browse files
committed
Improve render and dump CLI
`eio-trace render` and `eio-trace dump` now take a list of trace files rather than the name of the output. This works better with tab-completion and is consistent with how the `show` sub-command works.
1 parent 5ae6801 commit 0b970a0

File tree

4 files changed

+74
-36
lines changed

4 files changed

+74
-36
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ dune exec -- eio-trace show trace.fxt
8181
To convert a trace to SVG format:
8282

8383
```
84-
dune exec -- eio-trace render -f trace.fxt trace.svg
84+
dune exec -- eio-trace render trace.fxt
8585
```
8686

87+
You can also use `--format=png` for PNG output.
88+
8789
## Reading traces
8890

8991
Eio fibers are shown as horizontal bars.

gtk/main.ml

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ open Eio_trace
22

33
let (_ : string) = GMain.init ()
44

5+
let format_of_string = function
6+
| ".svg" -> `Svg
7+
| ".png" -> `Png
8+
| x -> Fmt.failwith "Unknown format %S (should be .svg or .png)" x
9+
510
let load tracefile =
611
let ch = open_in_bin tracefile in
712
let len = in_channel_length ch in
@@ -22,7 +27,13 @@ let show ?args tracefile =
2227
in
2328
Gtk_ui.create ~title (load tracefile)
2429

25-
let render ~output ~start_time ?duration ~format tracefile =
30+
let render ?output ~start_time ?duration ~format tracefile =
31+
let output =
32+
match output with
33+
| Some x -> x
34+
| None -> Filename.remove_extension tracefile ^ format
35+
in
36+
let format = format_of_string format in
2637
let l = load (tracefile) in
2738
let v =
2839
View.of_layout l
@@ -48,28 +59,32 @@ let render ~output ~start_time ?duration ~format tracefile =
4859
| `Svg -> ()
4960
| `Png -> Cairo.PNG.write surface output
5061
end;
51-
Cairo.Surface.finish surface
52-
53-
let format_of_string = function
54-
| ".svg" -> `Svg
55-
| ".png" -> `Png
56-
| x -> Fmt.failwith "Unknown format %S (should be .svg or .png)" x
62+
Cairo.Surface.finish surface;
63+
Printf.printf "Wrote %S\n" output
5764

5865
let () =
5966
match Array.to_list Sys.argv with
6067
| _ :: "show" :: tracefiles -> List.iter show tracefiles; GMain.main ()
6168
| _ :: "run" :: tracefile :: args -> show ~args tracefile; GMain.main ()
62-
| [ _; "render-svg"; tracefile; format; output; start_time; duration ] ->
69+
| _ :: "render-svg" :: format :: output :: start_time :: duration :: tracefiles ->
6370
let duration =
6471
match duration with
6572
| "" -> None
6673
| x -> Some (float_of_string x *. 1e9)
6774
in
68-
render tracefile
69-
~output
70-
~start_time:(float_of_string start_time *. 1e9)
71-
?duration
72-
~format:(format_of_string format)
75+
let output =
76+
match output with
77+
| "" -> None
78+
| x -> Some x
79+
in
80+
let render =
81+
render
82+
?output
83+
~start_time:(float_of_string start_time *. 1e9)
84+
?duration
85+
~format
86+
in
87+
List.iter render tracefiles
7388
| args ->
7489
Fmt.failwith "Invalid arguments (eio-trace-gtk should be run via eio-trace)@.(got %a)"
7590
Fmt.(Dump.list string) args

src/dump.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module Read = Fxt.Read
22

3-
let main out tracefile =
4-
Eio.Path.with_open_in tracefile @@
5-
Eio.Buf_read.parse_exn ~max_size:max_int @@ fun r ->
6-
Fmt.pf out "@[<v>%a@]@." (Fmt.seq Read.pp_record) (Read.records r);
3+
let main out tracefiles =
4+
tracefiles |> List.iter (fun tracefile ->
5+
Eio.Path.with_open_in tracefile @@
6+
Eio.Buf_read.parse_exn ~max_size:max_int @@ fun r ->
7+
Fmt.pf out "@[<v>%a@]@." (Fmt.seq Read.pp_record) (Read.records r);
8+
);
79
Ok ()

src/main.ml

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ let tracefile =
2424
Arg.(value @@ opt string "trace.fxt" @@ info ["f"; "tracefile"] ~docv:"PATH" ~doc)
2525

2626
let tracefiles =
27-
let doc = "The path of the trace file(s)." in
27+
let doc = "The path of the trace file(s). The default is trace.fxt." in
2828
Arg.(value @@ pos_all string ["trace.fxt"] @@ info [] ~docv:"PATH" ~doc)
2929

3030
let imagefile =
31-
let doc = "The path of the output image." in
32-
Arg.(required @@ pos 0 (some string) None @@ info [] ~docv:"OUTPUT" ~doc)
31+
let doc = "The path of the output image. The default is the input path with the new type extension." in
32+
Arg.(value @@ opt (some string) None @@ info ["o"; "output"] ~docv:"OUTPUT" ~doc)
33+
34+
let fmt =
35+
let doc = "Output image file-type." in
36+
let formats = Arg.enum [
37+
"png", ".png";
38+
"svg", ".svg";
39+
]
40+
in
41+
Arg.(value @@ opt (some formats) None @@ info ["T"; "format"] ~docv:"TYPE" ~doc)
3342

3443
let freq =
3544
let doc = "How many times per second to check for events." in
@@ -80,29 +89,39 @@ let run ~fs ~proc_mgr freq args =
8089

8190
let record ~fs ~proc_mgr freq tracefile args =
8291
Record.run ~fs ~proc_mgr ~freq ~tracefile args
83-
84-
let render tracefile output start_time duration =
85-
match Filename.extension output with
86-
| "" -> Fmt.error "No extension on %S; can't determine format" output
87-
| ".svg"
88-
| ".png" as format ->
89-
let start_time = Option.value start_time ~default:0.0 |> string_of_float in
90-
let duration = Option.map string_of_float duration |> Option.value ~default:"" in
91-
exec_gtk ["render-svg"; tracefile; format; output; start_time; duration]
92-
| _ ->
93-
Fmt.error "Unknown file extension in %S (should end in e.g. '.svg')" output
92+
93+
let ( let* ) = Result.bind
94+
95+
let render tracefiles output fmt start_time duration =
96+
let* fmt =
97+
match fmt, output with
98+
| None, None -> Ok ".svg"
99+
| _, Some _ when List.length tracefiles > 1 -> Error "Can't use --output with multiple input files"
100+
| Some x, _ -> Ok x
101+
| None, Some output ->
102+
match Filename.extension output with
103+
| "" -> Fmt.error "No extension on %S; can't determine format" output
104+
| ".svg"
105+
| ".png" as f -> Ok f
106+
| _ ->
107+
Fmt.error "Unknown file extension in %S (should end in e.g. '.svg')" output
108+
in
109+
let start_time = Option.value start_time ~default:0.0 |> string_of_float in
110+
let duration = Option.map string_of_float duration |> Option.value ~default:"" in
111+
let output = Option.value output ~default:"" in
112+
exec_gtk @@ "render-svg" :: fmt :: output :: start_time :: duration :: tracefiles
94113

95114
let cmd env =
96115
let fs = Eio.Stdenv.fs env in
97116
let proc_mgr = Eio.Stdenv.process_mgr env in
98-
let path x = Eio.Path.( / ) fs $$ x in
117+
let path = Eio.Path.( / ) fs in
99118
Cmd.group (Cmd.info "eio-trace")
100119
@@ List.map (fun (name, term) -> Cmd.v (Cmd.info name) term) [
101-
"record", record ~fs ~proc_mgr $$ freq $ path tracefile $ child_args;
102-
"dump", Dump.main Format.std_formatter $$ path tracefile;
120+
"record", record ~fs ~proc_mgr $$ freq $ (path $$ tracefile) $ child_args;
121+
"dump", Dump.main Format.std_formatter $$ (List.map path $$ tracefiles);
103122
"run", run ~fs ~proc_mgr $$ freq $ child_args;
104123
"show", show $$ tracefiles;
105-
"render", render $$ tracefile $ imagefile $ start_time $ duration;
124+
"render", render $$ tracefiles $ imagefile $ fmt $ start_time $ duration;
106125
]
107126

108127
let () =

0 commit comments

Comments
 (0)