[libvirt] [PATCHv3 2/8] qemu: bulk stats: implement CPU stats group

Francesco Romani fromani at redhat.com
Mon Sep 8 13:05:46 UTC 2014


This patch implements the VIR_DOMAIN_STATS_CPU_TOTAL
group of statistics.

Signed-off-by: Francesco Romani <fromani at redhat.com>
---
 include/libvirt/libvirt.h.in |  1 +
 src/libvirt.c                |  9 ++++++++
 src/qemu/qemu_driver.c       | 51 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index aced31c..e6ed803 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -2511,6 +2511,7 @@ struct _virDomainStatsRecord {
 
 typedef enum {
     VIR_DOMAIN_STATS_STATE = (1 << 0), /* return domain state */
+    VIR_DOMAIN_STATS_CPU_TOTAL = (1 << 1), /* return domain CPU info */
 } virDomainStatsTypes;
 
 typedef enum {
diff --git a/src/libvirt.c b/src/libvirt.c
index 4806535..4d504ff 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -21554,6 +21554,15 @@ virConnectGetDomainCapabilities(virConnectPtr conn,
  * "state.reason" - reason for entering given state, returned as int from
  *                  virDomain*Reason enum corresponding to given state.
  *
+ * VIR_DOMAIN_STATS_CPU_TOTAL: Return CPU statistics and usage information.
+ * The typed parameter keys are in this format:
+ * "cpu.count" - number as unsigned int of physical cpus available to
+ *               this domain.
+ * "cpu.time" - total cpu time spent for this domain as unsigned long long.
+ * "cpu.user" - user cpu time spent as unsigned long long.
+ * "cpu.system" - system cpu time spent as unsigned long long.
+ *
+ *
  * Using 0 for @stats returns all stats groups supported by the given
  * hypervisor.
  *
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 2950a4b..cfc5941 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -96,6 +96,7 @@
 #include "storage/storage_driver.h"
 #include "virhostdev.h"
 #include "domain_capabilities.h"
+#include "vircgroup.h"
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
@@ -17338,6 +17339,55 @@ qemuDomainGetStatsState(virConnectPtr conn ATTRIBUTE_UNUSED,
 }
 
 
+static int
+qemuDomainGetStatsCpu(virConnectPtr conn ATTRIBUTE_UNUSED,
+                      virDomainObjPtr dom,
+                      virDomainStatsRecordPtr record,
+                      int *maxparams,
+                      unsigned int privflags ATTRIBUTE_UNUSED)
+{
+    qemuDomainObjPrivatePtr priv = dom->privateData;
+    unsigned long long cpu_time = 0;
+    unsigned long long user_time = 0;
+    unsigned long long sys_time = 0;
+    int ncpus = 0;
+    int err;
+
+    ncpus = nodeGetCPUCount();
+    if (ncpus > 0 &&
+        virTypedParamsAddUInt(&record->params,
+                              &record->nparams,
+                              maxparams,
+                              "cpu.count",
+                              (unsigned int)ncpus) < 0)
+        return -1;
+
+    err = virCgroupGetCpuacctUsage(priv->cgroup, &cpu_time);
+    if (!err && virTypedParamsAddULLong(&record->params,
+                                        &record->nparams,
+                                        maxparams,
+                                        "cpu.time",
+                                        cpu_time) < 0)
+        return -1;
+
+    err = virCgroupGetCpuacctStat(priv->cgroup, &user_time, &sys_time);
+    if (!err && virTypedParamsAddULLong(&record->params,
+                                        &record->nparams,
+                                        maxparams,
+                                        "cpu.user",
+                                        user_time) < 0)
+        return -1;
+    if (!err && virTypedParamsAddULLong(&record->params,
+                                        &record->nparams,
+                                        maxparams,
+                                        "cpu.system",
+                                        sys_time) < 0)
+        return -1;
+
+    return 0;
+}
+
+
 typedef int
 (*qemuDomainGetStatsFunc)(virConnectPtr conn,
                           virDomainObjPtr dom,
@@ -17353,6 +17403,7 @@ struct qemuDomainGetStatsWorker {
 
 static struct qemuDomainGetStatsWorker qemuDomainGetStatsWorkers[] = {
     { qemuDomainGetStatsState, VIR_DOMAIN_STATS_STATE, false },
+    { qemuDomainGetStatsCpu, VIR_DOMAIN_STATS_CPU_TOTAL, false },
     { NULL, 0, false }
 };
 
-- 
1.9.3




More information about the libvir-list mailing list