[PATCH] conf: improve the way we format blkiotune and cputune

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

 



Just refactor existing code to use a child buf instead of
check all element before format <blkiotune> and <cputune>.
This will avoid the more and more bigger element check during
we introduce new elements in <blkiotune> and <cputune> in the
future.

Signed-off-by: Luyao Huang <lhuang@xxxxxxxxxx>
---
 src/conf/domain_conf.c | 168 +++++++++++++++++++------------------------------
 1 file changed, 65 insertions(+), 103 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e592adf..466355a 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -20976,24 +20976,6 @@ virDomainHubDefFormat(virBufferPtr buf,
     return 0;
 }
 
-/*
- * Return true if no <vcpupin> specified in domain XML
- * (I.e. all vcpus inherit the cpuset from "cpuset" of
- * <vcpu>). Or false otherwise.
- */
-static bool
-virDomainIsAllVcpupinInherited(virDomainDefPtr def)
-{
-    size_t i;
-
-    for (i = 0; i < def->cputune.nvcpupin; i++) {
-        if (!virBitmapEqual(def->cputune.vcpupin[i]->cpumask, def->cpumask))
-            return false;
-    }
-
-    return true;
-}
-
 
 static void
 virDomainResourceDefFormat(virBufferPtr buf,
@@ -21124,8 +21106,8 @@ virDomainDefFormatInternal(virDomainDefPtr def,
     const char *type = NULL;
     int n;
     size_t i;
-    bool blkio = false;
-    bool cputune = false;
+    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
+    int indent;
 
     virCheckFlags(VIR_DOMAIN_DEF_FORMAT_COMMON_FLAGS |
                   VIR_DOMAIN_DEF_FORMAT_STATUS |
@@ -21201,62 +21183,47 @@ virDomainDefFormatInternal(virDomainDefPtr def,
     virBufferAsprintf(buf, "<currentMemory unit='KiB'>%llu</currentMemory>\n",
                       def->mem.cur_balloon);
 
-    /* add blkiotune only if there are any */
-    if (def->blkio.weight) {
-        blkio = true;
-    } else {
-        for (n = 0; n < def->blkio.ndevices; n++) {
-            if (def->blkio.devices[n].weight ||
-                def->blkio.devices[n].riops ||
-                def->blkio.devices[n].wiops ||
-                def->blkio.devices[n].rbps ||
-                def->blkio.devices[n].wbps) {
-                blkio = true;
-                break;
-            }
-        }
-    }
-
-    if (blkio) {
-        virBufferAddLit(buf, "<blkiotune>\n");
-        virBufferAdjustIndent(buf, 2);
-
-        if (def->blkio.weight)
-            virBufferAsprintf(buf, "<weight>%u</weight>\n",
-                              def->blkio.weight);
-
-        for (n = 0; n < def->blkio.ndevices; n++) {
-            virBlkioDevicePtr dev = &def->blkio.devices[n];
+    /* start format blkiotune */
+    indent = virBufferGetIndent(buf, false);
+    virBufferAdjustIndent(&childrenBuf, indent + 2);
+    if (def->blkio.weight)
+        virBufferAsprintf(&childrenBuf, "<weight>%u</weight>\n",
+                          def->blkio.weight);
 
-            if (!dev->weight && !dev->riops && !dev->wiops &&
-                !dev->rbps && !dev->wbps)
-                continue;
-            virBufferAddLit(buf, "<device>\n");
-            virBufferAdjustIndent(buf, 2);
-            virBufferEscapeString(buf, "<path>%s</path>\n",
-                                  dev->path);
-            if (dev->weight)
-                virBufferAsprintf(buf, "<weight>%u</weight>\n",
-                                  dev->weight);
-            if (dev->riops)
-                virBufferAsprintf(buf, "<read_iops_sec>%u</read_iops_sec>\n",
-                                  dev->riops);
-            if (dev->wiops)
-                virBufferAsprintf(buf, "<write_iops_sec>%u</write_iops_sec>\n",
-                                  dev->wiops);
-            if (dev->rbps)
-                virBufferAsprintf(buf, "<read_bytes_sec>%llu</read_bytes_sec>\n",
-                                  dev->rbps);
-            if (dev->wbps)
-                virBufferAsprintf(buf, "<write_bytes_sec>%llu</write_bytes_sec>\n",
-                                  dev->wbps);
-            virBufferAdjustIndent(buf, -2);
-            virBufferAddLit(buf, "</device>\n");
-        }
+    for (n = 0; n < def->blkio.ndevices; n++) {
+        virBlkioDevicePtr dev = &def->blkio.devices[n];
 
-        virBufferAdjustIndent(buf, -2);
+        if (!dev->weight && !dev->riops && !dev->wiops &&
+            !dev->rbps && !dev->wbps)
+            continue;
+        virBufferAddLit(&childrenBuf, "<device>\n");
+        virBufferAdjustIndent(&childrenBuf, 2);
+        virBufferEscapeString(&childrenBuf, "<path>%s</path>\n",
+                              dev->path);
+        if (dev->weight)
+            virBufferAsprintf(&childrenBuf, "<weight>%u</weight>\n",
+                              dev->weight);
+        if (dev->riops)
+            virBufferAsprintf(&childrenBuf, "<read_iops_sec>%u</read_iops_sec>\n",
+                              dev->riops);
+        if (dev->wiops)
+            virBufferAsprintf(&childrenBuf, "<write_iops_sec>%u</write_iops_sec>\n",
+                              dev->wiops);
+        if (dev->rbps)
+            virBufferAsprintf(&childrenBuf, "<read_bytes_sec>%llu</read_bytes_sec>\n",
+                              dev->rbps);
+        if (dev->wbps)
+            virBufferAsprintf(&childrenBuf, "<write_bytes_sec>%llu</write_bytes_sec>\n",
+                              dev->wbps);
+        virBufferAdjustIndent(&childrenBuf, -2);
+        virBufferAddLit(&childrenBuf, "</device>\n");
+    }
+    if (virBufferUse(&childrenBuf)) {
+        virBufferAddLit(buf, "<blkiotune>\n");
+        virBufferAddBuffer(buf, &childrenBuf);
         virBufferAddLit(buf, "</blkiotune>\n");
     }
+    virBufferFreeAndReset(&childrenBuf);
 
     /* add memtune only if there are any */
     if (virMemoryLimitIsSet(def->mem.hard_limit) ||
@@ -21335,35 +21302,26 @@ virDomainDefFormatInternal(virDomainDefPtr def,
         }
     }
 
-    if (def->cputune.sharesSpecified ||
-        (def->cputune.nvcpupin && !virDomainIsAllVcpupinInherited(def)) ||
-        def->cputune.period || def->cputune.quota ||
-        def->cputune.emulatorpin ||
-        def->cputune.emulator_period || def->cputune.emulator_quota ||
-        virDomainIOThreadIDArrayHasPin(def) ||
-        def->cputune.vcpusched || def->cputune.iothreadsched) {
-        virBufferAddLit(buf, "<cputune>\n");
-        cputune = true;
-    }
-
-    virBufferAdjustIndent(buf, 2);
+    /* start format cputune */
+    indent = virBufferGetIndent(buf, false);
+    virBufferAdjustIndent(&childrenBuf, indent + 2);
     if (def->cputune.sharesSpecified)
-        virBufferAsprintf(buf, "<shares>%lu</shares>\n",
+        virBufferAsprintf(&childrenBuf, "<shares>%lu</shares>\n",
                           def->cputune.shares);
     if (def->cputune.period)
-        virBufferAsprintf(buf, "<period>%llu</period>\n",
+        virBufferAsprintf(&childrenBuf, "<period>%llu</period>\n",
                           def->cputune.period);
     if (def->cputune.quota)
-        virBufferAsprintf(buf, "<quota>%lld</quota>\n",
+        virBufferAsprintf(&childrenBuf, "<quota>%lld</quota>\n",
                           def->cputune.quota);
 
     if (def->cputune.emulator_period)
-        virBufferAsprintf(buf, "<emulator_period>%llu"
+        virBufferAsprintf(&childrenBuf, "<emulator_period>%llu"
                           "</emulator_period>\n",
                           def->cputune.emulator_period);
 
     if (def->cputune.emulator_quota)
-        virBufferAsprintf(buf, "<emulator_quota>%lld"
+        virBufferAsprintf(&childrenBuf, "<emulator_quota>%lld"
                           "</emulator_quota>\n",
                           def->cputune.emulator_quota);
 
@@ -21373,24 +21331,24 @@ virDomainDefFormatInternal(virDomainDefPtr def,
         if (virBitmapEqual(def->cpumask, def->cputune.vcpupin[i]->cpumask))
             continue;
 
-        virBufferAsprintf(buf, "<vcpupin vcpu='%u' ",
+        virBufferAsprintf(&childrenBuf, "<vcpupin vcpu='%u' ",
                           def->cputune.vcpupin[i]->id);
 
         if (!(cpumask = virBitmapFormat(def->cputune.vcpupin[i]->cpumask)))
             goto error;
 
-        virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask);
+        virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask);
         VIR_FREE(cpumask);
     }
 
     if (def->cputune.emulatorpin) {
         char *cpumask;
-        virBufferAddLit(buf, "<emulatorpin ");
+        virBufferAddLit(&childrenBuf, "<emulatorpin ");
 
         if (!(cpumask = virBitmapFormat(def->cputune.emulatorpin)))
             goto error;
 
-        virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask);
+        virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask);
         VIR_FREE(cpumask);
     }
 
@@ -21401,13 +21359,13 @@ virDomainDefFormatInternal(virDomainDefPtr def,
         if (!def->iothreadids[i]->cpumask)
             continue;
 
-        virBufferAsprintf(buf, "<iothreadpin iothread='%u' ",
+        virBufferAsprintf(&childrenBuf, "<iothreadpin iothread='%u' ",
                           def->iothreadids[i]->iothread_id);
 
         if (!(cpumask = virBitmapFormat(def->iothreadids[i]->cpumask)))
             goto error;
 
-        virBufferAsprintf(buf, "cpuset='%s'/>\n", cpumask);
+        virBufferAsprintf(&childrenBuf, "cpuset='%s'/>\n", cpumask);
         VIR_FREE(cpumask);
     }
 
@@ -21417,13 +21375,13 @@ virDomainDefFormatInternal(virDomainDefPtr def,
 
         if (!(ids = virBitmapFormat(sp->ids)))
             goto error;
-        virBufferAsprintf(buf, "<vcpusched vcpus='%s' scheduler='%s'",
+        virBufferAsprintf(&childrenBuf, "<vcpusched vcpus='%s' scheduler='%s'",
                           ids, virProcessSchedPolicyTypeToString(sp->policy));
         VIR_FREE(ids);
 
         if (sp->priority)
-            virBufferAsprintf(buf, " priority='%d'", sp->priority);
-        virBufferAddLit(buf, "/>\n");
+            virBufferAsprintf(&childrenBuf, " priority='%d'", sp->priority);
+        virBufferAddLit(&childrenBuf, "/>\n");
     }
 
     for (i = 0; i < def->cputune.niothreadsched; i++) {
@@ -21432,18 +21390,21 @@ virDomainDefFormatInternal(virDomainDefPtr def,
 
         if (!(ids = virBitmapFormat(sp->ids)))
             goto error;
-        virBufferAsprintf(buf, "<iothreadsched iothreads='%s' scheduler='%s'",
+        virBufferAsprintf(&childrenBuf, "<iothreadsched iothreads='%s' scheduler='%s'",
                           ids, virProcessSchedPolicyTypeToString(sp->policy));
         VIR_FREE(ids);
 
         if (sp->priority)
-            virBufferAsprintf(buf, " priority='%d'", sp->priority);
-        virBufferAddLit(buf, "/>\n");
+            virBufferAsprintf(&childrenBuf, " priority='%d'", sp->priority);
+        virBufferAddLit(&childrenBuf, "/>\n");
     }
 
-    virBufferAdjustIndent(buf, -2);
-    if (cputune)
+    if (virBufferUse(&childrenBuf)) {
+        virBufferAddLit(buf, "<cputune>\n");
+        virBufferAddBuffer(buf, &childrenBuf);
         virBufferAddLit(buf, "</cputune>\n");
+    }
+    virBufferFreeAndReset(&childrenBuf);
 
     if (virDomainNumatuneFormatXML(buf, def->numa) < 0)
         goto error;
@@ -22017,6 +21978,7 @@ virDomainDefFormatInternal(virDomainDefPtr def,
 
  error:
     virBufferFreeAndReset(buf);
+    virBufferFreeAndReset(&childrenBuf);
     return -1;
 }
 
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[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]