File 0001-Extract-stats-functions-from-the-qemu-driver.patch of Package libvirt
xxxxxxxxxx
1
From 6609ed5a377c3beaf8389e870b6851856cee42c7 Mon Sep 17 00:00:00 2001
2
From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= <cbosdonnat@suse.com>
3
Date: Thu, 4 Jan 2018 12:04:07 +0100
4
Subject: [PATCH 1/3] Extract stats functions from the qemu driver
5
6
Some of the qemu functions getting statistics can easily be reused in
7
other drivers. Create a conf/domain_stats.[ch] pair to host some of
8
them.
9
---
10
src/Makefile.am | 1 +
11
src/conf/domain_stats.c | 139 +++++++++++++++++++++++++++++++++++++++++
12
src/conf/domain_stats.h | 64 +++++++++++++++++++
13
src/libvirt_private.syms | 4 ++
14
src/qemu/qemu_driver.c | 158 +++--------------------------------------------
15
src/util/vircgroup.c | 46 ++++++++++++++
16
src/util/vircgroup.h | 4 ++
17
7 files changed, 265 insertions(+), 151 deletions(-)
18
create mode 100644 src/conf/domain_stats.c
19
create mode 100644 src/conf/domain_stats.h
20
21
Index: libvirt-7.1.0/src/conf/domain_stats.c
22
===================================================================
23
--- /dev/null
24
+++ libvirt-7.1.0/src/conf/domain_stats.c
25
26
+/*
27
+ * domain_stats.c: domain stats extraction helpers
28
+ *
29
+ * Copyright (C) 2006-2016 Red Hat, Inc.
30
+ * Copyright (C) 2006-2008 Daniel P. Berrange
31
+ * Copyright (c) 2018 SUSE LINUX Products GmbH, Nuernberg, Germany.
32
+ *
33
+ * This library is free software; you can redistribute it and/or
34
+ * modify it under the terms of the GNU Lesser General Public
35
+ * License as published by the Free Software Foundation; either
36
+ * version 2.1 of the License, or (at your option) any later version.
37
+ *
38
+ * This library is distributed in the hope that it will be useful,
39
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
40
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41
+ * Lesser General Public License for more details.
42
+ *
43
+ * You should have received a copy of the GNU Lesser General Public
44
+ * License along with this library. If not, see
45
+ * <http://www.gnu.org/licenses/>.
46
+ *
47
+ * Author: Daniel P. Berrange <berrange@redhat.com>
48
+ */
49
+
50
+#include <config.h>
51
+
52
+#include <stdio.h>
53
+
54
+#include "virlog.h"
55
+#include "domain_stats.h"
56
+#include "virtypedparam.h"
57
+#include "virnetdevtap.h"
58
+#include "virnetdevopenvswitch.h"
59
+
60
+#define VIR_FROM_THIS VIR_FROM_DOMAIN
61
+
62
+VIR_LOG_INIT("conf.domain_stats");
63
+
64
+int
65
+virDomainStatsGetState(virDomainObjPtr dom,
66
+ virTypedParamListPtr params)
67
+{
68
+ if (virTypedParamListAddInt(params, dom->state.state, "state.state") < 0)
69
+ return -1;
70
+
71
+ if (virTypedParamListAddInt(params, dom->state.reason, "state.reason") < 0)
72
+ return -1;
73
+
74
+ return 0;
75
+}
76
+
77
+#define STATS_ADD_NET_PARAM(params, num, name, value) \
78
+ if (value >= 0 && \
79
+ virTypedParamListAddULLong((params), (value), "net.%zu.%s", (num), (name)) < 0) \
80
+ return -1;
81
+
82
+int
83
+virDomainStatsGetInterface(virDomainObjPtr dom,
84
+ virTypedParamListPtr params)
85
+{
86
+ size_t i;
87
+ struct _virDomainInterfaceStats tmp;
88
+
89
+ if (!virDomainObjIsActive(dom))
90
+ return 0;
91
+
92
+ if (virTypedParamListAddUInt(params, dom->def->nnets, "net.count") < 0)
93
+ return -1;
94
+
95
+ /* Check the path is one of the domain's network interfaces. */
96
+ for (i = 0; i < dom->def->nnets; i++) {
97
+ virDomainNetDefPtr net = dom->def->nets[i];
98
+ virDomainNetType actualType;
99
+
100
+ if (!net->ifname)
101
+ continue;
102
+
103
+ memset(&tmp, 0, sizeof(tmp));
104
+
105
+ actualType = virDomainNetGetActualType(net);
106
+
107
+ if (virTypedParamListAddString(params, net->ifname, "net.%zu.name", i) < 0)
108
+ return -1;
109
+
110
+ if (actualType == VIR_DOMAIN_NET_TYPE_VHOSTUSER) {
111
+ if (virNetDevOpenvswitchInterfaceStats(net->ifname, &tmp) < 0) {
112
+ virResetLastError();
113
+ continue;
114
+ }
115
+ } else {
116
+ if (virNetDevTapInterfaceStats(net->ifname, &tmp,
117
+ !virDomainNetTypeSharesHostView(net)) < 0) {
118
+ virResetLastError();
119
+ continue;
120
+ }
121
+ }
122
+
123
+ STATS_ADD_NET_PARAM(params, i,
124
+ "rx.bytes", tmp.rx_bytes);
125
+ STATS_ADD_NET_PARAM(params, i,
126
+ "rx.pkts", tmp.rx_packets);
127
+ STATS_ADD_NET_PARAM(params, i,
128
+ "rx.errs", tmp.rx_errs);
129
+ STATS_ADD_NET_PARAM(params, i,
130
+ "rx.drop", tmp.rx_drop);
131
+ STATS_ADD_NET_PARAM(params, i,
132
+ "tx.bytes", tmp.tx_bytes);
133
+ STATS_ADD_NET_PARAM(params, i,
134
+ "tx.pkts", tmp.tx_packets);
135
+ STATS_ADD_NET_PARAM(params, i,
136
+ "tx.errs", tmp.tx_errs);
137
+ STATS_ADD_NET_PARAM(params, i,
138
+ "tx.drop", tmp.tx_drop);
139
+ }
140
+
141
+ return 0;
142
+}
143
+
144
+#undef STATS_ADD_NET_PARAM
145
Index: libvirt-7.1.0/src/conf/domain_stats.h
146
===================================================================
147
--- /dev/null
148
+++ libvirt-7.1.0/src/conf/domain_stats.h
149
150
+/*
151
+ * domain_stats.h: domain stats extraction helpers
152
+ *
153
+ * Copyright (C) 2006-2016 Red Hat, Inc.
154
+ * Copyright (C) 2006-2008 Daniel P. Berrange
155
+ * Copyright (c) 2018 SUSE LINUX Products GmbH, Nuernberg, Germany.
156
+ *
157
+ * This library is free software; you can redistribute it and/or
158
+ * modify it under the terms of the GNU Lesser General Public
159
+ * License as published by the Free Software Foundation; either
160
+ * version 2.1 of the License, or (at your option) any later version.
161
+ *
162
+ * This library is distributed in the hope that it will be useful,
163
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
164
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
165
+ * Lesser General Public License for more details.
166
+ *
167
+ * You should have received a copy of the GNU Lesser General Public
168
+ * License along with this library. If not, see
169
+ * <http://www.gnu.org/licenses/>.
170
+ *
171
+ * Author: Daniel P. Berrange <berrange@redhat.com>
172
+ */
173
+#ifndef __DOMAIN_STATS_H
174
+# define __DOMAIN_STATS_H
175
+
176
+# include "internal.h"
177
+# include "domain_conf.h"
178
+
179
+
180
+# define VIR_DOMAIN_STATS_ADD_COUNT_PARAM(record, maxparams, type, count) \
181
+do { \
182
+ char param_name[VIR_TYPED_PARAM_FIELD_LENGTH]; \
183
+ snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "%s.count", type); \
184
+ if (virTypedParamsAddUInt(&(record)->params, \
185
+ &(record)->nparams, \
186
+ maxparams, \
187
+ param_name, \
188
+ count) < 0) \
189
+ goto cleanup; \
190
+} while (0)
191
+
192
+# define VIR_DOMAIN_STATS_ADD_NAME_PARAM(record, maxparams, type, subtype, num, name) \
193
+do { \
194
+ char param_name[VIR_TYPED_PARAM_FIELD_LENGTH]; \
195
+ snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, \
196
+ "%s.%zu.%s", type, num, subtype); \
197
+ if (virTypedParamsAddString(&(record)->params, \
198
+ &(record)->nparams, \
199
+ maxparams, \
200
+ param_name, \
201
+ name) < 0) \
202
+ goto cleanup; \
203
+} while (0)
204
+
205
+int virDomainStatsGetState(virDomainObjPtr dom,
206
+ virTypedParamListPtr params);
207
+
208
+int virDomainStatsGetInterface(virDomainObjPtr dom,
209
+ virTypedParamListPtr params);
210
+
211
+#endif /* __DOMAIN_STATS_H */
212
Index: libvirt-7.1.0/src/libvirt_private.syms
213
===================================================================
214
--- libvirt-7.1.0.orig/src/libvirt_private.syms
215
+++ libvirt-7.1.0/src/libvirt_private.syms
216
217
virDomainConfVMNWFilterTeardown;
218
219
220
+# conf/domain_stats.h
221
+virDomainStatsGetInterface;
222
+virDomainStatsGetState;
223
+
224
+
225
# conf/domain_validate.h
226
virDomainActualNetDefValidate;
227
virDomainDefValidate;
228
virDomainDeviceValidateAliasForHotplug;
229
230
-
231
# conf/interface_conf.h
232
virInterfaceDefFormat;
233
virInterfaceDefFree;
234
235
virCgroupGetMemSwapHardLimit;
236
virCgroupGetMemSwapUsage;
237
virCgroupGetPercpuStats;
238
+virCgroupGetStatsCpu;
239
virCgroupHasController;
240
virCgroupHasEmptyTasks;
241
virCgroupKillPainfully;
242
Index: libvirt-7.1.0/src/qemu/qemu_driver.c
243
===================================================================
244
--- libvirt-7.1.0.orig/src/qemu/qemu_driver.c
245
+++ libvirt-7.1.0/src/qemu/qemu_driver.c
246
247
#include "virarptable.h"
248
#include "viruuid.h"
249
#include "domain_conf.h"
250
+#include "domain_stats.h"
251
#include "domain_audit.h"
252
#include "domain_cgroup.h"
253
#include "domain_driver.h"
254
255
virTypedParamListPtr params,
256
unsigned int privflags G_GNUC_UNUSED)
257
{
258
- if (virTypedParamListAddInt(params, dom->state.state, "state.state") < 0)
259
- return -1;
260
-
261
- if (virTypedParamListAddInt(params, dom->state.reason, "state.reason") < 0)
262
- return -1;
263
-
264
- return 0;
265
+ return virDomainStatsGetState(dom, params);
266
}
267
268
269
270
virTypedParamListPtr params)
271
{
272
qemuDomainObjPrivatePtr priv = dom->privateData;
273
- unsigned long long cpu_time = 0;
274
- unsigned long long user_time = 0;
275
- unsigned long long sys_time = 0;
276
- int err = 0;
277
278
if (!priv->cgroup)
279
return 0;
280
281
- err = virCgroupGetCpuacctUsage(priv->cgroup, &cpu_time);
282
- if (!err && virTypedParamListAddULLong(params, cpu_time, "cpu.time") < 0)
283
- return -1;
284
-
285
- err = virCgroupGetCpuacctStat(priv->cgroup, &user_time, &sys_time);
286
- if (!err && virTypedParamListAddULLong(params, user_time, "cpu.user") < 0)
287
- return -1;
288
- if (!err && virTypedParamListAddULLong(params, sys_time, "cpu.system") < 0)
289
- return -1;
290
-
291
- return 0;
292
+ return virCgroupGetStatsCpu(priv->cgroup, params);
293
}
294
295
296
297
return ret;
298
}
299
300
-#define QEMU_ADD_NET_PARAM(params, num, name, value) \
301
- if (value >= 0 && \
302
- virTypedParamListAddULLong((params), (value), "net.%zu.%s", (num), (name)) < 0) \
303
- return -1;
304
-
305
static int
306
qemuDomainGetStatsInterface(virQEMUDriverPtr driver G_GNUC_UNUSED,
307
virDomainObjPtr dom,
308
virTypedParamListPtr params,
309
unsigned int privflags G_GNUC_UNUSED)
310
{
311
- size_t i;
312
- struct _virDomainInterfaceStats tmp;
313
-
314
- if (!virDomainObjIsActive(dom))
315
- return 0;
316
-
317
- if (virTypedParamListAddUInt(params, dom->def->nnets, "net.count") < 0)
318
- return -1;
319
-
320
- /* Check the path is one of the domain's network interfaces. */
321
- for (i = 0; i < dom->def->nnets; i++) {
322
- virDomainNetDefPtr net = dom->def->nets[i];
323
- virDomainNetType actualType;
324
-
325
- if (!net->ifname)
326
- continue;
327
-
328
- memset(&tmp, 0, sizeof(tmp));
329
-
330
- actualType = virDomainNetGetActualType(net);
331
-
332
- if (virTypedParamListAddString(params, net->ifname, "net.%zu.name", i) < 0)
333
- return -1;
334
-
335
- if (actualType == VIR_DOMAIN_NET_TYPE_VHOSTUSER) {
336
- if (virNetDevOpenvswitchInterfaceStats(net->ifname, &tmp) < 0) {
337
- virResetLastError();
338
- continue;
339
- }
340
- } else {
341
- if (virNetDevTapInterfaceStats(net->ifname, &tmp,
342
- !virDomainNetTypeSharesHostView(net)) < 0) {
343
- virResetLastError();
344
- continue;
345
- }
346
- }
347
-
348
- QEMU_ADD_NET_PARAM(params, i,
349
- "rx.bytes", tmp.rx_bytes);
350
- QEMU_ADD_NET_PARAM(params, i,
351
- "rx.pkts", tmp.rx_packets);
352
- QEMU_ADD_NET_PARAM(params, i,
353
- "rx.errs", tmp.rx_errs);
354
- QEMU_ADD_NET_PARAM(params, i,
355
- "rx.drop", tmp.rx_drop);
356
- QEMU_ADD_NET_PARAM(params, i,
357
- "tx.bytes", tmp.tx_bytes);
358
- QEMU_ADD_NET_PARAM(params, i,
359
- "tx.pkts", tmp.tx_packets);
360
- QEMU_ADD_NET_PARAM(params, i,
361
- "tx.errs", tmp.tx_errs);
362
- QEMU_ADD_NET_PARAM(params, i,
363
- "tx.drop", tmp.tx_drop);
364
- }
365
-
366
- return 0;
367
+ return virDomainStatsGetInterface(dom,params);
368
}
369
370
-#undef QEMU_ADD_NET_PARAM
371
372
/* refresh information by opening images on the disk */
373
static int
374
Index: libvirt-7.1.0/src/util/vircgroup.c
375
===================================================================
376
--- libvirt-7.1.0.orig/src/util/vircgroup.c
377
+++ libvirt-7.1.0/src/util/vircgroup.c
378
379
return virCgroupHasController(cgroup, controller);
380
}
381
382
+int
383
+virCgroupGetStatsCpu(virCgroupPtr cgroup,
384
+ virTypedParamListPtr params)
385
+{
386
+ unsigned long long cpu_time = 0;
387
+ unsigned long long user_time = 0;
388
+ unsigned long long sys_time = 0;
389
+ int err = 0;
390
+
391
+ if (!cgroup)
392
+ return 0;
393
+
394
+ err = virCgroupGetCpuacctUsage(cgroup, &cpu_time);
395
+ if (!err && virTypedParamListAddULLong(params, cpu_time, "cpu.time") < 0)
396
+ return -1;
397
+
398
+ err = virCgroupGetCpuacctStat(cgroup, &user_time, &sys_time);
399
+ if (!err && virTypedParamListAddULLong(params, user_time, "cpu.user") < 0)
400
+ return -1;
401
+ if (!err && virTypedParamListAddULLong(params, sys_time, "cpu.system") < 0)
402
+ return -1;
403
+
404
+ return 0;
405
+}
406
+
407
#else /* !__linux__ */
408
409
bool
410
411
}
412
413
414
+int
415
+virCgroupGetStatsCpu(virCgroupPtr cgroup,
416
+ virTypedParamListPtr params)
417
+{
418
+ return 0;
419
+}
420
+
421
+
422
int
423
virCgroupNewPartition(const char *path G_GNUC_UNUSED,
424
bool create G_GNUC_UNUSED,
425
Index: libvirt-7.1.0/src/util/vircgroup.h
426
===================================================================
427
--- libvirt-7.1.0.orig/src/util/vircgroup.h
428
+++ libvirt-7.1.0/src/util/vircgroup.h
429
430
431
#include "virbitmap.h"
432
#include "virenum.h"
433
+#include "virtypedparam.h"
434
435
struct _virCgroup;
436
typedef struct _virCgroup virCgroup;
437
438
int virCgroupHasEmptyTasks(virCgroupPtr cgroup, int controller);
439
440
bool virCgroupControllerAvailable(int controller);
441
+
442
+int virCgroupGetStatsCpu(virCgroupPtr cgroup,
443
+ virTypedParamListPtr params);
444
Index: libvirt-7.1.0/src/conf/meson.build
445
===================================================================
446
--- libvirt-7.1.0.orig/src/conf/meson.build
447
+++ libvirt-7.1.0/src/conf/meson.build
448
449
'domain_conf.c',
450
'domain_nwfilter.c',
451
'domain_validate.c',
452
+ 'domain_stats.c',
453
'moment_conf.c',
454
'numa_conf.c',
455
'snapshot_conf.c',
456