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

[libvirt] [PATCH v2 2/5] cgroup macros refactoring



- Convert virCgroupGet* to VIR_CGROUP_SUPPORTED
- Convert virCgroup(Get|Set)FreezerState to VIR_CGROUP_SUPPORTED
- Convert virCgroupRemoveRecursively to VIR_CGROUP_SUPPORTED
---
 src/util/vircgroup.c | 363 +++++++++++++++++++++++++++++----------------------
 1 file changed, 204 insertions(+), 159 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 5f656b1..3640fbd 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -57,7 +57,7 @@
 #define VIR_FROM_THIS VIR_FROM_CGROUP
 
 #if defined(__linux__) && defined(HAVE_GETMNTENT_R) \
- && defined(_DIRENT_HAVE_D_TYPE)
+ && defined(_DIRENT_HAVE_D_TYPE) && defined(_SC_CLK_TCK)
 # define VIR_CGROUP_SUPPORTED
 #endif
 
@@ -1000,63 +1000,6 @@ error:
 }
 #endif
 
-#if defined _DIRENT_HAVE_D_TYPE
-int virCgroupRemoveRecursively(char *grppath)
-{
-    DIR *grpdir;
-    struct dirent *ent;
-    int rc = 0;
-
-    grpdir = opendir(grppath);
-    if (grpdir == NULL) {
-        if (errno == ENOENT)
-            return 0;
-        rc = -errno;
-        VIR_ERROR(_("Unable to open %s (%d)"), grppath, errno);
-        return rc;
-    }
-
-    for (;;) {
-        char *path;
-
-        errno = 0;
-        ent = readdir(grpdir);
-        if (ent == NULL) {
-            if ((rc = -errno))
-                VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
-            break;
-        }
-
-        if (ent->d_name[0] == '.') continue;
-        if (ent->d_type != DT_DIR) continue;
-
-        if (virAsprintf(&path, "%s/%s", grppath, ent->d_name) == -1) {
-            rc = -ENOMEM;
-            break;
-        }
-        rc = virCgroupRemoveRecursively(path);
-        VIR_FREE(path);
-        if (rc != 0)
-            break;
-    }
-    closedir(grpdir);
-
-    VIR_DEBUG("Removing cgroup %s", grppath);
-    if (rmdir(grppath) != 0 && errno != ENOENT) {
-        rc = -errno;
-        VIR_ERROR(_("Unable to remove %s (%d)"), grppath, errno);
-    }
-
-    return rc;
-}
-#else
-int virCgroupRemoveRecursively(char *grppath ATTRIBUTE_UNUSED)
-{
-    virReportSystemError(ENXIO, "%s",
-                         _("Control groups not supported on this platform"));
-    return -1;
-}
-#endif
 
 /**
  * virCgroupRemove:
@@ -2585,109 +2528,58 @@ int virCgroupSetCpuCfsQuota(virCgroupPtr group, long long cfs_quota)
                                 "cpu.cfs_quota_us", cfs_quota);
 }
 
-/**
- * virCgroupGetCpuCfsQuota:
- *
- * @group: The cgroup to get cpu.cfs_quota_us for
- * @cfs_quota: Pointer to the returned cpu bandwidth (in usecs) that this tg
- *             will be allowed to consume over period
- *
- * Returns: 0 on success
- */
-int virCgroupGetCpuCfsQuota(virCgroupPtr group, long long *cfs_quota)
-{
-    return virCgroupGetValueI64(group,
-                                VIR_CGROUP_CONTROLLER_CPU,
-                                "cpu.cfs_quota_us", cfs_quota);
-}
-
-int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
-{
-    return virCgroupGetValueU64(group,
-                                VIR_CGROUP_CONTROLLER_CPUACCT,
-                                "cpuacct.usage", usage);
-}
-
-int virCgroupGetCpuacctPercpuUsage(virCgroupPtr group, char **usage)
-{
-    return virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
-                                "cpuacct.usage_percpu", usage);
-}
-
-#ifdef _SC_CLK_TCK
-int virCgroupGetCpuacctStat(virCgroupPtr group, unsigned long long *user,
-                            unsigned long long *sys)
-{
-    char *str;
-    char *p;
-    int ret = -1;
-    static double scale = -1.0;
-
-    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
-                             "cpuacct.stat", &str) < 0)
-        return -1;
-
-    if (!(p = STRSKIP(str, "user ")) ||
-        virStrToLong_ull(p, &p, 10, user) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Cannot parse user stat '%s'"),
-                       p);
-        goto cleanup;
-    }
-    if (!(p = STRSKIP(p, "\nsystem ")) ||
-        virStrToLong_ull(p, NULL, 10, sys) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Cannot parse sys stat '%s'"),
-                       p);
-        goto cleanup;
-    }
-    /* times reported are in system ticks (generally 100 Hz), but that
-     * rate can theoretically vary between machines.  Scale things
-     * into approximate nanoseconds.  */
-    if (scale < 0) {
-        long ticks_per_sec = sysconf(_SC_CLK_TCK);
-        if (ticks_per_sec == -1) {
-            virReportSystemError(errno, "%s",
-                                 _("Cannot determine system clock HZ"));
-            goto cleanup;
-        }
-        scale = 1000000000.0 / ticks_per_sec;
-    }
-    *user *= scale;
-    *sys *= scale;
-
-    ret = 0;
-cleanup:
-    VIR_FREE(str);
-    return ret;
-}
-#else
-int virCgroupGetCpuacctStat(virCgroupPtr group ATTRIBUTE_UNUSED,
-                            unsigned long long *user ATTRIBUTE_UNUSED,
-                            unsigned long long *sys ATTRIBUTE_UNUSED)
-{
-    virReportSystemError(ENOSYS, "%s",
-                         _("Control groups not supported on this platform"));
-    return -1;
-}
-#endif
-
-int virCgroupSetFreezerState(virCgroupPtr group, const char *state)
-{
-    return virCgroupSetValueStr(group,
-                                VIR_CGROUP_CONTROLLER_FREEZER,
-                                "freezer.state", state);
-}
-
-int virCgroupGetFreezerState(virCgroupPtr group, char **state)
-{
-    return virCgroupGetValueStr(group,
-                                VIR_CGROUP_CONTROLLER_FREEZER,
-                                "freezer.state", state);
-}
-
 
 #ifdef VIR_CGROUP_SUPPORTED
+int virCgroupRemoveRecursively(char *grppath)
+{
+    DIR *grpdir;
+    struct dirent *ent;
+    int rc = 0;
+
+    grpdir = opendir(grppath);
+    if (grpdir == NULL) {
+        if (errno == ENOENT)
+            return 0;
+        rc = -errno;
+        VIR_ERROR(_("Unable to open %s (%d)"), grppath, errno);
+        return rc;
+    }
+
+    for (;;) {
+        char *path;
+
+        errno = 0;
+        ent = readdir(grpdir);
+        if (ent == NULL) {
+            if ((rc = -errno))
+                VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
+            break;
+        }
+
+        if (ent->d_name[0] == '.') continue;
+        if (ent->d_type != DT_DIR) continue;
+
+        if (virAsprintf(&path, "%s/%s", grppath, ent->d_name) == -1) {
+            rc = -ENOMEM;
+            break;
+        }
+        rc = virCgroupRemoveRecursively(path);
+        VIR_FREE(path);
+        if (rc != 0)
+            break;
+    }
+    closedir(grpdir);
+
+    VIR_DEBUG("Removing cgroup %s", grppath);
+    if (rmdir(grppath) != 0 && errno != ENOENT) {
+        rc = -errno;
+        VIR_ERROR(_("Unable to remove %s (%d)"), grppath, errno);
+    }
+
+    return rc;
+}
+
+
 /*
  * Returns 1 if some PIDs are killed, 0 if none are killed, or -1 on error
  */
@@ -2946,6 +2838,99 @@ static char *virCgroupIdentifyRoot(virCgroupPtr group)
 }
 
 
+/**
+ * virCgroupGetCpuCfsQuota:
+ *
+ * @group: The cgroup to get cpu.cfs_quota_us for
+ * @cfs_quota: Pointer to the returned cpu bandwidth (in usecs) that this tg
+ *             will be allowed to consume over period
+ *
+ * Returns: 0 on success
+ */
+int virCgroupGetCpuCfsQuota(virCgroupPtr group, long long *cfs_quota)
+{
+    return virCgroupGetValueI64(group,
+                                VIR_CGROUP_CONTROLLER_CPU,
+                                "cpu.cfs_quota_us", cfs_quota);
+}
+
+int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
+{
+    return virCgroupGetValueU64(group,
+                                VIR_CGROUP_CONTROLLER_CPUACCT,
+                                "cpuacct.usage", usage);
+}
+
+int virCgroupGetCpuacctPercpuUsage(virCgroupPtr group, char **usage)
+{
+    return virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
+                                "cpuacct.usage_percpu", usage);
+}
+
+
+int virCgroupGetCpuacctStat(virCgroupPtr group, unsigned long long *user,
+                            unsigned long long *sys)
+{
+    char *str;
+    char *p;
+    int ret = -1;
+    static double scale = -1.0;
+
+    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
+                             "cpuacct.stat", &str) < 0)
+        return -1;
+
+    if (!(p = STRSKIP(str, "user ")) ||
+        virStrToLong_ull(p, &p, 10, user) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Cannot parse user stat '%s'"),
+                       p);
+        goto cleanup;
+    }
+    if (!(p = STRSKIP(p, "\nsystem ")) ||
+        virStrToLong_ull(p, NULL, 10, sys) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Cannot parse sys stat '%s'"),
+                       p);
+        goto cleanup;
+    }
+    /* times reported are in system ticks (generally 100 Hz), but that
+     * rate can theoretically vary between machines.  Scale things
+     * into approximate nanoseconds.  */
+    if (scale < 0) {
+        long ticks_per_sec = sysconf(_SC_CLK_TCK);
+        if (ticks_per_sec == -1) {
+            virReportSystemError(errno, "%s",
+                                 _("Cannot determine system clock HZ"));
+            goto cleanup;
+        }
+        scale = 1000000000.0 / ticks_per_sec;
+    }
+    *user *= scale;
+    *sys *= scale;
+
+    ret = 0;
+cleanup:
+    VIR_FREE(str);
+    return ret;
+}
+
+
+int virCgroupSetFreezerState(virCgroupPtr group, const char *state)
+{
+    return virCgroupSetValueStr(group,
+                                VIR_CGROUP_CONTROLLER_FREEZER,
+                                "freezer.state", state);
+}
+
+int virCgroupGetFreezerState(virCgroupPtr group, char **state)
+{
+    return virCgroupGetValueStr(group,
+                                VIR_CGROUP_CONTROLLER_FREEZER,
+                                "freezer.state", state);
+}
+
+
 int virCgroupIsolateMount(virCgroupPtr group, const char *oldroot,
                           const char *mountopts)
 {
@@ -3031,6 +3016,14 @@ cleanup:
     return ret;
 }
 #else /* VIR_CGROUP_SUPPORTED */
+int virCgroupRemoveRecursively(char *grppath ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENXIO, "%s",
+                         _("Control groups not supported on this platform"));
+    return -1;
+}
+
+
 int virCgroupKill(virCgroupPtr group ATTRIBUTE_UNUSED,
                   int signum ATTRIBUTE_UNUSED)
 {
@@ -3039,6 +3032,7 @@ int virCgroupKill(virCgroupPtr group ATTRIBUTE_UNUSED,
     return -1;
 }
 
+
 int virCgroupKillRecursive(virCgroupPtr group ATTRIBUTE_UNUSED,
                            int signum ATTRIBUTE_UNUSED)
 {
@@ -3047,6 +3041,7 @@ int virCgroupKillRecursive(virCgroupPtr group ATTRIBUTE_UNUSED,
     return -1;
 }
 
+
 int virCgroupKillPainfully(virCgroupPtr group ATTRIBUTE_UNUSED)
 {
     virReportSystemError(ENOSYS, "%s",
@@ -3054,6 +3049,56 @@ int virCgroupKillPainfully(virCgroupPtr group ATTRIBUTE_UNUSED)
     return -1;
 }
 
+
+int virCgroupGetCpuCfsQuota(virCgroupPtr group, long long *cfs_quota)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("Control groups not supported on this platform"));
+    return -1;
+}
+
+
+int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("Control groups not supported on this platform"));
+    return -1;
+}
+
+
+int virCgroupGetCpuacctPercpuUsage(virCgroupPtr group, char **usage)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("Control groups not supported on this platform"));
+    return -1;
+}
+
+
+int virCgroupGetCpuacctStat(virCgroupPtr group ATTRIBUTE_UNUSED,
+                            unsigned long long *user ATTRIBUTE_UNUSED,
+                            unsigned long long *sys ATTRIBUTE_UNUSED)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("Control groups not supported on this platform"));
+    return -1;
+}
+
+
+int virCgroupSetFreezerState(virCgroupPtr group, const char *state)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("Control groups not supported on this platform"));
+    return -1;
+}
+
+int virCgroupGetFreezerState(virCgroupPtr group, char **state)
+{
+    virReportSystemError(ENOSYS, "%s",
+                         _("Control groups not supported on this platform"));
+    return -1;
+}
+
+
 int virCgroupIsolateMount(virCgroupPtr group ATTRIBUTE_UNUSED,
                           const char *oldroot ATTRIBUTE_UNUSED,
                           const char *mountopts ATTRIBUTE_UNUSED)
-- 
1.8.2.3


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