Blame SOURCES/0003-Add-a-binding-for-virConnectGetAllDomainStats-RHBZ-1.patch

bda309
From 380f1e05b244ae4750ca5101b5b5a182dcd0d1fd Mon Sep 17 00:00:00 2001
bda309
From: "Richard W.M. Jones" <rjones@redhat.com>
bda309
Date: Tue, 28 Mar 2017 10:08:06 +0100
bda309
Subject: [PATCH 3/5] Add a binding for virConnectGetAllDomainStats
bda309
 (RHBZ#1390171).
bda309
bda309
---
bda309
 .gitignore                       |   2 +
bda309
 Makefile.in                      |   1 +
bda309
 examples/.depend                 |   2 +
bda309
 examples/Makefile.in             |  13 ++++-
bda309
 examples/get_all_domain_stats.ml |  65 +++++++++++++++++++++
bda309
 libvirt/libvirt.ml               |  23 ++++++++
bda309
 libvirt/libvirt.mli              |  28 +++++++++
bda309
 libvirt/libvirt_c_oneoffs.c      | 119 ++++++++++++++++++++++++++++++++++++++-
bda309
 8 files changed, 250 insertions(+), 3 deletions(-)
bda309
 create mode 100644 examples/get_all_domain_stats.ml
bda309
bda309
diff --git a/.gitignore b/.gitignore
bda309
index 71a245e..366eb29 100644
bda309
--- a/.gitignore
bda309
+++ b/.gitignore
bda309
@@ -1,3 +1,4 @@
bda309
+.gdb_history
bda309
 META
bda309
 ocaml-libvirt-*.tar.gz
bda309
 ocaml-libvirt-*.exe
bda309
@@ -27,6 +28,7 @@ core.*
bda309
 *~
bda309
 libvirt/libvirt_version.ml
bda309
 examples/domain_events
bda309
+examples/get_all_domain_stats
bda309
 examples/get_cpu_stats
bda309
 examples/list_domains
bda309
 examples/node_info
bda309
diff --git a/Makefile.in b/Makefile.in
bda309
index 3b8b7ec..2605ddd 100644
bda309
--- a/Makefile.in
bda309
+++ b/Makefile.in
bda309
@@ -41,6 +41,7 @@ clean:
bda309
 	rm -f examples/node_info
bda309
 	rm -f examples/get_cpu_stats
bda309
 	rm -f examples/domain_events
bda309
+	rm -f examples/get_all_domain_stats
bda309
 
bda309
 distclean: clean
bda309
 	rm -f config.h config.log config.status configure
bda309
diff --git a/examples/.depend b/examples/.depend
bda309
index b5379d8..11f2c7c 100644
bda309
--- a/examples/.depend
bda309
+++ b/examples/.depend
bda309
@@ -1,5 +1,7 @@
bda309
 domain_events.cmo : ../libvirt/libvirt.cmi
bda309
 domain_events.cmx : ../libvirt/libvirt.cmx
bda309
+get_all_domain_stats.cmo : ../libvirt/libvirt.cmi
bda309
+get_all_domain_stats.cmx : ../libvirt/libvirt.cmx
bda309
 get_cpu_stats.cmo : ../libvirt/libvirt.cmi
bda309
 get_cpu_stats.cmx : ../libvirt/libvirt.cmx
bda309
 list_domains.cmo : ../libvirt/libvirt.cmi
bda309
diff --git a/examples/Makefile.in b/examples/Makefile.in
bda309
index 46006a0..8530edc 100644
bda309
--- a/examples/Makefile.in
bda309
+++ b/examples/Makefile.in
bda309
@@ -27,7 +27,8 @@ OCAMLOPTLIBS	:= $(OCAMLCLIBS)
bda309
 export LIBRARY_PATH=../libvirt
bda309
 export LD_LIBRARY_PATH=../libvirt
bda309
 
bda309
-BYTE_TARGETS	:= list_domains node_info get_cpu_stats domain_events
bda309
+BYTE_TARGETS	:= list_domains node_info get_cpu_stats \
bda309
+		   get_all_domain_stats domain_events
bda309
 OPT_TARGETS	:= $(BYTE_TARGETS:%=%.opt)
bda309
 
bda309
 all: $(BYTE_TARGETS)
bda309
@@ -64,6 +65,16 @@ get_cpu_stats.opt: get_cpu_stats.cmx
bda309
 	  $(OCAMLOPTPACKAGES) $(OCAMLOPTFLAGS) $(OCAMLOPTLIBS) \
bda309
 	  ../libvirt/mllibvirt.cmxa -o $@ $<
bda309
 
bda309
+get_all_domain_stats: get_all_domain_stats.cmo
bda309
+	$(OCAMLFIND) ocamlc \
bda309
+	  $(OCAMLCPACKAGES) $(OCAMLCFLAGS) $(OCAMLCLIBS) \
bda309
+	  ../libvirt/mllibvirt.cma -o $@ $<
bda309
+
bda309
+get_all_domain_stats.opt: get_all_domain_stats.cmx
bda309
+	$(OCAMLFIND) ocamlopt \
bda309
+	  $(OCAMLOPTPACKAGES) $(OCAMLOPTFLAGS) $(OCAMLOPTLIBS) \
bda309
+	  ../libvirt/mllibvirt.cmxa -o $@ $<
bda309
+
bda309
 domain_events: domain_events.cmo
bda309
 	$(OCAMLFIND) ocamlc \
bda309
 	  $(OCAMLCPACKAGES) $(OCAMLCFLAGS) $(OCAMLCLIBS) \
bda309
diff --git a/examples/get_all_domain_stats.ml b/examples/get_all_domain_stats.ml
bda309
new file mode 100644
bda309
index 0000000..4375639
bda309
--- /dev/null
bda309
+++ b/examples/get_all_domain_stats.ml
bda309
@@ -0,0 +1,65 @@
bda309
+(* Example of using Domain.get_all_domain_stats (virConnectGetAllDomainStats).
bda309
+ * Usage: get_all_domain_stats
bda309
+ * http://libvirt.org/
bda309
+ *)
bda309
+
bda309
+open Printf
bda309
+
bda309
+module C = Libvirt.Connect
bda309
+module D = Libvirt.Domain
bda309
+
bda309
+let print_stats stats =
bda309
+  try
bda309
+    Array.iter (
bda309
+      fun { D.dom = dom; D.params = params } ->
bda309
+        printf "domain %s:\n" (D.get_name dom);
bda309
+        Array.iteri (
bda309
+          fun i (field, value) ->
bda309
+            printf "\t%-20s = " field;
bda309
+            (match value with
bda309
+             | D.TypedFieldInt32 i -> printf "%ld" i
bda309
+             | D.TypedFieldUInt32 i -> printf "%ld" i
bda309
+             | D.TypedFieldInt64 i -> printf "%Ld" i
bda309
+             | D.TypedFieldUInt64 i -> printf "%Ld" i
bda309
+             | D.TypedFieldFloat f -> printf "%g" f
bda309
+             | D.TypedFieldBool b -> printf "%b" b
bda309
+             | D.TypedFieldString s -> printf "%S" s);
bda309
+            printf "\n";
bda309
+        ) params;
bda309
+        printf "\n"
bda309
+    ) stats
bda309
+  with
bda309
+    Libvirt.Virterror err ->
bda309
+      eprintf "error: %s\n" (Libvirt.Virterror.to_string err)
bda309
+
bda309
+let () =
bda309
+  if Array.length Sys.argv <> 1 then (
bda309
+    eprintf "error: get_all_domain_stats\n";
bda309
+    exit 1
bda309
+  );
bda309
+
bda309
+  let conn = C.connect_readonly () in
bda309
+
bda309
+  let what_stats = [D.StatsCpuTotal; D.StatsInterface; D.StatsBlock] in
bda309
+  let flags = [D.GetAllDomainsStatsActive; D.GetAllDomainsStatsInactive] in
bda309
+
bda309
+  let quit = ref false in
bda309
+
bda309
+  while not !quit do
bda309
+    let stats = D.get_all_domain_stats conn what_stats flags in
bda309
+
bda309
+    if stats <> [||] then print_stats stats
bda309
+    else (
bda309
+      printf "no guests found\n";
bda309
+      quit := true
bda309
+    );
bda309
+    flush stdout;
bda309
+
bda309
+    (* Run the garbage collector which is a good way to check for
bda309
+     * memory corruption errors and reference counting issues in
bda309
+     * libvirt.  You shouldn't do this in ordinary programs.
bda309
+     *)
bda309
+    Gc.compact ();
bda309
+
bda309
+    if not !quit then Unix.sleep 3
bda309
+  done
bda309
diff --git a/libvirt/libvirt.ml b/libvirt/libvirt.ml
bda309
index 1be023d..ce1878a 100644
bda309
--- a/libvirt/libvirt.ml
bda309
+++ b/libvirt/libvirt.ml
bda309
@@ -392,6 +392,27 @@ struct
bda309
     tx_drop : int64;
bda309
   }
bda309
 
bda309
+  type get_all_domain_stats_flag =
bda309
+    | GetAllDomainsStatsActive
bda309
+    | GetAllDomainsStatsInactive
bda309
+    | GetAllDomainsStatsOther
bda309
+    | GetAllDomainsStatsPaused
bda309
+    | GetAllDomainsStatsPersistent
bda309
+    | GetAllDomainsStatsRunning
bda309
+    | GetAllDomainsStatsShutoff
bda309
+    | GetAllDomainsStatsTransient
bda309
+    | GetAllDomainsStatsBacking
bda309
+    | GetAllDomainsStatsEnforceStats
bda309
+
bda309
+  type stats_type =
bda309
+    | StatsState | StatsCpuTotal | StatsBalloon | StatsVcpu
bda309
+    | StatsInterface | StatsBlock | StatsPerf
bda309
+
bda309
+  type 'a domain_stats_record = {
bda309
+    dom : 'a t;
bda309
+    params : typed_param array;
bda309
+  }
bda309
+
bda309
   (* The maximum size for Domain.memory_peek and Domain.block_peek
bda309
    * supported by libvirt.  This may change with different versions
bda309
    * of libvirt in the future, hence it's a function.
bda309
@@ -446,6 +467,8 @@ struct
bda309
   external block_peek : [>`W] t -> string -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_block_peek_bytecode" "ocaml_libvirt_domain_block_peek_native"
bda309
   external memory_peek : [>`W] t -> memory_flag list -> int64 -> int -> string -> int -> unit = "ocaml_libvirt_domain_memory_peek_bytecode" "ocaml_libvirt_domain_memory_peek_native"
bda309
 
bda309
+  external get_all_domain_stats : 'a Connect.t -> stats_type list -> get_all_domain_stats_flag list -> 'a domain_stats_record array = "ocaml_libvirt_domain_get_all_domain_stats"
bda309
+
bda309
   external const : [>`R] t -> ro t = "%identity"
bda309
 
bda309
   let get_domains conn flags =
bda309
diff --git a/libvirt/libvirt.mli b/libvirt/libvirt.mli
bda309
index 8cfcae2..d1b5992 100644
bda309
--- a/libvirt/libvirt.mli
bda309
+++ b/libvirt/libvirt.mli
bda309
@@ -478,6 +478,27 @@ sig
bda309
     tx_drop : int64;
bda309
   }
bda309
 
bda309
+  type get_all_domain_stats_flag =
bda309
+    | GetAllDomainsStatsActive
bda309
+    | GetAllDomainsStatsInactive
bda309
+    | GetAllDomainsStatsOther
bda309
+    | GetAllDomainsStatsPaused
bda309
+    | GetAllDomainsStatsPersistent
bda309
+    | GetAllDomainsStatsRunning
bda309
+    | GetAllDomainsStatsShutoff
bda309
+    | GetAllDomainsStatsTransient
bda309
+    | GetAllDomainsStatsBacking
bda309
+    | GetAllDomainsStatsEnforceStats
bda309
+
bda309
+  type stats_type =
bda309
+    | StatsState | StatsCpuTotal | StatsBalloon | StatsVcpu
bda309
+    | StatsInterface | StatsBlock | StatsPerf
bda309
+
bda309
+  type 'a domain_stats_record = {
bda309
+    dom : 'a t;
bda309
+    params : typed_param array;
bda309
+  }
bda309
+
bda309
   val max_peek : [>`R] t -> int
bda309
     (** Maximum size supported by the {!block_peek} and {!memory_peek}
bda309
 	functions.  If you want to peek more than this then you must
bda309
@@ -615,6 +636,13 @@ sig
bda309
 
bda309
 	See also {!max_peek}. *)
bda309
 
bda309
+  external get_all_domain_stats : 'a Connect.t -> stats_type list -> get_all_domain_stats_flag list -> 'a domain_stats_record array = "ocaml_libvirt_domain_get_all_domain_stats"
bda309
+    (** [get_all_domain_stats conn stats flags] allows you to read
bda309
+        all stats across multiple/all domains in a single call.
bda309
+
bda309
+        See the libvirt documentation for
bda309
+        [virConnectGetAllDomainStats]. *)
bda309
+
bda309
   external const : [>`R] t -> ro t = "%identity"
bda309
     (** [const dom] turns a read/write domain handle into a read-only
bda309
 	domain handle.  Note that the opposite operation is impossible.
bda309
diff --git a/libvirt/libvirt_c_oneoffs.c b/libvirt/libvirt_c_oneoffs.c
bda309
index 5d82194..17412f5 100644
bda309
--- a/libvirt/libvirt_c_oneoffs.c
bda309
+++ b/libvirt/libvirt_c_oneoffs.c
bda309
@@ -1,5 +1,5 @@
bda309
 /* OCaml bindings for libvirt.
bda309
- * (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
bda309
+ * (C) Copyright 2007-2017 Richard W.M. Jones, Red Hat Inc.
bda309
  * http://libvirt.org/
bda309
  *
bda309
  * This library is free software; you can redistribute it and/or
bda309
@@ -184,7 +184,6 @@ ocaml_libvirt_connect_set_keep_alive(value connv,
bda309
   CAMLreturn(Val_unit);
bda309
 }
bda309
 
bda309
-
bda309
 CAMLprim value
bda309
 ocaml_libvirt_domain_get_id (value domv)
bda309
 {
bda309
@@ -560,6 +559,122 @@ ocaml_libvirt_domain_get_cpu_stats (value domv)
bda309
   CAMLreturn (cpustats);
bda309
 }
bda309
 
bda309
+value
bda309
+ocaml_libvirt_domain_get_all_domain_stats (value connv,
bda309
+                                           value statsv, value flagsv)
bda309
+{
bda309
+  CAMLparam3 (connv, statsv, flagsv);
bda309
+  CAMLlocal5 (rv, dsv, tpv, v, v1);
bda309
+  CAMLlocal1 (v2);
bda309
+  virConnectPtr conn = Connect_val (connv);
bda309
+  virDomainStatsRecordPtr *rstats;
bda309
+  unsigned int stats = 0, flags = 0;
bda309
+  int i, j, r;
bda309
+
bda309
+  /* Get stats and flags. */
bda309
+  for (; statsv != Val_int (0); statsv = Field (statsv, 1)) {
bda309
+    v = Field (statsv, 0);
bda309
+    if (v == Val_int (0))
bda309
+      stats |= VIR_DOMAIN_STATS_STATE;
bda309
+    else if (v == Val_int (1))
bda309
+      stats |= VIR_DOMAIN_STATS_CPU_TOTAL;
bda309
+    else if (v == Val_int (2))
bda309
+      stats |= VIR_DOMAIN_STATS_BALLOON;
bda309
+    else if (v == Val_int (3))
bda309
+      stats |= VIR_DOMAIN_STATS_VCPU;
bda309
+    else if (v == Val_int (4))
bda309
+      stats |= VIR_DOMAIN_STATS_INTERFACE;
bda309
+    else if (v == Val_int (5))
bda309
+      stats |= VIR_DOMAIN_STATS_BLOCK;
bda309
+    else if (v == Val_int (6))
bda309
+      stats |= VIR_DOMAIN_STATS_PERF;
bda309
+  }
bda309
+  for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
bda309
+    v = Field (flagsv, 0);
bda309
+    if (v == Val_int (0))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_ACTIVE;
bda309
+    else if (v == Val_int (1))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_INACTIVE;
bda309
+    else if (v == Val_int (2))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_OTHER;
bda309
+    else if (v == Val_int (3))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_PAUSED;
bda309
+    else if (v == Val_int (4))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_PERSISTENT;
bda309
+    else if (v == Val_int (5))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_RUNNING;
bda309
+    else if (v == Val_int (6))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_SHUTOFF;
bda309
+    else if (v == Val_int (7))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_TRANSIENT;
bda309
+    else if (v == Val_int (8))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_BACKING;
bda309
+    else if (v == Val_int (9))
bda309
+      flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_ENFORCE_STATS;
bda309
+  }
bda309
+
bda309
+  NONBLOCKING (r = virConnectGetAllDomainStats (conn, stats, &rstats, flags));
bda309
+  CHECK_ERROR (r == -1, "virConnectGetAllDomainStats");
bda309
+
bda309
+  rv = caml_alloc (r, 0);       /* domain_stats_record array. */
bda309
+  for (i = 0; i < r; ++i) {
bda309
+    dsv = caml_alloc (2, 0);    /* domain_stats_record */
bda309
+    virDomainRef (rstats[i]->dom);
bda309
+    Store_field (dsv, 0, Val_domain (rstats[i]->dom, connv));
bda309
+
bda309
+    tpv = caml_alloc (rstats[i]->nparams, 0); /* typed_param array */
bda309
+    for (j = 0; j < rstats[i]->nparams; ++j) {
bda309
+      v2 = caml_alloc (2, 0);   /* typed_param: field name, value */
bda309
+      Store_field (v2, 0, caml_copy_string (rstats[i]->params[j].field));
bda309
+
bda309
+      switch (rstats[i]->params[j].type) {
bda309
+      case VIR_TYPED_PARAM_INT:
bda309
+        v1 = caml_alloc (1, 0);
bda309
+        v = caml_copy_int32 (rstats[i]->params[j].value.i);
bda309
+        break;
bda309
+      case VIR_TYPED_PARAM_UINT:
bda309
+        v1 = caml_alloc (1, 1);
bda309
+        v = caml_copy_int32 (rstats[i]->params[j].value.ui);
bda309
+        break;
bda309
+      case VIR_TYPED_PARAM_LLONG:
bda309
+        v1 = caml_alloc (1, 2);
bda309
+        v = caml_copy_int64 (rstats[i]->params[j].value.l);
bda309
+        break;
bda309
+      case VIR_TYPED_PARAM_ULLONG:
bda309
+        v1 = caml_alloc (1, 3);
bda309
+        v = caml_copy_int64 (rstats[i]->params[j].value.ul);
bda309
+        break;
bda309
+      case VIR_TYPED_PARAM_DOUBLE:
bda309
+        v1 = caml_alloc (1, 4);
bda309
+        v = caml_copy_double (rstats[i]->params[j].value.d);
bda309
+        break;
bda309
+      case VIR_TYPED_PARAM_BOOLEAN:
bda309
+        v1 = caml_alloc (1, 5);
bda309
+        v = Val_bool (rstats[i]->params[j].value.b);
bda309
+        break;
bda309
+      case VIR_TYPED_PARAM_STRING:
bda309
+        v1 = caml_alloc (1, 6);
bda309
+        v = caml_copy_string (rstats[i]->params[j].value.s);
bda309
+        break;
bda309
+      default:
bda309
+        virDomainStatsRecordListFree (rstats);
bda309
+        caml_failwith ("virConnectGetAllDomainStats: "
bda309
+                       "unknown parameter type returned");
bda309
+      }
bda309
+      Store_field (v1, 0, v);
bda309
+
bda309
+      Store_field (v2, 1, v1);
bda309
+      Store_field (tpv, j, v2);
bda309
+    }
bda309
+
bda309
+    Store_field (dsv, 1, tpv);
bda309
+    Store_field (rv, i, dsv);
bda309
+  }
bda309
+
bda309
+  virDomainStatsRecordListFree (rstats);
bda309
+  CAMLreturn (rv);
bda309
+}
bda309
+
bda309
 CAMLprim value
bda309
 ocaml_libvirt_domain_migrate_native (value domv, value dconnv, value flagsv, value optdnamev, value opturiv, value optbandwidthv, value unitv)
bda309
 {
bda309
-- 
bda309
2.9.3
bda309