package octez-libs

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

Source file p2p_stat.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type t = {
  total_sent : int64;
  total_recv : int64;
  current_inflow : int;
  current_outflow : int;
}

let empty =
  {total_sent = 0L; total_recv = 0L; current_inflow = 0; current_outflow = 0}

let print_size ppf sz =
  let ratio n = float_of_int sz /. float_of_int (1 lsl n) in
  if sz < 1 lsl 10 then Format.fprintf ppf "%d B" sz
  else if sz < 1 lsl 20 then Format.fprintf ppf "%.2f kiB" (ratio 10)
  else Format.fprintf ppf "%.2f MiB" (ratio 20)

let print_size64 ppf sz =
  let open Int64 in
  let ratio n = to_float sz /. float_of_int (1 lsl n) in
  if sz < shift_left 1L 10 then Format.fprintf ppf "%Ld B" sz
  else if sz < shift_left 1L 20 then Format.fprintf ppf "%.2f kiB" (ratio 10)
  else if sz < shift_left 1L 30 then Format.fprintf ppf "%.2f MiB" (ratio 20)
  else if sz < shift_left 1L 40 then Format.fprintf ppf "%.2f GiB" (ratio 30)
  else Format.fprintf ppf "%.2f TiB" (ratio 40)

let pp ppf stat =
  Format.fprintf
    ppf
    "↗ %a (%a/s) ↘ %a (%a/s)"
    print_size64
    stat.total_sent
    print_size
    stat.current_outflow
    print_size64
    stat.total_recv
    print_size
    stat.current_inflow

let encoding =
  let open Data_encoding in
  def "p2p_stat" ~description:"Statistics about the p2p network."
  @@ conv
       (fun {total_sent; total_recv; current_inflow; current_outflow} ->
         (total_sent, total_recv, current_inflow, current_outflow))
       (fun (total_sent, total_recv, current_inflow, current_outflow) ->
         {total_sent; total_recv; current_inflow; current_outflow})
       (obj4
          (req "total_sent" int64)
          (req "total_recv" int64)
          (req "current_inflow" int31)
          (req "current_outflow" int31))

let () = Data_encoding.Registration.register ~pp encoding
OCaml

Innovation. Community. Security.