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

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