c1c534
From 12745d1c8180273ad1028da0da7440cb959819eb Mon Sep 17 00:00:00 2001
c1c534
Message-Id: <12745d1c8180273ad1028da0da7440cb959819eb@dist-git>
c1c534
From: Martin Kletzander <mkletzan@redhat.com>
c1c534
Date: Wed, 31 Jan 2018 16:32:40 +0100
c1c534
Subject: [PATCH] util: Add helpers for getting resctrl group allocs
c1c534
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
(cherry picked from commit f6199295a9656a79e29cab9ed5694f040c266998)
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
---
c1c534
 src/util/virresctrl.c | 81 ++++++++++++++++++++++++++++++++++-----------------
c1c534
 1 file changed, 54 insertions(+), 27 deletions(-)
c1c534
c1c534
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
c1c534
index 42876c6e2b..df6461a046 100644
c1c534
--- a/src/util/virresctrl.c
c1c534
+++ b/src/util/virresctrl.c
c1c534
@@ -1041,6 +1041,55 @@ virResctrlAllocParse(virResctrlInfoPtr resctrl,
c1c534
 }
c1c534
 
c1c534
 
c1c534
+static int
c1c534
+virResctrlAllocGetGroup(virResctrlInfoPtr resctrl,
c1c534
+                        const char *groupname,
c1c534
+                        virResctrlAllocPtr *alloc)
c1c534
+{
c1c534
+    char *schemata = NULL;
c1c534
+    int rv = virFileReadValueString(&schemata,
c1c534
+                                     SYSFS_RESCTRL_PATH
c1c534
+                                     "/%s/schemata",
c1c534
+                                     groupname);
c1c534
+
c1c534
+    *alloc = NULL;
c1c534
+
c1c534
+    if (rv < 0)
c1c534
+        return rv;
c1c534
+
c1c534
+    *alloc = virResctrlAllocNew();
c1c534
+    if (!*alloc)
c1c534
+        goto error;
c1c534
+
c1c534
+    if (virResctrlAllocParse(resctrl, *alloc, schemata) < 0)
c1c534
+        goto error;
c1c534
+
c1c534
+    VIR_FREE(schemata);
c1c534
+    return 0;
c1c534
+
c1c534
+ error:
c1c534
+    VIR_FREE(schemata);
c1c534
+    virObjectUnref(*alloc);
c1c534
+    *alloc = NULL;
c1c534
+    return -1;
c1c534
+}
c1c534
+
c1c534
+
c1c534
+static virResctrlAllocPtr
c1c534
+virResctrlAllocGetDefault(virResctrlInfoPtr resctrl)
c1c534
+{
c1c534
+    virResctrlAllocPtr ret = NULL;
c1c534
+    int rv = virResctrlAllocGetGroup(resctrl, ".", &ret;;
c1c534
+
c1c534
+    if (rv == -2) {
c1c534
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c1c534
+                       _("Could not read schemata file for the default group"));
c1c534
+    }
c1c534
+
c1c534
+    return ret;
c1c534
+}
c1c534
+
c1c534
+
c1c534
 static void
c1c534
 virResctrlAllocSubtractPerType(virResctrlAllocPerTypePtr dst,
c1c534
                                virResctrlAllocPerTypePtr src)
c1c534
@@ -1141,7 +1190,6 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
c1c534
     virResctrlAllocPtr alloc = NULL;
c1c534
     struct dirent *ent = NULL;
c1c534
     DIR *dirp = NULL;
c1c534
-    char *schemata = NULL;
c1c534
     int rv = -1;
c1c534
 
c1c534
     if (virResctrlInfoIsEmpty(resctrl)) {
c1c534
@@ -1154,22 +1202,12 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
c1c534
     if (!ret)
c1c534
         return NULL;
c1c534
 
c1c534
-    if (virFileReadValueString(&schemata,
c1c534
-                               SYSFS_RESCTRL_PATH
c1c534
-                               "/schemata") < 0) {
c1c534
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c1c534
-                       _("Could not read schemata file for the default group"));
c1c534
-        goto error;
c1c534
-    }
c1c534
-
c1c534
-    alloc = virResctrlAllocNew();
c1c534
+    alloc = virResctrlAllocGetDefault(resctrl);
c1c534
     if (!alloc)
c1c534
         goto error;
c1c534
 
c1c534
-    if (virResctrlAllocParse(resctrl, alloc, schemata) < 0)
c1c534
-        goto error;
c1c534
-
c1c534
     virResctrlAllocSubtract(ret, alloc);
c1c534
+    virObjectUnref(alloc);
c1c534
 
c1c534
     if (virDirOpen(&dirp, SYSFS_RESCTRL_PATH) < 0)
c1c534
         goto error;
c1c534
@@ -1178,11 +1216,7 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
c1c534
         if (STREQ(ent->d_name, "info"))
c1c534
             continue;
c1c534
 
c1c534
-        VIR_FREE(schemata);
c1c534
-        rv = virFileReadValueString(&schemata,
c1c534
-                                    SYSFS_RESCTRL_PATH
c1c534
-                                    "/%s/schemata",
c1c534
-                                    ent->d_name);
c1c534
+        rv = virResctrlAllocGetGroup(resctrl, ent->d_name, &alloc);
c1c534
         if (rv == -2)
c1c534
             continue;
c1c534
 
c1c534
@@ -1193,15 +1227,9 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
c1c534
             goto error;
c1c534
         }
c1c534
 
c1c534
-        virObjectUnref(alloc);
c1c534
-        alloc = virResctrlAllocNew();
c1c534
-        if (!alloc)
c1c534
-            goto error;
c1c534
-
c1c534
-        if (virResctrlAllocParse(resctrl, alloc, schemata) < 0)
c1c534
-            goto error;
c1c534
-
c1c534
         virResctrlAllocSubtract(ret, alloc);
c1c534
+        virObjectUnref(alloc);
c1c534
+        alloc = NULL;
c1c534
     }
c1c534
     if (rv < 0)
c1c534
         goto error;
c1c534
@@ -1209,7 +1237,6 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
c1c534
  cleanup:
c1c534
     virObjectUnref(alloc);
c1c534
     VIR_DIR_CLOSE(dirp);
c1c534
-    VIR_FREE(schemata);
c1c534
     return ret;
c1c534
 
c1c534
  error:
c1c534
-- 
c1c534
2.16.1
c1c534