- let rec find_cpu_usages = function
- | ("cpu_time", D.TypedFieldUInt64 usages) :: _ -> usages
- | _ :: params -> find_cpu_usages params
- | [] -> 0L in
+
+ (* Note the terminology is confusing.
+ *
+ * In libvirt, cpu_time is the total time (hypervisor + vCPU).
+ * vcpu_time is the time only taken by the vCPU,
+ * excluding time taken inside the hypervisor.
+ *
+ * For each pCPU, libvirt may return either "cpu_time"
+ * or "vcpu_time" or neither or both. This function
+ * returns an array pair [|cpu_time, vcpu_time|];
+ * if either is missing it is returned as 0.
+ *)
+ let find_cpu_usages params =
+ let rec find_uint64_field name = function
+ | (n, D.TypedFieldUInt64 usage) :: _ when n = name -> usage
+ | _ :: params -> find_uint64_field name params
+ | [] -> 0L
+ in
+ [| find_uint64_field "cpu_time" params;
+ find_uint64_field "vcpu_time" params |]
+ in
+