1 (* 'top'-like tool for libvirt domains.
2 (C) Copyright 2007-2017 Richard W.M. Jones, Red Hat Inc.
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.
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.
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.
20 (* [--stream] mode output functions. *)
28 module C = Libvirt.Connect
29 module D = Libvirt.Domain
31 let append_stream (_, _, _, _, _, node_info, hostname, _) (* setup *)
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"
44 printf " ID S %s %s RXBY TXBY %%CPU %%MEM TIME NAME\n" rd wr;
50 | Active {rd_domid = id1 }, Active {rd_domid = id2} ->
52 | Active _, Inactive -> -1
53 | Inactive, Active _ -> 1
54 | Inactive, Inactive -> 0)
56 let cmp (name1, dom1) (name2, dom2) = compare(dom1, dom2) in
57 List.sort ~cmp doms in
59 let dump_domain = fun name rd
61 let state = Screen.show_state rd.rd_info.D.state in
63 if rd.rd_block_rd_reqs = None then " 0"
65 if block_in_bytes then Show.int64_option rd.rd_block_rd_bytes
66 else Show.int64_option rd.rd_block_rd_reqs in
68 if rd.rd_block_wr_reqs = None then " 0"
70 if block_in_bytes then Show.int64_option rd.rd_block_wr_bytes
71 else Show.int64_option rd.rd_block_wr_reqs in
72 let rx_bytes = if rd.rd_net_rx_bytes = None then " 0"
73 else Show.int64_option rd.rd_net_rx_bytes in
74 let tx_bytes = if rd.rd_net_tx_bytes = None then " 0"
75 else Show.int64_option rd.rd_net_tx_bytes in
76 let percent_cpu = Show.percent rd.rd_percent_cpu in
77 let percent_mem = Int64.to_float rd.rd_mem_percent in
78 let percent_mem = Show.percent percent_mem in
79 let time = Show.time rd.rd_info.D.cpu_time in
80 printf "%5d %c %s %s %s %s %s %s %s %s\n"
81 rd.rd_domid state rd_req wr_req rx_bytes tx_bytes
82 percent_cpu percent_mem time name;
87 | name, Active dom -> dump_domain name dom
88 | name, Inactive -> ()