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