|
| 1 | +(* |
| 2 | + Copyright 2008-2025 Nikhil Swamy and Microsoft Research |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*) |
| 16 | +module FStarC.Stats |
| 17 | + |
| 18 | +open FStarC.Effect |
| 19 | +open FStarC.Class.Monoid |
| 20 | + |
| 21 | +let enabled = alloc false |
| 22 | +let ever_enabled = alloc false |
| 23 | + |
| 24 | +type stat = { |
| 25 | + ns_tree : int; |
| 26 | + ns_exn : int; |
| 27 | + ns_sub : int; |
| 28 | + ncalls : int; |
| 29 | +} |
| 30 | + |
| 31 | +instance _ : monoid stat = { |
| 32 | + mzero = { |
| 33 | + ns_tree = 0; |
| 34 | + ns_exn = 0; |
| 35 | + ns_sub = 0; |
| 36 | + ncalls = 0; |
| 37 | + }; |
| 38 | + mplus = (fun s1 s2 -> |
| 39 | + { |
| 40 | + ns_tree = s1.ns_tree + s2.ns_tree; |
| 41 | + ns_exn = s1.ns_exn + s2.ns_exn; |
| 42 | + ns_sub = s1.ns_sub + s2.ns_sub; |
| 43 | + ncalls = s1.ncalls + s2.ncalls; |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +(* the ref bool marks whether a given key is currently |
| 48 | + being recorded. This is so we avoid double counting |
| 49 | + the time taken by reentrant calls. *) |
| 50 | +let st : SMap.t (ref bool & stat) = SMap.create 10 |
| 51 | + |
| 52 | +(* Current stats we are logging. This is used to distinguish |
| 53 | + "tree" time (all the time taken by some call) |
| 54 | + vs "point" time (time taken by some call, subtracting |
| 55 | + the time in subcalls, if any). *) |
| 56 | +let stack : ref (list string) = mk_ref [] |
| 57 | + |
| 58 | +let r_running (k : string) : ref bool = |
| 59 | + match SMap.try_find st k with |
| 60 | + | None -> |
| 61 | + let r = alloc false in |
| 62 | + SMap.add st k (r, mzero); |
| 63 | + r |
| 64 | + | Some (r, _) -> |
| 65 | + r |
| 66 | + |
| 67 | +let add (k : string) (s1 : stat) : unit = |
| 68 | + let (r, s0) = |
| 69 | + match SMap.try_find st k with |
| 70 | + | None -> (alloc false, mzero) |
| 71 | + | Some rs -> rs |
| 72 | + in |
| 73 | + SMap.add st k (r, mplus s0 s1) |
| 74 | + |
| 75 | +let do_record |
| 76 | + (key : string) |
| 77 | + (f : unit -> 'a) |
| 78 | + : 'a |
| 79 | += |
| 80 | + stack := key :: !stack; |
| 81 | + let running = r_running key in |
| 82 | + let was_running = !running in |
| 83 | + running := true; |
| 84 | + let t0 = Timing.now_ns () in |
| 85 | + let resexn = |
| 86 | + try Inr (f ()) |
| 87 | + with | e -> Inl e |
| 88 | + in |
| 89 | + running := was_running; |
| 90 | + let t1 = Timing.now_ns () in |
| 91 | + let ns = Timing.diff_ns t0 t1 in |
| 92 | + stack := List.tl !stack; |
| 93 | + if not was_running then ( |
| 94 | + add key { mzero with ns_tree = ns }; |
| 95 | + (* Take time out of the parent, if any. *) |
| 96 | + begin match !stack with |
| 97 | + | [] -> () |
| 98 | + | k_par::_ -> add k_par { mzero with ns_sub = ns } |
| 99 | + end |
| 100 | + ); |
| 101 | + add key { mzero with ncalls = 1 }; |
| 102 | + match resexn with |
| 103 | + | Inr r -> |
| 104 | + r |
| 105 | + | Inl e -> |
| 106 | + add key { mzero with ns_exn = ns }; |
| 107 | + raise e |
| 108 | + |
| 109 | +let record key f = |
| 110 | + if !enabled then |
| 111 | + do_record key f |
| 112 | + else |
| 113 | + f () |
| 114 | + |
| 115 | +let lpad (len:int) (s:string) : string = |
| 116 | + let l = String.length s in |
| 117 | + if l >= len then s else String.make (len - l) ' ' ^ s |
| 118 | +
|
| 119 | +let max x y = |
| 120 | + if x > y then x else y |
| 121 | +
|
| 122 | +let print_all () : string = |
| 123 | + let keys = SMap.keys st in |
| 124 | + let points = List.map (fun k -> k, snd <| Some?.v <| SMap.try_find st k) keys in |
| 125 | + (* Sort by (point) time. *) |
| 126 | + let points = |
| 127 | + points |> |
| 128 | + Class.Ord.sort_by (fun (_, s1) (_, s2) -> |
| 129 | + (s2.ns_tree - s2.ns_sub) `Class.Ord.cmp` (s1.ns_tree - s1.ns_sub)) |
| 130 | + in |
| 131 | + let longest_key = List.fold_left (fun acc (k, _) -> max acc (String.length k)) 20 points in |
| 132 | + let pr1 (p : (string & stat)) : string = |
| 133 | + let k, st = p in |
| 134 | + Util.format5 " %s %s %s ms %s ms %s ms" |
| 135 | + (lpad longest_key k) |
| 136 | + (lpad 8 (string_of_int st.ncalls)) |
| 137 | + (lpad 6 (string_of_int (st.ns_tree / 1000000))) |
| 138 | + (lpad 6 (string_of_int ((st.ns_tree - st.ns_sub) / 1000000))) |
| 139 | + (lpad 6 (string_of_int (st.ns_exn / 1000000))) |
| 140 | + in |
| 141 | + Util.format5 " %s %s %s %s %s" (lpad longest_key "key") (lpad 8 "calls") (lpad 9 "tree") (lpad 9 "point") (lpad 9 "exn") ^ "\n" ^ |
| 142 | + (points |> List.map pr1 |> String.concat "\n") |
0 commit comments