remove parameter nr_pcpus of Libvirt.Domain.get_cpu_stats
[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     let nr_pcpus =
23       let info = C.get_node_info conn in
24       C.maxcpus_of_node_info info in
25
26     let stats =
27       let dom = D.lookup_by_name conn domname in
28       D.get_cpu_stats dom in
29
30     Array.iteri (
31       fun n params ->
32         printf "pCPU %d:" n;
33         List.iter (
34           fun (name, value) ->
35             printf " %s=" name;
36             match value with
37             | D.TypedFieldInt32 i -> printf "%ld" i
38             | D.TypedFieldUInt32 i -> printf "%ld" i
39             | D.TypedFieldInt64 i -> printf "%Ld" i
40             | D.TypedFieldUInt64 i -> printf "%Ld" i
41             | D.TypedFieldFloat f -> printf "%g" f
42             | D.TypedFieldBool b -> printf "%b" b
43             | D.TypedFieldString s -> printf "%S" s
44         ) params;
45         printf "\n"
46     ) stats
47   with
48     Libvirt.Virterror err ->
49       eprintf "error: %s\n" (Libvirt.Virterror.to_string err)
50
51 let () =
52   (* Run the garbage collector which is a good way to check for
53    * memory corruption errors and reference counting issues in libvirt.
54    *)
55   Gc.compact ()