99cbc7
From 58c87fb5d45a70bc10593398d9db5a91a43eb280 Mon Sep 17 00:00:00 2001
99cbc7
Message-Id: <58c87fb5d45a70bc10593398d9db5a91a43eb280@dist-git>
99cbc7
From: Andrea Bolognani <abologna@redhat.com>
99cbc7
Date: Tue, 4 Jun 2019 16:22:05 +0200
99cbc7
Subject: [PATCH] util: Introduce virNumaNodesetToCPUset()
99cbc7
MIME-Version: 1.0
99cbc7
Content-Type: text/plain; charset=UTF-8
99cbc7
Content-Transfer-Encoding: 8bit
99cbc7
99cbc7
This helper converts a set of NUMA node to the set of CPUs
99cbc7
they contain.
99cbc7
99cbc7
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
99cbc7
Reviewed-by: Ján Tomko <jtomko@redhat.com>
99cbc7
(cherry picked from commit 2d2b26f96fed1e95dd4495168cee73c5c4092634)
99cbc7
99cbc7
https://bugzilla.redhat.com/show_bug.cgi?id=1703661
99cbc7
99cbc7
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
99cbc7
Message-Id: <20190604142207.2036-5-abologna@redhat.com>
99cbc7
Reviewed-by: Ján Tomko <jtomko@redhat.com>
99cbc7
---
99cbc7
 src/libvirt_private.syms |  1 +
99cbc7
 src/util/virnuma.c       | 55 ++++++++++++++++++++++++++++++++++++++++
99cbc7
 src/util/virnuma.h       |  2 ++
99cbc7
 3 files changed, 58 insertions(+)
99cbc7
99cbc7
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
99cbc7
index e9c5b5ea33..bf99930802 100644
99cbc7
--- a/src/libvirt_private.syms
99cbc7
+++ b/src/libvirt_private.syms
99cbc7
@@ -2475,6 +2475,7 @@ virNumaGetPages;
99cbc7
 virNumaIsAvailable;
99cbc7
 virNumaNodeIsAvailable;
99cbc7
 virNumaNodesetIsAvailable;
99cbc7
+virNumaNodesetToCPUset;
99cbc7
 virNumaSetPagePoolSize;
99cbc7
 virNumaSetupMemoryPolicy;
99cbc7
 
99cbc7
diff --git a/src/util/virnuma.c b/src/util/virnuma.c
99cbc7
index fd15714553..75f80dcd30 100644
99cbc7
--- a/src/util/virnuma.c
99cbc7
+++ b/src/util/virnuma.c
99cbc7
@@ -311,6 +311,49 @@ virNumaGetNodeCPUs(int node,
99cbc7
 # undef MASK_CPU_ISSET
99cbc7
 # undef n_bits
99cbc7
 
99cbc7
+/**
99cbc7
+ * virNumaNodesetToCPUset:
99cbc7
+ * @nodeset: bitmap containing a set of NUMA nodes
99cbc7
+ * @cpuset: return location for a bitmap containing a set of CPUs
99cbc7
+ *
99cbc7
+ * Convert a set of NUMA node to the set of CPUs they contain.
99cbc7
+ *
99cbc7
+ * Returns 0 on success, <0 on failure.
99cbc7
+ */
99cbc7
+int
99cbc7
+virNumaNodesetToCPUset(virBitmapPtr nodeset,
99cbc7
+                       virBitmapPtr *cpuset)
99cbc7
+{
99cbc7
+    VIR_AUTOPTR(virBitmap) allNodesCPUs = NULL;
99cbc7
+    size_t nodesetSize;
99cbc7
+    size_t i;
99cbc7
+
99cbc7
+    *cpuset = NULL;
99cbc7
+
99cbc7
+    if (!nodeset)
99cbc7
+        return 0;
99cbc7
+
99cbc7
+    allNodesCPUs = virBitmapNewEmpty();
99cbc7
+    nodesetSize = virBitmapSize(nodeset);
99cbc7
+
99cbc7
+    for (i = 0; i < nodesetSize; i++) {
99cbc7
+        VIR_AUTOPTR(virBitmap) nodeCPUs = NULL;
99cbc7
+
99cbc7
+        if (!virBitmapIsBitSet(nodeset, i))
99cbc7
+            continue;
99cbc7
+
99cbc7
+        if (virNumaGetNodeCPUs(i, &nodeCPUs) < 0)
99cbc7
+            return -1;
99cbc7
+
99cbc7
+        if (virBitmapUnion(allNodesCPUs, nodeCPUs) < 0)
99cbc7
+            return -1;
99cbc7
+    }
99cbc7
+
99cbc7
+    VIR_STEAL_PTR(*cpuset, allNodesCPUs);
99cbc7
+
99cbc7
+    return 0;
99cbc7
+}
99cbc7
+
99cbc7
 #else /* !WITH_NUMACTL */
99cbc7
 
99cbc7
 int
99cbc7
@@ -365,6 +408,18 @@ virNumaGetNodeCPUs(int node ATTRIBUTE_UNUSED,
99cbc7
                    _("NUMA isn't available on this host"));
99cbc7
     return -1;
99cbc7
 }
99cbc7
+
99cbc7
+int
99cbc7
+virNumaNodesetToCPUset(virBitmapPtr nodeset ATTRIBUTE_UNUSED,
99cbc7
+                       virBitmapPtr *cpuset)
99cbc7
+{
99cbc7
+    *cpuset = NULL;
99cbc7
+
99cbc7
+    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
99cbc7
+                   _("NUMA isn't available on this host"));
99cbc7
+    return -1;
99cbc7
+}
99cbc7
+
99cbc7
 #endif /* !WITH_NUMACTL */
99cbc7
 
99cbc7
 /**
99cbc7
diff --git a/src/util/virnuma.h b/src/util/virnuma.h
99cbc7
index a3ffb6d6c7..df7911db4e 100644
99cbc7
--- a/src/util/virnuma.h
99cbc7
+++ b/src/util/virnuma.h
99cbc7
@@ -48,6 +48,8 @@ int virNumaGetNodeMemory(int node,
99cbc7
 unsigned int virNumaGetMaxCPUs(void);
99cbc7
 
99cbc7
 int virNumaGetNodeCPUs(int node, virBitmapPtr *cpus) ATTRIBUTE_NOINLINE;
99cbc7
+int virNumaNodesetToCPUset(virBitmapPtr nodeset,
99cbc7
+                           virBitmapPtr *cpuset);
99cbc7
 
99cbc7
 int virNumaGetPageInfo(int node,
99cbc7
                        unsigned int page_size,
99cbc7
-- 
99cbc7
2.21.0
99cbc7