+ let cpu_percent =
+ if time_prev > 0. then (
+ let cpu_now = Int64.to_float cpu_now in
+ let cpu_prev = Int64.to_float cpu_prev in
+ let cpu_used = cpu_now -. cpu_prev in
+ let cpu_available = 1_000_000_000. *. float nr_cpus in
+ let time_passed = time_now -. time_prev in
+
+ let cpu_percent =
+ 100. *. (cpu_used /. cpu_available) /. time_passed in
+
+ let cpu_percent_str = sprintf "%.1f %%" cpu_percent in
+ model#set ~row ~column:col_cpu cpu_percent_str;
+ int_of_float cpu_percent
+ ) else -1 in
+
+ (* Store history. *)
+ let store arr posn datum =
+ if posn >= hist_max then (
+ (* rotate the array *)
+ Array.blit arr hist_rot arr 0 (hist_max - hist_rot);
+ let posn = posn - hist_rot in
+ arr.(posn) <- datum;
+ (arr, posn+1)
+ ) else (
+ let len = Array.length arr in
+ if posn < len then (
+ (* normal update *)
+ arr.(posn) <- datum;
+ (arr, posn+1)
+ ) else (
+ (* extend the array *)
+ let len' = min (max (2*len) 1) hist_max in
+ let arr' = Array.make len' datum in
+ Array.blit arr 0 arr' 0 len;
+ (arr', posn+1)
+ )
+ )
+ in
+ let hist_cpu, hist_cpu_posn =
+ store dh.hist_cpu dh.hist_cpu_posn cpu_percent in
+ dh.hist_cpu <- hist_cpu; dh.hist_cpu_posn <- hist_cpu_posn;
+ let hist_mem, hist_mem_posn =
+ store dh.hist_mem dh.hist_mem_posn info.D.memory in
+ dh.hist_mem <- hist_mem; dh.hist_mem_posn <- hist_mem_posn
+
+ with
+ Libvirt.Virterror _ -> () (* Ignore any transient error *)