]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
capabilities: Separate <cpu/> formatting into a function
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 26 May 2021 13:39:38 +0000 (15:39 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 15 Jun 2021 08:41:22 +0000 (10:41 +0200)
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@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/conf/capabilities.c

index d3b22f7dd0bb9532f073d07975cecf7007f01114..c825cb0503a63cd6ae559a6011bb2f6ecb80c103 100644 (file)
@@ -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 (cell->cpus[j].siblings) {
-                g_autofree char *siblings = NULL;
-
-                if (!(siblings = virBitmapFormat(cell->cpus[j].siblings)))
-                    return -1;
+        if (virCapsHostNUMACellCPUFormat(buf, cell->cpus, cell->ncpus) < 0)
+            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");
     }