0a7476
From 19f434a391d8675609adc02ada6398ef1db32830 Mon Sep 17 00:00:00 2001
0a7476
Message-Id: <19f434a391d8675609adc02ada6398ef1db32830@dist-git>
0a7476
From: Bing Niu <bing.niu@intel.com>
0a7476
Date: Mon, 15 Apr 2019 17:32:50 +0200
0a7476
Subject: [PATCH] util: Add MBA schemata parse and format methods
0a7476
MIME-Version: 1.0
0a7476
Content-Type: text/plain; charset=UTF-8
0a7476
Content-Transfer-Encoding: 8bit
0a7476
0a7476
Introduce virResctrlAllocMemoryBandwidthFormat and
0a7476
virResctrlAllocParseMemoryBandwidthLine which will format
0a7476
and parse an entry in the schemata file for MBA.
0a7476
0a7476
Signed-off-by: Bing Niu <bing.niu@intel.com>
0a7476
Reviewed-by: John Ferlan <jferlan@redhat.com>
0a7476
(cherry picked from commit 34a2ba2c466fe62611c9284d7de923879821b11a)
0a7476
0a7476
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1468650
0a7476
0a7476
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
0a7476
Message-Id: <48c9580bb6f7ce738b567c72a54d3ecfe930a2a9.1555342313.git.phrdina@redhat.com>
0a7476
Reviewed-by: Ján Tomko <jtomko@redhat.com>
0a7476
---
0a7476
 src/util/virresctrl.c | 140 ++++++++++++++++++++++++++++++++++++++++++
0a7476
 1 file changed, 140 insertions(+)
0a7476
0a7476
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
0a7476
index 8a25798f7d..3148184d28 100644
0a7476
--- a/src/util/virresctrl.c
0a7476
+++ b/src/util/virresctrl.c
0a7476
@@ -986,6 +986,138 @@ virResctrlAllocGetID(virResctrlAllocPtr alloc)
0a7476
 }
0a7476
 
0a7476
 
0a7476
+/* Format the Memory Bandwidth Allocation line that will be found in
0a7476
+ * the schemata files. The line should be start with "MB:" and be
0a7476
+ * followed by "id=value" pairs separated by a semi-colon such as:
0a7476
+ *
0a7476
+ *     MB:0=100;1=100
0a7476
+ *
0a7476
+ * which indicates node id 0 has 100 percent bandwith and node id 1
0a7476
+ * has 100 percent bandwidth. A trailing semi-colon is not formatted.
0a7476
+ */
0a7476
+static int
0a7476
+virResctrlAllocMemoryBandwidthFormat(virResctrlAllocPtr alloc,
0a7476
+                                     virBufferPtr buf)
0a7476
+{
0a7476
+    size_t i;
0a7476
+
0a7476
+    if (!alloc->mem_bw)
0a7476
+        return 0;
0a7476
+
0a7476
+    virBufferAddLit(buf, "MB:");
0a7476
+
0a7476
+    for (i = 0; i < alloc->mem_bw->nbandwidths; i++) {
0a7476
+        if (alloc->mem_bw->bandwidths[i]) {
0a7476
+            virBufferAsprintf(buf, "%zd=%u;", i,
0a7476
+                              *(alloc->mem_bw->bandwidths[i]));
0a7476
+        }
0a7476
+    }
0a7476
+
0a7476
+    virBufferTrim(buf, ";", 1);
0a7476
+    virBufferAddChar(buf, '\n');
0a7476
+    return virBufferCheckError(buf);
0a7476
+}
0a7476
+
0a7476
+
0a7476
+static int
0a7476
+virResctrlAllocParseProcessMemoryBandwidth(virResctrlInfoPtr resctrl,
0a7476
+                                           virResctrlAllocPtr alloc,
0a7476
+                                           char *mem_bw)
0a7476
+{
0a7476
+    unsigned int bandwidth;
0a7476
+    unsigned int id;
0a7476
+    char *tmp = NULL;
0a7476
+
0a7476
+    tmp = strchr(mem_bw, '=');
0a7476
+    if (!tmp)
0a7476
+        return 0;
0a7476
+    *tmp = '\0';
0a7476
+    tmp++;
0a7476
+
0a7476
+    if (virStrToLong_uip(mem_bw, NULL, 10, &id) < 0) {
0a7476
+        virReportError(VIR_ERR_INTERNAL_ERROR,
0a7476
+                       _("Invalid node id %u "), id);
0a7476
+        return -1;
0a7476
+    }
0a7476
+    if (virStrToLong_uip(tmp, NULL, 10, &bandwidth) < 0) {
0a7476
+        virReportError(VIR_ERR_INTERNAL_ERROR,
0a7476
+                       _("Invalid bandwidth %u"), bandwidth);
0a7476
+        return -1;
0a7476
+    }
0a7476
+    if (bandwidth < resctrl->membw_info->min_bandwidth ||
0a7476
+        id > resctrl->membw_info->max_id) {
0a7476
+        virReportError(VIR_ERR_INTERNAL_ERROR,
0a7476
+                       _("Missing or inconsistent resctrl info for "
0a7476
+                         "memory bandwidth node '%u'"), id);
0a7476
+        return -1;
0a7476
+    }
0a7476
+    if (alloc->mem_bw->nbandwidths <= id &&
0a7476
+        VIR_EXPAND_N(alloc->mem_bw->bandwidths, alloc->mem_bw->nbandwidths,
0a7476
+                     id - alloc->mem_bw->nbandwidths + 1) < 0) {
0a7476
+        return -1;
0a7476
+    }
0a7476
+    if (!alloc->mem_bw->bandwidths[id]) {
0a7476
+        if (VIR_ALLOC(alloc->mem_bw->bandwidths[id]) < 0)
0a7476
+            return -1;
0a7476
+    }
0a7476
+
0a7476
+    *(alloc->mem_bw->bandwidths[id]) = bandwidth;
0a7476
+    return 0;
0a7476
+}
0a7476
+
0a7476
+
0a7476
+/* Parse a schemata formatted MB: entry. Format details are described in
0a7476
+ * virResctrlAllocMemoryBandwidthFormat.
0a7476
+ */
0a7476
+static int
0a7476
+virResctrlAllocParseMemoryBandwidthLine(virResctrlInfoPtr resctrl,
0a7476
+                                        virResctrlAllocPtr alloc,
0a7476
+                                        char *line)
0a7476
+{
0a7476
+    char **mbs = NULL;
0a7476
+    char *tmp = NULL;
0a7476
+    size_t nmbs = 0;
0a7476
+    size_t i;
0a7476
+    int ret = -1;
0a7476
+
0a7476
+    /* For no reason there can be spaces */
0a7476
+    virSkipSpaces((const char **) &line);
0a7476
+
0a7476
+    if (STRNEQLEN(line, "MB", 2))
0a7476
+        return 0;
0a7476
+
0a7476
+    if (!resctrl || !resctrl->membw_info ||
0a7476
+        !resctrl->membw_info->min_bandwidth ||
0a7476
+        !resctrl->membw_info->bandwidth_granularity) {
0a7476
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
0a7476
+                       _("Missing or inconsistent resctrl info for "
0a7476
+                         "memory bandwidth allocation"));
0a7476
+        return -1;
0a7476
+    }
0a7476
+
0a7476
+    if (!alloc->mem_bw) {
0a7476
+        if (VIR_ALLOC(alloc->mem_bw) < 0)
0a7476
+            return -1;
0a7476
+    }
0a7476
+
0a7476
+    tmp = strchr(line, ':');
0a7476
+    if (!tmp)
0a7476
+        return 0;
0a7476
+    tmp++;
0a7476
+
0a7476
+    mbs = virStringSplitCount(tmp, ";", 0, &nmbs);
0a7476
+    for (i = 0; i < nmbs; i++) {
0a7476
+        if (virResctrlAllocParseProcessMemoryBandwidth(resctrl, alloc, mbs[i]) < 0)
0a7476
+            goto cleanup;
0a7476
+    }
0a7476
+
0a7476
+    ret = 0;
0a7476
+ cleanup:
0a7476
+    virStringListFree(mbs);
0a7476
+    return ret;
0a7476
+}
0a7476
+
0a7476
+
0a7476
 static int
0a7476
 virResctrlAllocFormatCache(virResctrlAllocPtr alloc,
0a7476
                            virBufferPtr buf)
0a7476
@@ -1045,6 +1177,11 @@ virResctrlAllocFormat(virResctrlAllocPtr alloc)
0a7476
         return NULL;
0a7476
     }
0a7476
 
0a7476
+    if (virResctrlAllocMemoryBandwidthFormat(alloc, &buf) < 0) {
0a7476
+        virBufferFreeAndReset(&buf;;
0a7476
+        return NULL;
0a7476
+    }
0a7476
+
0a7476
     return virBufferContentAndReset(&buf;;
0a7476
 }
0a7476
 
0a7476
@@ -1173,6 +1310,9 @@ virResctrlAllocParse(virResctrlInfoPtr resctrl,
0a7476
     for (i = 0; i < nlines; i++) {
0a7476
         if (virResctrlAllocParseCacheLine(resctrl, alloc, lines[i]) < 0)
0a7476
             goto cleanup;
0a7476
+        if (virResctrlAllocParseMemoryBandwidthLine(resctrl, alloc, lines[i]) < 0)
0a7476
+            goto cleanup;
0a7476
+
0a7476
     }
0a7476
 
0a7476
     ret = 0;
0a7476
-- 
0a7476
2.21.0
0a7476