Summary: Generate output on stderr containing lines such as: ``` solve_for_vars time: 0.105 ms 0.871 ms 79 calls ``` which indicates that the current maximal time of a single `solve_for_vars` query is `0.105 ms` and so far the total time spent in `solve_for_vars` over `79` queries is `0.871 ms`. At program exit, there is also a line for each timer indicating the max query time and final accumulated duration and number of queries: ``` solve_for_vars time: 0.173 ms 17.242 ms 1108 calls ``` Additionally, replay sexp are dumped interactively. A query exceeding 1 sec is dumped, then one exceeding 2 sec, then 4 sec, etc. Reviewed By: jvillard Differential Revision: D20852160 fbshipit-source-id: 0a316891emaster
parent
a4e523b5b6
commit
c8e75e3b82
@ -0,0 +1,68 @@
|
||||
(*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*)
|
||||
|
||||
(** Timers for runtime statistics *)
|
||||
|
||||
type t =
|
||||
{ mutable ustart: float
|
||||
; mutable sstart: float
|
||||
; mutable uaggregate: float
|
||||
; mutable saggregate: float
|
||||
; mutable count: int
|
||||
; mutable max: float
|
||||
; mutable threshold: float
|
||||
; name: string }
|
||||
|
||||
let enabled = ref false
|
||||
|
||||
let start t =
|
||||
if !enabled then (
|
||||
let {Unix.tms_utime; tms_stime} = Unix.times () in
|
||||
t.ustart <- tms_utime ;
|
||||
t.sstart <- tms_stime )
|
||||
|
||||
let stop_ t =
|
||||
let {Unix.tms_utime; tms_stime} = Unix.times () in
|
||||
let ud = tms_utime -. t.ustart in
|
||||
let sd = tms_stime -. t.sstart in
|
||||
t.uaggregate <- t.uaggregate +. ud ;
|
||||
t.saggregate <- t.saggregate +. sd ;
|
||||
let usd = ud +. sd in
|
||||
if Float.(t.max < usd) then t.max <- usd ;
|
||||
t.count <- t.count + 1 ;
|
||||
(tms_utime, tms_stime)
|
||||
|
||||
let stop t = if !enabled then stop_ t |> (ignore : float * float -> unit)
|
||||
|
||||
let stop_report t report =
|
||||
if !enabled then
|
||||
let tms_utime, tms_stime = stop_ t in
|
||||
let elapsed = tms_utime +. tms_stime -. (t.ustart +. t.sstart) in
|
||||
if Float.(elapsed > t.threshold) then (
|
||||
t.threshold <- elapsed ;
|
||||
report ~name:t.name ~elapsed:(elapsed *. 1000.)
|
||||
~aggregate:((t.uaggregate +. t.saggregate) *. 1000.)
|
||||
~count:t.count )
|
||||
|
||||
let create ?at_exit:printf name =
|
||||
let t =
|
||||
{ ustart= 0.
|
||||
; uaggregate= 0.
|
||||
; sstart= 0.
|
||||
; saggregate= 0.
|
||||
; count= 0
|
||||
; max= 0.
|
||||
; threshold= 0.
|
||||
; name }
|
||||
in
|
||||
Option.iter printf ~f:(fun report ->
|
||||
at_exit (fun () ->
|
||||
if !enabled then
|
||||
report ~name:t.name ~elapsed:(t.max *. 1000.)
|
||||
~aggregate:((t.uaggregate +. t.saggregate) *. 1000.)
|
||||
~count:t.count ) ) ;
|
||||
t
|
@ -0,0 +1,53 @@
|
||||
(*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*)
|
||||
|
||||
(** Timers for runtime statistics *)
|
||||
|
||||
type t = private
|
||||
{ mutable ustart: float
|
||||
; mutable sstart: float
|
||||
; mutable uaggregate: float
|
||||
; mutable saggregate: float
|
||||
; mutable count: int
|
||||
; mutable max: float
|
||||
; mutable threshold: float
|
||||
; name: string }
|
||||
|
||||
val create :
|
||||
?at_exit:
|
||||
( name:string
|
||||
-> elapsed:float
|
||||
-> aggregate:float
|
||||
-> count:int
|
||||
-> unit)
|
||||
-> string
|
||||
-> t
|
||||
(** Construct a timer with the given name and register the given function to
|
||||
run at exit. The [at_exit] function receives [name]: the name of the
|
||||
timer passed to [create], [elapsed]: the number of milliseconds between
|
||||
the longest single [start]-[stop] pair, [aggregate]: the sum of the time
|
||||
that elapsed while the named timer was running, [count]: the number of
|
||||
times [stop] was called on the timer. *)
|
||||
|
||||
val start : t -> unit
|
||||
(** Start a timer. *)
|
||||
|
||||
val stop : t -> unit
|
||||
(** Stop a timer. *)
|
||||
|
||||
val stop_report :
|
||||
t
|
||||
-> (name:string -> elapsed:float -> aggregate:float -> count:int -> unit)
|
||||
-> unit
|
||||
(** Stop a timer and report using the given function, which receives [name]:
|
||||
the name of the timer passed to [create], [elapsed]: the number of
|
||||
milliseconds since [start] was called, [aggregate]: the sum of the time
|
||||
that has elapsed while the timer was running, [count]: the number of
|
||||
times [stop] has been called on the timer. *)
|
||||
|
||||
val enabled : bool ref
|
||||
(** Timers do nothing unless [enabled] is set. *)
|
Loading…
Reference in new issue