1 (* 'top'-like tool for libvirt domains.
2 (C) Copyright 2007-2009 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.
19 This file contains utility functions.
24 open Opt_gettext.Gettext
26 module C = Libvirt.Connect
27 module D = Libvirt.Domain
28 module N = Libvirt.Network
30 let (//) = Filename.concat
32 (* Int64 operators for convenience. *)
35 let ( *^ ) = Int64.mul
38 (* failwithf is a printf-like version of failwith. *)
39 let failwithf fs = ksprintf failwith fs
41 (* Input a whole file as a list of lines. *)
42 let input_all_lines chan =
46 lines := input_line chan :: !lines
52 (* Trim whitespace from the beginning and end of strings. *)
55 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
57 let triml ?(test = isspace) str =
59 let n = ref (String.length str) in
60 while !n > 0 && test str.[!i]; do
65 else String.sub str !i !n
67 let trimr ?(test = isspace) str =
68 let n = ref (String.length str) in
69 while !n > 0 && test str.[!n-1]; do
72 if !n = String.length str then str
73 else String.sub str 0 !n
75 let trim ?(test = isspace) str =
78 (* Read a configuration file as a list of (key, value) pairs.
79 * If the config file is missing this returns an empty list.
81 let blanks_and_comments = Str.regexp "^[ \t]*\\(#.*\\)?$"
83 let read_config_file filename =
86 let chan = open_in filename in
87 let lines = input_all_lines chan in
91 Sys_error _ -> [] in (* Ignore errors opening file. *)
95 let i = ref 0 in List.map (fun line -> (incr i; !i), line) lines in
97 (* Remove blank lines and comment lines. *)
100 (fun (lineno, line) ->
101 not (Str.string_match blanks_and_comments line 0)) lines in
103 (* Convert to key, value pairs. *)
105 fun (lineno, line) ->
106 let key, value = ExtString.String.split line " " in
107 lineno, trim key, trim value
110 (* Pad a string to the full width with spaces. If too long, truncate. *)
112 if width <= 0 then ""
114 let n = String.length str in
115 if n = width then str
116 else if n > width then String.sub str 0 width
117 else (* if n < width then *) str ^ String.make (width-n) ' '
121 (* Show a percentage in 4 chars. *)
122 let percent percent =
123 if percent <= 0. then " 0.0"
124 else if percent <= 9.9 then sprintf " %1.1f" percent
125 else if percent <= 99.9 then sprintf "%2.1f" percent
128 (* Show an int64 option in 4 chars. *)
129 let rec int64_option = function
132 (* Show an int64 in 4 chars. *)
134 | n when n < 0L -> "-!!!"
135 | n when n <= 9999L ->
137 | n when n /^ 1024L <= 999L ->
138 sprintf "%3LdK" (n /^ 1024L)
139 | n when n /^ 1_048_576L <= 999L ->
140 sprintf "%3LdM" (n /^ 1_048_576L)
141 | n when n /^ 1_073_741_824L <= 999L ->
142 sprintf "%3LdG" (n /^ 1_073_741_824L)
145 (* Format the total time (may be large!) in 9 chars. *)
147 let secs_in_ns = 1_000_000_000L in
148 let mins_in_ns = 60_000_000_000L in
149 let hours_in_ns = 3_600_000_000_000L in
151 let hours = ns /^ hours_in_ns in
152 let ns = ns -^ (hours *^ hours_in_ns) in
153 let mins = ns /^ mins_in_ns in
154 let ns = ns -^ (mins *^ mins_in_ns) in
155 let secs = ns /^ secs_in_ns in
156 let ns = ns -^ (secs *^ secs_in_ns) in
157 let pennies = ns /^ 10_000_000L in
160 sprintf "%3Ld:%02Ld.%02Ld" (hours *^ 60L +^ mins) secs pennies
161 else if hours <= 999L then
162 sprintf "%3Ld:%02Ld:%02Ld" hours mins secs
164 let days = hours /^ 24L in
165 let hours = hours -^ (days *^ 24L) in
166 sprintf "%3Ldd%02Ld:%02Ld" days hours mins
170 (* Sum Domain.block_stats structures together. Missing fields
171 * get forced to 0. Empty list returns all 0.
173 let zero_block_stats =
174 { D.rd_req = 0L; rd_bytes = 0L; wr_req = 0L; wr_bytes = 0L; errs = 0L }
175 let add_block_stats bs1 bs2 =
176 let add f1 f2 = if f1 >= 0L && f2 >= 0L then f1 +^ f2 else 0L in
177 { D.rd_req = add bs1.D.rd_req bs2.D.rd_req;
178 rd_bytes = add bs1.D.rd_bytes bs2.D.rd_bytes;
179 wr_req = add bs1.D.wr_req bs2.D.wr_req;
180 wr_bytes = add bs1.D.wr_bytes bs2.D.wr_bytes;
181 errs = add bs1.D.errs bs2.D.errs }
182 let sum_block_stats =
183 List.fold_left add_block_stats zero_block_stats
185 (* Get the difference between two block_stats structures. Missing data
186 * forces the difference to -1.
188 let diff_block_stats curr prev =
189 let sub f1 f2 = if f1 >= 0L && f2 >= 0L then f1 -^ f2 else -1L in
190 { D.rd_req = sub curr.D.rd_req prev.D.rd_req;
191 rd_bytes = sub curr.D.rd_bytes prev.D.rd_bytes;
192 wr_req = sub curr.D.wr_req prev.D.wr_req;
193 wr_bytes = sub curr.D.wr_bytes prev.D.wr_bytes;
194 errs = sub curr.D.errs prev.D.errs }
196 (* Sum Domain.interface_stats structures together. Missing fields
197 * get forced to 0. Empty list returns all 0.
199 let zero_interface_stats =
200 { D.rx_bytes = 0L; rx_packets = 0L; rx_errs = 0L; rx_drop = 0L;
201 tx_bytes = 0L; tx_packets = 0L; tx_errs = 0L; tx_drop = 0L }
202 let add_interface_stats is1 is2 =
203 let add f1 f2 = if f1 >= 0L && f2 >= 0L then f1 +^ f2 else 0L in
204 { D.rx_bytes = add is1.D.rx_bytes is2.D.rx_bytes;
205 rx_packets = add is1.D.rx_packets is2.D.rx_packets;
206 rx_errs = add is1.D.rx_errs is2.D.rx_errs;
207 rx_drop = add is1.D.rx_drop is2.D.rx_drop;
208 tx_bytes = add is1.D.tx_bytes is2.D.tx_bytes;
209 tx_packets = add is1.D.tx_packets is2.D.tx_packets;
210 tx_errs = add is1.D.tx_errs is2.D.tx_errs;
211 tx_drop = add is1.D.tx_drop is2.D.tx_drop }
212 let sum_interface_stats =
213 List.fold_left add_interface_stats zero_interface_stats
215 (* Get the difference between two interface_stats structures.
216 * Missing data forces the difference to -1.
218 let diff_interface_stats curr prev =
219 let sub f1 f2 = if f1 >= 0L && f2 >= 0L then f1 -^ f2 else -1L in
220 { D.rx_bytes = sub curr.D.rx_bytes prev.D.rx_bytes;
221 rx_packets = sub curr.D.rx_packets prev.D.rx_packets;
222 rx_errs = sub curr.D.rx_errs prev.D.rx_errs;
223 rx_drop = sub curr.D.rx_drop prev.D.rx_drop;
224 tx_bytes = sub curr.D.tx_bytes prev.D.tx_bytes;
225 tx_packets = sub curr.D.tx_packets prev.D.tx_packets;
226 tx_errs = sub curr.D.tx_errs prev.D.tx_errs;
227 tx_drop = sub curr.D.tx_drop prev.D.tx_drop }