99cbc7
From b6573a089823817a1aa7ae19d7df3aed4845508e Mon Sep 17 00:00:00 2001
99cbc7
Message-Id: <b6573a089823817a1aa7ae19d7df3aed4845508e@dist-git>
99cbc7
From: Bing Niu <bing.niu@intel.com>
99cbc7
Date: Mon, 15 Apr 2019 17:32:52 +0200
99cbc7
Subject: [PATCH] util: Introduce virResctrlAllocForeachMemory
99cbc7
MIME-Version: 1.0
99cbc7
Content-Type: text/plain; charset=UTF-8
99cbc7
Content-Transfer-Encoding: 8bit
99cbc7
99cbc7
Introduce an API that will traverse the memory bandwidth data calling
99cbc7
a callback function for each defined bandwidth entry.
99cbc7
99cbc7
Signed-off-by: Bing Niu <bing.niu@intel.com>
99cbc7
Reviewed-by: John Ferlan <jferlan@redhat.com>
99cbc7
(cherry picked from commit 10e699dac9f86a4a266a43a5190def7756f3b762)
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: <40beb201ddf7c987958e792e47925e8438a5984f.1555342313.git.phrdina@redhat.com>
99cbc7
Reviewed-by: Ján Tomko <jtomko@redhat.com>
99cbc7
---
99cbc7
 src/libvirt_private.syms |  1 +
99cbc7
 src/util/virresctrl.c    | 33 +++++++++++++++++++++++++++++++++
99cbc7
 src/util/virresctrl.h    |  9 +++++++++
99cbc7
 3 files changed, 43 insertions(+)
99cbc7
99cbc7
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
99cbc7
index a3b30bf675..de328d7d22 100644
99cbc7
--- a/src/libvirt_private.syms
99cbc7
+++ b/src/libvirt_private.syms
99cbc7
@@ -2658,6 +2658,7 @@ virResctrlAllocAddPID;
99cbc7
 virResctrlAllocCreate;
99cbc7
 virResctrlAllocDeterminePath;
99cbc7
 virResctrlAllocForeachCache;
99cbc7
+virResctrlAllocForeachMemory;
99cbc7
 virResctrlAllocFormat;
99cbc7
 virResctrlAllocGetID;
99cbc7
 virResctrlAllocGetUnused;
99cbc7
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
99cbc7
index 21a9247cb6..45fc6fc847 100644
99cbc7
--- a/src/util/virresctrl.c
99cbc7
+++ b/src/util/virresctrl.c
99cbc7
@@ -965,6 +965,39 @@ virResctrlAllocForeachCache(virResctrlAllocPtr alloc,
99cbc7
 }
99cbc7
 
99cbc7
 
99cbc7
+/* virResctrlAllocForeachMemory
99cbc7
+ * @alloc: Pointer to an active allocation
99cbc7
+ * @cb: Callback function
99cbc7
+ * @opaque: Opaque data to be passed to @cb
99cbc7
+ *
99cbc7
+ * If available, traverse the defined memory bandwidth allocations and
99cbc7
+ * call the @cb function.
99cbc7
+ *
99cbc7
+ * Returns 0 on success, -1 and immediate failure if the @cb has any failure.
99cbc7
+ */
99cbc7
+int
99cbc7
+virResctrlAllocForeachMemory(virResctrlAllocPtr alloc,
99cbc7
+                             virResctrlAllocForeachMemoryCallback cb,
99cbc7
+                             void *opaque)
99cbc7
+{
99cbc7
+    size_t i = 0;
99cbc7
+    virResctrlAllocMemBWPtr mem_bw;
99cbc7
+
99cbc7
+    if (!alloc || !alloc->mem_bw)
99cbc7
+        return 0;
99cbc7
+
99cbc7
+    mem_bw = alloc->mem_bw;
99cbc7
+    for (i = 0; i < mem_bw->nbandwidths; i++) {
99cbc7
+        if (mem_bw->bandwidths[i]) {
99cbc7
+            if (cb(i, *mem_bw->bandwidths[i], opaque) < 0)
99cbc7
+                return -1;
99cbc7
+        }
99cbc7
+    }
99cbc7
+
99cbc7
+    return 0;
99cbc7
+}
99cbc7
+
99cbc7
+
99cbc7
 int
99cbc7
 virResctrlAllocSetID(virResctrlAllocPtr alloc,
99cbc7
                      const char *id)
99cbc7
diff --git a/src/util/virresctrl.h b/src/util/virresctrl.h
99cbc7
index d657c06008..5ea5b27d3b 100644
99cbc7
--- a/src/util/virresctrl.h
99cbc7
+++ b/src/util/virresctrl.h
99cbc7
@@ -73,6 +73,10 @@ typedef int virResctrlAllocForeachCacheCallback(unsigned int level,
99cbc7
                                                 unsigned long long size,
99cbc7
                                                 void *opaque);
99cbc7
 
99cbc7
+typedef int virResctrlAllocForeachMemoryCallback(unsigned int id,
99cbc7
+                                                 unsigned int size,
99cbc7
+                                                 void *opaque);
99cbc7
+
99cbc7
 virResctrlAllocPtr
99cbc7
 virResctrlAllocNew(void);
99cbc7
 
99cbc7
@@ -91,6 +95,11 @@ virResctrlAllocForeachCache(virResctrlAllocPtr alloc,
99cbc7
                             virResctrlAllocForeachCacheCallback cb,
99cbc7
                             void *opaque);
99cbc7
 
99cbc7
+int
99cbc7
+virResctrlAllocForeachMemory(virResctrlAllocPtr resctrl,
99cbc7
+                             virResctrlAllocForeachMemoryCallback cb,
99cbc7
+                             void *opaque);
99cbc7
+
99cbc7
 int
99cbc7
 virResctrlAllocSetID(virResctrlAllocPtr alloc,
99cbc7
                      const char *id);
99cbc7
-- 
99cbc7
2.21.0
99cbc7