]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virnuma: Introduce virNumaCPUSetToNodeset()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 7 Mar 2023 13:05:27 +0000 (14:05 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 15 Mar 2023 11:46:16 +0000 (12:46 +0100)
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>
Reviewed-by: Kristina Hanicova <khanicov@redhat.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
src/libvirt_private.syms
src/util/virnuma.c
src/util/virnuma.h

index 6f447882339aa91ac8e4e30ed4f62e9541a0d142..605522c9f6683c3f2e92703187cb4f0e70c25450 100644 (file)
@@ -2976,6 +2976,7 @@ virNodeSuspendGetTargetMask;
 
 
 # util/virnuma.h
+virNumaCPUSetToNodeset;
 virNumaGetAutoPlacementAdvice;
 virNumaGetDistances;
 virNumaGetHostMemoryNodeset;
@@ -2983,6 +2984,7 @@ virNumaGetMaxCPUs;
 virNumaGetMaxNode;
 virNumaGetNodeCPUs;
 virNumaGetNodeMemory;
+virNumaGetNodeOfCPU;
 virNumaGetPageInfo;
 virNumaGetPages;
 virNumaIsAvailable;
index dae0827c6517cc4fc1d583ad36bd38da03333f79..4a15bf32c83f660d0aaa1b697ff1dd4d5c365613 100644 (file)
@@ -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)
 }
 
 
+/**
+ * virNumaCPUSetToNodeset:
+ * @cpuset: bitmap containing a set of CPUs
+ * @nodeset: returned bitmap containing a set of NUMA nodes
+ *
+ * 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 *cpuset,
+                       virBitmap **nodeset)
+{
+    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
index c35acd47cb157d51f5422dec28239c15a11247d1..2c30ef4e3136d243908cc0aa1a5c4661b7f8f590 100644 (file)
@@ -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 *cpuset,
+                           virBitmap **nodeset);
 int virNumaNodesetToCPUset(virBitmap *nodeset,
                            virBitmap **cpuset);