Skip to content
This repository
tree: 57f483c72f
Fetching contributors…

Cannot retrieve contributors at this time

file 161 lines (132 sloc) 4.949 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(*
@author Adam Koprowski
**)

module List = Base.List

let (|>) = InfixOperator.(|>)

module NA = NetAddr

type prot_stats =
  { packets : int
  ; size : int
  }

type protocol = string

let make_protocol prot = prot

module CommMap = StringMap

type stats = prot_stats CommMap.t

type t =
  { started_at : Time.t
  ; started_procTime : float
  ; mutable send_stats : stats
  ; mutable recv_stats : stats
  }

let make () =
  let empty_stats = CommMap.empty in
  { started_at = Time.now ()
  ; started_procTime = Sys.time ()
  ; send_stats = empty_stats
  ; recv_stats = empty_stats
  }

let empty_prot_stats =
  { packets=0; size=0 }

let register_packet ~size ~conn stats =
  let protocol = NA.get_protocol conn in
  let update s =
    { packets = s.packets + 1
    ; size = s.size + size
    }
  in
  let old = CommMap.find_opt protocol stats
         |> Option.default empty_prot_stats
  in
  CommMap.add protocol (update old) stats

let register_recv ~size:_size ~conn:_conn _stats =
  #<If:SERVER_STATS>
    _stats.recv_stats <- register_packet ~size:_size ~conn:_conn _stats.recv_stats
  #<Else>
    ()
  #<End>

let register_send ~size:_size ~conn:_conn _stats =
  #<If:SERVER_STATS>
    _stats.send_stats <- register_packet ~size:_size ~conn:_conn _stats.send_stats
  #<Else>
    ()
  #<End>

let stats_header =
  Printf.sprintf "%20s -------------------- %s ------------------- -------------------- %s ------------------\n" ""
    (Terminal.strong "send") (Terminal.strong "recv")

let stats_separator =
  Printf.sprintf "%20s --------------------------------------------- --------------------------------------------\n" ""

let merge_stats m1 m2 =
  { packets = m1.packets + m2.packets
  ; size = m1.size + m2.size
  }

let padding n s =
  let len = n - String.length s in
  if len > 0 then String.make len ' ' else ""

let print_denomination f d max_l units =
  let rec aux f units =
    if f < float_of_int d || List.length units == 1 then
      let unit = List.hd units in
      let txt = Printf.sprintf "%5.1f %s" f unit in
      (Terminal.emph txt) ^ padding max_l txt
    else
      aux (f /. float_of_int d) (List.tl units)
  in
  aux f units

let print_packets () f =
  print_denomination f 1000 2 [""; "k"; "M"; "G"; "T"]

let print_bytes () f =
  print_denomination f 1000 2 ["b"; "kB"; "MB"; "GB"; "TB"]

let print_time () f =
  print_denomination f 60 3 ["sec"; "min"; "h"]

let print_category_stats time cat send recv =
  let print_stats () stats =
    let packets = float_of_int stats.packets in
    let bytes = float_of_int stats.size in
    let packets_sec = packets /. time in
    let bytes_sec = bytes /. time in
    Printf.sprintf "[ %a %a | %a/sec %a/sec ]" print_packets packets print_bytes bytes
      print_packets packets_sec print_bytes bytes_sec
  in
  Printf.sprintf "[%s%s] %a %a\n" (padding 18 cat) (Terminal.strong cat) print_stats send print_stats recv

let print_categories time categories send_stats recv_stats =
  let rec aux acc (send_totals, recv_totals) = function
    | [] ->
      acc, print_category_stats time "TOTAL" send_totals recv_totals
    | x::xs ->
      let get_stats map cat = CommMap.find_opt cat map |> Option.default empty_prot_stats in
      let send = get_stats send_stats x in
      let recv = get_stats recv_stats x in
      let acc' = acc ^ print_category_stats time x send recv in
      let send_totals' = merge_stats send_totals send in
      let recv_totals' = merge_stats recv_totals recv in
      aux acc' (send_totals', recv_totals') xs
  in
  aux "" (empty_prot_stats, empty_prot_stats) categories

let to_string stats =
  let total_time = Time.in_seconds (Time.difference
                                      stats.started_at (Time.now ()))
  in
  let cpu_time = Sys.time () -. stats.started_procTime in
  let load = cpu_time /. total_time in
  let categories = CommMap.keys stats.send_stats @ CommMap.keys stats.recv_stats
                |> List.sort String.compare
                |> List.uniq
  in
  let cat_stats, totals = print_categories total_time categories stats.send_stats stats.recv_stats in
  Printf.sprintf "Total run-time: %s (CPU load: %s)\n%s%s%s%s\n"
    (Terminal.emph (print_time () total_time))
    (Terminal.emph (Printf.sprintf "%.2f" load))
    stats_header cat_stats stats_separator totals
Something went wrong with that request. Please try again.