Split up huge Top module into smaller modules.
[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 open ExtList
24
25 open Utils
26 open Collect
27
28 module C = Libvirt.Connect
29 module D = Libvirt.Domain
30
31 let append_stream (_, _, _, _, _, node_info, hostname, _) (* setup *)
32                   block_in_bytes
33                   { rd_doms = doms;
34                     rd_printable_time = printable_time;
35                     rd_nr_pcpus = nr_pcpus; rd_total_cpu = total_cpu;
36                     rd_totals = totals } (* state *) =
37   (* Header for this iteration *)
38   printf "virt-top time  %s Host %s %s %d/%dCPU %dMHz %LdMB \n"
39     printable_time hostname node_info.C.model node_info.C.cpus nr_pcpus
40     node_info.C.mhz (node_info.C.memory /^ 1024L);
41   (* dump domain information one by one *)
42    let rd, wr = if block_in_bytes then "RDBY", "WRBY" else "RDRQ", "WRRQ"
43    in
44      printf "   ID S %s %s RXBY TXBY %%CPU %%MEM   TIME    NAME\n" rd wr;
45
46   (* sort by ID *)
47   let doms =
48     let compare =
49       (function
50        | Active {rd_domid = id1 }, Active {rd_domid = id2} ->
51            compare id1 id2
52        | Active _, Inactive -> -1
53        | Inactive, Active _ -> 1
54        | Inactive, Inactive -> 0)
55     in
56     let cmp  (name1, dom1) (name2, dom2) = compare(dom1, dom2) in
57     List.sort ~cmp doms in
58   (*Print domains *)
59   let dump_domain = fun name rd
60   -> begin
61     let state = Screen.show_state rd.rd_info.D.state in
62          let rd_req = if rd.rd_block_rd_info = None then "   0"
63                       else Show.int64_option rd.rd_block_rd_info in
64          let wr_req = if rd.rd_block_wr_info = None then "   0"
65                       else Show.int64_option rd.rd_block_wr_info in
66     let rx_bytes = if rd.rd_net_rx_bytes = None then "   0"
67     else Show.int64_option rd.rd_net_rx_bytes in
68     let tx_bytes = if rd.rd_net_tx_bytes = None then "   0"
69     else Show.int64_option rd.rd_net_tx_bytes in
70     let percent_cpu = Show.percent rd.rd_percent_cpu in
71     let percent_mem = Int64.to_float rd.rd_mem_percent in
72     let percent_mem = Show.percent percent_mem in
73     let time = Show.time rd.rd_info.D.cpu_time in
74     printf "%5d %c %s %s %s %s %s %s %s %s\n"
75       rd.rd_domid state rd_req wr_req rx_bytes tx_bytes
76       percent_cpu percent_mem time name;
77   end
78   in
79   List.iter (
80     function
81     | name, Active dom -> dump_domain name dom
82     | name, Inactive -> ()
83   ) doms;
84   flush stdout