[PATCH 3/4] numa_conf: Expose virNumaDistance formatter

Michal Privoznik posted 4 patches 4 years, 8 months ago
[PATCH 3/4] numa_conf: Expose virNumaDistance formatter
Posted by Michal Privoznik 4 years, 8 months ago
Expose virNumaDistance XML formatter so that it can be re-used by
other parts of the code.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/conf/numa_conf.c     | 47 +++++++++++++++++++++-------------------
 src/conf/numa_conf.h     | 10 +++++++++
 src/libvirt_private.syms |  1 +
 3 files changed, 36 insertions(+), 22 deletions(-)

diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c
index fa39c1ccc0..8fd5635aca 100644
--- a/src/conf/numa_conf.c
+++ b/src/conf/numa_conf.c
@@ -82,8 +82,6 @@ VIR_ENUM_IMPL(virDomainMemoryLatency,
               "write"
 );
 
-typedef struct _virNumaDistance virNumaDistance;
-
 typedef struct _virDomainNumaCache virDomainNumaCache;
 
 typedef struct _virDomainNumaInterconnect virDomainNumaInterconnect;
@@ -106,10 +104,7 @@ struct _virDomainNuma {
         virDomainMemoryAccess memAccess; /* shared memory access configuration */
         virTristateBool discard; /* discard-data for memory-backend-file */
 
-        struct _virNumaDistance {
-            unsigned int value; /* locality value for node i->j or j->i */
-            unsigned int cellid;
-        } *distances;           /* remote node distances */
+        virNumaDistance *distances; /* remote node distances */
         size_t ndistances;
 
         struct _virDomainNumaCache {
@@ -1132,22 +1127,9 @@ virDomainNumaDefFormatXML(virBuffer *buf,
             virBufferAsprintf(&attrBuf, " discard='%s'",
                               virTristateBoolTypeToString(discard));
 
-        if (def->mem_nodes[i].ndistances) {
-            virNumaDistance *distances = def->mem_nodes[i].distances;
-
-            virBufferAddLit(&childBuf, "<distances>\n");
-            virBufferAdjustIndent(&childBuf, 2);
-            for (j = 0; j < def->mem_nodes[i].ndistances; j++) {
-                if (distances[j].value) {
-                    virBufferAddLit(&childBuf, "<sibling");
-                    virBufferAsprintf(&childBuf, " id='%d'", distances[j].cellid);
-                    virBufferAsprintf(&childBuf, " value='%d'", distances[j].value);
-                    virBufferAddLit(&childBuf, "/>\n");
-                }
-            }
-            virBufferAdjustIndent(&childBuf, -2);
-            virBufferAddLit(&childBuf, "</distances>\n");
-        }
+        virNumaDistanceFormat(&childBuf,
+                              def->mem_nodes[i].distances,
+                              def->mem_nodes[i].ndistances);
 
         for (j = 0; j < def->mem_nodes[i].ncaches; j++) {
             virDomainNumaCache *cache = &def->mem_nodes[i].caches[j];
@@ -1836,3 +1818,24 @@ virDomainNumaGetInterconnect(const virDomainNuma *numa,
     *value = l->value;
     return 0;
 }
+
+
+void
+virNumaDistanceFormat(virBuffer *buf,
+                      const virNumaDistance *distances,
+                      size_t ndistances)
+{
+    g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf);
+    size_t i;
+
+    for (i = 0; i < ndistances; i++) {
+        if (distances[i].value == 0)
+            continue;
+        virBufferAddLit(&childBuf, "<sibling");
+        virBufferAsprintf(&childBuf, " id='%d'", distances[i].cellid);
+        virBufferAsprintf(&childBuf, " value='%d'", distances[i].value);
+        virBufferAddLit(&childBuf, "/>\n");
+    }
+
+    virXMLFormatElement(buf, "distances", NULL, &childBuf);
+}
diff --git a/src/conf/numa_conf.h b/src/conf/numa_conf.h
index 8f6597b0b7..6682580ded 100644
--- a/src/conf/numa_conf.h
+++ b/src/conf/numa_conf.h
@@ -249,3 +249,13 @@ int virDomainNumaGetInterconnect(const virDomainNuma *numa,
                                  unsigned int *cache,
                                  virDomainMemoryLatency *accessType,
                                  unsigned long *value);
+
+typedef struct _virNumaDistance virNumaDistance;
+struct _virNumaDistance {
+    unsigned int value; /* locality value for node i->j or j->i */
+    unsigned int cellid;
+};
+
+void virNumaDistanceFormat(virBuffer *buf,
+                           const virNumaDistance *distances,
+                           size_t ndistances);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 6fbdee4124..fcf833429b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -913,6 +913,7 @@ virDomainNumatunePlacementTypeFromString;
 virDomainNumatunePlacementTypeToString;
 virDomainNumatuneSet;
 virDomainNumatuneSpecifiedMaxNode;
+virNumaDistanceFormat;
 
 
 # conf/nwfilter_conf.h
-- 
2.26.3

Re: [PATCH 3/4] numa_conf: Expose virNumaDistance formatter
Posted by Peter Krempa 4 years, 8 months ago
On Thu, May 20, 2021 at 17:24:55 +0200, Michal Privoznik wrote:
> Expose virNumaDistance XML formatter so that it can be re-used by
> other parts of the code.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/conf/numa_conf.c     | 47 +++++++++++++++++++++-------------------
>  src/conf/numa_conf.h     | 10 +++++++++
>  src/libvirt_private.syms |  1 +
>  3 files changed, 36 insertions(+), 22 deletions(-)
> 
> diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c
> index fa39c1ccc0..8fd5635aca 100644
> --- a/src/conf/numa_conf.c
> +++ b/src/conf/numa_conf.c

[...]

> @@ -1132,22 +1127,9 @@ virDomainNumaDefFormatXML(virBuffer *buf,
>              virBufferAsprintf(&attrBuf, " discard='%s'",
>                                virTristateBoolTypeToString(discard));
>  
> -        if (def->mem_nodes[i].ndistances) {
> -            virNumaDistance *distances = def->mem_nodes[i].distances;
> -
> -            virBufferAddLit(&childBuf, "<distances>\n");

A slight semantic difference between the two impls is that this will
format an empty <distances> pair tag ...

> -            virBufferAdjustIndent(&childBuf, 2);
> -            for (j = 0; j < def->mem_nodes[i].ndistances; j++) {
> -                if (distances[j].value) {

... if all of these are 0.

> -                    virBufferAddLit(&childBuf, "<sibling");
> -                    virBufferAsprintf(&childBuf, " id='%d'", distances[j].cellid);
> -                    virBufferAsprintf(&childBuf, " value='%d'", distances[j].value);
> -                    virBufferAddLit(&childBuf, "/>\n");
> -                }
> -            }
> -            virBufferAdjustIndent(&childBuf, -2);
> -            virBufferAddLit(&childBuf, "</distances>\n");
> -        }
> +        virNumaDistanceFormat(&childBuf,
> +                              def->mem_nodes[i].distances,
> +                              def->mem_nodes[i].ndistances);
>  
>          for (j = 0; j < def->mem_nodes[i].ncaches; j++) {
>              virDomainNumaCache *cache = &def->mem_nodes[i].caches[j];
> @@ -1836,3 +1818,24 @@ virDomainNumaGetInterconnect(const virDomainNuma *numa,
>      *value = l->value;
>      return 0;
>  }
> +
> +
> +void
> +virNumaDistanceFormat(virBuffer *buf,
> +                      const virNumaDistance *distances,
> +                      size_t ndistances)
> +{
> +    g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf);
> +    size_t i;
> +
> +    for (i = 0; i < ndistances; i++) {
> +        if (distances[i].value == 0)
> +            continue;
> +        virBufferAddLit(&childBuf, "<sibling");
> +        virBufferAsprintf(&childBuf, " id='%d'", distances[i].cellid);
> +        virBufferAsprintf(&childBuf, " value='%d'", distances[i].value);
> +        virBufferAddLit(&childBuf, "/>\n");
> +    }
> +
> +    virXMLFormatElement(buf, "distances", NULL, &childBuf);

while here we won't.

> +}

In this case it shouldn't be a problem though unlike in the NBD part of
the migration cookie ;). If you think it might be a problem you can use
virXMLFormatElementEmpty.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>