Move libraries into subdirectory, programs into their own subdirs.
[virt-mem.git] / lib / virt_mem_utils.ml
diff --git a/lib/virt_mem_utils.ml b/lib/virt_mem_utils.ml
new file mode 100644 (file)
index 0000000..db759dd
--- /dev/null
@@ -0,0 +1,97 @@
+(* Memory info command for virtual domains.
+   (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
+   http://libvirt.org/
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+   Common & utility functions.
+ *)
+
+open Printf
+
+let ( +^ ) = Int64.add
+let ( -^ ) = Int64.sub
+let ( *^ ) = Int64.mul
+let ( /^ ) = Int64.div
+let ( &^ ) = Int64.logand
+let ( |^ ) = Int64.logor
+
+type architecture =
+  | I386 | X86_64 | IA64 | PPC | PPC64 | SPARC | SPARC64
+
+let string_of_architecture = function
+  | I386 -> "i386"
+  | X86_64 -> "x86_64"
+  | IA64 -> "ia64"
+  | PPC -> "ppc"
+  | PPC64 -> "ppc64"
+  | SPARC -> "sparc"
+  | SPARC64 -> "sparc64"
+
+let architecture_of_string = function
+  | str when
+      String.length str = 4 &&
+      (str.[0] = 'i' || str.[0] = 'I') &&
+      (str.[1] >= '3' && str.[1] <= '6') &&
+      str.[2] = '8' && str.[3] = '6' -> I386
+  | "x86_64" | "X86_64" | "x86-64" | "X86-64" -> X86_64
+  | "ia64" | "IA64" -> IA64
+  | "ppc" | "PPC" | "ppc32" | "PPC32" -> PPC
+  | "ppc64" | "PPC64" -> PPC64
+  | "sparc" | "SPARC" | "sparc32" | "SPARC32" -> SPARC
+  | "sparc64" | "SPARC64" -> SPARC64
+  | str ->
+      failwith (sprintf "architecture_of_string: %s: unknown architecture"
+                 str)
+
+let endian_of_architecture = function
+  | I386 | X86_64 -> Bitmatch.LittleEndian
+  | IA64 -> Bitmatch.LittleEndian (* XXX usually? *)
+  | PPC | PPC64 | SPARC | SPARC64 -> Bitmatch.BigEndian
+
+type wordsize =
+  | W32 | W64
+
+let wordsize_of_architecture = function
+  | I386 -> W32
+  | X86_64 -> W64
+  | IA64 -> W64
+  | PPC -> W32
+  | PPC64 -> W64
+  | SPARC -> W32
+  | SPARC64 -> W64
+
+let bits_of_wordsize = function
+  | W32 -> 32 | W64 -> 64
+let bytes_of_wordsize = function
+  | W32 -> 4 | W64 -> 8
+
+(* Returns (count, value) in order of highest frequency occurring in the
+ * list.
+ *)
+let frequency xs =
+  let xs = List.sort compare xs in
+  let rec loop = function
+    | [] -> []
+    | [x] -> [1, x]
+    | x :: y :: xs when x = y ->
+        let rest = loop (y :: xs) in
+        let (count, _), rest = List.hd rest, List.tl rest in
+        (count+1, y) :: rest
+    | x :: xs ->
+        (1, x) :: loop xs
+  in
+  let xs = loop xs in
+  List.rev (List.sort compare xs)