From 1b0d47a01dd24b670be9d8f8ef38d3bed2b7867e Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 18 Aug 2022 20:56:17 -0400 Subject: [PATCH] feat(profile): add basic counters --- src/tef/Sidekick_tef.real.ml | 15 +++++++++++++++ src/util/Profile.ml | 10 ++++++++++ src/util/Profile.mli | 2 ++ 3 files changed, 27 insertions(+) diff --git a/src/tef/Sidekick_tef.real.ml b/src/tef/Sidekick_tef.real.ml index 0285da98..60b4193a 100644 --- a/src/tef/Sidekick_tef.real.ml +++ b/src/tef/Sidekick_tef.real.ml @@ -59,6 +59,21 @@ module Make () : P.BACKEND = struct pid tid ts name; () + let emit_count_event ~name ~ts (cs : _ list) : unit = + let pid = Unix.getpid () in + let tid = Thread.id (Thread.self ()) in + emit_sep_ (); + Printf.fprintf oc + {json|{"pid": %d,"cat":"","tid": %d,"ts": %.2f,"name":"%s","ph":"C","args":{|json} + pid tid ts name; + List.iteri + (fun i (n, value) -> + if i > 0 then Printf.fprintf oc ","; + Printf.fprintf oc {json|"%s":%d|json} n value) + cs; + Printf.fprintf oc {json|}}|json}; + () + let teardown () = teardown_ oc end diff --git a/src/util/Profile.ml b/src/util/Profile.ml index 7c55409f..6124726d 100644 --- a/src/util/Profile.ml +++ b/src/util/Profile.ml @@ -5,6 +5,7 @@ module type BACKEND = sig name:string -> start:float -> end_:float -> unit -> unit val emit_instant_event : name:string -> ts:float -> unit -> unit + val emit_count_event : name:string -> ts:float -> (string * int) list -> unit val teardown : unit -> unit end @@ -36,6 +37,15 @@ let[@inline] instant name = let now = B.get_ts () in B.emit_instant_event ~name ~ts:now () +let[@inline] count name cs = + if cs <> [] then ( + match !out_ with + | None -> () + | Some (module B) -> + let now = B.get_ts () in + B.emit_count_event ~name ~ts:now cs + ) + (* slow path *) let[@inline never] exit_full_ (module B : BACKEND) name start = let now = B.get_ts () in diff --git a/src/util/Profile.mli b/src/util/Profile.mli index e1e0e054..11603d9d 100644 --- a/src/util/Profile.mli +++ b/src/util/Profile.mli @@ -14,6 +14,7 @@ val exit : probe -> unit val with_ : string -> (unit -> 'a) -> 'a val with1 : string -> ('a -> 'b) -> 'a -> 'b val with2 : string -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c +val count : string -> (string * int) list -> unit module type BACKEND = sig val get_ts : unit -> float @@ -22,6 +23,7 @@ module type BACKEND = sig name:string -> start:float -> end_:float -> unit -> unit val emit_instant_event : name:string -> ts:float -> unit -> unit + val emit_count_event : name:string -> ts:float -> (string * int) list -> unit val teardown : unit -> unit end