[PATCH v2 05/10] numa_conf: Expose virNumaCache formatter

Michal Privoznik mprivozn at redhat.com
Thu Jun 10 13:57:14 UTC 2021


Expose virNumaCache XML formatter so that it can be re-used by
other parts of the code.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
Reviewed-by: Martin Kletzander <mkletzan at redhat.com>
---
 src/conf/numa_conf.c     | 81 +++++++++++++++++++++-------------------
 src/conf/numa_conf.h     | 13 +++++++
 src/libvirt_private.syms |  1 +
 3 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/src/conf/numa_conf.c b/src/conf/numa_conf.c
index 2a32b4ca06..c4f2ef10c1 100644
--- a/src/conf/numa_conf.c
+++ b/src/conf/numa_conf.c
@@ -82,8 +82,6 @@ VIR_ENUM_IMPL(virDomainMemoryLatency,
               "write"
 );
 
-typedef struct _virNumaCache virNumaCache;
-
 typedef struct _virDomainNumaInterconnect virDomainNumaInterconnect;
 
 typedef struct _virDomainNumaNode virDomainNumaNode;
@@ -107,13 +105,7 @@ struct _virDomainNuma {
         virNumaDistance *distances; /* remote node distances */
         size_t ndistances;
 
-        struct _virNumaCache {
-            unsigned int level; /* cache level */
-            unsigned int size;  /* cache size */
-            unsigned int line;  /* line size, !!! in bytes !!! */
-            virNumaCacheAssociativity associativity; /* cache associativity */
-            virNumaCachePolicy policy; /* cache policy */
-        } *caches;
+        virNumaCache *caches;
         size_t ncaches;
     } *mem_nodes;           /* guest node configuration */
     size_t nmem_nodes;
@@ -1102,7 +1094,6 @@ virDomainNumaDefFormatXML(virBuffer *buf,
         virBitmap *cpumask = virDomainNumaGetNodeCpumask(def, i);
         g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
         g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf);
-        size_t j;
 
         memAccess = virDomainNumaGetNodeMemoryAccessMode(def, i);
         discard = virDomainNumaGetNodeDiscard(def, i);
@@ -1131,35 +1122,9 @@ virDomainNumaDefFormatXML(virBuffer *buf,
                               def->mem_nodes[i].distances,
                               def->mem_nodes[i].ndistances);
 
-        for (j = 0; j < def->mem_nodes[i].ncaches; j++) {
-            virNumaCache *cache = &def->mem_nodes[i].caches[j];
-
-            virBufferAsprintf(&childBuf, "<cache level='%u'", cache->level);
-            if (cache->associativity) {
-                virBufferAsprintf(&childBuf, " associativity='%s'",
-                                  virNumaCacheAssociativityTypeToString(cache->associativity));
-            }
-
-            if (cache->policy) {
-                virBufferAsprintf(&childBuf, " policy='%s'",
-                                  virNumaCachePolicyTypeToString(cache->policy));
-            }
-            virBufferAddLit(&childBuf, ">\n");
-
-            virBufferAdjustIndent(&childBuf, 2);
-            virBufferAsprintf(&childBuf,
-                              "<size value='%u' unit='KiB'/>\n",
-                              cache->size);
-
-            if (cache->line) {
-                virBufferAsprintf(&childBuf,
-                                  "<line value='%u' unit='B'/>\n",
-                                  cache->line);
-            }
-
-            virBufferAdjustIndent(&childBuf, -2);
-            virBufferAddLit(&childBuf, "</cache>\n");
-        }
+        virNumaCacheFormat(&childBuf,
+                           def->mem_nodes[i].caches,
+                           def->mem_nodes[i].ncaches);
 
         virXMLFormatElement(buf, "cell", &attrBuf, &childBuf);
     }
@@ -1839,3 +1804,41 @@ virNumaDistanceFormat(virBuffer *buf,
 
     virXMLFormatElement(buf, "distances", NULL, &childBuf);
 }
+
+
+void
+virNumaCacheFormat(virBuffer *buf,
+                   const virNumaCache *caches,
+                   size_t ncaches)
+{
+    size_t i;
+
+    for (i = 0; i < ncaches; i++) {
+        const virNumaCache *cache = &caches[i];
+        g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+        g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf);
+
+        virBufferAsprintf(&attrBuf, " level='%u'", cache->level);
+        if (cache->associativity) {
+            virBufferAsprintf(&attrBuf, " associativity='%s'",
+                              virNumaCacheAssociativityTypeToString(cache->associativity));
+        }
+
+        if (cache->policy) {
+            virBufferAsprintf(&attrBuf, " policy='%s'",
+                              virNumaCachePolicyTypeToString(cache->policy));
+        }
+
+        virBufferAsprintf(&childBuf,
+                          "<size value='%u' unit='KiB'/>\n",
+                          cache->size);
+
+        if (cache->line) {
+            virBufferAsprintf(&childBuf,
+                              "<line value='%u' unit='B'/>\n",
+                              cache->line);
+        }
+
+        virXMLFormatElement(buf, "cache", &attrBuf, &childBuf);
+    }
+}
diff --git a/src/conf/numa_conf.h b/src/conf/numa_conf.h
index 1ef6455fb5..9dee4f3a40 100644
--- a/src/conf/numa_conf.h
+++ b/src/conf/numa_conf.h
@@ -259,3 +259,16 @@ struct _virNumaDistance {
 void virNumaDistanceFormat(virBuffer *buf,
                            const virNumaDistance *distances,
                            size_t ndistances);
+
+typedef struct _virNumaCache virNumaCache;
+struct _virNumaCache {
+    unsigned int level; /* cache level */
+    unsigned int size;  /* cache size */
+    unsigned int line;  /* line size, !!! in bytes !!! */
+    virNumaCacheAssociativity associativity; /* cache associativity */
+    virNumaCachePolicy policy; /* cache policy */
+};
+
+void virNumaCacheFormat(virBuffer *buf,
+                        const virNumaCache *caches,
+                        size_t ncaches);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 9e9171a6d9..abe4525163 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -911,6 +911,7 @@ virDomainNumatuneSet;
 virDomainNumatuneSpecifiedMaxNode;
 virNumaCacheAssociativityTypeFromString;
 virNumaCacheAssociativityTypeToString;
+virNumaCacheFormat;
 virNumaCachePolicyTypeFromString;
 virNumaCachePolicyTypeToString;
 virNumaDistanceFormat;
-- 
2.31.1




More information about the libvir-list mailing list