-(* Callback function to open a connection.
- * This should be a lot more sophisticated. XXX
- *)
-let open_connection () =
- let title = "Open connection to hypervisor" in
- let uri =
- GToolbox.input_string ~title ~text:"xen:///" ~ok:"Open" "Connection:" in
- match uri with
- | None -> ()
- | Some uri ->
- (* If this fails, let the exception escape and be printed
- * in the global exception handler.
- *)
- let conn = C.connect ~name:uri () in
-
- let node_info = C.get_node_info conn in
- let hostname =
- try Some (C.get_hostname conn)
- with
- | Libvirt.Not_supported "virConnectGetHostname"
- | Libvirt.Virterror _ -> None in
-
- (* Add it to our list of connections. *)
- let conn_id = add_conn conn in
- Hashtbl.add static_conn_info conn_id (node_info, hostname, uri)
+(* Get historical data size. *)
+let get_hist_size connid domid =
+ try
+ let dh = Hashtbl.find domhistory (connid, domid) in
+ dh.hist_posn
+ with
+ Not_found -> 0
+
+(* Get historical data entries. *)
+let _get_hist ?(latest=0) ?earliest ?(granularity=1)
+ extract fold zero connid domid =
+ try
+ let dh = Hashtbl.find domhistory (connid, domid) in
+ let earliest =
+ match earliest with
+ | None -> dh.hist_posn
+ | Some e -> min e dh.hist_posn in
+
+ let src = dh.hist in
+ let src_start = dh.hist_posn - earliest in assert (src_start >= 0);
+ let src_end = dh.hist_posn - latest in assert (src_end <= dh.hist_posn);
+
+ (* Create a sufficiently large array to store the result. *)
+ let len = (earliest-latest) / granularity in
+ let r = Array.make len zero in
+
+ if granularity = 1 then (
+ for j = 0 to len-1 do
+ r.(j) <- extract src.(src_start+j)
+ done
+ ) else (
+ let i = ref src_start in
+ for j = 0 to len-1 do
+ let sub = Array.sub src !i (min (!i+granularity) src_end - !i) in
+ let sub = Array.map extract sub in
+ r.(j) <- fold sub;
+ i := !i + granularity
+ done
+ );
+ r
+ with
+ Not_found -> [| |]
+
+let get_hist_cpu ?latest ?earliest ?granularity connid domid =
+ let zero = 0 in
+ let extract { hist_cpu = c } = c in
+ let fold a =
+ let len = Array.length a in
+ if len > 0 then Array.fold_left (+) zero a / len else -1 in
+ _get_hist ?latest ?earliest ?granularity extract fold zero connid domid
+
+let get_hist_mem ?latest ?earliest ?granularity connid domid =
+ let zero = 0L in
+ let extract { hist_mem = m } = m in
+ let fold a =
+ let len = Array.length a in
+ if len > 0 then
+ Int64.div (Array.fold_left (Int64.add) zero a) (Int64.of_int len)
+ else
+ -1L in
+ _get_hist ?latest ?earliest ?granularity extract fold zero connid domid