Source file offline.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
open Core
open Core_profiler_disabled

module Profiler = struct
  let is_enabled = true

  let safe_to_delay () =
    Common.maybe_do_slow_tasks Common.Offline_profiler ~reluctance:1;
    Protocol.Buffer.ensure_free 2048

  let dump_stats () =
    Protocol.Writer.dump_stats ()

  let configure
    ?don't_require_core_profiler_env
    ?offline_profiler_data_file
    ?online_print_time_interval_secs:_
    ?online_print_by_default:_
    () =
    Option.iter don't_require_core_profiler_env ~f:(fun () ->
      Check_environment.don't_require_core_profiler_env ());
    Option.iter offline_profiler_data_file ~f:(fun file ->
      Protocol.set_current_output_filename file);
  ;;
end

module Timer = struct
  type timer = Probe_id.t
  type t = timer
  type probe = t

  let create ~name =
    Check_environment.check_safety_exn ();
    let id = Probe_id.create () in
    Protocol.Writer.write_new_single id name Probe_type.Timer;
    id

  let record id =
    let n = Common.now Common.Offline_profiler ~reluctance:3 () in
    Protocol.Writer.write_timer_at id n

  module Group = struct
    type t = Probe_id.t

    let create ~name =
      Check_environment.check_safety_exn ();
      let id = Probe_id.create () in
      Protocol.Writer.write_new_group id name Probe_type.Timer;
      id

    let add_probe group ?(sources=[||]) ~name () =
      let id = Probe_id.create () in
      (* Note! sources : Point.t array = Point_id.t array *)
      Protocol.Writer.write_new_group_point ~group_id:group ~id name sources;
      id

    let reset group =
      let n = Common.now Common.Offline_profiler ~reluctance:2 () in
      Protocol.Writer.write_group_reset group n
  end
end

let%bench_module "Timer" = (module struct
  let () = Profiler.configure ()
             ~don't_require_core_profiler_env:()

  let timer = Timer.create ~name:"bench_timer"

  (* let group = Timer.Group.create "bench_timer_group" ()
   * let group_probe = Timer.Group.add_probe group "bench_timer_group_probe" *)

  let%bench "at" = Timer.record timer

  (* BENCH "group_probe_at" = Timer.Group.Probe.at group_probe
   *
   * BENCH "group_reset" = Timer.Group.reset group *)

  let () = Protocol.Writer.set_at_exit_handler `Disable
end)


module Probe = struct
  type probe = Probe_id.t
  type t = probe

  let create ~name ~units =
    Check_environment.check_safety_exn ();
    let id = Probe_id.create () in
    Protocol.Writer.write_new_single id name (Probe_type.Probe units);
    id

  let record id value =
    let n = Common.now Common.Offline_profiler ~reluctance:3 () in
    Protocol.Writer.write_probe_at id n value

  module Group = struct
    type t = Probe_id.t

    let create ~name ~units =
      Check_environment.check_safety_exn ();
      let id = Probe_id.create () in
      Protocol.Writer.write_new_group id name (Probe_type.Probe units);
      id

    let add_probe group ?(sources=[||]) ~name () =
      let id = Probe_id.create () in
      Protocol.Writer.write_new_group_point ~group_id:group ~id name sources;
      id

    let reset group =
      let n = Common.now Common.Offline_profiler ~reluctance:2 () in
      Protocol.Writer.write_group_reset group n
  end
end

let%bench_module "Probe" = (module struct
  let () = Profiler.configure ()
             ~don't_require_core_profiler_env:()

  let timer = Probe.create ~name:"bench_probe" ~units:Profiler_units.Seconds

  (* let group = Probe.Group.create "bench_probe_group" Profiler_units.Int
   * let group_probe = Probe.Group.add_probe group "bench_probe_group_probe" *)

  let%bench "at" = Probe.record timer 19827312

  (* BENCH "group_probe_at" = Probe.Group.Probe.at group_probe 123812
   *
   * BENCH "group_reset" = Probe.Group.reset group *)

  let () = Protocol.Writer.set_at_exit_handler `Disable
end)

module Delta_timer = struct
  type state = Time_ns.t
  type t =
    { probe : Probe.t
    ; mutable state : state
    ; mutable accum : int
    }

  let create ~name =
    { probe = Probe.create ~name ~units:Profiler_units.Nanoseconds
    ; state = Time_ns.epoch
    ; accum = 0
    }

  let diff n state =
    Time_ns.diff n state
    |> Time_ns.Span.to_int_ns

  (* If we calibrate on start, we get back the time before we started calibrating,
     and those 300ns will be included in the delta. *)
  let stateless_start _t = Common.now Common.Offline_profiler ~reluctance:4 ()
  let stateless_stop t state =
    (* Avoid calling Common.now () twice: *)
    let n = Common.now Common.Offline_profiler ~reluctance:2 () in
    let d = diff n state in
    Protocol.Writer.write_probe_at t.probe n d

  let start t =
    let n = Common.now Common.Offline_profiler ~reluctance:4 () in
    t.state <- n

  let accumulate t n =
    t.accum <- t.accum + (diff n t.state);
    t.state <- n

  let write_probe_at t n =
    Protocol.Writer.write_probe_at t.probe n t.accum;
    t.accum <- 0;
    t.state <- n

  let pause t =
    let n = Common.now Common.Offline_profiler ~reluctance:4 () in
    accumulate t n

  let record t =
    let n = Common.now Common.Offline_profiler ~reluctance:2 () in
    write_probe_at t n

  let stop t =
    let n = Common.now Common.Offline_profiler ~reluctance:2 () in
    accumulate t n;
    write_probe_at t n

  let wrap_sync t f x =
    let state = stateless_start t in
    let r =
      try
        f x
      with ex ->
        stateless_stop t state;
        Exn.reraise ex "Core_profiler Delta_timer.wrap_sync"
    in
    stateless_stop t state;
    r

  let wrap_sync2 t f x y =
    let state = stateless_start t in
    let r =
      try
        f x y
      with ex ->
        stateless_stop t state;
        Exn.reraise ex "Core_profiler Delta_timer.wrap_sync2"
    in
    stateless_stop t state;
    r

  let wrap_sync3 t f x y z =
    let state = stateless_start t in
    let r =
      try
        f x y z
      with ex ->
        stateless_stop t state;
        Exn.reraise ex "Core_profiler Delta_timer.wrap_sync3"
    in
    stateless_stop t state;
    r

  let wrap_sync4 t f x y z w =
    let state = stateless_start t in
    let r =
      try
        f x y z w
      with ex ->
        stateless_stop t state;
        Exn.reraise ex "Core_profiler Delta_timer.wrap_sync4"
    in
    stateless_stop t state;
    r

  (* let wrap_async t f x =
   *   let open Async in
   *   let state = start_async t in
   *   try_with ~run:`Now (fun () -> f x) >>= fun res ->
   *   stop_async t state;
   *   match res with
   *   | Ok x -> return x
   *   | Error ex -> Exn.reraise ex "Core_profiler Delta_timer.wrap_async" *)
end

let%bench_module "Delta_timer" = (module struct
  let () = Profiler.configure ()
             ~don't_require_core_profiler_env:()

  let delta = Delta_timer.create ~name:"unittest"
  let started = Delta_timer.stateless_start delta

  let%bench "start_async" = Delta_timer.stateless_start delta
  let%bench "stop_async" = Delta_timer.stateless_stop delta started
  let%bench "start" = Delta_timer.start delta
  let%bench "stop" = Delta_timer.stop delta
end)

let%bench_module "Delta_timer.wrap_sync" = (module struct
  let () = Profiler.configure ()
             ~don't_require_core_profiler_env:()

  let nop () = ()

  let wrapped_nop =
    let delta = Delta_timer.create ~name:"nop" in
    Delta_timer.wrap_sync delta nop

  let count_256 () =
    for _ = 1 to 256 do
      ()
    done

  let wrapped_count_256 =
    let delta = Delta_timer.create ~name:"count_256" in
    Delta_timer.wrap_sync delta count_256

  let%bench "nop" = nop ()
  let%bench "wrapped_nop" = wrapped_nop ()
  let%bench "count_256" = count_256 ()
  let%bench "wrapped_count_256" = wrapped_count_256 ()
end)

(* stateless Delta_probe does not support pausing *)
module Delta_probe = struct
  type state = int
  type t =
    { probe : Probe.t
    ; mutable state : state
    ; mutable accum : state
    }

  let create ~name ~units =
    { probe = Probe.create ~name ~units
    ; state = 0
    ; accum = 0
    }

  let stateless_start _t value = value
  let stateless_stop t state value = Probe.record t.probe (value - state)

  let start t value =
    t.state <- value
  let record t =
    Probe.record t.probe t.accum;
    t.accum <- 0
  let pause t value =
    t.accum <- t.accum + (value - t.state)
  let stop t value =
    pause t value;
    record t;

end

let%bench_module "Delta_probe" = (module struct
  let () = Profiler.configure ()
             ~don't_require_core_profiler_env:()

  let delta = Delta_probe.create ~name:"unittest" ~units:Profiler_units.Int
  let started = Delta_probe.stateless_start delta 123

  let%bench "start" = Delta_probe.start delta 123
  let%bench "stop" = Delta_probe.stop delta 456
  let%bench "start_async" = Delta_probe.stateless_start delta 123
  let%bench "stop_async" = Delta_probe.stateless_stop delta started 456
end)