11module B = Fxt_buf
22
3- type thread = {
4- pid : int64 ;
5- tid : int64 ;
6- }
3+ type thread = { pid : int64 ; tid : int64 }
74
85type strings = {
96 mutable str_next : int ;
@@ -17,12 +14,7 @@ type threads = {
1714 thr_index : (int64 * int64 , int ) Hashtbl .t ;
1815}
1916
20- type t = {
21- oc : out_channel ;
22- buf : B .t ;
23- strings : strings ;
24- threads : threads ;
25- }
17+ type t = { oc : out_channel ; buf : B .t ; strings : strings ; threads : threads }
2618
2719(* Number of 8-byte words for a padded string *)
2820let [@ inline] str_word_len s = (String. length s + 7 ) asr 3
@@ -34,12 +26,12 @@ type str_ref = Str_ref of int | Str_inline of string
3426let str_ref_lookup strings s =
3527 match s with
3628 | "" -> Str_ref 0
37- | _ ->
38- ( match Hashtbl. find_opt strings.str_index s with
39- | Some i -> Str_ref i
40- | None ->
41- if String. length s > 32000 then invalid_arg " FXT: string too long" ;
42- Str_inline s)
29+ | _ -> (
30+ match Hashtbl. find_opt strings.str_index s with
31+ | Some i -> Str_ref i
32+ | None ->
33+ if String. length s > 32000 then invalid_arg " FXT: string too long" ;
34+ Str_inline s)
4335
4436let [@ inline] str_ref_encode = function
4537 | Str_ref x -> x
@@ -57,7 +49,7 @@ let str_ref_add t s =
5749 if s <> " " && not (Hashtbl. mem t.strings.str_index s) then begin
5850 let st = t.strings in
5951 let i = st.str_next in
60- st.str_next < - if i = 0x7fff then 1 else i + 1 ;
52+ st.str_next < - ( if i = 0x7fff then 1 else i + 1 ) ;
6153 Option. iter (Hashtbl. remove st.str_index) st.str_table.(i);
6254 st.str_table.(i) < - Some s;
6355 Hashtbl. replace st.str_index s i;
@@ -67,8 +59,9 @@ let str_ref_add t s =
6759 Int64. (logor (of_int i) (shift_left (of_int (String. length s)) 16 ))
6860 in
6961 B. put_64 t.buf
70- Int64. (logor (of_int 2 ) (logor (shift_left (of_int words) 4 )
71- (shift_left data 16 )));
62+ Int64. (
63+ logor (of_int 2 )
64+ (logor (shift_left (of_int words) 4 ) (shift_left data 16 )));
7265 B. put_string_padded t.buf s
7366 end
7467
@@ -81,33 +74,28 @@ let thr_ref_lookup threads v =
8174 | Some i -> Thr_ref i
8275 | None -> Thr_inline { pid = v.pid; tid = v.tid }
8376
84- let [@ inline] thr_ref_encode = function
85- | Thr_ref x -> x
86- | Thr_inline _ -> 0
87-
88- let [@ inline] thr_ref_size = function
89- | Thr_ref _ -> 0
90- | Thr_inline _ -> 2
77+ let [@ inline] thr_ref_encode = function Thr_ref x -> x | Thr_inline _ -> 0
78+ let [@ inline] thr_ref_size = function Thr_ref _ -> 0 | Thr_inline _ -> 2
9179
9280let thr_ref_write buf = function
9381 | Thr_ref _ -> ()
9482 | Thr_inline { pid; tid } ->
95- B. put_64 buf pid;
96- B. put_64 buf tid
83+ B. put_64 buf pid;
84+ B. put_64 buf tid
9785
9886let thr_ref_add t v =
9987 let key = (v.pid, v.tid) in
10088 if not (Hashtbl. mem t.threads.thr_index key) then begin
10189 let th = t.threads in
10290 let i = th.thr_next in
103- th.thr_next < - if i = 0xff then 1 else i + 1 ;
91+ th.thr_next < - ( if i = 0xff then 1 else i + 1 ) ;
10492 Option. iter (Hashtbl. remove th.thr_index) th.thr_table.(i);
10593 th.thr_table.(i) < - Some key;
10694 Hashtbl. replace th.thr_index key i;
10795 B. put_64 t.buf
108- Int64. (logor (of_int 3 )
109- ( logor (shift_left ( of_int 3 ) 4 )
110- (shift_left (of_int i) 16 )));
96+ Int64. (
97+ logor (of_int 3 )
98+ (logor (shift_left (of_int 3 ) 4 ) (shift_left (of_int i) 16 )));
11199 B. put_64 t.buf v.pid;
112100 B. put_64 t.buf v.tid
113101 end
@@ -118,21 +106,24 @@ let buf_size = 65536
118106let max_event_bytes = 33000
119107
120108let ensure_space t =
121- if B. available t.buf < max_event_bytes then
122- B. flush t.buf t.oc
109+ if B. available t.buf < max_event_bytes then B. flush t.buf t.oc
123110
124111let create oc =
125112 let buf = B. create buf_size in
126- let strings = {
127- str_next = 1 ;
128- str_table = Array. make 0x8000 None ;
129- str_index = Hashtbl. create 200 ;
130- } in
131- let threads = {
132- thr_next = 1 ;
133- thr_table = Array. make 0x100 None ;
134- thr_index = Hashtbl. create 20 ;
135- } in
113+ let strings =
114+ {
115+ str_next = 1 ;
116+ str_table = Array. make 0x8000 None ;
117+ str_index = Hashtbl. create 200 ;
118+ }
119+ in
120+ let threads =
121+ {
122+ thr_next = 1 ;
123+ thr_table = Array. make 0x100 None ;
124+ thr_index = Hashtbl. create 20 ;
125+ }
126+ in
136127 let t = { oc; buf; strings; threads } in
137128 (* Magic record *)
138129 B. put_64 buf 0x0016547846040010L ;
@@ -148,10 +139,10 @@ let close t =
148139let [@ inline] is_i32 (i : int ) : bool = Int32. (to_int (of_int i) = i)
149140
150141(* Write an event header via C stub (zero Int64 boxing), then advance pos *)
151- let [@ inline] write_event_header buf ~words ~event_ty ~n_args ~thread_ref ~name_ref =
142+ let [@ inline] write_event_header buf ~words ~event_ty ~n_args ~thread_ref
143+ ~name_ref =
152144 let pos = B. pos buf in
153- B. put_event_header (B. bytes buf) pos
154- words event_ty n_args thread_ref name_ref;
145+ B. put_event_header (B. bytes buf) pos words event_ty n_args thread_ref name_ref;
155146 B. advance buf 8
156147
157148(* Emit a span or instant event. Zero-allocation when name and thread are
@@ -176,8 +167,7 @@ let duration_begin t ~thread ~name ~ts =
176167let duration_end t ~thread ~name ~ts =
177168 emit_event t ~event_ty: 3 ~thread ~name ~ts
178169
179- let instant t ~thread ~name ~ts =
180- emit_event t ~event_ty: 0 ~thread ~name ~ts
170+ let instant t ~thread ~name ~ts = emit_event t ~event_ty: 0 ~thread ~name ~ts
181171
182172(* Counter events include one int argument named "v" and a counter_id word. *)
183173let counter t ~thread ~name ~ts ~value =
@@ -190,8 +180,8 @@ let counter t ~thread ~name ~ts ~value =
190180 let thread_ref = thr_ref_lookup t.threads thread in
191181 let arg_words = (if is_i32 value then 1 else 2 ) + str_ref_words v_ref in
192182 let words =
193- 2 + thr_ref_size thread_ref + str_ref_words name_ref
194- + arg_words + 1 (* counter_id *)
183+ 2 + thr_ref_size thread_ref + str_ref_words name_ref + arg_words
184+ + 1 (* counter_id *)
195185 in
196186 write_event_header t.buf ~words ~event_ty: 1 ~n_args: 1
197187 ~thread_ref: (thr_ref_encode thread_ref)
@@ -206,7 +196,8 @@ let counter t ~thread ~name ~ts ~value =
206196 B. put_arg_header_i32 (B. bytes t.buf) arg_pos arg_words v_enc value;
207197 B. advance t.buf 8 ;
208198 str_ref_write t.buf v_ref
209- end else begin
199+ end
200+ else begin
210201 B. put_arg_header_i64 (B. bytes t.buf) arg_pos arg_words v_enc;
211202 B. advance t.buf 8 ;
212203 str_ref_write t.buf v_ref;
0 commit comments