Skip to content

Commit e7077cf

Browse files
committed
Simplify the state machine
Signed-off-by: Etienne Millon <[email protected]>
1 parent aaa8058 commit e7077cf

File tree

3 files changed

+31
-35
lines changed

3 files changed

+31
-35
lines changed

src/catapult/catapult.ml

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ type ops =
77
; gc_stat : unit -> Gc.stat
88
}
99

10-
type state =
10+
type mode =
1111
| Disabled
12-
| Path of string
1312
| Using of ops
1413

1514
type t =
16-
{ mutable state : state
15+
{ mutable mode : mode
16+
; mutable after_first_event : bool
1717
}
1818

1919
let make () =
20-
{ state = Disabled
20+
{ mode = Disabled
21+
; after_first_event = false
2122
}
2223

2324
let fake_gc_stat =
@@ -44,43 +45,47 @@ let fake time_ref buf =
4445
let close () = () in
4546
let get_time () = !time_ref in
4647
let gc_stat () = fake_gc_stat in
47-
{ state =
48+
{ mode =
4849
Using
4950
{ print
5051
; close
5152
; get_time
5253
; gc_stat
5354
}
55+
; after_first_event = false
5456
}
5557

56-
let close t = match t.state with
58+
let close t = match t.mode with
5759
| Disabled -> ()
58-
| Path _ -> ()
5960
| Using {print; close; _} ->
6061
print "]\n";
6162
close ()
6263

63-
let enable t path =
64-
t.state <- Path path
65-
66-
let make_reporter path =
64+
let path_ops path =
6765
let channel = Pervasives.open_out path in
6866
let print s = Pervasives.output_string channel s in
6967
let close () = Pervasives.close_out channel in
7068
let get_time () = Unix.gettimeofday () in
7169
let gc_stat () = Gc.stat () in
7270
{print; close; get_time; gc_stat}
7371

72+
let enable t path =
73+
t.mode <- Using (path_ops path)
74+
75+
let next_leading_char t =
76+
match t.after_first_event with
77+
| true -> ','
78+
| false ->
79+
t.after_first_event <- true;
80+
'['
81+
7482
let printf t format_string =
75-
match t.state with
83+
match t.mode with
7684
| Disabled ->
7785
Printf.ifprintf () format_string
7886
| Using {print; _} ->
79-
Printf.ksprintf print ("," ^^ format_string ^^ "\n")
80-
| Path path ->
81-
let reporter = make_reporter path in
82-
t.state <- Using reporter;
83-
Printf.ksprintf reporter.print ("[" ^^ format_string ^^ "\n")
87+
let c = next_leading_char t in
88+
Printf.ksprintf print ("%c" ^^ format_string ^^ "\n") c
8489

8590
let color_of_name = function
8691
| "ocamlc" | "ocamlc.opt" -> "thread_state_uninterruptible"
@@ -144,20 +149,13 @@ let emit_counters t ~time (stat: Gc.stat) =
144149
emit_counter t ~time "free_words" stat.free_words;
145150
emit_counter t ~time "stack_size" stat.stack_size
146151

147-
let get_time t = match t.state with
148-
| Disabled
149-
| Path _
150-
-> 0.
151-
| Using {get_time; _}
152-
->
153-
get_time ()
152+
let get_time t = match t.mode with
153+
| Disabled -> 0.
154+
| Using {get_time; _} -> get_time ()
154155

155-
let gc_stat t = match t.state with
156-
| Disabled
157-
| Path _
158-
-> fake_gc_stat
159-
| Using {gc_stat; _} ->
160-
gc_stat ()
156+
let gc_stat t = match t.mode with
157+
| Disabled -> fake_gc_stat
158+
| Using {gc_stat; _} -> gc_stat ()
161159

162160
let on_process_start t ~program ~args =
163161
{ start_time = get_time t

src/catapult/catapult.mli

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ type t
1111
val make : unit -> t
1212

1313
(** Return a fake reporter that reads time in a reference and writes JSON
14-
objects to a buffer. Note that in this case, the output is not valid JSON
15-
since there is no leading '['. *)
14+
objects to a buffer. *)
1615
val fake : float ref -> Buffer.t -> t
1716

1817
(** Output trailing data to make the underlying file valid JSON, and close it. *)
1918
val close : t -> unit
2019

2120
(** Enable tracing: open a trace file and further events will be logged into it.
22-
The file is only created when the first event is logged. It is necessary to
23-
call [close] on the reporter to make the file valid. *)
21+
It is necessary to call [close] on the reporter to make the file valid. *)
2422
val enable : t -> string -> unit
2523

2624
type event

test/unit-tests/catapult.mlt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let buffer_lines () =
3737
buffer_lines ();;
3838
[%%expect{|
3939
- : string list =
40-
[",{\"name\": \"program\", \"pid\": 0, \"tid\": 0, \"ph\": \"X\", \"dur\": 20000000, \"ts\": 30000000, \"color\": \"generic_work\", \"args\": [\"arg1\",\"arg2\"]}";
40+
["[{\"name\": \"program\", \"pid\": 0, \"tid\": 0, \"ph\": \"X\", \"dur\": 20000000, \"ts\": 30000000, \"color\": \"generic_work\", \"args\": [\"arg1\",\"arg2\"]}";
4141
",{\"name\": \"live_words\", \"pid\": 0, \"tid\": 0, \"ph\": \"C\", \"ts\": 30000000, \"args\": {\"value\": 0}}";
4242
",{\"name\": \"free_words\", \"pid\": 0, \"tid\": 0, \"ph\": \"C\", \"ts\": 30000000, \"args\": {\"value\": 0}}";
4343
",{\"name\": \"stack_size\", \"pid\": 0, \"tid\": 0, \"ph\": \"C\", \"ts\": 30000000, \"args\": {\"value\": 0}}";

0 commit comments

Comments
 (0)