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