[PATCH v2 03/10] capabilities: Separate <cpu/> formatting into a function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The way we format <cpu/> element for capabilities is not ideal,
because if there are no CPUs, i.e. no child elements, we still
output opening and closing element. To solve this,
virXMLFormatElement() could be used but that would introduce more
variables into the loop. Therefore, move the formatter into a
separate function and use virXMLFormatElement().

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
Reviewed-by: Martin Kletzander <mkletzan@xxxxxxxxxx>
---
 src/conf/capabilities.c | 58 ++++++++++++++++++++++++++---------------
 1 file changed, 37 insertions(+), 21 deletions(-)

diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index 2f9a1e7d1f..9332daf6a6 100644
--- a/src/conf/capabilities.c
+++ b/src/conf/capabilities.c
@@ -802,6 +802,41 @@ virCapabilitiesAddStoragePool(virCaps *caps,
 }
 
 
+static int
+virCapsHostNUMACellCPUFormat(virBuffer *buf,
+                             const virCapsHostNUMACellCPU *cpus,
+                             int ncpus)
+{
+    g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf);
+    size_t j;
+
+    virBufferAsprintf(&attrBuf, " num='%d'", ncpus);
+
+    for (j = 0; j < ncpus; j++) {
+        virBufferAsprintf(&childBuf, "<cpu id='%d'", cpus[j].id);
+
+        if (cpus[j].siblings) {
+            g_autofree char *siblings = NULL;
+
+            if (!(siblings = virBitmapFormat(cpus[j].siblings)))
+                return -1;
+
+            virBufferAsprintf(&childBuf,
+                              " socket_id='%d' die_id='%d' core_id='%d' siblings='%s'",
+                              cpus[j].socket_id,
+                              cpus[j].die_id,
+                              cpus[j].core_id,
+                              siblings);
+        }
+        virBufferAddLit(&childBuf, "/>\n");
+    }
+
+    virXMLFormatElement(buf, "cpus", &attrBuf, &childBuf);
+    return 0;
+}
+
+
 static int
 virCapabilitiesHostNUMAFormat(virBuffer *buf,
                               virCapsHostNUMA *caps)
@@ -835,28 +870,9 @@ virCapabilitiesHostNUMAFormat(virBuffer *buf,
 
         virNumaDistanceFormat(buf, cell->distances, cell->ndistances);
 
-        virBufferAsprintf(buf, "<cpus num='%d'>\n", cell->ncpus);
-        virBufferAdjustIndent(buf, 2);
-        for (j = 0; j < cell->ncpus; j++) {
-            virBufferAsprintf(buf, "<cpu id='%d'", cell->cpus[j].id);
+        if (virCapsHostNUMACellCPUFormat(buf, cell->cpus, cell->ncpus) < 0)
+            return -1;
 
-            if (cell->cpus[j].siblings) {
-                g_autofree char *siblings = NULL;
-
-                if (!(siblings = virBitmapFormat(cell->cpus[j].siblings)))
-                    return -1;
-
-                virBufferAsprintf(buf,
-                                  " socket_id='%d' die_id='%d' core_id='%d' siblings='%s'",
-                                  cell->cpus[j].socket_id,
-                                  cell->cpus[j].die_id,
-                                  cell->cpus[j].core_id,
-                                  siblings);
-            }
-            virBufferAddLit(buf, "/>\n");
-        }
-        virBufferAdjustIndent(buf, -2);
-        virBufferAddLit(buf, "</cpus>\n");
         virBufferAdjustIndent(buf, -2);
         virBufferAddLit(buf, "</cell>\n");
     }
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux