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

[libvirt] [PATCH 05/10] util: resctrl: refactoring some functions



Some code, in virresctrl.c, manupulating the file objects of resctrlfs
could be reused for cache monitor interfaces. This patch refactor these
functions for purpose of reusing code in later patch:

virResctrlAllocDeterminePath
virResctrlAllocCreate
virResctrlAddPID

Signed-off-by: Wang Huaqiang <huaqiang wang intel com>
---
 src/util/virresctrl.c | 126 +++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 93 insertions(+), 33 deletions(-)

diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index 2f6923a..b3bae6e 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -2082,25 +2082,94 @@ virResctrlAllocAssign(virResctrlInfoPtr resctrl,
 }
 
 
-int
-virResctrlAllocDeterminePath(virResctrlAllocPtr alloc,
-                             const char *machinename)
+static int
+virResctrlDeterminePath(const char *id,
+                        const char *root,
+                        const char *parentpath,
+                        const char *prefix,
+                        char **path)
 {
-    if (!alloc->id) {
+    if (!id) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Resctrl Allocation ID must be set before creation"));
+                       _("Resctrl resource ID must be set before creation"));
         return -1;
     }
 
-    if (!alloc->path &&
-        virAsprintf(&alloc->path, "%s/%s-%s",
-                    SYSFS_RESCTRL_PATH, machinename, alloc->id) < 0)
+    if (*path) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Resctrl group (%s) already created, path=%s."),
+                      id, *path);
         return -1;
+    }
+
+    if (!parentpath && !root) {
+        if (virAsprintf(path, "%s/%s-%s",
+                        SYSFS_RESCTRL_PATH, prefix, id) < 0)
+            return -1;
+    } else if (!parentpath) {
+        if (virAsprintf(path, "%s/%s/%s-%s",
+                        SYSFS_RESCTRL_PATH, parentpath, prefix, id) < 0)
+            return -1;
+    } else {
+        if (virAsprintf(path, "%s/%s/%s-%s",
+                        root, parentpath, prefix, id) < 0)
+            return -1;
+    }
 
     return 0;
 }
 
 
+int
+virResctrlAllocDeterminePath(virResctrlAllocPtr alloc,
+                             const char *machinename)
+{
+    return virResctrlDeterminePath(alloc->id, NULL, NULL,
+                                   machinename, &alloc->path);
+}
+
+static int
+virResctrlCreateGroup(virResctrlInfoPtr resctrl,
+                      char *path)
+{
+    int ret = -1;
+    int lockfd = -1;
+
+    if (!path)
+        return -1;
+
+    if (virResctrlInfoIsEmpty(resctrl)) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Resource control is not supported on this host"));
+        return -1;
+    }
+
+    if (STREQ(path, SYSFS_RESCTRL_PATH))
+        return 0;
+
+    if (virFileExists(path)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Path '%s' for resctrl resource group exists"), path);
+        goto cleanup;
+    }
+
+    lockfd = virResctrlLockWrite();
+    if (lockfd < 0)
+        goto cleanup;
+
+    if (virFileMakePath(path) < 0) {
+        virReportSystemError(errno,
+                             _("Cannot create resctrl directory '%s'"), path);
+        goto cleanup;
+    }
+
+    ret = 0;
+ cleanup:
+    virResctrlUnlock(lockfd);
+    return ret;
+}
+
+
 /* This checks if the directory for the alloc exists.  If not it tries to create
  * it and apply appropriate alloc settings. */
 int
@@ -2116,21 +2185,11 @@ virResctrlAllocCreate(virResctrlInfoPtr resctrl,
     if (!alloc)
         return 0;
 
-    if (virResctrlInfoIsEmpty(resctrl)) {
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                       _("Resource control is not supported on this host"));
-        return -1;
-    }
-
     if (virResctrlAllocDeterminePath(alloc, machinename) < 0)
         return -1;
 
-    if (virFileExists(alloc->path)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Path '%s' for resctrl allocation exists"),
-                       alloc->path);
-        goto cleanup;
-    }
+    if (virResctrlCreateGroup(resctrl, alloc->path) < 0)
+        return -1;
 
     lockfd = virResctrlLockWrite();
     if (lockfd < 0)
@@ -2146,13 +2205,6 @@ virResctrlAllocCreate(virResctrlInfoPtr resctrl,
     if (virAsprintf(&schemata_path, "%s/schemata", alloc->path) < 0)
         goto cleanup;
 
-    if (virFileMakePath(alloc->path) < 0) {
-        virReportSystemError(errno,
-                             _("Cannot create resctrl directory '%s'"),
-                             alloc->path);
-        goto cleanup;
-    }
-
     VIR_DEBUG("Writing resctrl schemata '%s' into '%s'", alloc_str, schemata_path);
     if (virFileWriteStr(schemata_path, alloc_str, 0) < 0) {
         rmdir(alloc->path);
@@ -2171,21 +2223,21 @@ virResctrlAllocCreate(virResctrlInfoPtr resctrl,
 }
 
 
-int
-virResctrlAllocAddPID(virResctrlAllocPtr alloc,
-                      pid_t pid)
+static int
+virResctrlAddPID(char *path,
+                 pid_t pid)
 {
     char *tasks = NULL;
     char *pidstr = NULL;
     int ret = 0;
 
-    if (!alloc->path) {
+    if (!path) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Cannot add pid to non-existing resctrl allocation"));
+                       _("Cannot add pid to non-existing resctrl group"));
         return -1;
     }
 
-    if (virAsprintf(&tasks, "%s/tasks", alloc->path) < 0)
+    if (virAsprintf(&tasks, "%s/tasks", path) < 0)
         return -1;
 
     if (virAsprintf(&pidstr, "%lld", (long long int) pid) < 0)
@@ -2207,6 +2259,14 @@ virResctrlAllocAddPID(virResctrlAllocPtr alloc,
 
 
 int
+virResctrlAllocAddPID(virResctrlAllocPtr alloc,
+                      pid_t pid)
+{
+    return virResctrlAddPID(alloc->path, pid);
+}
+
+
+int
 virResctrlAllocRemove(virResctrlAllocPtr alloc)
 {
     int ret = 0;
-- 
2.7.4


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