[libvirt] [PATCHv2] cgroup: Move [qemu|lxc]GetCpuBWStatus to vicgroup.c and refactor it

Peter Krempa pkrempa at redhat.com
Mon Sep 16 09:33:57 UTC 2013


The function existed in two identical instances in lxc and qemu. Move it
to vircgroup.c and simplify it. Refactor the callers too.
---

Notes:
    Version 2:
    - added a fallback implementation that doesn't spam logs with errors

 src/libvirt_private.syms |  1 +
 src/lxc/lxc_driver.c     | 52 +++++-------------------------------------------
 src/qemu/qemu_driver.c   | 51 +++++------------------------------------------
 src/util/vircgroup.c     | 38 +++++++++++++++++++++++++++++++++++
 src/util/vircgroup.h     |  2 ++
 5 files changed, 51 insertions(+), 93 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 35f0f1b..82e3d6f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1217,6 +1217,7 @@ virCgroupSetMemory;
 virCgroupSetMemoryHardLimit;
 virCgroupSetMemorySoftLimit;
 virCgroupSetMemSwapHardLimit;
+virCgroupSupportsCpuBW;


 # util/virclosecallbacks.h
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index b587c22..87ced95 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -1574,45 +1574,10 @@ static char *lxcConnectGetHostname(virConnectPtr conn)
 }


-
-/*
- * check whether the host supports CFS bandwidth
- *
- * Return 1 when CFS bandwidth is supported, 0 when CFS bandwidth is not
- * supported, -1 on error.
- */
-static int lxcGetCpuBWStatus(virCgroupPtr cgroup)
-{
-    char *cfs_period_path = NULL;
-    int ret = -1;
-
-    if (!cgroup)
-        return 0;
-
-    if (virCgroupPathOfController(cgroup, VIR_CGROUP_CONTROLLER_CPU,
-                                  "cpu.cfs_period_us", &cfs_period_path) < 0) {
-        VIR_INFO("cannot get the path of cgroup CPU controller");
-        ret = 0;
-        goto cleanup;
-    }
-
-    if (access(cfs_period_path, F_OK) < 0) {
-        ret = 0;
-    } else {
-        ret = 1;
-    }
-
-cleanup:
-    VIR_FREE(cfs_period_path);
-    return ret;
-}
-
-
 static char *lxcDomainGetSchedulerType(virDomainPtr dom,
                                        int *nparams)
 {
     char *ret = NULL;
-    int rc;
     virDomainObjPtr vm;
     virLXCDomainObjPrivatePtr priv;

@@ -1639,13 +1604,10 @@ static char *lxcDomainGetSchedulerType(virDomainPtr dom,
     }

     if (nparams) {
-        rc = lxcGetCpuBWStatus(priv->cgroup);
-        if (rc < 0)
-            goto cleanup;
-        else if (rc == 0)
-            *nparams = 1;
-        else
+        if (virCgroupSupportsCpuBW(priv->cgroup))
             *nparams = 3;
+        else
+            *nparams = 1;
     }

     ignore_value(VIR_STRDUP(ret, "posix"));
@@ -1872,12 +1834,8 @@ lxcDomainGetSchedulerParametersFlags(virDomainPtr dom,
     if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;

-    if (*nparams > 1) {
-        rc = lxcGetCpuBWStatus(priv->cgroup);
-        if (rc < 0)
-            goto cleanup;
-        cpu_bw_status = !!rc;
-    }
+    if (*nparams > 1)
+        cpu_bw_status = virCgroupSupportsCpuBW(priv->cgroup);

     if (!(caps = virLXCDriverGetCapabilities(driver, false)))
         goto cleanup;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index ae1948f..0caeb08 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -7325,44 +7325,10 @@ cleanup:
 }


-/*
- * check whether the host supports CFS bandwidth
- *
- * Return 1 when CFS bandwidth is supported, 0 when CFS bandwidth is not
- * supported, -1 on error.
- */
-static int qemuGetCpuBWStatus(virCgroupPtr cgroup)
-{
-    char *cfs_period_path = NULL;
-    int ret = -1;
-
-    if (!cgroup)
-        return 0;
-
-    if (virCgroupPathOfController(cgroup, VIR_CGROUP_CONTROLLER_CPU,
-                                  "cpu.cfs_period_us", &cfs_period_path) < 0) {
-        VIR_INFO("cannot get the path of cgroup CPU controller");
-        ret = 0;
-        goto cleanup;
-    }
-
-    if (access(cfs_period_path, F_OK) < 0) {
-        ret = 0;
-    } else {
-        ret = 1;
-    }
-
-cleanup:
-    VIR_FREE(cfs_period_path);
-    return ret;
-}
-
-
 static char *qemuDomainGetSchedulerType(virDomainPtr dom,
                                         int *nparams)
 {
     char *ret = NULL;
-    int rc;
     virDomainObjPtr vm = NULL;
     qemuDomainObjPrivatePtr priv;

@@ -7389,13 +7355,10 @@ static char *qemuDomainGetSchedulerType(virDomainPtr dom,
     }

     if (nparams) {
-        rc = qemuGetCpuBWStatus(priv->cgroup);
-        if (rc < 0)
-            goto cleanup;
-        else if (rc == 0)
-            *nparams = 1;
-        else
+        if (virCgroupSupportsCpuBW(priv->cgroup))
             *nparams = 5;
+        else
+            *nparams = 1;
     }

     ignore_value(VIR_STRDUP(ret, "posix"));
@@ -8728,12 +8691,8 @@ qemuDomainGetSchedulerParametersFlags(virDomainPtr dom,
     if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;

-    if (*nparams > 1) {
-        rc = qemuGetCpuBWStatus(priv->cgroup);
-        if (rc < 0)
-            goto cleanup;
-        cpu_bw_status = !!rc;
-    }
+    if (*nparams > 1)
+        cpu_bw_status = virCgroupSupportsCpuBW(priv->cgroup);

     if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
         goto cleanup;
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index a615f28..e99caf5 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -3061,6 +3061,36 @@ cleanup:
 }


+/**
+ * virCgroupSupportsCpuBW():
+ * Check whether the host supports CFS bandwidth.
+ *
+ * Return true when CFS bandwidth is supported,
+ * false when CFS bandwidth is not supported.
+ */
+bool
+virCgroupSupportsCpuBW(virCgroupPtr cgroup)
+{
+    char *path = NULL;
+    int ret = false;
+
+    if (!cgroup)
+        return false;
+
+    if (virCgroupPathOfController(cgroup, VIR_CGROUP_CONTROLLER_CPU,
+                                  "cpu.cfs_period_us", &path) < 0) {
+        virResetLastError();
+        goto cleanup;
+    }
+
+    ret = virFileExists(path);
+
+cleanup:
+    VIR_FREE(path);
+    return ret;
+}
+
+
 #else /* !VIR_CGROUP_SUPPORTED */

 bool
@@ -3646,4 +3676,12 @@ virCgroupIsolateMount(virCgroupPtr group ATTRIBUTE_UNUSED,
     return -1;
 }

+
+bool
+virCgroupSupportsCpuBW(virCgroupPtr cgroup ATTRIBUTE_UNUSED)
+{
+    VIR_DEBUG("Control groups not supported on this platform");
+    return false;
+}
+
 #endif /* !VIR_CGROUP_SUPPORTED */
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index 7bb4b2a..835eb30 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -207,4 +207,6 @@ int virCgroupIsolateMount(virCgroupPtr group,
                           const char *oldroot,
                           const char *mountopts);

+bool virCgroupSupportsCpuBW(virCgroupPtr cgroup);
+
 #endif /* __VIR_CGROUP_H__ */
-- 
1.8.3.2




More information about the libvir-list mailing list