0a7476
From e7c6fd7c476effdc12330c22ea716186310226ec Mon Sep 17 00:00:00 2001
0a7476
Message-Id: <e7c6fd7c476effdc12330c22ea716186310226ec@dist-git>
0a7476
From: Bing Niu <bing.niu@intel.com>
0a7476
Date: Mon, 15 Apr 2019 17:32:51 +0200
0a7476
Subject: [PATCH] util: Add support to calculate MBA utilization
0a7476
MIME-Version: 1.0
0a7476
Content-Type: text/plain; charset=UTF-8
0a7476
Content-Transfer-Encoding: 8bit
0a7476
0a7476
Introduce virResctrlMemoryBandwidthSubtract and
0a7476
virResctrlAllocMemoryBandwidth to be used as part of
0a7476
the virResctrlAllocAssign processing to configure
0a7476
the available memory bandwidth.
0a7476
0a7476
Signed-off-by: Bing Niu <bing.niu@intel.com>
0a7476
Reviewed-by: John Ferlan <jferlan@redhat.com>
0a7476
(cherry picked from commit f977ad89e0202b5bcf5a90de45e19afb0159b458)
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: <5d177636966efd2f1132ad5a1a38b5bb2fad7e92.1555342313.git.phrdina@redhat.com>
0a7476
Reviewed-by: Ján Tomko <jtomko@redhat.com>
0a7476
---
0a7476
 src/util/virresctrl.c | 105 +++++++++++++++++++++++++++++++++++++-----
0a7476
 1 file changed, 93 insertions(+), 12 deletions(-)
0a7476
0a7476
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
0a7476
index 3148184d28..21a9247cb6 100644
0a7476
--- a/src/util/virresctrl.c
0a7476
+++ b/src/util/virresctrl.c
0a7476
@@ -1408,6 +1408,22 @@ virResctrlAllocSubtract(virResctrlAllocPtr dst,
0a7476
 }
0a7476
 
0a7476
 
0a7476
+static void
0a7476
+virResctrlMemoryBandwidthSubtract(virResctrlAllocPtr free,
0a7476
+                                  virResctrlAllocPtr used)
0a7476
+{
0a7476
+    size_t i;
0a7476
+
0a7476
+    if (!used->mem_bw)
0a7476
+        return;
0a7476
+
0a7476
+    for (i = 0; i < used->mem_bw->nbandwidths; i++) {
0a7476
+        if (used->mem_bw->bandwidths[i])
0a7476
+            *(free->mem_bw->bandwidths[i]) -= *(used->mem_bw->bandwidths[i]);
0a7476
+    }
0a7476
+}
0a7476
+
0a7476
+
0a7476
 static virResctrlAllocPtr
0a7476
 virResctrlAllocNewFromInfo(virResctrlInfoPtr info)
0a7476
 {
0a7476
@@ -1471,14 +1487,15 @@ virResctrlAllocNewFromInfo(virResctrlInfoPtr info)
0a7476
 }
0a7476
 
0a7476
 /*
0a7476
- * This function creates an allocation that represents all unused parts of all
0a7476
- * caches in the system.  It uses virResctrlInfo for creating a new full
0a7476
- * allocation with all bits set (using virResctrlAllocNewFromInfo()) and then
0a7476
- * scans for all allocations under /sys/fs/resctrl and subtracts each one of
0a7476
- * them from it.  That way it can then return an allocation with only bit set
0a7476
- * being those that are not mentioned in any other allocation.  It is used for
0a7476
- * two things, a) calculating the masks when creating allocations and b) from
0a7476
- * tests.
0a7476
+ * This function creates an allocation that represents all unused parts of
0a7476
+ * all caches and memory bandwidth in the system. It uses virResctrlInfo
0a7476
+ * for creating a new full allocation with all bits set (using the
0a7476
+ * virResctrlAllocNewFromInfo()), sets memory bandwidth 100%, and then scans
0a7476
+ * for all allocations under /sys/fs/resctrl and subtracts each one of them
0a7476
+ * from it. That way it can then return an allocation with only bit set
0a7476
+ * being those that are not mentioned in any other allocation for CAT and
0a7476
+ * available memory bandwidth for MBA. It is used for two things, calculating
0a7476
+ * the masks and bandwidth available when creating allocations and from tests.
0a7476
  */
0a7476
 virResctrlAllocPtr
0a7476
 virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
0a7476
@@ -1524,6 +1541,7 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl)
0a7476
             goto error;
0a7476
         }
0a7476
 
0a7476
+        virResctrlMemoryBandwidthSubtract(ret, alloc);
0a7476
         virResctrlAllocSubtract(ret, alloc);
0a7476
         virObjectUnref(alloc);
0a7476
         alloc = NULL;
0a7476
@@ -1674,6 +1692,66 @@ virResctrlAllocFindUnused(virResctrlAllocPtr alloc,
0a7476
 }
0a7476
 
0a7476
 
0a7476
+static int
0a7476
+virResctrlAllocMemoryBandwidth(virResctrlInfoPtr resctrl,
0a7476
+                               virResctrlAllocPtr alloc,
0a7476
+                               virResctrlAllocPtr free)
0a7476
+{
0a7476
+    size_t i;
0a7476
+    virResctrlAllocMemBWPtr mem_bw_alloc = alloc->mem_bw;
0a7476
+    virResctrlAllocMemBWPtr mem_bw_free = free->mem_bw;
0a7476
+    virResctrlInfoMemBWPtr mem_bw_info = resctrl->membw_info;
0a7476
+
0a7476
+    if (!mem_bw_alloc)
0a7476
+        return 0;
0a7476
+
0a7476
+    if (mem_bw_alloc && !mem_bw_info) {
0a7476
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
0a7476
+                       _("RDT Memory Bandwidth allocation unsupported"));
0a7476
+        return -1;
0a7476
+    }
0a7476
+
0a7476
+    for (i = 0; i < mem_bw_alloc->nbandwidths; i++) {
0a7476
+        if (!mem_bw_alloc->bandwidths[i])
0a7476
+            continue;
0a7476
+
0a7476
+        if (*(mem_bw_alloc->bandwidths[i]) % mem_bw_info->bandwidth_granularity) {
0a7476
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
0a7476
+                           _("Memory Bandwidth allocation of size "
0a7476
+                             "%u is not divisible by granularity %u"),
0a7476
+                           *(mem_bw_alloc->bandwidths[i]),
0a7476
+                           mem_bw_info->bandwidth_granularity);
0a7476
+            return -1;
0a7476
+        }
0a7476
+        if (*(mem_bw_alloc->bandwidths[i]) < mem_bw_info->min_bandwidth) {
0a7476
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
0a7476
+                           _("Memory Bandwidth allocation of size "
0a7476
+                             "%u is smaller than the minimum "
0a7476
+                             "allowed allocation %u"),
0a7476
+                           *(mem_bw_alloc->bandwidths[i]),
0a7476
+                           mem_bw_info->min_bandwidth);
0a7476
+            return -1;
0a7476
+        }
0a7476
+        if (i > mem_bw_info->max_id) {
0a7476
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
0a7476
+                           _("bandwidth controller id %zd does not "
0a7476
+                             "exist, max controller id %u"),
0a7476
+                           i, mem_bw_info->max_id);
0a7476
+            return -1;
0a7476
+        }
0a7476
+        if (*(mem_bw_alloc->bandwidths[i]) > *(mem_bw_free->bandwidths[i])) {
0a7476
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
0a7476
+                           _("Not enough room for allocation of %u%% "
0a7476
+                             "bandwidth on node %zd, available bandwidth %u%%"),
0a7476
+                           *(mem_bw_alloc->bandwidths[i]), i,
0a7476
+                           *(mem_bw_free->bandwidths[i]));
0a7476
+            return -1;
0a7476
+        }
0a7476
+    }
0a7476
+    return 0;
0a7476
+}
0a7476
+
0a7476
+
0a7476
 static int
0a7476
 virResctrlAllocCopyMasks(virResctrlAllocPtr dst,
0a7476
                          virResctrlAllocPtr src)
0a7476
@@ -1713,10 +1791,10 @@ virResctrlAllocCopyMasks(virResctrlAllocPtr dst,
0a7476
 
0a7476
 
0a7476
 /*
0a7476
- * This function is called when creating an allocation in the system.  What it
0a7476
- * does is that it gets all the unused bits using virResctrlAllocGetUnused() and
0a7476
- * then tries to find a proper space for every requested allocation effectively
0a7476
- * transforming `sizes` into `masks`.
0a7476
+ * This function is called when creating an allocation in the system.
0a7476
+ * What it does is that it gets all the unused resources using
0a7476
+ * virResctrlAllocGetUnused and then tries to find a proper space for
0a7476
+ * every requested allocation effectively transforming `sizes` into `masks`.
0a7476
  */
0a7476
 static int
0a7476
 virResctrlAllocAssign(virResctrlInfoPtr resctrl,
0a7476
@@ -1735,6 +1813,9 @@ virResctrlAllocAssign(virResctrlInfoPtr resctrl,
0a7476
     if (!alloc_default)
0a7476
         goto cleanup;
0a7476
 
0a7476
+    if (virResctrlAllocMemoryBandwidth(resctrl, alloc, alloc_free) < 0)
0a7476
+        goto cleanup;
0a7476
+
0a7476
     if (virResctrlAllocCopyMasks(alloc, alloc_default) < 0)
0a7476
         goto cleanup;
0a7476
 
0a7476
-- 
0a7476
2.21.0
0a7476