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