[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH V4 4/5] qemu driver for virDomainGetCPUStats()



qemu driver for virDomainGetCPUstats using cpuacct cgroup.

* Now, only "cpu_time" is supported.
* cpuacct cgroup is used for providing percpu cputime information.

* include/libvirt/libvirt.h.in  - defines VIR_DOMAIN_CPU_STATS_CPUTIME
* src/qemu/qemu.conf     - take care of cpuacct cgroup.
* src/qemu/qemu_conf.c   - take care of cpuacct cgroup.
* src/qemu/qemu_driver.c - added an interface
* src/util/cgroup.c/h    - added interface for getting percpu cputime
---
 include/libvirt/libvirt.h.in |    6 ++
 src/qemu/qemu.conf           |    3 +-
 src/qemu/qemu_conf.c         |    3 +-
 src/qemu/qemu_driver.c       |  157 ++++++++++++++++++++++++++++++++++++++++++
 src/util/cgroup.c            |    6 ++
 src/util/cgroup.h            |    1 +
 6 files changed, 174 insertions(+), 2 deletions(-)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index e8b5116..2020059 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -3797,6 +3797,12 @@ int virConnectSetKeepAlive(virConnectPtr conn,
                            int interval,
                            unsigned int count);
 
+/**
+ * VIR_DOMAIN_CPU_STATS_CPUTIME:
+ * cpu usage in nanosecond, as a ullong
+ */
+#define VIR_DOMAIN_CPU_STATS_CPUTIME "cpu_time"
+
 int virDomainGetCPUStats(virDomainPtr domain,
                          virTypedParameterPtr params,
                          unsigned int nparams,
diff --git a/src/qemu/qemu.conf b/src/qemu/qemu.conf
index 4ec5e6c..181d853 100644
--- a/src/qemu/qemu.conf
+++ b/src/qemu/qemu.conf
@@ -158,6 +158,7 @@
 #  - 'memory' - use for memory tunables
 #  - 'blkio' - use for block devices I/O tunables
 #  - 'cpuset' - use for CPUs and memory nodes
+#  -  cpuacct - use for CPUs statistics.
 #
 # NB, even if configured here, they won't be used unless
 # the administrator has mounted cgroups, e.g.:
@@ -169,7 +170,7 @@
 # can be mounted in different locations. libvirt will detect
 # where they are located.
 #
-# cgroup_controllers = [ "cpu", "devices", "memory", "blkio", "cpuset" ]
+# cgroup_controllers = [ "cpu", "devices", "memory", "blkio", "cpuset", "cpuacct" ]
 
 # This is the basic set of devices allowed / required by
 # all virtual machines.
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index bc0a646..4775638 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -307,7 +307,8 @@ int qemudLoadDriverConfig(struct qemud_driver *driver,
             (1 << VIR_CGROUP_CONTROLLER_DEVICES) |
             (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
             (1 << VIR_CGROUP_CONTROLLER_BLKIO) |
-            (1 << VIR_CGROUP_CONTROLLER_CPUSET);
+            (1 << VIR_CGROUP_CONTROLLER_CPUSET) |
+            (1 << VIR_CGROUP_CONTROLLER_CPUACCT);
     }
     for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
         if (driver->cgroupControllers & (1 << i)) {
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index ab69dca..52f9ebd 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -11740,6 +11740,162 @@ cleanup:
     return ret;
 }
 
+
+/* qemuDomainGetCPUStats() with start_cpu == -1 */
+static int qemuDomainGetTotalcpuStats(virDomainPtr domain ATTRIBUTE_UNUSED,
+                                      virCgroupPtr group,
+                                      virTypedParameterPtr params,
+                                      int nparams,
+                                      unsigned int flags)
+{
+    unsigned long long cpu_time;
+    int param_idx = 0;
+    int ret;
+
+    virCheckFlags(0, -1);
+
+    if (nparams == 0) /* return supprted number of params */
+        return 1;
+    /* entry 0 is cputime */
+    ret = virCgroupGetCpuacctUsage(group, &cpu_time);
+    if (ret < 0) {
+        virReportSystemError(-ret, "%s", _("unable to get cpu account"));
+        return -1;
+    }
+
+    virTypedParameterAssign(&params[param_idx], VIR_DOMAIN_CPU_STATS_CPUTIME,
+                            VIR_TYPED_PARAM_ULLONG, cpu_time);
+    return param_idx + 1;
+}
+
+static int qemuDomainGetPercpuStats(virDomainPtr domain,
+                                    virCgroupPtr group,
+                                    virTypedParameterPtr params,
+                                    unsigned int nparams,
+                                    int start_cpu,
+                                    unsigned int ncpus,
+                                    unsigned int flags)
+{
+    virBitmapPtr map = NULL;
+    int rv = -1;
+    int i, max_id;
+    char *pos;
+    char *buf = NULL;
+    virTypedParameterPtr ent;
+    int param_idx;
+
+    virCheckFlags(0, -1);
+    /* return the number of supported params ? */
+    if (nparams == 0 && ncpus != 0)
+        return 1; /* only cpu_time is supported */
+
+    /* return percpu cputime in index 0 */
+    param_idx = 0;
+    /* to parse account file, we need "present" cpu map */
+    map = nodeGetCPUmap(domain->conn, &max_id, "present");
+    if (!map)
+        return rv;
+
+    if (ncpus == 0) { /* returns max cpu ID */
+        rv = max_id;
+        goto cleanup;
+    }
+    /* we get percpu cputime accoutning info. */
+    if (virCgroupGetCpuacctPercpuUsage(group, &buf))
+        goto cleanup;
+    pos = buf;
+
+    if (max_id > start_cpu + ncpus - 1)
+        max_id = start_cpu + ncpus - 1;
+
+    for (i = 0; i <= max_id; i++) {
+        bool exist;
+        unsigned long long cpu_time;
+
+        if (virBitmapGetBit(map, i, &exist) < 0) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR, _("bitmap parse error"));
+            goto cleanup;
+        }
+        if (!exist)
+            continue;
+        if (virStrToLong_ull(pos, &pos, 10, &cpu_time) < 0) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                            _("cpuacct parse error"));
+            goto cleanup;
+        }
+        if (i < start_cpu)
+            continue;
+        ent = &params[ (i - start_cpu) * nparams + param_idx];
+        virTypedParameterAssign(ent, VIR_DOMAIN_CPU_STATS_CPUTIME,
+                                VIR_TYPED_PARAM_ULLONG, cpu_time);
+    }
+    rv = param_idx + 1;
+cleanup:
+    VIR_FREE(buf);
+    virBitmapFree(map);
+    return rv;
+}
+
+
+static int
+qemuDomainGetCPUStats(virDomainPtr domain,
+                virTypedParameterPtr params,
+                unsigned int nparams,
+                int start_cpu,
+                unsigned int ncpus,
+                unsigned int flags)
+{
+    struct qemud_driver *driver = domain->conn->privateData;
+    virCgroupPtr group = NULL;
+    virDomainObjPtr vm = NULL;
+    int ret = -1;
+    bool isActive;
+
+    virCheckFlags(0, -1);
+
+    qemuDriverLock(driver);
+
+    vm = virDomainFindByUUID(&driver->domains, domain->uuid);
+    if (vm == NULL) {
+        qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                        _("No such domain %s"), domain->uuid);
+        goto cleanup;
+    }
+
+    isActive = virDomainObjIsActive(vm);
+    if (!isActive) {
+        qemuReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                        _("domain is not running"));
+        goto cleanup;
+    }
+
+    if (!qemuCgroupControllerActive(driver, VIR_CGROUP_CONTROLLER_CPUACCT)) {
+        qemuReportError(VIR_ERR_OPERATION_INVALID,
+                        "%s", _("cgroup CPUACCT controller is not mounted"));
+        goto cleanup;
+    }
+
+    if (virCgroupForDomain(driver->cgroup, vm->def->name, &group, 0) != 0) {
+        qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                        _("cannot find cgroup for domain %s"), vm->def->name);
+        goto cleanup;
+    }
+
+    if (nparams == 0 && ncpus == 0) /* returns max cpu id */
+        ret = qemuDomainGetPercpuStats(domain, group, NULL, 0, 0, 0, flags);
+    else if (start_cpu == -1) /* get total */
+        ret = qemuDomainGetTotalcpuStats(domain, group, params, nparams, flags);
+    else
+        ret = qemuDomainGetPercpuStats(domain, group, params, nparams,
+                                       start_cpu, ncpus ,flags);
+cleanup:
+    virCgroupFree(&group);
+    if (vm)
+        virDomainObjUnlock(vm);
+    qemuDriverUnlock(driver);
+    return ret;
+}
+
 static virDriver qemuDriver = {
     .no = VIR_DRV_QEMU,
     .name = "QEMU",
@@ -11893,6 +12049,7 @@ static virDriver qemuDriver = {
     .domainGetNumaParameters = qemuDomainGetNumaParameters, /* 0.9.9 */
     .domainGetInterfaceParameters = qemuDomainGetInterfaceParameters, /* 0.9.9 */
     .domainSetInterfaceParameters = qemuDomainSetInterfaceParameters, /* 0.9.9 */
+    .domainGetCPUStats = qemuDomainGetCPUStats, /* 0.9.10 */
 };
 
 
diff --git a/src/util/cgroup.c b/src/util/cgroup.c
index 15b870d..2d34c50 100644
--- a/src/util/cgroup.c
+++ b/src/util/cgroup.c
@@ -1555,6 +1555,12 @@ int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
                                 "cpuacct.usage", usage);
 }
 
+int virCgroupGetCpuacctPercpuUsage(virCgroupPtr group, char **usage)
+{
+    return virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
+                                "cpuacct.usage_percpu", usage);
+}
+
 int virCgroupSetFreezerState(virCgroupPtr group, const char *state)
 {
     return virCgroupSetValueStr(group,
diff --git a/src/util/cgroup.h b/src/util/cgroup.h
index 8d75735..b4e0f37 100644
--- a/src/util/cgroup.h
+++ b/src/util/cgroup.h
@@ -115,6 +115,7 @@ int virCgroupSetCpuCfsQuota(virCgroupPtr group, long long cfs_quota);
 int virCgroupGetCpuCfsQuota(virCgroupPtr group, long long *cfs_quota);
 
 int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage);
+int virCgroupGetCpuacctPercpuUsage(virCgroupPtr group, char **usage);
 
 int virCgroupSetFreezerState(virCgroupPtr group, const char *state);
 int virCgroupGetFreezerState(virCgroupPtr group, char **state);
-- 
1.7.4.1



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]