minor cleanup: stats type

This commit is contained in:
Hannes Mehnert 2018-10-23 21:53:44 +02:00
parent f3c67f626a
commit 46548418cd
6 changed files with 26 additions and 24 deletions

View file

@ -199,7 +199,7 @@ let rec read_sock_write_tcp c ?fd addr addrtype =
end else end else
let name = string_of_id hdr.Vmm_asn.id in let name = string_of_id hdr.Vmm_asn.id in
let ru = P.encode_ru name ru in let ru = P.encode_ru name ru in
let vmm = match vmm with [] -> [] | _ -> [ P.encode_vmm name vmm ] in let vmm = match vmm with None -> [] | Some xs -> [ P.encode_vmm name xs ] in
let taps = List.map (P.encode_if name) ifs in let taps = List.map (P.encode_if name) ifs in
let out = (String.concat ~sep:"\n" (ru :: vmm @ taps)) ^ "\n" in let out = (String.concat ~sep:"\n" (ru :: vmm @ taps)) ^ "\n" in
Logs.debug (fun m -> m "writing %d via tcp" (String.length out)) ; Logs.debug (fun m -> m "writing %d via tcp" (String.length out)) ;

View file

@ -211,37 +211,26 @@ type stats_cmd = [
| `Stats_add of int * string list | `Stats_add of int * string list
| `Stats_remove | `Stats_remove
| `Stats_subscribe | `Stats_subscribe
| `Stats_data of rusage * (string * int64) list * ifdata list | `Stats_data of stats
] ]
let pp_stats_cmd ppf = function let pp_stats_cmd ppf = function
| `Stats_add (pid, taps) -> Fmt.pf ppf "stats add: pid %d taps %a" pid Fmt.(list ~sep:(unit ", ") string) taps | `Stats_add (pid, taps) -> Fmt.pf ppf "stats add: pid %d taps %a" pid Fmt.(list ~sep:(unit ", ") string) taps
| `Stats_remove -> Fmt.string ppf "stat remove" | `Stats_remove -> Fmt.string ppf "stat remove"
| `Stats_subscribe -> Fmt.string ppf "stat subscribe" | `Stats_subscribe -> Fmt.string ppf "stat subscribe"
| `Stats_data (ru, vmm, ifs) -> Fmt.pf ppf "stats data: %a %a %a" | `Stats_data stats -> Fmt.pf ppf "stats data: %a" pp_stats stats
pp_rusage ru
pp_vmm vmm
Fmt.(list ~sep:(unit "@.@.") pp_ifdata) ifs
let stats_cmd = let stats_cmd =
let f = function let f = function
| `C1 (pid, taps) -> `Stats_add (pid, taps) | `C1 (pid, taps) -> `Stats_add (pid, taps)
| `C2 () -> `Stats_remove | `C2 () -> `Stats_remove
| `C3 () -> `Stats_subscribe | `C3 () -> `Stats_subscribe
| `C4 (ru, vmm, ifdata) -> | `C4 (ru, ifs, vmm) -> `Stats_data (ru, vmm, ifs)
let vmm = match vmm with None -> [] | Some vmm -> vmm
and ifdata = match ifdata with None -> [] | Some ifs -> ifs
in
`Stats_data (ru, vmm, ifdata)
and g = function and g = function
| `Stats_add (pid, taps) -> `C1 (pid, taps) | `Stats_add (pid, taps) -> `C1 (pid, taps)
| `Stats_remove -> `C2 () | `Stats_remove -> `C2 ()
| `Stats_subscribe -> `C3 () | `Stats_subscribe -> `C3 ()
| `Stats_data (ru, vmm, ifdata) -> | `Stats_data (ru, ifs, vmm) -> `C4 (ru, vmm, ifs)
let vmm = match vmm with [] -> None | xs -> Some xs
and ifs = match ifdata with [] -> None | xs -> Some xs
in
`C4 (ru, vmm, ifs)
in in
Asn.S.map f g @@ Asn.S.map f g @@
Asn.S.(choice4 Asn.S.(choice4
@ -252,12 +241,11 @@ let stats_cmd =
(explicit 2 null) (explicit 2 null)
(explicit 3 (sequence3 (explicit 3 (sequence3
(required ~label:"resource_usage" ru) (required ~label:"resource_usage" ru)
(optional ~label:"vmm_stats" @@ explicit 0 (required ~label:"ifdata" (sequence_of ifdata))
(optional ~label:"vmm_stats"
(sequence_of (sequence2 (sequence_of (sequence2
(required ~label:"key" utf8_string) (required ~label:"key" utf8_string)
(required ~label:"value" int64)))) (required ~label:"value" int64)))))))
(optional ~label:"ifdata" @@ explicit 1
(sequence_of ifdata)))))
let addr = let addr =
Asn.S.(sequence2 Asn.S.(sequence2

View file

@ -30,7 +30,7 @@ type stats_cmd = [
| `Stats_add of int * string list | `Stats_add of int * string list
| `Stats_remove | `Stats_remove
| `Stats_subscribe | `Stats_subscribe
| `Stats_data of rusage * (string * int64) list * ifdata list | `Stats_data of stats
] ]
type log_cmd = [ type log_cmd = [

View file

@ -209,7 +209,9 @@ let pp_rusage ppf r =
Fmt.pf ppf "utime %Lu.%d stime %Lu.%d maxrss %Lu ixrss %Lu idrss %Lu isrss %Lu minflt %Lu majflt %Lu nswap %Lu inblock %Lu outblock %Lu msgsnd %Lu msgrcv %Lu signals %Lu nvcsw %Lu nivcsw %Lu" Fmt.pf ppf "utime %Lu.%d stime %Lu.%d maxrss %Lu ixrss %Lu idrss %Lu isrss %Lu minflt %Lu majflt %Lu nswap %Lu inblock %Lu outblock %Lu msgsnd %Lu msgrcv %Lu signals %Lu nvcsw %Lu nivcsw %Lu"
(fst r.utime) (snd r.utime) (fst r.stime) (snd r.stime) r.maxrss r.ixrss r.idrss r.isrss r.minflt r.majflt r.nswap r.inblock r.outblock r.msgsnd r.msgrcv r.nsignals r.nvcsw r.nivcsw (fst r.utime) (snd r.utime) (fst r.stime) (snd r.stime) r.maxrss r.ixrss r.idrss r.isrss r.minflt r.majflt r.nswap r.inblock r.outblock r.msgsnd r.msgrcv r.nsignals r.nvcsw r.nivcsw
let pp_vmm ppf vmm =
type vmm_stats = (string * int64) list
let pp_vmm_stats ppf vmm =
Fmt.(list ~sep:(unit "@,") (pair ~sep:(unit ": ") string int64)) ppf vmm Fmt.(list ~sep:(unit "@,") (pair ~sep:(unit ": ") string int64)) ppf vmm
type ifdata = { type ifdata = {
@ -237,6 +239,13 @@ let pp_ifdata ppf i =
Fmt.pf ppf "name %s flags %lX send_length %lu max_send_length %lu send_drops %lu mtu %lu baudrate %Lu input_packets %Lu input_errors %Lu output_packets %Lu output_errors %Lu collisions %Lu input_bytes %Lu output_bytes %Lu input_mcast %Lu output_mcast %Lu input_dropped %Lu output_dropped %Lu" Fmt.pf ppf "name %s flags %lX send_length %lu max_send_length %lu send_drops %lu mtu %lu baudrate %Lu input_packets %Lu input_errors %Lu output_packets %Lu output_errors %Lu collisions %Lu input_bytes %Lu output_bytes %Lu input_mcast %Lu output_mcast %Lu input_dropped %Lu output_dropped %Lu"
i.name i.flags i.send_length i.max_send_length i.send_drops i.mtu i.baudrate i.input_packets i.input_errors i.output_packets i.output_errors i.collisions i.input_bytes i.output_bytes i.input_mcast i.output_mcast i.input_dropped i.output_dropped i.name i.flags i.send_length i.max_send_length i.send_drops i.mtu i.baudrate i.input_packets i.input_errors i.output_packets i.output_errors i.collisions i.input_bytes i.output_bytes i.input_mcast i.output_mcast i.input_dropped i.output_dropped
type stats = rusage * vmm_stats option * ifdata list
let pp_stats ppf (ru, vmm, ifs) =
Fmt.pf ppf "%a@.%a@.%a"
pp_rusage ru
Fmt.(option ~none:(unit "no vmm stats") pp_vmm_stats) vmm
Fmt.(list ~sep:(unit "@.@.") pp_ifdata) ifs
module Log = struct module Log = struct
type event = type event =
[ `Startup [ `Startup

View file

@ -98,7 +98,9 @@ type rusage = {
nivcsw : int64; nivcsw : int64;
} }
val pp_rusage : rusage Fmt.t val pp_rusage : rusage Fmt.t
val pp_vmm : (string * int64) list Fmt.t
type vmm_stats = (string * int64) list
val pp_vmm_stats : vmm_stats Fmt.t
type ifdata = { type ifdata = {
name : string; name : string;
@ -122,6 +124,9 @@ type ifdata = {
} }
val pp_ifdata : ifdata Fmt.t val pp_ifdata : ifdata Fmt.t
type stats = rusage * vmm_stats option * ifdata list
val pp_stats : stats Fmt.t
module Log : module Log :
sig sig
type event = type event =

View file

@ -112,7 +112,7 @@ let tick t =
| None -> Logs.err (fun m -> m "failed to get rusage for %d" pid) ; out | None -> Logs.err (fun m -> m "failed to get rusage for %d" pid) ; out
| Some ru' -> | Some ru' ->
let stats = let stats =
let vmm' = match vmm with None -> [] | Some xs -> List.combine !descr xs in let vmm' = match vmm with None -> None | Some xs -> Some (List.combine !descr xs) in
ru', vmm', ifd ru', vmm', ifd
in in
List.fold_left (fun out (id, socket) -> List.fold_left (fun out (id, socket) ->