|
16 | 16 | val get_finished_timer: timer:string -> t -> (float * float) option |
17 | 17 | val to_json: t -> Hh_json.json |
18 | 18 | end = struct |
| 19 | + type time_measurement = { |
| 20 | + start_age: float; |
| 21 | + duration: float; |
| 22 | + } |
| 23 | + |
19 | 24 | type result = { |
20 | | - start_wall_age: float; |
21 | | - wall_duration: float; |
| 25 | + user: time_measurement; |
| 26 | + system: time_measurement; |
| 27 | + worker_user: time_measurement; |
| 28 | + worker_system: time_measurement; |
| 29 | + wall: time_measurement; |
| 30 | + } |
| 31 | + |
| 32 | + type running_timer = { |
| 33 | + user_start: float; |
| 34 | + system_start: float; |
| 35 | + worker_user_start: float; |
| 36 | + worker_system_start: float; |
| 37 | + wall_start: float; |
22 | 38 | } |
23 | 39 |
|
24 | 40 | type t = { |
25 | | - running_timers: float SMap.t; |
| 41 | + running_timers: running_timer SMap.t; |
26 | 42 | results: result SMap.t; |
27 | 43 | } |
28 | 44 |
|
29 | 45 | let empty = { running_timers = SMap.empty; results = SMap.empty } |
30 | 46 |
|
| 47 | + (* Returns the user cpu and system cpu times *) |
| 48 | + let times () = Unix.( |
| 49 | + let tm = times () in |
| 50 | + (* Warning - cutime and cstime (children times) don't work on Windows *) |
| 51 | + (tm.tms_utime +. tm.tms_cutime, tm.tms_stime +. tm.tms_cstime) |
| 52 | + ) |
| 53 | + |
| 54 | + let worker_times () = |
| 55 | + let worker_user_time = match Measure.get_sum "worker_user_time" with |
| 56 | + | None -> 0.0 |
| 57 | + | Some time -> time in |
| 58 | + let worker_system_time = match Measure.get_sum "worker_system_time" with |
| 59 | + | None -> 0.0 |
| 60 | + | Some time -> time in |
| 61 | + (worker_user_time, worker_system_time) |
| 62 | + |
31 | 63 | let start_timer ~timer { running_timers; results; } = |
| 64 | + let wall_start = Unix.gettimeofday () in |
| 65 | + let (user_start, system_start) = times () in |
| 66 | + let (worker_user_start, worker_system_start) = worker_times () in |
| 67 | + let running_timer = { |
| 68 | + user_start; |
| 69 | + system_start; |
| 70 | + worker_user_start; |
| 71 | + worker_system_start; |
| 72 | + wall_start; |
| 73 | + } in |
32 | 74 | { |
33 | | - running_timers = SMap.add timer (Unix.gettimeofday ()) running_timers; |
| 75 | + running_timers = SMap.add timer running_timer running_timers; |
34 | 76 | results; |
35 | 77 | } |
36 | 78 |
|
|
39 | 81 | let stop_timer ~timer { running_timers; results; } = |
40 | 82 | match SMap.get timer running_timers with |
41 | 83 | | None -> { running_timers; results; } |
42 | | - | Some timer_start_time -> |
| 84 | + | Some running_timer -> |
| 85 | + let wall_end = Unix.gettimeofday () in |
| 86 | + let (user_end, system_end) = times () in |
| 87 | + let (worker_user_end, worker_system_end) = worker_times () in |
43 | 88 | let result = { |
44 | | - start_wall_age = timer_start_time -. flow_start_time; |
45 | | - wall_duration = Unix.gettimeofday () -. timer_start_time; |
| 89 | + user = { |
| 90 | + start_age= running_timer.user_start; |
| 91 | + duration= user_end -. running_timer.user_start; |
| 92 | + }; |
| 93 | + system = { |
| 94 | + start_age= running_timer.system_start; |
| 95 | + duration= system_end -. running_timer.system_start; |
| 96 | + }; |
| 97 | + worker_user = { |
| 98 | + start_age= running_timer.worker_user_start; |
| 99 | + duration= worker_user_end -. running_timer.worker_user_start; |
| 100 | + }; |
| 101 | + worker_system = { |
| 102 | + start_age= running_timer.worker_system_start; |
| 103 | + duration= worker_system_end -. running_timer.worker_system_start; |
| 104 | + }; |
| 105 | + wall = { |
| 106 | + start_age = running_timer.wall_start -. flow_start_time; |
| 107 | + duration = wall_end -. running_timer.wall_start; |
| 108 | + } |
46 | 109 | } in |
47 | 110 | { |
48 | 111 | running_timers = SMap.remove timer running_timers; |
|
52 | 115 | let get_finished_timer ~timer { results; _; } = |
53 | 116 | match SMap.get timer results with |
54 | 117 | | None -> None |
55 | | - | Some { start_wall_age; wall_duration; } -> |
56 | | - Some (start_wall_age, wall_duration) |
| 118 | + | Some { wall = { start_age; duration; }; _; } -> |
| 119 | + Some (start_age, duration) |
| 120 | + |
| 121 | + let json_of_time_measurement { start_age; duration; } = |
| 122 | + let open Hh_json in |
| 123 | + let open Utils_js in |
| 124 | + JSON_Object [ |
| 125 | + "start_age", JSON_Number (string_of_float_trunc start_age); |
| 126 | + "duration", JSON_Number (string_of_float_trunc duration); |
| 127 | + ] |
57 | 128 |
|
58 | | - let json_of_result { start_wall_age; wall_duration; } = |
| 129 | + let json_of_result { wall; user; system; worker_user; worker_system; } = |
59 | 130 | let open Hh_json in |
60 | 131 | let open Utils_js in |
61 | 132 | JSON_Object [ |
62 | | - "start_wall_age", JSON_Number (string_of_float_trunc start_wall_age); |
63 | | - "wall_duration", JSON_Number (string_of_float_trunc wall_duration); |
| 133 | + "wall", json_of_time_measurement wall; |
| 134 | + "user", json_of_time_measurement user; |
| 135 | + "system", json_of_time_measurement system; |
| 136 | + "worker_user", json_of_time_measurement worker_user; |
| 137 | + "worker_system", json_of_time_measurement worker_system; |
| 138 | + (* legacy fields *) |
| 139 | + "start_wall_age", JSON_Number (string_of_float_trunc wall.start_age); |
| 140 | + "wall_duration", JSON_Number (string_of_float_trunc wall.duration); |
64 | 141 | ] |
65 | 142 |
|
66 | 143 | let to_json { results; _; } = |
|
0 commit comments