package core_profiler

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file header_protocol.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
(** This file is automatically generated by a target in the build system.
    Do not modify it by hand.  *)

open! Core
module Time_ns = Time_ns_unix

let module_name = __MODULE__
let padding = ' '
let _ = padding

type ('ty, -'rw) t = ('rw, Iobuf.no_seek) Iobuf.t constraint 'rw = [> read ]
type ('ty, 'rw) message = ('ty, 'rw) t
type ('ty, 'rw) t_no_exn = ('ty, 'rw) t

module R = struct
  type 'message t =
    | Need_more_data
    | Ok of 'message * int
    | Junk of exn * (int, exn) Result.t
  [@@deriving sexp_of]
end

module Message_type_and_errors = struct
  type _ t =
    | New_single : [ `New_single ] t
    | New_group : [ `New_group ] t
    | New_group_point : [ `New_group_point ] t
    | End_of_header : [ `End_of_header ] t
    | Epoch : [ `Epoch ] t
    | Need_more_data : [ `Error ] t
    | Invalid_message_type_or_subtype : [ `Error ] t
    | Message_length_too_short : [ `Error ] t
  [@@deriving sexp_of]

  type packed = T : _ t -> packed [@@deriving sexp_of] [@@unboxed]

  let to_wire_exn : type ty. ty t -> char = function
    | Epoch -> 'E'
    | New_single -> 'N'
    | New_group_point -> 'O'
    | New_group -> 'P'
    | End_of_header -> 'Z'
    | Need_more_data -> invalid_arg "to_wire_exn: received Need_more_data"
    | Invalid_message_type_or_subtype ->
      invalid_arg "to_wire_exn: received Invalid_message_type_or_subtype"
    | Message_length_too_short ->
      invalid_arg "to_wire_exn: received Message_length_too_short"
  ;;

  let of_wire = function
    | 'E' -> T Epoch
    | 'N' -> T New_single
    | 'O' -> T New_group_point
    | 'P' -> T New_group
    | 'Z' -> T End_of_header
    | _ -> T Invalid_message_type_or_subtype
  ;;

  let to_index_exn : type ty. ty t -> int = function
    | New_single -> 0
    | New_group -> 1
    | New_group_point -> 2
    | End_of_header -> 3
    | Epoch -> 4
    | Need_more_data -> invalid_arg "to_index_exn: received Need_more_data"
    | Invalid_message_type_or_subtype ->
      invalid_arg "to_index_exn: received Invalid_message_type_or_subtype"
    | Message_length_too_short ->
      invalid_arg "to_index_exn: received Message_length_too_short"
  ;;

  let of_index_exn = function
    | 0 -> T New_single
    | 1 -> T New_group
    | 2 -> T New_group_point
    | 3 -> T End_of_header
    | 4 -> T Epoch
    | _ -> invalid_arg "of_index_exn: invalid index"
  ;;

  let max_index = 4
  let all_of_packed = List.init (max_index + 1) ~f:of_index_exn

  module Packed = struct
    module T = struct
      type 'ty message_type_and_errors = 'ty t [@@deriving sexp_of]

      type t = packed = T : _ message_type_and_errors -> t
      [@@deriving sexp_of] [@@unboxed]

      let to_index_exn (T t) = to_index_exn t
      let compare t1 t2 = Int.compare (to_index_exn t1) (to_index_exn t2)
      let hash t = Int.hash (to_index_exn t)
      let hash_fold_t state t = Int.hash_fold_t state (to_index_exn t)
      let t_of_sexp _ = failwith "unimplemented"
      let all = all_of_packed
    end

    include T
    include Comparable.Make (T)
    include Hashable.Make (T)
  end
end

let[@inline] get_message_type buf =
  let len = Iobuf.length buf in
  let pos = 0 in
  if len < 2
  then Message_type_and_errors.(T Need_more_data)
  else (
    let message_num_bytes = 0 + Iobuf.Unsafe.Peek.uint8 buf ~pos in
    if len < message_num_bytes
    then Message_type_and_errors.(T Need_more_data)
    else (
      match Iobuf.Unsafe.Peek.char buf ~pos:(pos + 1) with
      | 'E' ->
        if message_num_bytes < 10
        then T Message_length_too_short
        else Message_type_and_errors.(T Epoch)
      | 'N' ->
        if message_num_bytes < 69
        then T Message_length_too_short
        else Message_type_and_errors.(T New_single)
      | 'O' ->
        if message_num_bytes < 72
        || message_num_bytes
           <
           let pos = 0 in
           72 + (2 * Iobuf.Unsafe.Peek.uint16_le buf ~pos:(pos + 70))
        then T Message_length_too_short
        else Message_type_and_errors.(T New_group_point)
      | 'P' ->
        if message_num_bytes < 69
        then T Message_length_too_short
        else Message_type_and_errors.(T New_group)
      | 'Z' ->
        if message_num_bytes < 2
        then T Message_length_too_short
        else Message_type_and_errors.(T End_of_header)
      | other ->
        ignore other;
        Message_type_and_errors.(T Invalid_message_type_or_subtype)))
;;

let[@inline] of_iobuf buf ~trusted:_ = Iobuf.no_seek buf

let[@inline] of_iobuf_local (buf [@local]) ~trusted:_ =
   (Iobuf.no_seek_local buf)
;;

let[@inline] of_iobuf_exn buf ty =
  let (Message_type_and_errors.T mt) = get_message_type buf in
  if Message_type_and_errors.to_index_exn mt = Message_type_and_errors.to_index_exn ty
  then of_iobuf buf ~trusted:ty
  else
    failwiths
      ~here:[%here]
      "unexpected message type"
      mt
      [%sexp_of: _ Message_type_and_errors.t]
;;

let[@inline] of_iobuf_local_exn (buf [@local]) ty =
  
    (let (Message_type_and_errors.T mt) = get_message_type buf in
     if Message_type_and_errors.to_index_exn mt = Message_type_and_errors.to_index_exn ty
     then of_iobuf_local buf ~trusted:ty
     else
       failwiths
         ~here:[%here]
         "unexpected message type"
         mt
         [%sexp_of: _ Message_type_and_errors.t])
;;

module New_single = struct
  type phantom = [ `New_single ]
  type nonrec -'rw t = (phantom, 'rw) t

  let message_type = 'N'
  let buffer_length = 69
  let of_iobuf_exn buf = of_iobuf_exn buf Message_type_and_errors.New_single

  let of_iobuf_local_exn (buf [@local]) =
     (of_iobuf_local_exn buf Message_type_and_errors.New_single)
  ;;

  let write ~id ~spec ~name buf =
    let pos = 0 in
    assert (Iobuf.length buf >= 69);
    Iobuf.Unsafe.Poke.char buf ~pos:(pos + 1) message_type;
    (let int_to_pack = Probe_id.to_int_exn id in
     Iobuf.Unsafe.Poke.uint16_le_trunc buf ~pos:(pos + 2) int_to_pack);
    Iobuf.Unsafe.Poke.char buf ~pos:(pos + 4) (Probe_type.to_char spec);
    Iobuf.Unsafe.Poke.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 5) name;
    let total_bytes_packed = 69 in
    (let int_to_pack = total_bytes_packed - 0 in
     Iobuf.Unsafe.Poke.uint8_trunc buf ~pos int_to_pack);
    total_bytes_packed
  ;;

  let create ~id ~spec ~name =
    let iobuf = Iobuf.create ~len:69 in
    let size = write ~id ~spec ~name iobuf in
    assert (Iobuf.length iobuf = size);
    iobuf
  ;;

  let[@inline] get_message_length (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.uint8 buf ~pos
  ;;

  let[@inline] get_message_type (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.char buf ~pos:(pos + 1)
  ;;

  let[@inline] get_id (buf [@local]) =
    let pos = 0 in
    Probe_id.of_int_exn (Iobuf.Unsafe.Peek.uint16_le buf ~pos:(pos + 2))
  ;;

  let[@inline] get_spec (buf [@local]) =
    let pos = 0 in
    Probe_type.of_char (Iobuf.Unsafe.Peek.char buf ~pos:(pos + 4))
  ;;

  let name_max_len = 64

  let[@inline] get_name (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 5)
  ;;

  let get_name_zero buf f =
    let buf = Iobuf.read_only buf in
    let pos = 5 in
    let len = ref 64 in
    while
      !len > 0 && Char.( = ) padding (Iobuf.Unsafe.Peek.char buf ~pos:(pos + !len - 1))
    do
      decr len
    done;
    f buf ~safe_pos:pos ~safe_len:!len
  ;;

  let get_name_zero_padded buf f =
    let buf = Iobuf.read_only buf in
    let pos = 5 in
    f buf ~safe_pos:pos ~safe_len:64
  ;;

  let[@inline] set_id buf field =
    let pos = 0 in
    let int_to_pack = Probe_id.to_int_exn field in
    Iobuf.Poke.uint16_le_trunc buf ~pos:(pos + 2) int_to_pack
  ;;

  let[@inline] set_spec buf field =
    let pos = 0 in
    Iobuf.Poke.char buf ~pos:(pos + 4) (Probe_type.to_char field)
  ;;

  let[@inline] set_name buf field =
    let pos = 0 in
    Iobuf.Poke.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 5) field
  ;;

  let set_name_zero buf f a =
    let lo = Iobuf.Lo_bound.window buf in
    let hi = Iobuf.Hi_bound.window buf in
    Iobuf.advance buf 5;
    Iobuf.resize buf ~len:64;
    f a buf;
    while not (Iobuf.is_empty buf) do
      Iobuf.Unsafe.Fill.char buf padding
    done;
    Iobuf.Lo_bound.restore lo buf;
    Iobuf.Hi_bound.restore hi buf
  ;;

  let to_sub_iobuf t = Iobuf.sub_shared t ~len:(get_message_length t + 0)

  module Unpacked = struct
    type t =
      { message_length : int
      ; message_type : char
      ; id : Probe_id.t
      ; spec : Probe_type.t
      ; name : string
      }
    [@@deriving sexp]

    let num_bytes t = t.message_length + 0

    let write (t : t) iobuf =
      let res = write iobuf ~id:t.id ~spec:t.spec ~name:t.name in
      res
    ;;
  end

  let to_unpacked buf : Unpacked.t =
    { message_length = get_message_length buf
    ; message_type = get_message_type buf
    ; id = get_id buf
    ; spec = get_spec buf
    ; name = get_name buf
    }
  ;;

  let sexp_of_t _ t = Unpacked.sexp_of_t (to_unpacked t)

  let of_unpacked (unpacked : Unpacked.t) =
    let t = Iobuf.create ~len:69 in
    ignore (Unpacked.write unpacked t : int);
    Iobuf.of_string (Iobuf.to_string t)
  ;;

  let t_of_sexp _ sexp = of_unpacked (Unpacked.t_of_sexp sexp)
end

module New_group = struct
  type phantom = [ `New_group ]
  type nonrec -'rw t = (phantom, 'rw) t

  let message_type = 'P'
  let buffer_length = 69
  let of_iobuf_exn buf = of_iobuf_exn buf Message_type_and_errors.New_group

  let of_iobuf_local_exn (buf [@local]) =
     (of_iobuf_local_exn buf Message_type_and_errors.New_group)
  ;;

  let write ~id ~spec ~name buf =
    let pos = 0 in
    assert (Iobuf.length buf >= 69);
    Iobuf.Unsafe.Poke.char buf ~pos:(pos + 1) message_type;
    (let int_to_pack = Probe_id.to_int_exn id in
     Iobuf.Unsafe.Poke.uint16_le_trunc buf ~pos:(pos + 2) int_to_pack);
    Iobuf.Unsafe.Poke.char buf ~pos:(pos + 4) (Probe_type.to_char spec);
    Iobuf.Unsafe.Poke.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 5) name;
    let total_bytes_packed = 69 in
    (let int_to_pack = total_bytes_packed - 0 in
     Iobuf.Unsafe.Poke.uint8_trunc buf ~pos int_to_pack);
    total_bytes_packed
  ;;

  let create ~id ~spec ~name =
    let iobuf = Iobuf.create ~len:69 in
    let size = write ~id ~spec ~name iobuf in
    assert (Iobuf.length iobuf = size);
    iobuf
  ;;

  let[@inline] get_message_length (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.uint8 buf ~pos
  ;;

  let[@inline] get_message_type (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.char buf ~pos:(pos + 1)
  ;;

  let[@inline] get_id (buf [@local]) =
    let pos = 0 in
    Probe_id.of_int_exn (Iobuf.Unsafe.Peek.uint16_le buf ~pos:(pos + 2))
  ;;

  let[@inline] get_spec (buf [@local]) =
    let pos = 0 in
    Probe_type.of_char (Iobuf.Unsafe.Peek.char buf ~pos:(pos + 4))
  ;;

  let name_max_len = 64

  let[@inline] get_name (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 5)
  ;;

  let get_name_zero buf f =
    let buf = Iobuf.read_only buf in
    let pos = 5 in
    let len = ref 64 in
    while
      !len > 0 && Char.( = ) padding (Iobuf.Unsafe.Peek.char buf ~pos:(pos + !len - 1))
    do
      decr len
    done;
    f buf ~safe_pos:pos ~safe_len:!len
  ;;

  let get_name_zero_padded buf f =
    let buf = Iobuf.read_only buf in
    let pos = 5 in
    f buf ~safe_pos:pos ~safe_len:64
  ;;

  let[@inline] set_id buf field =
    let pos = 0 in
    let int_to_pack = Probe_id.to_int_exn field in
    Iobuf.Poke.uint16_le_trunc buf ~pos:(pos + 2) int_to_pack
  ;;

  let[@inline] set_spec buf field =
    let pos = 0 in
    Iobuf.Poke.char buf ~pos:(pos + 4) (Probe_type.to_char field)
  ;;

  let[@inline] set_name buf field =
    let pos = 0 in
    Iobuf.Poke.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 5) field
  ;;

  let set_name_zero buf f a =
    let lo = Iobuf.Lo_bound.window buf in
    let hi = Iobuf.Hi_bound.window buf in
    Iobuf.advance buf 5;
    Iobuf.resize buf ~len:64;
    f a buf;
    while not (Iobuf.is_empty buf) do
      Iobuf.Unsafe.Fill.char buf padding
    done;
    Iobuf.Lo_bound.restore lo buf;
    Iobuf.Hi_bound.restore hi buf
  ;;

  let to_sub_iobuf t = Iobuf.sub_shared t ~len:(get_message_length t + 0)

  module Unpacked = struct
    type t =
      { message_length : int
      ; message_type : char
      ; id : Probe_id.t
      ; spec : Probe_type.t
      ; name : string
      }
    [@@deriving sexp]

    let num_bytes t = t.message_length + 0

    let write (t : t) iobuf =
      let res = write iobuf ~id:t.id ~spec:t.spec ~name:t.name in
      res
    ;;
  end

  let to_unpacked buf : Unpacked.t =
    { message_length = get_message_length buf
    ; message_type = get_message_type buf
    ; id = get_id buf
    ; spec = get_spec buf
    ; name = get_name buf
    }
  ;;

  let sexp_of_t _ t = Unpacked.sexp_of_t (to_unpacked t)

  let of_unpacked (unpacked : Unpacked.t) =
    let t = Iobuf.create ~len:69 in
    ignore (Unpacked.write unpacked t : int);
    Iobuf.of_string (Iobuf.to_string t)
  ;;

  let t_of_sexp _ sexp = of_unpacked (Unpacked.t_of_sexp sexp)
end

module New_group_point = struct
  type phantom = [ `New_group_point ]
  type nonrec -'rw t = (phantom, 'rw) t

  let message_type = 'O'
  let buffer_length ~sources_count = 72 + (2 * sources_count)
  let of_iobuf_exn buf = of_iobuf_exn buf Message_type_and_errors.New_group_point

  let of_iobuf_local_exn (buf [@local]) =
     (of_iobuf_local_exn buf Message_type_and_errors.New_group_point)
  ;;

  let write ~group_id ~id ~name ~sources_count buf =
    let pos = 0 in
    assert (Iobuf.length buf >= 72 + (2 * sources_count));
    Iobuf.Unsafe.Poke.char buf ~pos:(pos + 1) message_type;
    (let int_to_pack = Probe_id.to_int_exn group_id in
     Iobuf.Unsafe.Poke.uint16_le_trunc buf ~pos:(pos + 2) int_to_pack);
    (let int_to_pack = Probe_id.to_int_exn id in
     Iobuf.Unsafe.Poke.uint16_le_trunc buf ~pos:(pos + 4) int_to_pack);
    Iobuf.Unsafe.Poke.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 6) name;
    (let int_to_pack = sources_count in
     Iobuf.Unsafe.Poke.uint16_le_trunc buf ~pos:(pos + 70) int_to_pack);
    let pos_after_sources = pos + 72 + (sources_count * 2) in
    let total_bytes_packed = pos_after_sources - pos in
    (let int_to_pack = total_bytes_packed - 0 in
     Iobuf.Unsafe.Poke.uint8_trunc buf ~pos int_to_pack);
    total_bytes_packed
  ;;

  let create ~group_id ~id ~name ~sources_count =
    let iobuf = Iobuf.create ~len:(72 + (2 * sources_count)) in
    let size = write ~group_id ~id ~name ~sources_count iobuf in
    assert (Iobuf.length iobuf = size);
    iobuf
  ;;

  let[@inline] get_message_length (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.uint8 buf ~pos
  ;;

  let[@inline] get_message_type (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.char buf ~pos:(pos + 1)
  ;;

  let[@inline] get_group_id (buf [@local]) =
    let pos = 0 in
    Probe_id.of_int_exn (Iobuf.Unsafe.Peek.uint16_le buf ~pos:(pos + 2))
  ;;

  let[@inline] get_id (buf [@local]) =
    let pos = 0 in
    Probe_id.of_int_exn (Iobuf.Unsafe.Peek.uint16_le buf ~pos:(pos + 4))
  ;;

  let name_max_len = 64

  let[@inline] get_name (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 6)
  ;;

  let get_name_zero buf f =
    let buf = Iobuf.read_only buf in
    let pos = 6 in
    let len = ref 64 in
    while
      !len > 0 && Char.( = ) padding (Iobuf.Unsafe.Peek.char buf ~pos:(pos + !len - 1))
    do
      decr len
    done;
    f buf ~safe_pos:pos ~safe_len:!len
  ;;

  let get_name_zero_padded buf f =
    let buf = Iobuf.read_only buf in
    let pos = 6 in
    f buf ~safe_pos:pos ~safe_len:64
  ;;

  let[@inline] get_sources_count (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.uint16_le buf ~pos:(pos + 70)
  ;;

  let[@inline] get_sources_source_id buf ~count ~index =
    if index < 0 || index >= count then invalid_arg "index out of bounds";
    let pos = 0 in
    let pos_of_source_id_using_count_and_index = pos + 72 + (count * 0) + (index * 2) in
    Probe_id.of_int_exn
      (Iobuf.Unsafe.Peek.uint16_le buf ~pos:pos_of_source_id_using_count_and_index)
  ;;

  let[@inline] set_group_id buf field =
    let pos = 0 in
    let int_to_pack = Probe_id.to_int_exn field in
    Iobuf.Poke.uint16_le_trunc buf ~pos:(pos + 2) int_to_pack
  ;;

  let[@inline] set_id buf field =
    let pos = 0 in
    let int_to_pack = Probe_id.to_int_exn field in
    Iobuf.Poke.uint16_le_trunc buf ~pos:(pos + 4) int_to_pack
  ;;

  let[@inline] set_name buf field =
    let pos = 0 in
    Iobuf.Poke.tail_padded_fixed_string ~padding buf ~len:64 ~pos:(pos + 6) field
  ;;

  let set_name_zero buf f a =
    let lo = Iobuf.Lo_bound.window buf in
    let hi = Iobuf.Hi_bound.window buf in
    Iobuf.advance buf 6;
    Iobuf.resize buf ~len:64;
    f a buf;
    while not (Iobuf.is_empty buf) do
      Iobuf.Unsafe.Fill.char buf padding
    done;
    Iobuf.Lo_bound.restore lo buf;
    Iobuf.Hi_bound.restore hi buf
  ;;

  let[@inline] set_sources_source_id buf ~count ~index field =
    if index < 0 || index >= count then invalid_arg "index out of bounds";
    let pos = 0 in
    let pos_of_source_id_using_count_and_index = pos + 72 + (count * 0) + (index * 2) in
    let int_to_pack = Probe_id.to_int_exn field in
    Iobuf.Poke.uint16_le_trunc buf ~pos:pos_of_source_id_using_count_and_index int_to_pack
  ;;

  let write_sources buf ~count ~index ~source_id =
    set_sources_source_id buf ~count ~index source_id
  ;;

  let to_sub_iobuf t = Iobuf.sub_shared t ~len:(get_message_length t + 0)

  module Unpacked = struct
    type t_sources = { source_id : Probe_id.t } [@@deriving sexp]

    type t =
      { message_length : int
      ; message_type : char
      ; group_id : Probe_id.t
      ; id : Probe_id.t
      ; name : string
      ; sources_grp : t_sources array
      }
    [@@deriving sexp]

    let num_bytes t = t.message_length + 0

    let write (t : t) iobuf =
      let res =
        write
          iobuf
          ~group_id:t.group_id
          ~id:t.id
          ~name:t.name
          ~sources_count:(Array.length t.sources_grp)
      in
      Array.iteri t.sources_grp ~f:(fun i (record : t_sources) ->
        write_sources
          iobuf
          ~count:(Array.length t.sources_grp)
          ~index:i
          ~source_id:record.source_id);
      res
    ;;
  end

  let to_unpacked buf : Unpacked.t =
    { message_length = get_message_length buf
    ; message_type = get_message_type buf
    ; group_id = get_group_id buf
    ; id = get_id buf
    ; name = get_name buf
    ; sources_grp =
        (let sources_count = get_sources_count buf in
         Array.init sources_count ~f:(fun i ->
           { Unpacked.source_id = get_sources_source_id buf ~count:sources_count ~index:i
           }))
    }
  ;;

  let sexp_of_t _ t = Unpacked.sexp_of_t (to_unpacked t)

  let of_unpacked (unpacked : Unpacked.t) =
    let t = Iobuf.create ~len:(72 + (2 * Array.length unpacked.sources_grp)) in
    ignore (Unpacked.write unpacked t : int);
    Iobuf.of_string (Iobuf.to_string t)
  ;;

  let t_of_sexp _ sexp = of_unpacked (Unpacked.t_of_sexp sexp)
end

module End_of_header = struct
  type phantom = [ `End_of_header ]
  type nonrec -'rw t = (phantom, 'rw) t

  let message_type = 'Z'
  let buffer_length = 2
  let of_iobuf_exn buf = of_iobuf_exn buf Message_type_and_errors.End_of_header

  let of_iobuf_local_exn (buf [@local]) =
     (of_iobuf_local_exn buf Message_type_and_errors.End_of_header)
  ;;

  let write buf =
    let pos = 0 in
    assert (Iobuf.length buf >= 2);
    Iobuf.Unsafe.Poke.char buf ~pos:(pos + 1) message_type;
    let total_bytes_packed = 2 in
    (let int_to_pack = total_bytes_packed - 0 in
     Iobuf.Unsafe.Poke.uint8_trunc buf ~pos int_to_pack);
    total_bytes_packed
  ;;

  let create () =
    let iobuf = Iobuf.create ~len:2 in
    let size = write iobuf in
    assert (Iobuf.length iobuf = size);
    iobuf
  ;;

  let[@inline] get_message_length (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.uint8 buf ~pos
  ;;

  let[@inline] get_message_type (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.char buf ~pos:(pos + 1)
  ;;

  let to_sub_iobuf t = Iobuf.sub_shared t ~len:(get_message_length t + 0)

  module Unpacked = struct
    type t =
      { message_length : int
      ; message_type : char
      }
    [@@deriving sexp]

    let num_bytes t = t.message_length + 0

    let write (_ : t) iobuf =
      let res = write iobuf in
      res
    ;;
  end

  let to_unpacked buf : Unpacked.t =
    { message_length = get_message_length buf; message_type = get_message_type buf }
  ;;

  let sexp_of_t _ t = Unpacked.sexp_of_t (to_unpacked t)

  let of_unpacked (unpacked : Unpacked.t) =
    let t = Iobuf.create ~len:2 in
    ignore (Unpacked.write unpacked t : int);
    Iobuf.of_string (Iobuf.to_string t)
  ;;

  let t_of_sexp _ sexp = of_unpacked (Unpacked.t_of_sexp sexp)
end

module Epoch = struct
  type phantom = [ `Epoch ]
  type nonrec -'rw t = (phantom, 'rw) t

  let message_type = 'E'
  let buffer_length = 10
  let of_iobuf_exn buf = of_iobuf_exn buf Message_type_and_errors.Epoch

  let of_iobuf_local_exn (buf [@local]) =
     (of_iobuf_local_exn buf Message_type_and_errors.Epoch)
  ;;

  let write ~epoch buf =
    let pos = 0 in
    assert (Iobuf.length buf >= 10);
    Iobuf.Unsafe.Poke.char buf ~pos:(pos + 1) message_type;
    (let int_to_pack = Profiler_epoch.to_int epoch in
     Iobuf.Unsafe.Poke.int64_le buf ~pos:(pos + 2) int_to_pack);
    let total_bytes_packed = 10 in
    (let int_to_pack = total_bytes_packed - 0 in
     Iobuf.Unsafe.Poke.uint8_trunc buf ~pos int_to_pack);
    total_bytes_packed
  ;;

  let create ~epoch =
    let iobuf = Iobuf.create ~len:10 in
    let size = write ~epoch iobuf in
    assert (Iobuf.length iobuf = size);
    iobuf
  ;;

  let[@inline] get_message_length (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.uint8 buf ~pos
  ;;

  let[@inline] get_message_type (buf [@local]) =
    let pos = 0 in
    Iobuf.Unsafe.Peek.char buf ~pos:(pos + 1)
  ;;

  let[@inline] get_epoch (buf [@local]) =
    let pos = 0 in
    Profiler_epoch.of_int (Iobuf.Unsafe.Peek.int64_le_exn buf ~pos:(pos + 2))
  ;;

  let[@inline] set_epoch buf field =
    let pos = 0 in
    let int_to_pack = Profiler_epoch.to_int field in
    Iobuf.Poke.int64_le buf ~pos:(pos + 2) int_to_pack
  ;;

  let to_sub_iobuf t = Iobuf.sub_shared t ~len:(get_message_length t + 0)

  module Unpacked = struct
    type t =
      { message_length : int
      ; message_type : char
      ; epoch : Profiler_epoch.t
      }
    [@@deriving sexp]

    let num_bytes t = t.message_length + 0

    let write (t : t) iobuf =
      let res = write iobuf ~epoch:t.epoch in
      res
    ;;
  end

  let to_unpacked buf : Unpacked.t =
    { message_length = get_message_length buf
    ; message_type = get_message_type buf
    ; epoch = get_epoch buf
    }
  ;;

  let sexp_of_t _ t = Unpacked.sexp_of_t (to_unpacked t)

  let of_unpacked (unpacked : Unpacked.t) =
    let t = Iobuf.create ~len:10 in
    ignore (Unpacked.write unpacked t : int);
    Iobuf.of_string (Iobuf.to_string t)
  ;;

  let t_of_sexp _ sexp = of_unpacked (Unpacked.t_of_sexp sexp)
end

module Unpacked = struct
  type t =
    | New_single of New_single.Unpacked.t
    | New_group of New_group.Unpacked.t
    | New_group_point of New_group_point.Unpacked.t
    | End_of_header of End_of_header.Unpacked.t
    | Epoch of Epoch.Unpacked.t
  [@@deriving sexp]

  let num_bytes = function
    | New_single m -> New_single.Unpacked.num_bytes m
    | New_group m -> New_group.Unpacked.num_bytes m
    | New_group_point m -> New_group_point.Unpacked.num_bytes m
    | End_of_header m -> End_of_header.Unpacked.num_bytes m
    | Epoch m -> Epoch.Unpacked.num_bytes m
  ;;

  let message_type = function
    | New_single m -> m.message_type
    | New_group m -> m.message_type
    | New_group_point m -> m.message_type
    | End_of_header m -> m.message_type
    | Epoch m -> m.message_type
  ;;

  let write t iobuf =
    match t with
    | New_single msg -> New_single.Unpacked.write msg iobuf
    | New_group msg -> New_group.Unpacked.write msg iobuf
    | New_group_point msg -> New_group_point.Unpacked.write msg iobuf
    | End_of_header msg -> End_of_header.Unpacked.write msg iobuf
    | Epoch msg -> Epoch.Unpacked.write msg iobuf
  ;;
end

let num_bytes_needed_for_message_length = 1

let[@inline] num_bytes_in_message buf =
  let pos = 0 in
  if Iobuf.length buf < 1 then failwith "Not enough data to read a message length!";
  0 + Iobuf.Unsafe.Peek.uint8 buf ~pos
;;

let skip_message buf = Iobuf.advance buf (num_bytes_in_message buf)

let buffer_contains_full_message buf =
  let len = Iobuf.length buf in
  len >= num_bytes_needed_for_message_length && len >= num_bytes_in_message buf
;;

let of_unpacked (u : Unpacked.t) =
  match u with
  | New_single msg -> New_single.of_unpacked msg
  | New_group msg -> New_group.of_unpacked msg
  | New_group_point msg -> New_group_point.of_unpacked msg
  | End_of_header msg -> End_of_header.of_unpacked msg
  | Epoch msg -> Epoch.of_unpacked msg
;;

let to_unpacked (buf [@local]) =
  let (Message_type_and_errors.T mt) = get_message_type buf in
  let m = of_iobuf_local buf ~trusted:mt in
  match mt with
  | Message_type_and_errors.Need_more_data -> R.Need_more_data
  | Message_type_and_errors.Invalid_message_type_or_subtype ->
    let len_or_error = Result.try_with (fun () -> num_bytes_in_message buf) in
    R.Junk (Failure "Invalid_message_type_or_subtype", len_or_error)
  | Message_type_and_errors.Message_length_too_short ->
    let len_or_error = Result.try_with (fun () -> num_bytes_in_message buf) in
    R.Junk (Failure "Message_length_too_short", len_or_error)
  | Message_type_and_errors.New_single ->
    R.Ok (Unpacked.New_single (New_single.to_unpacked m), num_bytes_in_message buf)
  | Message_type_and_errors.New_group ->
    R.Ok (Unpacked.New_group (New_group.to_unpacked m), num_bytes_in_message buf)
  | Message_type_and_errors.New_group_point ->
    R.Ok
      (Unpacked.New_group_point (New_group_point.to_unpacked m), num_bytes_in_message buf)
  | Message_type_and_errors.End_of_header ->
    R.Ok (Unpacked.End_of_header (End_of_header.to_unpacked m), num_bytes_in_message buf)
  | Message_type_and_errors.Epoch ->
    R.Ok (Unpacked.Epoch (Epoch.to_unpacked m), num_bytes_in_message buf)
;;

let to_unpacked_exn buf =
  match to_unpacked buf with
  | Ok (msg, _len) -> msg
  | (Need_more_data | Junk _) as failure ->
    let (Message_type_and_errors.T message_type) = get_message_type buf in
    raise_s
      [%message
        "[to_unpacked_exn] failure"
          (message_type : _ Message_type_and_errors.t)
          (failure : _ R.t)]
;;

let sexp_of_t _ _ t =
  match to_unpacked t with
  | (R.Need_more_data | R.Junk _) as e ->
    failwiths ~here:[%here] "invalid message" e [%sexp_of: Nothing.t R.t]
  | R.Ok (t, _) -> Unpacked.sexp_of_t t
;;

let sexp_of_t_no_exn _ _ t =
  let sexp_of_error error =
    let len =
      try num_bytes_in_message t with
      | _ -> Iobuf.length t
    in
    let len = Int.min (Int.min (Iobuf.length t) len) 1000 in
    let data = Iobuf.to_string t ~len in
    [%sexp ("invalid message" : string), { error : Error.t; data : string }]
  in
  match to_unpacked t with
  | exception exn -> sexp_of_error (Error.of_exn exn)
  | R.Need_more_data -> sexp_of_error (Error.of_string "Need_more_data")
  | R.Junk (exn, _) -> sexp_of_error (Error.of_exn exn)
  | R.Ok (t, _) -> Unpacked.sexp_of_t t
;;

let backing_iobuf t = t
let backing_iobuf_local t = t
OCaml

Innovation. Community. Security.