Remove dependency on ocaml-extlib
[virt-top.git] / src / stream_output.ml
1 (* 'top'-like tool for libvirt domains.
2    (C) Copyright 2007-2017 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *)
19
20 (* [--stream] mode output functions. *)
21
22 open Printf
23
24 open Utils
25 open Collect
26
27 module C = Libvirt.Connect
28 module D = Libvirt.Domain
29
30 let append_stream (_, _, _, _, _, node_info, hostname, _) (* setup *)
31                   block_in_bytes
32                   { rd_doms = doms;
33                     rd_printable_time = printable_time;
34                     rd_nr_pcpus = nr_pcpus; rd_total_cpu = total_cpu;
35                     rd_totals = totals } (* state *) =
36   (* Header for this iteration *)
37   printf "virt-top time  %s Host %s %s %d/%dCPU %dMHz %LdMB \n"
38     printable_time hostname node_info.C.model node_info.C.cpus nr_pcpus
39     node_info.C.mhz (node_info.C.memory /^ 1024L);
40   (* dump domain information one by one *)
41    let rd, wr = if block_in_bytes then "RDBY", "WRBY" else "RDRQ", "WRRQ"
42    in
43      printf "   ID S %s %s RXBY TXBY %%CPU %%MEM   TIME    NAME\n" rd wr;
44
45   (* sort by ID *)
46   let doms =
47     let compare =
48       (function
49        | Active {rd_domid = id1 }, Active {rd_domid = id2} ->
50            compare id1 id2
51        | Active _, Inactive -> -1
52        | Inactive, Active _ -> 1
53        | Inactive, Inactive -> 0)
54     in
55     let cmp  (name1, dom1) (name2, dom2) = compare(dom1, dom2) in
56     List.sort cmp doms in
57   (*Print domains *)
58   let dump_domain = fun name rd
59   -> begin
60     let state = Screen.show_state rd.rd_info.D.state in
61          let rd_req =
62            if rd.rd_block_rd_reqs = None then "   0"
63            else
64              if block_in_bytes then Show.int64_option rd.rd_block_rd_bytes
65              else Show.int64_option rd.rd_block_rd_reqs in
66          let wr_req =
67            if rd.rd_block_wr_reqs = None then "   0"
68            else
69              if block_in_bytes then Show.int64_option rd.rd_block_wr_bytes
70              else Show.int64_option rd.rd_block_wr_reqs in
71     let rx_bytes = if rd.rd_net_rx_bytes = None then "   0"
72     else Show.int64_option rd.rd_net_rx_bytes in
73     let tx_bytes = if rd.rd_net_tx_bytes = None then "   0"
74     else Show.int64_option rd.rd_net_tx_bytes in
75     let percent_cpu = Show.percent rd.rd_percent_cpu in
76     let percent_mem = Int64.to_float rd.rd_mem_percent in
77     let percent_mem = Show.percent percent_mem in
78     let time = Show.time rd.rd_info.D.cpu_time in
79     printf "%5d %c %s %s %s %s %s %s %s %s\n"
80       rd.rd_domid state rd_req wr_req rx_bytes tx_bytes
81       percent_cpu percent_mem time name;
82   end
83   in
84   List.iter (
85     function
86     | name, Active dom -> dump_domain name dom
87     | name, Inactive -> ()
88   ) doms;
89   flush stdout