Fix typo in ocaml_libvirt_storage_vol_get_info
[ocaml-libvirt.git] / contrib / 0001-Add-Libvirt.Domain.get_cpu_stats_total.patch
1 From 99ba7e0eae5ea7567aded458ab63c5984ed43191 Mon Sep 17 00:00:00 2001
2 From: Hu Tao <hutao@cn.fujitsu.com>
3 Date: Wed, 9 May 2012 16:48:31 +0800
4 Subject: [PATCH] Add Libvirt.Domain.get_cpu_stats_total.
5
6 Original patch by Hu Tao.
7
8 RWMJ modified the patch to split this into two functions
9 (old get_cpu_stats and new get_cpu_stats_total).  Apart
10 from that split, the code is identical.
11 ---
12  examples/.depend            | 12 +++---
13  examples/get_cpu_stats.ml   | 48 +++++++++++++-----------
14  libvirt/.depend             | 12 +++---
15  libvirt/libvirt.ml          |  1 +
16  libvirt/libvirt.mli         |  6 ++-
17  libvirt/libvirt_c_oneoffs.c | 89 +++++++++++++++++++++++++++++++++++++++++++++
18  6 files changed, 134 insertions(+), 34 deletions(-)
19
20 diff --git a/examples/.depend b/examples/.depend
21 index f58db3d..3d955f9 100644
22 --- a/examples/.depend
23 +++ b/examples/.depend
24 @@ -1,6 +1,6 @@
25 -get_cpu_stats.cmo: ../libvirt/libvirt.cmi
26 -get_cpu_stats.cmx: ../libvirt/libvirt.cmx
27 -list_domains.cmo: ../libvirt/libvirt.cmi
28 -list_domains.cmx: ../libvirt/libvirt.cmx
29 -node_info.cmo: ../libvirt/libvirt.cmi
30 -node_info.cmx: ../libvirt/libvirt.cmx
31 +node_info.cmo : ../libvirt/libvirt.cmi
32 +node_info.cmx : ../libvirt/libvirt.cmx
33 +get_cpu_stats.cmo : ../libvirt/libvirt.cmi
34 +get_cpu_stats.cmx : ../libvirt/libvirt.cmx
35 +list_domains.cmo : ../libvirt/libvirt.cmi
36 +list_domains.cmx : ../libvirt/libvirt.cmx
37 diff --git a/examples/get_cpu_stats.ml b/examples/get_cpu_stats.ml
38 index d7a8d0c..10b3840 100644
39 --- a/examples/get_cpu_stats.ml
40 +++ b/examples/get_cpu_stats.ml
41 @@ -18,32 +18,38 @@ let () =
42      let domname = Sys.argv.(1) in
43  
44      let conn = C.connect_readonly () in
45 -
46 -    let nr_pcpus =
47 -      let info = C.get_node_info conn in
48 -      C.maxcpus_of_node_info info in
49 -
50 -    let stats =
51 -      let dom = D.lookup_by_name conn domname in
52 -      D.get_cpu_stats dom in
53 +    let dom = D.lookup_by_name conn domname in
54 +    let stats = D.get_cpu_stats dom in
55 +    let total_stats = D.get_cpu_stats_total dom in
56 +
57 +    let print_params n params =
58 +      List.iter (
59 +        fun (name, value) ->
60 +          printf " %s=" name;
61 +          match value with
62 +          | D.TypedFieldInt32 i -> printf "%ld" i
63 +          | D.TypedFieldUInt32 i -> printf "%ld" i
64 +          | D.TypedFieldInt64 i -> printf "%Ld" i
65 +          | D.TypedFieldUInt64 i -> printf "%Ld" i
66 +          | D.TypedFieldFloat f -> printf "%g" f
67 +          | D.TypedFieldBool b -> printf "%b" b
68 +          | D.TypedFieldString s -> printf "%S" s
69 +      ) params in
70  
71      Array.iteri (
72        fun n params ->
73          printf "pCPU %d:" n;
74 -        List.iter (
75 -          fun (name, value) ->
76 -            printf " %s=" name;
77 -            match value with
78 -            | D.TypedFieldInt32 i -> printf "%ld" i
79 -            | D.TypedFieldUInt32 i -> printf "%ld" i
80 -            | D.TypedFieldInt64 i -> printf "%Ld" i
81 -            | D.TypedFieldUInt64 i -> printf "%Ld" i
82 -            | D.TypedFieldFloat f -> printf "%g" f
83 -            | D.TypedFieldBool b -> printf "%b" b
84 -            | D.TypedFieldString s -> printf "%S" s
85 -        ) params;
86 +        print_params n params;
87          printf "\n"
88 -    ) stats
89 +    ) stats;
90 +
91 +    Array.iteri (
92 +      fun n params ->
93 +        printf "total:";
94 +        print_params n params;
95 +        printf "\n"
96 +    ) total_stats
97 +
98    with
99      Libvirt.Virterror err ->
100        eprintf "error: %s\n" (Libvirt.Virterror.to_string err)
101 diff --git a/libvirt/.depend b/libvirt/.depend
102 index 3f2297e..7d32e13 100644
103 --- a/libvirt/.depend
104 +++ b/libvirt/.depend
105 @@ -1,6 +1,6 @@
106 -libvirt.cmi:
107 -libvirt_version.cmi:
108 -libvirt.cmo: libvirt.cmi
109 -libvirt.cmx: libvirt.cmi
110 -libvirt_version.cmo: libvirt_version.cmi
111 -libvirt_version.cmx: libvirt_version.cmi
112 +libvirt_version.cmi :
113 +libvirt.cmi :
114 +libvirt_version.cmo : libvirt_version.cmi
115 +libvirt_version.cmx : libvirt_version.cmi
116 +libvirt.cmo : libvirt.cmi
117 +libvirt.cmx : libvirt.cmi
118 diff --git a/libvirt/libvirt.ml b/libvirt/libvirt.ml
119 index 07542a9..2997b0b 100644
120 --- a/libvirt/libvirt.ml
121 +++ b/libvirt/libvirt.ml
122 @@ -418,6 +418,7 @@ struct
123    external pin_vcpu : [>`W] t -> int -> string -> unit = "ocaml_libvirt_domain_pin_vcpu"
124    external get_vcpus : [>`R] t -> int -> int -> int * vcpu_info array * string = "ocaml_libvirt_domain_get_vcpus"
125    external get_cpu_stats : [>`R] t -> typed_param list array = "ocaml_libvirt_domain_get_cpu_stats"
126 +  external get_cpu_stats_total : [>`R] t -> typed_param list array = "ocaml_libvirt_domain_get_cpu_stats_total"
127    external get_max_vcpus : [>`R] t -> int = "ocaml_libvirt_domain_get_max_vcpus"
128    external attach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_attach_device"
129    external detach_device : [>`W] t -> xml -> unit = "ocaml_libvirt_domain_detach_device"
130 diff --git a/libvirt/libvirt.mli b/libvirt/libvirt.mli
131 index 5a288c0..020be59 100644
132 --- a/libvirt/libvirt.mli
133 +++ b/libvirt/libvirt.mli
134 @@ -560,7 +560,11 @@ sig
135         of the array and bitmap returned from this function.
136      *)
137    val get_cpu_stats : [>`R] t -> typed_param list array
138 -    (** [get_pcpu_stats dom] returns the physical CPU stats
139 +    (** [get_cpu_stats dom] returns the per-CPU physical CPU stats
140 +       for a domain.  See the libvirt documentation for details.
141 +    *)
142 +  val get_cpu_stats_total : [>`R] t -> typed_param list array
143 +    (** [get_cpu_stats dom] returns the total physical CPU stats
144         for a domain.  See the libvirt documentation for details.
145      *)
146    val get_max_vcpus : [>`R] t -> int
147 diff --git a/libvirt/libvirt_c_oneoffs.c b/libvirt/libvirt_c_oneoffs.c
148 index 70cf96f..2a1850f 100644
149 --- a/libvirt/libvirt_c_oneoffs.c
150 +++ b/libvirt/libvirt_c_oneoffs.c
151 @@ -638,6 +638,95 @@ ocaml_libvirt_domain_get_cpu_stats (value domv)
152  #endif
153  }
154  
155 +CAMLprim value
156 +ocaml_libvirt_domain_get_cpu_stats_total (value domv)
157 +{
158 +#ifdef HAVE_VIRDOMAINGETCPUSTATS
159 +  CAMLparam1 (domv);
160 +  CAMLlocal5 (cpustats, param_head, param_node, typed_param, typed_param_value);
161 +  CAMLlocal1 (v);
162 +  virDomainPtr dom = Domain_val (domv);
163 +  virConnectPtr conn = Connect_domv (domv);
164 +  virTypedParameterPtr params;
165 +  int r, nparams, j, pos;
166 +
167 +  /* get total information */
168 +  NONBLOCKING (nparams = virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0));
169 +  CHECK_ERROR (nparams < 0, conn, "virDomainGetCPUStats");
170 +
171 +  if ((params = malloc(sizeof(*params) * nparams)) == NULL)
172 +    caml_failwith ("virDomainGetCPUStats: malloc");
173 +
174 +  cpustats = caml_alloc (1, 0); /* cpustats: array of params(list of typed_param) */
175 +
176 +  NONBLOCKING (r = virDomainGetCPUStats(dom, params, nparams, -1, 1, 0));
177 +  CHECK_ERROR (r < 0, conn, "virDomainGetCPUStats");
178 +
179 +  param_head = Val_emptylist;
180 +  if (params[nparams].type != 0) {
181 +      for (j = r - 1; j >= 0; j--) {
182 +        pos = j;
183 +
184 +        param_node = caml_alloc(2, 0); /* param_node: typed_param, next param_node */
185 +        Store_field(param_node, 1, param_head);
186 +        param_head = param_node;
187 +
188 +        typed_param = caml_alloc(2, 0); /* typed_param: field name(string), typed_param_value */
189 +        Store_field(param_node, 0, typed_param);
190 +        Store_field(typed_param, 0, caml_copy_string(params[pos].field));
191 +
192 +        /* typed_param_value: value with the corresponding type tag */
193 +        switch(params[pos].type) {
194 +        case VIR_TYPED_PARAM_INT:
195 +          typed_param_value = caml_alloc (1, 0);
196 +          v = caml_copy_int32 (params[pos].value.i);
197 +          break;
198 +        case VIR_TYPED_PARAM_UINT:
199 +          typed_param_value = caml_alloc (1, 1);
200 +          v = caml_copy_int32 (params[pos].value.ui);
201 +          break;
202 +        case VIR_TYPED_PARAM_LLONG:
203 +          typed_param_value = caml_alloc (1, 2);
204 +          v = caml_copy_int64 (params[pos].value.l);
205 +          break;
206 +        case VIR_TYPED_PARAM_ULLONG:
207 +          typed_param_value = caml_alloc (1, 3);
208 +          v = caml_copy_int64 (params[pos].value.ul);
209 +          break;
210 +        case VIR_TYPED_PARAM_DOUBLE:
211 +          typed_param_value = caml_alloc (1, 4);
212 +          v = caml_copy_double (params[pos].value.d);
213 +          break;
214 +        case VIR_TYPED_PARAM_BOOLEAN:
215 +          typed_param_value = caml_alloc (1, 5);
216 +          v = Val_bool (params[pos].value.b);
217 +          break;
218 +        case VIR_TYPED_PARAM_STRING:
219 +          typed_param_value = caml_alloc (1, 6);
220 +          v = caml_copy_string (params[pos].value.s);
221 +          free (params[pos].value.s);
222 +          break;
223 +        default:
224 +            /* XXX Memory leak on this path, if there are more
225 +             * VIR_TYPED_PARAM_STRING past this point in the array.
226 +             */
227 +          free (params);
228 +          caml_failwith ("virDomainGetCPUStats: "
229 +                         "unknown parameter type returned");
230 +        }
231 +        Store_field (typed_param_value, 0, v);
232 +        Store_field (typed_param, 1, typed_param_value);
233 +      }
234 +  }
235 +  Store_field (cpustats, 0, param_head);
236 +
237 +  free(params);
238 +  CAMLreturn (cpustats);
239 +#else
240 +  not_supported ("virDomainGetCPUStats");
241 +#endif
242 +}
243 +
244  #ifdef HAVE_WEAK_SYMBOLS
245  #ifdef HAVE_VIRDOMAINMIGRATE
246  extern virDomainPtr virDomainMigrate (virDomainPtr domain, virConnectPtr dconn,
247 -- 
248 1.7.11.4
249