Skip to content

Commit 30c823d

Browse files
authored
Merge pull request #41 from talex5/cpu
Add --cpu option
2 parents 6753018 + 142b3d1 commit 30c823d

File tree

6 files changed

+58
-20
lines changed

6 files changed

+58
-20
lines changed

dune-project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
(synopsis "Trace visualisation for Eio programs")
1212
(depends
1313
(ocaml (>= 5.1.0))
14+
processor
1415
(eio_main (>= 0.14))
1516
(cmdliner (>= 1.2.0))
1617
(lablgtk3 (>= 3.1.4))

eio-trace.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bug-reports: "https://github.com/ocaml-multicore/eio-trace/issues"
99
depends: [
1010
"dune" {>= "3.11"}
1111
"ocaml" {>= "5.1.0"}
12+
"processor"
1213
"eio_main" {>= "0.14"}
1314
"cmdliner" {>= "1.2.0"}
1415
"lablgtk3" {>= "3.1.4"}

src/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(executable
22
(public_name eio-trace)
33
(name main)
4-
(libraries eio_main eio_trace cmdliner fxt))
4+
(libraries eio_main eio_trace cmdliner fxt processor))

src/main.ml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ let fmt =
2727
in
2828
Arg.(value @@ opt (some formats) None @@ info ["T"; "format"] ~docv:"TYPE" ~doc)
2929

30-
let freq =
31-
let doc = "How many times per second to check for events." in
32-
Arg.(value @@ opt float 100.0 @@ info ["F"; "freq"] ~docv:"RATE" ~doc)
33-
3430
let start_time =
3531
let doc = "Seconds to skip before the section to display." in
3632
Arg.(value @@ opt (some time) None @@ info ["s"; "start-time"] ~doc)
@@ -39,10 +35,6 @@ let duration =
3935
let doc = "Width of the output image in seconds." in
4036
Arg.(value @@ opt (some time) None @@ info ["d"; "duration"] ~doc)
4137

42-
let child_args =
43-
let doc = "The command to be executed and monitored." in
44-
Arg.(non_empty @@ pos_all string [] @@ info [] ~docv:"command" ~doc)
45-
4638
let eio_trace_gtk = "eio-trace-gtk"
4739

4840
let find_eio_trace_gtk () =
@@ -66,16 +58,16 @@ let exec_gtk args =
6658

6759
let show tracefiles = exec_gtk ("show" :: tracefiles)
6860

69-
let run ~fs ~proc_mgr freq args =
61+
let run ~fs ~proc_mgr config =
7062
let gtk_exe = find_eio_trace_gtk () in
7163
let ui tracefile =
72-
Eio.Process.run proc_mgr (gtk_exe :: "run" :: tracefile :: args);
64+
Eio.Process.run proc_mgr (gtk_exe :: "run" :: tracefile :: config.Record.child_args);
7365
Ok ()
7466
in
75-
Record.run ~fs ~proc_mgr ~freq ~ui args
67+
Record.run ~fs ~proc_mgr ~ui config
7668

77-
let record ~fs ~proc_mgr freq tracefile args =
78-
Record.run ~fs ~proc_mgr ~freq ~tracefile args
69+
let record ~fs ~proc_mgr tracefile config =
70+
Record.run ~fs ~proc_mgr ~tracefile config
7971

8072
let ( let* ) = Result.bind
8173

@@ -104,9 +96,9 @@ let cmd env =
10496
let path = Eio.Path.( / ) fs in
10597
Cmd.group (Cmd.info "eio-trace")
10698
@@ List.map (fun (name, term) -> Cmd.v (Cmd.info name) term) [
107-
"record", record ~fs ~proc_mgr $$ freq $ (path $$ tracefile) $ child_args;
99+
"record", record ~fs ~proc_mgr $$ (path $$ tracefile) $ Record.cmdliner;
108100
"dump", Dump.main Format.std_formatter $$ (List.map path $$ tracefiles);
109-
"run", run ~fs ~proc_mgr $$ freq $ child_args;
101+
"run", run ~fs ~proc_mgr $$ Record.cmdliner;
110102
"show", show $$ tracefiles;
111103
"render", render $$ tracefiles $ imagefile $ fmt $ start_time $ duration;
112104
"gc-stats", Gc_stats.main Format.std_formatter $$ (List.map path $$ tracefiles);

src/record.ml

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ module Fibers = Map.Make(Int)
55

66
module Write = Fxt.Write
77

8+
type config = {
9+
freq : float;
10+
cpu : int option;
11+
child_args : string list;
12+
}
13+
814
type fiber = {
915
id : int;
1016
mutable op : string option; (* If suspended *)
@@ -199,7 +205,7 @@ let rec get_cursor tmp_dir child =
199205

200206
let ( / ) = Eio.Path.( / )
201207

202-
let run ?ui ?tracefile ~proc_mgr ~fs ~freq args =
208+
let run ?ui ?tracefile ~proc_mgr ~fs { freq; cpu; child_args } =
203209
let delay = 1. /. freq in
204210
let fs = (fs :> Eio.Fs.dir_ty Eio.Path.t) in
205211
let tracefile = (tracefile :> Eio.Fs.dir_ty Eio.Path.t option) in
@@ -216,7 +222,8 @@ let run ?ui ?tracefile ~proc_mgr ~fs ~freq args =
216222
Eio.Buf_write.with_flow out @@ fun w ->
217223
let fxt = Write.of_writer w in
218224
traceln "Recording to %a" Eio.Path.pp tracefile;
219-
let child = spawn_child ~sw ~proc_mgr ~tmp_dir args in
225+
let child = spawn_child ~sw ~proc_mgr ~tmp_dir child_args in
226+
cpu |> Option.iter (fun cpu -> Processor.Affinity.set_cpus [List.nth Processor.Topology.t cpu]);
220227
let t = {
221228
fxt;
222229
pid = Int64.of_int (Eio.Process.pid child);
@@ -246,3 +253,33 @@ let run ?ui ?tracefile ~proc_mgr ~fs ~freq args =
246253
(fun () -> Promise.await finished)
247254
(fun () -> Eio_unix.sleep 1.);
248255
ui (Eio.Path.native_exn tracefile)
256+
257+
open Cmdliner
258+
259+
let ( $ ) = Term.app
260+
let ( $$ ) f x = Term.const f $ x
261+
262+
let freq =
263+
let doc = "How many times per second to check for events." in
264+
Arg.(value @@ opt float 100.0 @@ info ["F"; "freq"] ~docv:"RATE" ~doc)
265+
266+
let cpu =
267+
let doc = "CPU for eio-trace" in
268+
let parse_cpu s =
269+
match Arg.(conv_parser int) s with
270+
| Error _ as e -> e
271+
| Ok x ->
272+
let l = List.length Processor.Topology.t in
273+
if x >= 0 && x < l then Ok x
274+
else Fmt.error_msg "CPU %d not in range 0..%d" x (l - 1)
275+
in
276+
let cpu_conv = Arg.conv (parse_cpu, Fmt.int) in
277+
Arg.(value @@ opt (some cpu_conv) None @@ info ["cpu"] ~docv:"CPU" ~doc)
278+
279+
let child_args =
280+
let doc = "The command to be executed and monitored." in
281+
Arg.(non_empty @@ pos_all string [] @@ info [] ~docv:"command" ~doc)
282+
283+
let cmdliner =
284+
let make freq cpu child_args = { freq; cpu; child_args } in
285+
make $$ freq $ cpu $ child_args

src/record.mli

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
type config = {
2+
freq : float;
3+
cpu : int option;
4+
child_args : string list;
5+
}
6+
17
val run :
28
?ui : (string -> (unit, string) result) ->
39
?tracefile:_ Eio.Path.t ->
410
proc_mgr:_ Eio.Process.mgr ->
511
fs:_ Eio.Path.t ->
6-
freq:float ->
7-
string list ->
12+
config ->
813
(unit, string) result
14+
15+
val cmdliner : config Cmdliner.Term.t

0 commit comments

Comments
 (0)