[PATCH 02/14] virnuma: Introduce virNumaCPUSetToNodeset()

Michal Privoznik posted 14 patches 2 years, 9 months ago
There is a newer version of this series
[PATCH 02/14] virnuma: Introduce virNumaCPUSetToNodeset()
Posted by Michal Privoznik 2 years, 9 months ago
So far, we have a function that expands given list of NUMA nodes
into list of CPUs. But soon, we are going to need the inverse -
expand list of CPUs into list of NUMA nodes. Introduce
virNumaCPUSetToNodeset() for that.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virnuma.c       | 59 ++++++++++++++++++++++++++++++++++++++++
 src/util/virnuma.h       |  3 ++
 3 files changed, 64 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index d5b1b9cb72..3ae97e62de 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2975,6 +2975,7 @@ virNodeSuspendGetTargetMask;
 
 
 # util/virnuma.h
+virNumaCPUSetToNodeset;
 virNumaGetAutoPlacementAdvice;
 virNumaGetDistances;
 virNumaGetHostMemoryNodeset;
@@ -2982,6 +2983,7 @@ virNumaGetMaxCPUs;
 virNumaGetMaxNode;
 virNumaGetNodeCPUs;
 virNumaGetNodeMemory;
+virNumaGetNodeOfCPU;
 virNumaGetPageInfo;
 virNumaGetPages;
 virNumaIsAvailable;
diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index dae0827c65..5f484a16f5 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -311,6 +311,22 @@ virNumaGetNodeCPUs(int node,
 # undef MASK_CPU_ISSET
 # undef n_bits
 
+
+/**
+ * virNumaGetNodeOfCPU:
+ * @cpu: CPU ID
+ *
+ * For given @cpu, return NUMA node which it belongs to.
+ *
+ * Returns: NUMA node # on success,
+ *          -1 on failure (with errno set).
+ */
+int
+virNumaGetNodeOfCPU(int cpu)
+{
+    return numa_node_of_cpu(cpu);
+}
+
 #else /* !WITH_NUMACTL */
 
 int
@@ -366,6 +382,14 @@ virNumaGetNodeCPUs(int node G_GNUC_UNUSED,
     return -1;
 }
 
+int
+virNumaGetNodeOfCPU(int cpu G_GNUC_UNUSED)
+{
+    errno = ENOSYS;
+    return -1;
+}
+
+
 #endif /* !WITH_NUMACTL */
 
 /**
@@ -990,6 +1014,41 @@ virNumaGetHostMemoryNodeset(void)
 }
 
 
+/**
+ * virNumaNodesetToCPUset:
+ * @nodeset: returned bitmap containing a set of NUMA nodes
+ * @cpuset: bitmap containing a set of CPUs
+ *
+ * Convert a set of CPUs to set of NUMA nodes that contain the CPUs.
+ *
+ * Returns: 0 on success,
+ *          -1 on failure (with error reported)
+ */
+int
+virNumaCPUSetToNodeset(virBitmap **nodeset,
+                       virBitmap *cpuset)
+{
+    g_autoptr(virBitmap) nodes = virBitmapNew(0);
+    ssize_t pos = -1;
+
+    while ((pos = virBitmapNextSetBit(cpuset, pos)) >= 0) {
+        int node = virNumaGetNodeOfCPU(pos);
+
+        if (node < 0) {
+            virReportSystemError(errno,
+                                 _("Unable to get NUMA node of cpu %zd"),
+                                 pos);
+            return -1;
+        }
+
+        virBitmapSetBitExpand(nodes, node);
+    }
+
+    *nodeset = g_steal_pointer(&nodes);
+    return 0;
+}
+
+
 /**
  * virNumaNodesetToCPUset:
  * @nodeset: bitmap containing a set of NUMA nodes
diff --git a/src/util/virnuma.h b/src/util/virnuma.h
index c35acd47cb..67e699f2cf 100644
--- a/src/util/virnuma.h
+++ b/src/util/virnuma.h
@@ -45,7 +45,10 @@ int virNumaGetNodeMemory(int node,
 
 unsigned int virNumaGetMaxCPUs(void) G_NO_INLINE;
 
+int virNumaGetNodeOfCPU(int cpu);
 int virNumaGetNodeCPUs(int node, virBitmap **cpus) G_NO_INLINE;
+int virNumaCPUSetToNodeset(virBitmap **nodeset,
+                           virBitmap *cpuset);
 int virNumaNodesetToCPUset(virBitmap *nodeset,
                            virBitmap **cpuset);
 
-- 
2.39.2
Re: [PATCH 02/14] virnuma: Introduce virNumaCPUSetToNodeset()
Posted by Andrea Bolognani 2 years, 9 months ago
On Wed, Mar 08, 2023 at 12:14:29PM +0100, Michal Privoznik wrote:
> +/**
> + * virNumaNodesetToCPUset:
> + * @nodeset: returned bitmap containing a set of NUMA nodes
> + * @cpuset: bitmap containing a set of CPUs
> + *
> + * Convert a set of CPUs to set of NUMA nodes that contain the CPUs.
> + *
> + * Returns: 0 on success,
> + *          -1 on failure (with error reported)
> + */
> +int
> +virNumaCPUSetToNodeset(virBitmap **nodeset,
> +                       virBitmap *cpuset)

The name of the function in the docstring is wrong.

Please also consider swapping the arguments, to match the name of the
function and how the existing virNumaNodesetToCPUset() does it.

-- 
Andrea Bolognani / Red Hat / Virtualization
Re: [PATCH 02/14] virnuma: Introduce virNumaCPUSetToNodeset()
Posted by Kristina Hanicova 2 years, 9 months ago
On Wed, Mar 8, 2023 at 12:15 PM Michal Privoznik <mprivozn@redhat.com>
wrote:

> So far, we have a function that expands given list of NUMA nodes
> into list of CPUs. But soon, we are going to need the inverse -
> expand list of CPUs into list of NUMA nodes. Introduce
> virNumaCPUSetToNodeset() for that.
>
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/libvirt_private.syms |  2 ++
>  src/util/virnuma.c       | 59 ++++++++++++++++++++++++++++++++++++++++
>  src/util/virnuma.h       |  3 ++
>  3 files changed, 64 insertions(+)
>


Reviewed-by: Kristina Hanicova <khanicov@redhat.com>

Kristina