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 let (//) = Filename.concat
26 (* Int64 operators for convenience. *)
29 let ( *^ ) = Int64.mul
32 (* failwithf is a printf-like version of failwith. *)
33 let failwithf fs = ksprintf failwith fs
35 (* Input a whole file as a list of lines. *)
36 let input_all_lines chan =
40 lines := input_line chan :: !lines
46 (* Trim whitespace from the beginning and end of strings. *)
49 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
51 let triml ?(test = isspace) str =
53 let n = ref (String.length str) in
54 while !n > 0 && test str.[!i]; do
59 else String.sub str !i !n
61 let trimr ?(test = isspace) str =
62 let n = ref (String.length str) in
63 while !n > 0 && test str.[!n-1]; do
66 if !n = String.length str then str
67 else String.sub str 0 !n
69 let trim ?(test = isspace) str =
72 (* Read a configuration file as a list of (key, value) pairs.
73 * If the config file is missing this returns an empty list.
75 let blanks_and_comments = Str.regexp "^[ \t]*\\(#.*\\)?$"
77 let read_config_file filename =
80 let chan = open_in filename in
81 let lines = input_all_lines chan in
85 Sys_error _ -> [] in (* Ignore errors opening file. *)
89 let i = ref 0 in List.map (fun line -> (incr i; !i), line) lines in
91 (* Remove blank lines and comment lines. *)
94 (fun (lineno, line) ->
95 not (Str.string_match blanks_and_comments line 0)) lines in
97 (* Convert to key, value pairs. *)
100 let key, value = ExtString.String.split line " " in
101 lineno, trim key, trim value
104 (* Pad a string to the full width with spaces. If too long, truncate. *)
106 if width <= 0 then ""
108 let n = String.length str in
109 if n = width then str
110 else if n > width then String.sub str 0 width
111 else (* if n < width then *) str ^ String.make (width-n) ' '
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
122 (* Show an int64 option in 4 chars. *)
123 let rec int64_option = function
126 (* Show an int64 in 4 chars. *)
128 | n when n < 0L -> "-!!!"
129 | n when n <= 9999L ->
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)
139 (* Format the total time (may be large!) in 9 chars. *)
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
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
154 sprintf "%3Ld:%02Ld.%02Ld" (hours *^ 60L +^ mins) secs pennies
155 else if hours <= 999L then
156 sprintf "%3Ld:%02Ld:%02Ld" hours mins secs
158 let days = hours /^ 24L in
159 let hours = hours -^ (days *^ 24L) in
160 sprintf "%3Ldd%02Ld:%02Ld" days hours mins