e8e641
From d8c969c521efbd38df526f085db32c605661e2d1 Mon Sep 17 00:00:00 2001
e8e641
Message-Id: <d8c969c521efbd38df526f085db32c605661e2d1@dist-git>
e8e641
From: Michal Privoznik <mprivozn@redhat.com>
e8e641
Date: Tue, 7 Mar 2023 14:05:27 +0100
e8e641
Subject: [PATCH] virnuma: Introduce virNumaCPUSetToNodeset()
e8e641
e8e641
So far, we have a function that expands given list of NUMA nodes
e8e641
into list of CPUs. But soon, we are going to need the inverse -
e8e641
expand list of CPUs into list of NUMA nodes. Introduce
e8e641
virNumaCPUSetToNodeset() for that.
e8e641
e8e641
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
e8e641
Reviewed-by: Kristina Hanicova <khanicov@redhat.com>
e8e641
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
e8e641
(cherry picked from commit b6cfd348e9fd1c748481416b1ef42b482db4b4cb)
e8e641
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2185039
e8e641
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
e8e641
---
e8e641
 src/libvirt_private.syms |  2 ++
e8e641
 src/util/virnuma.c       | 59 ++++++++++++++++++++++++++++++++++++++++
e8e641
 src/util/virnuma.h       |  3 ++
e8e641
 3 files changed, 64 insertions(+)
e8e641
e8e641
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
e8e641
index e20421e7cd..28a6efda8f 100644
e8e641
--- a/src/libvirt_private.syms
e8e641
+++ b/src/libvirt_private.syms
e8e641
@@ -2961,6 +2961,7 @@ virNodeSuspendGetTargetMask;
e8e641
 
e8e641
 
e8e641
 # util/virnuma.h
e8e641
+virNumaCPUSetToNodeset;
e8e641
 virNumaGetAutoPlacementAdvice;
e8e641
 virNumaGetDistances;
e8e641
 virNumaGetHostMemoryNodeset;
e8e641
@@ -2968,6 +2969,7 @@ virNumaGetMaxCPUs;
e8e641
 virNumaGetMaxNode;
e8e641
 virNumaGetNodeCPUs;
e8e641
 virNumaGetNodeMemory;
e8e641
+virNumaGetNodeOfCPU;
e8e641
 virNumaGetPageInfo;
e8e641
 virNumaGetPages;
e8e641
 virNumaIsAvailable;
e8e641
diff --git a/src/util/virnuma.c b/src/util/virnuma.c
e8e641
index dae0827c65..4a15bf32c8 100644
e8e641
--- a/src/util/virnuma.c
e8e641
+++ b/src/util/virnuma.c
e8e641
@@ -311,6 +311,22 @@ virNumaGetNodeCPUs(int node,
e8e641
 # undef MASK_CPU_ISSET
e8e641
 # undef n_bits
e8e641
 
e8e641
+
e8e641
+/**
e8e641
+ * virNumaGetNodeOfCPU:
e8e641
+ * @cpu: CPU ID
e8e641
+ *
e8e641
+ * For given @cpu, return NUMA node which it belongs to.
e8e641
+ *
e8e641
+ * Returns: NUMA node # on success,
e8e641
+ *          -1 on failure (with errno set).
e8e641
+ */
e8e641
+int
e8e641
+virNumaGetNodeOfCPU(int cpu)
e8e641
+{
e8e641
+    return numa_node_of_cpu(cpu);
e8e641
+}
e8e641
+
e8e641
 #else /* !WITH_NUMACTL */
e8e641
 
e8e641
 int
e8e641
@@ -366,6 +382,14 @@ virNumaGetNodeCPUs(int node G_GNUC_UNUSED,
e8e641
     return -1;
e8e641
 }
e8e641
 
e8e641
+int
e8e641
+virNumaGetNodeOfCPU(int cpu G_GNUC_UNUSED)
e8e641
+{
e8e641
+    errno = ENOSYS;
e8e641
+    return -1;
e8e641
+}
e8e641
+
e8e641
+
e8e641
 #endif /* !WITH_NUMACTL */
e8e641
 
e8e641
 /**
e8e641
@@ -990,6 +1014,41 @@ virNumaGetHostMemoryNodeset(void)
e8e641
 }
e8e641
 
e8e641
 
e8e641
+/**
e8e641
+ * virNumaCPUSetToNodeset:
e8e641
+ * @cpuset: bitmap containing a set of CPUs
e8e641
+ * @nodeset: returned bitmap containing a set of NUMA nodes
e8e641
+ *
e8e641
+ * Convert a set of CPUs to set of NUMA nodes that contain the CPUs.
e8e641
+ *
e8e641
+ * Returns: 0 on success,
e8e641
+ *          -1 on failure (with error reported)
e8e641
+ */
e8e641
+int
e8e641
+virNumaCPUSetToNodeset(virBitmap *cpuset,
e8e641
+                       virBitmap **nodeset)
e8e641
+{
e8e641
+    g_autoptr(virBitmap) nodes = virBitmapNew(0);
e8e641
+    ssize_t pos = -1;
e8e641
+
e8e641
+    while ((pos = virBitmapNextSetBit(cpuset, pos)) >= 0) {
e8e641
+        int node = virNumaGetNodeOfCPU(pos);
e8e641
+
e8e641
+        if (node < 0) {
e8e641
+            virReportSystemError(errno,
e8e641
+                                 _("Unable to get NUMA node of cpu %zd"),
e8e641
+                                 pos);
e8e641
+            return -1;
e8e641
+        }
e8e641
+
e8e641
+        virBitmapSetBitExpand(nodes, node);
e8e641
+    }
e8e641
+
e8e641
+    *nodeset = g_steal_pointer(&nodes);
e8e641
+    return 0;
e8e641
+}
e8e641
+
e8e641
+
e8e641
 /**
e8e641
  * virNumaNodesetToCPUset:
e8e641
  * @nodeset: bitmap containing a set of NUMA nodes
e8e641
diff --git a/src/util/virnuma.h b/src/util/virnuma.h
e8e641
index c35acd47cb..2c30ef4e31 100644
e8e641
--- a/src/util/virnuma.h
e8e641
+++ b/src/util/virnuma.h
e8e641
@@ -45,7 +45,10 @@ int virNumaGetNodeMemory(int node,
e8e641
 
e8e641
 unsigned int virNumaGetMaxCPUs(void) G_NO_INLINE;
e8e641
 
e8e641
+int virNumaGetNodeOfCPU(int cpu);
e8e641
 int virNumaGetNodeCPUs(int node, virBitmap **cpus) G_NO_INLINE;
e8e641
+int virNumaCPUSetToNodeset(virBitmap *cpuset,
e8e641
+                           virBitmap **nodeset);
e8e641
 int virNumaNodesetToCPUset(virBitmap *nodeset,
e8e641
                            virBitmap **cpuset);
e8e641
 
e8e641
-- 
e8e641
2.40.0