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