Use -g -warn-error.
[ocaml-libvirt.git] / examples / get_cpu_stats.ml
1 (* List CPU stats for a domain.
2  * Usage: get_cpu_stats domain
3  * http://libvirt.org/
4  *)
5
6 open Printf
7
8 module C = Libvirt.Connect
9 module D = Libvirt.Domain
10 module N = Libvirt.Network
11
12 let () =
13   try
14     if Array.length Sys.argv <> 2 then (
15       eprintf "error: get_cpu_stats domain\n";
16       exit 1
17     );
18     let domname = Sys.argv.(1) in
19
20     let conn = C.connect_readonly () in
21
22     (*
23     let nr_pcpus =
24       let info = C.get_node_info conn in
25       C.maxcpus_of_node_info info in
26      *)
27
28     let stats =
29       let dom = D.lookup_by_name conn domname in
30       D.get_cpu_stats dom in
31
32     Array.iteri (
33       fun n params ->
34         printf "pCPU %d:" n;
35         List.iter (
36           fun (name, value) ->
37             printf " %s=" name;
38             match value with
39             | D.TypedFieldInt32 i -> printf "%ld" i
40             | D.TypedFieldUInt32 i -> printf "%ld" i
41             | D.TypedFieldInt64 i -> printf "%Ld" i
42             | D.TypedFieldUInt64 i -> printf "%Ld" i
43             | D.TypedFieldFloat f -> printf "%g" f
44             | D.TypedFieldBool b -> printf "%b" b
45             | D.TypedFieldString s -> printf "%S" s
46         ) params;
47         printf "\n"
48     ) stats
49   with
50     Libvirt.Virterror err ->
51       eprintf "error: %s\n" (Libvirt.Virterror.to_string err)
52
53 let () =
54   (* Run the garbage collector which is a good way to check for
55    * memory corruption errors and reference counting issues in libvirt.
56    *)
57   Gc.compact ()