Make '--version' flag work.
[virt-mem.git] / lib / virt_mem_utils.ml
1 (* Memory info command for virtual domains.
2    (C) Copyright 2008 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    Common & utility functions.
20  *)
21
22 open Printf
23
24 let ( +^ ) = Int64.add
25 let ( -^ ) = Int64.sub
26 let ( *^ ) = Int64.mul
27 let ( /^ ) = Int64.div
28 let ( &^ ) = Int64.logand
29 let ( |^ ) = Int64.logor
30
31 type architecture =
32   | I386 | X86_64 | IA64 | PPC | PPC64 | SPARC | SPARC64
33
34 let string_of_architecture = function
35   | I386 -> "i386"
36   | X86_64 -> "x86_64"
37   | IA64 -> "ia64"
38   | PPC -> "ppc"
39   | PPC64 -> "ppc64"
40   | SPARC -> "sparc"
41   | SPARC64 -> "sparc64"
42
43 let architecture_of_string = function
44   | str when
45       String.length str = 4 &&
46       (str.[0] = 'i' || str.[0] = 'I') &&
47       (str.[1] >= '3' && str.[1] <= '6') &&
48       str.[2] = '8' && str.[3] = '6' -> I386
49   | "x86_64" | "X86_64" | "x86-64" | "X86-64" -> X86_64
50   | "ia64" | "IA64" -> IA64
51   | "ppc" | "PPC" | "ppc32" | "PPC32" -> PPC
52   | "ppc64" | "PPC64" -> PPC64
53   | "sparc" | "SPARC" | "sparc32" | "SPARC32" -> SPARC
54   | "sparc64" | "SPARC64" -> SPARC64
55   | str ->
56       failwith (sprintf "architecture_of_string: %s: unknown architecture"
57                   str)
58
59 let endian_of_architecture = function
60   | I386 | X86_64 -> Bitmatch.LittleEndian
61   | IA64 -> Bitmatch.LittleEndian (* XXX usually? *)
62   | PPC | PPC64 | SPARC | SPARC64 -> Bitmatch.BigEndian
63
64 type wordsize =
65   | W32 | W64
66
67 let wordsize_of_architecture = function
68   | I386 -> W32
69   | X86_64 -> W64
70   | IA64 -> W64
71   | PPC -> W32
72   | PPC64 -> W64
73   | SPARC -> W32
74   | SPARC64 -> W64
75
76 let bits_of_wordsize = function
77   | W32 -> 32 | W64 -> 64
78 let bytes_of_wordsize = function
79   | W32 -> 4 | W64 -> 8
80
81 (* Returns (count, value) in order of highest frequency occurring in the
82  * list.
83  *)
84 let frequency xs =
85   let xs = List.sort compare xs in
86   let rec loop = function
87     | [] -> []
88     | [x] -> [1, x]
89     | x :: y :: xs when x = y ->
90         let rest = loop (y :: xs) in
91         let (count, _), rest = List.hd rest, List.tl rest in
92         (count+1, y) :: rest
93     | x :: xs ->
94         (1, x) :: loop xs
95   in
96   let xs = loop xs in
97   List.rev (List.sort compare xs)