Rename te_IN.po -> te.po (Piotr Drąg).
[virt-top.git] / virt-top / virt_top_utils.ml
1 (* 'top'-like tool for libvirt domains.
2    (C) Copyright 2007-2009 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    This file contains utility functions.
20 *)
21
22 open Printf
23
24 open Virt_top_gettext.Gettext
25
26 module C = Libvirt.Connect
27 module D = Libvirt.Domain
28 module N = Libvirt.Network
29
30 let (//) = Filename.concat
31
32 (* Int64 operators for convenience. *)
33 let (+^) = Int64.add
34 let (-^) = Int64.sub
35 let ( *^ ) = Int64.mul
36 let (/^) = Int64.div
37
38 (* failwithf is a printf-like version of failwith. *)
39 let failwithf fs = ksprintf failwith fs
40
41 (* Input a whole file as a list of lines. *)
42 let input_all_lines chan =
43   let lines = ref [] in
44   (try
45      while true; do
46        lines := input_line chan :: !lines
47      done
48    with
49      End_of_file -> ());
50   List.rev !lines
51
52 (* Trim whitespace from the beginning and end of strings. *)
53 let isspace c =
54   c = ' '
55   (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
56
57 let triml ?(test = isspace) str =
58   let i = ref 0 in
59   let n = ref (String.length str) in
60   while !n > 0 && test str.[!i]; do
61     decr n;
62     incr i
63   done;
64   if !i = 0 then str
65   else String.sub str !i !n
66
67 let trimr ?(test = isspace) str =
68   let n = ref (String.length str) in
69   while !n > 0 && test str.[!n-1]; do
70     decr n
71   done;
72   if !n = String.length str then str
73   else String.sub str 0 !n
74
75 let trim ?(test = isspace) str =
76   trimr (triml str)
77
78 (* Read a configuration file as a list of (key, value) pairs.
79  * If the config file is missing this returns an empty list.
80  *)
81 let blanks_and_comments = Str.regexp "^[ \t]*\\(#.*\\)?$"
82
83 let read_config_file filename =
84   let lines =
85     try
86       let chan = open_in filename in
87       let lines = input_all_lines chan in
88       close_in chan;
89       lines
90     with
91       Sys_error _ -> [] in           (* Ignore errors opening file. *)
92
93   (* Line numbers. *)
94   let lines =
95     let i = ref 0 in List.map (fun line -> (incr i; !i), line) lines in
96
97   (* Remove blank lines and comment lines. *)
98   let lines =
99     List.filter
100       (fun (lineno, line) ->
101          not (Str.string_match blanks_and_comments line 0)) lines in
102
103   (* Convert to key, value pairs. *)
104   List.map (
105     fun (lineno, line) ->
106       let key, value = ExtString.String.split line " " in
107       lineno, trim key, trim value
108   ) lines
109
110 (* Pad a string to the full width with spaces.  If too long, truncate. *)
111 let pad width str =
112   let n = String.length str in
113   if n = width then str
114   else if n > width then String.sub str 0 width
115   else (* if n < width then *) str ^ String.make (width-n) ' '
116
117 module Show = struct
118   (* Show a percentage in 4 chars. *)
119   let percent percent =
120     if percent <= 0. then " 0.0"
121     else if percent <= 9.9 then sprintf " %1.1f" percent
122     else if percent <= 99.9 then sprintf "%2.1f" percent
123     else "100 "
124
125   (* Show an int64 option in 4 chars. *)
126   let rec int64_option = function
127     | None -> "    "
128     | Some n -> int64 n
129   (* Show an int64 in 4 chars. *)
130   and int64 = function
131     | n when n < 0L -> "-!!!"
132     | n when n <= 9999L ->
133         sprintf "%4Ld" n
134     | n when n /^ 1024L <= 999L ->
135         sprintf "%3LdK" (n /^ 1024L)
136     | n when n /^ 1_048_576L <= 999L ->
137         sprintf "%3LdM" (n /^ 1_048_576L)
138     | n when n /^ 1_073_741_824L <= 999L ->
139         sprintf "%3LdG" (n /^ 1_073_741_824L)
140     | _ -> ">!!!"
141
142   (* Format the total time (may be large!) in 9 chars. *)
143   let time ns =
144     let secs_in_ns = 1_000_000_000L in
145     let mins_in_ns = 60_000_000_000L in
146     let hours_in_ns = 3_600_000_000_000L in
147
148     let hours = ns /^ hours_in_ns in
149     let ns = ns -^ (hours *^ hours_in_ns) in
150     let mins = ns /^ mins_in_ns in
151     let ns = ns -^ (mins *^ mins_in_ns) in
152     let secs = ns /^ secs_in_ns in
153     let ns = ns -^ (secs *^ secs_in_ns) in
154     let pennies = ns /^ 10_000_000L in
155
156     if hours < 12L then
157       sprintf "%3Ld:%02Ld.%02Ld" (hours *^ 60L +^ mins) secs pennies
158     else if hours <= 999L then
159       sprintf "%3Ld:%02Ld:%02Ld" hours mins secs
160     else (
161       let days = hours /^ 24L in
162       let hours = hours -^ (days *^ 24L) in
163       sprintf "%3Ldd%02Ld:%02Ld" days hours mins
164     )
165 end
166
167 (* Sum Domain.block_stats structures together.  Missing fields
168  * get forced to 0.  Empty list returns all 0.
169  *)
170 let zero_block_stats =
171   { D.rd_req = 0L; rd_bytes = 0L; wr_req = 0L; wr_bytes = 0L; errs = 0L }
172 let add_block_stats bs1 bs2 =
173   let add f1 f2 = if f1 >= 0L && f2 >= 0L then f1 +^ f2 else 0L in
174   { D.rd_req = add bs1.D.rd_req   bs2.D.rd_req;
175     rd_bytes = add bs1.D.rd_bytes bs2.D.rd_bytes;
176     wr_req   = add bs1.D.wr_req   bs2.D.wr_req;
177     wr_bytes = add bs1.D.wr_bytes bs2.D.wr_bytes;
178     errs     = add bs1.D.errs     bs2.D.errs }
179 let sum_block_stats =
180   List.fold_left add_block_stats zero_block_stats
181
182 (* Get the difference between two block_stats structures.  Missing data
183  * forces the difference to -1.
184  *)
185 let diff_block_stats curr prev =
186   let sub f1 f2 = if f1 >= 0L && f2 >= 0L then f1 -^ f2 else -1L in
187   { D.rd_req = sub curr.D.rd_req   prev.D.rd_req;
188     rd_bytes = sub curr.D.rd_bytes prev.D.rd_bytes;
189     wr_req   = sub curr.D.wr_req   prev.D.wr_req;
190     wr_bytes = sub curr.D.wr_bytes prev.D.wr_bytes;
191     errs     = sub curr.D.errs     prev.D.errs }
192
193 (* Sum Domain.interface_stats structures together.  Missing fields
194  * get forced to 0.  Empty list returns all 0.
195  *)
196 let zero_interface_stats =
197   { D.rx_bytes = 0L; rx_packets = 0L; rx_errs = 0L; rx_drop = 0L;
198     tx_bytes = 0L; tx_packets = 0L; tx_errs = 0L; tx_drop = 0L }
199 let add_interface_stats is1 is2 =
200   let add f1 f2 = if f1 >= 0L && f2 >= 0L then f1 +^ f2 else 0L in
201   { D.rx_bytes = add is1.D.rx_bytes   is2.D.rx_bytes;
202     rx_packets = add is1.D.rx_packets is2.D.rx_packets;
203     rx_errs    = add is1.D.rx_errs    is2.D.rx_errs;
204     rx_drop    = add is1.D.rx_drop    is2.D.rx_drop;
205     tx_bytes   = add is1.D.tx_bytes   is2.D.tx_bytes;
206     tx_packets = add is1.D.tx_packets is2.D.tx_packets;
207     tx_errs    = add is1.D.tx_errs    is2.D.tx_errs;
208     tx_drop    = add is1.D.tx_drop    is2.D.tx_drop }
209 let sum_interface_stats =
210   List.fold_left add_interface_stats zero_interface_stats
211
212 (* Get the difference between two interface_stats structures.
213  * Missing data forces the difference to -1.
214  *)
215 let diff_interface_stats curr prev =
216   let sub f1 f2 = if f1 >= 0L && f2 >= 0L then f1 -^ f2 else -1L in
217   { D.rx_bytes = sub curr.D.rx_bytes   prev.D.rx_bytes;
218     rx_packets = sub curr.D.rx_packets prev.D.rx_packets;
219     rx_errs    = sub curr.D.rx_errs    prev.D.rx_errs;
220     rx_drop    = sub curr.D.rx_drop    prev.D.rx_drop;
221     tx_bytes   = sub curr.D.tx_bytes   prev.D.tx_bytes;
222     tx_packets = sub curr.D.tx_packets prev.D.tx_packets;
223     tx_errs    = sub curr.D.tx_errs    prev.D.tx_errs;
224     tx_drop    = sub curr.D.tx_drop    prev.D.tx_drop }