Split up huge Top module into smaller modules.
[virt-top.git] / src / 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 let (//) = Filename.concat
25
26 (* Int64 operators for convenience. *)
27 let (+^) = Int64.add
28 let (-^) = Int64.sub
29 let ( *^ ) = Int64.mul
30 let (/^) = Int64.div
31
32 (* failwithf is a printf-like version of failwith. *)
33 let failwithf fs = ksprintf failwith fs
34
35 (* Input a whole file as a list of lines. *)
36 let input_all_lines chan =
37   let lines = ref [] in
38   (try
39      while true; do
40        lines := input_line chan :: !lines
41      done
42    with
43      End_of_file -> ());
44   List.rev !lines
45
46 (* Trim whitespace from the beginning and end of strings. *)
47 let isspace c =
48   c = ' '
49   (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
50
51 let triml ?(test = isspace) str =
52   let i = ref 0 in
53   let n = ref (String.length str) in
54   while !n > 0 && test str.[!i]; do
55     decr n;
56     incr i
57   done;
58   if !i = 0 then str
59   else String.sub str !i !n
60
61 let trimr ?(test = isspace) str =
62   let n = ref (String.length str) in
63   while !n > 0 && test str.[!n-1]; do
64     decr n
65   done;
66   if !n = String.length str then str
67   else String.sub str 0 !n
68
69 let trim ?(test = isspace) str =
70   trimr (triml str)
71
72 (* Read a configuration file as a list of (key, value) pairs.
73  * If the config file is missing this returns an empty list.
74  *)
75 let blanks_and_comments = Str.regexp "^[ \t]*\\(#.*\\)?$"
76
77 let read_config_file filename =
78   let lines =
79     try
80       let chan = open_in filename in
81       let lines = input_all_lines chan in
82       close_in chan;
83       lines
84     with
85       Sys_error _ -> [] in           (* Ignore errors opening file. *)
86
87   (* Line numbers. *)
88   let lines =
89     let i = ref 0 in List.map (fun line -> (incr i; !i), line) lines in
90
91   (* Remove blank lines and comment lines. *)
92   let lines =
93     List.filter
94       (fun (lineno, line) ->
95          not (Str.string_match blanks_and_comments line 0)) lines in
96
97   (* Convert to key, value pairs. *)
98   List.map (
99     fun (lineno, line) ->
100       let key, value = ExtString.String.split line " " in
101       lineno, trim key, trim value
102   ) lines
103
104 (* Pad a string to the full width with spaces.  If too long, truncate. *)
105 let pad width str =
106   if width <= 0 then ""
107   else (
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) ' '
112   )
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