[PATCH 3/3] Rewrite virDomainGetBlkioParametersInternal

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

 



Use:
  if (n <= 1)
    goto success;
  ...
    fill in params[1]
  ...

  if (n <= 2)
    goto success;

Instead of:
   for (i = 0; i < n; i++) {
     switch(i) {
       case 0:
         fill in params[0]
       case 1:
         fill in params[1]
   }

Also end the string with a comma unconditionally and trim it after.
---
 src/conf/domain_conf.c | 291 ++++++++++++++++++++-----------------------------
 1 file changed, 116 insertions(+), 175 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 99ab232..75e708e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -19788,10 +19788,12 @@ virDomainGetBlkioParametersInternal(virCgroupPtr cgroup,
                                     int *nparams,
                                     unsigned int flags)
 {
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
     int ret = -1;
-    size_t i, j;
+    size_t i;
     unsigned int val;
     virDomainDefPtr def;
+    char *str;
 
     if (flags & VIR_DOMAIN_AFFECT_LIVE)
         def = liveDef;
@@ -19812,193 +19814,132 @@ virDomainGetBlkioParametersInternal(virCgroupPtr cgroup,
             goto cleanup;
         }
 
-        for (i = 0; i < *nparams && i < CONF_NB_BLKIO_PARAM; i++) {
-            virTypedParameterPtr param = &params[i];
-            val = 0;
+        if (virCgroupGetBlkioWeight(cgroup, &val) < 0)
+            goto cleanup;
+    } else {
+        val = def->blkio.weight;
+    }
+    /* 0: fill blkio weight here */
+    if (virTypedParameterAssign(&params[0], VIR_DOMAIN_BLKIO_WEIGHT,
+                                VIR_TYPED_PARAM_UINT, val) < 0)
+        goto cleanup;
 
-            switch (i) {
-            case 0: /* fill blkio weight here */
-                if (virCgroupGetBlkioWeight(cgroup, &val) < 0)
-                    goto cleanup;
-                if (virTypedParameterAssign(param, VIR_DOMAIN_BLKIO_WEIGHT,
-                                            VIR_TYPED_PARAM_UINT, val) < 0)
-                    goto cleanup;
-                break;
-            }
-        }
-    } else if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
-        for (i = 0; i < *nparams && i < CONF_NB_BLKIO_PARAM; i++) {
-            virTypedParameterPtr param = &params[i];
-            val = 0;
-            param->value.ui = 0;
-            param->type = VIR_TYPED_PARAM_UINT;
+    /* 1: blkiotune.device_weight */
+    if (*nparams <= 1)
+        goto success;
 
-            switch (i) {
-            case 0: /* fill blkio weight here */
-                if (virStrcpyStatic(param->field, VIR_DOMAIN_BLKIO_WEIGHT) == NULL) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("Field name '%s' too long"),
-                                   VIR_DOMAIN_BLKIO_WEIGHT);
-                    goto cleanup;
-                }
-                param->value.ui = persistentDef->blkio.weight;
-                break;
-            }
-        }
+    for (i = 0; i < def->blkio.ndevices; i++) {
+        if (!def->blkio.devices[i].weight)
+            continue;
+        virBufferAsprintf(&buf, "%s,%u,",
+                          def->blkio.devices[i].path,
+                          def->blkio.devices[i].weight);
     }
+    if (virBufferError(&buf)) {
+        virReportOOMError();
+        goto cleanup;
+    }
+    virBufferTrim(&buf, ",", -1);
+    str = virBufferContentAndReset(&buf);
+    if (virTypedParameterAssign(&params[1],
+                                VIR_DOMAIN_BLKIO_DEVICE_WEIGHT,
+                                VIR_TYPED_PARAM_STRING,
+                                str) < 0)
+        goto cleanup;
 
-    for (i = 1; i < *nparams && i < CONF_NB_BLKIO_PARAM; i++) {
-        virTypedParameterPtr param = &params[i];
-        val = 0;
+    /* 2: blkiotune.device_read_iops */
+    if (*nparams <= 2)
+        goto success;
 
-        switch (i) {
-        case 1: /* blkiotune.device_weight */
-            if (def->blkio.ndevices > 0) {
-                virBuffer buf = VIR_BUFFER_INITIALIZER;
-                bool comma = false;
+    for (i = 0; i < def->blkio.ndevices; i++) {
+        if (!def->blkio.devices[i].riops)
+            continue;
+        virBufferAsprintf(&buf, "%s,%u,",
+                          def->blkio.devices[i].path,
+                          def->blkio.devices[i].riops);
+    }
+    if (virBufferError(&buf)) {
+        virReportOOMError();
+        goto cleanup;
+    }
+    virBufferTrim(&buf, ",", -1);
+    str = virBufferContentAndReset(&buf);
+    if (virTypedParameterAssign(&params[2],
+                                VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS,
+                                VIR_TYPED_PARAM_STRING,
+                                str) < 0)
+        goto cleanup;
 
-                for (j = 0; j < def->blkio.ndevices; j++) {
-                    if (!def->blkio.devices[j].weight)
-                        continue;
-                    if (comma)
-                        virBufferAddChar(&buf, ',');
-                    else
-                        comma = true;
-                    virBufferAsprintf(&buf, "%s,%u",
-                                      def->blkio.devices[j].path,
-                                      def->blkio.devices[j].weight);
-                }
-                if (virBufferError(&buf)) {
-                    virReportOOMError();
-                    goto cleanup;
-                }
-                param->value.s = virBufferContentAndReset(&buf);
-            }
-            if (virTypedParameterAssign(param,
-                                        VIR_DOMAIN_BLKIO_DEVICE_WEIGHT,
-                                        VIR_TYPED_PARAM_STRING,
-                                        param->value.s) < 0)
-                goto cleanup;
-            break;
+    /* 3: blkiotune.device_write_iops */
+    if (*nparams <= 3)
+        goto success;
 
-        case 2: /* blkiotune.device_read_iops */
-            if (def->blkio.ndevices > 0) {
-                virBuffer buf = VIR_BUFFER_INITIALIZER;
-                bool comma = false;
-
-                for (j = 0; j < def->blkio.ndevices; j++) {
-                    if (!def->blkio.devices[j].riops)
-                        continue;
-                    if (comma)
-                        virBufferAddChar(&buf, ',');
-                    else
-                        comma = true;
-                    virBufferAsprintf(&buf, "%s,%u",
-                                      def->blkio.devices[j].path,
-                                      def->blkio.devices[j].riops);
-                }
-                if (virBufferError(&buf)) {
-                    virReportOOMError();
-                    goto cleanup;
-                }
-                param->value.s = virBufferContentAndReset(&buf);
-            }
-            if (virTypedParameterAssign(param,
-                                        VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS,
-                                        VIR_TYPED_PARAM_STRING,
-                                        param->value.s) < 0)
-                goto cleanup;
-            break;
+    for (i = 0; i < def->blkio.ndevices; i++) {
+        if (!def->blkio.devices[i].wiops)
+            continue;
+        virBufferAsprintf(&buf, "%s,%u,",
+                          def->blkio.devices[i].path,
+                          def->blkio.devices[i].wiops);
+    }
+    if (virBufferError(&buf)) {
+        virReportOOMError();
+        goto cleanup;
+    }
+    virBufferTrim(&buf, ",", -1);
+    str = virBufferContentAndReset(&buf);
+    if (virTypedParameterAssign(&params[3],
+                                VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS,
+                                VIR_TYPED_PARAM_STRING,
+                                str) < 0)
+        goto cleanup;
 
-        case 3: /* blkiotune.device_write_iops */
-            if (def->blkio.ndevices > 0) {
-                virBuffer buf = VIR_BUFFER_INITIALIZER;
-                bool comma = false;
-
-                for (j = 0; j < def->blkio.ndevices; j++) {
-                    if (!def->blkio.devices[j].wiops)
-                        continue;
-                    if (comma)
-                        virBufferAddChar(&buf, ',');
-                    else
-                        comma = true;
-                    virBufferAsprintf(&buf, "%s,%u",
-                                      def->blkio.devices[j].path,
-                                      def->blkio.devices[j].wiops);
-                }
-                if (virBufferError(&buf)) {
-                    virReportOOMError();
-                    goto cleanup;
-                }
-                param->value.s = virBufferContentAndReset(&buf);
-            }
-            if (virTypedParameterAssign(param,
-                                        VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS,
-                                        VIR_TYPED_PARAM_STRING,
-                                        param->value.s) < 0)
-                goto cleanup;
-            break;
+    /* 4: blkiotune.device_read_bps */
+    if (*nparams <= 4)
+        goto success;
 
-        case 4: /* blkiotune.device_read_bps */
-            if (def->blkio.ndevices > 0) {
-                virBuffer buf = VIR_BUFFER_INITIALIZER;
-                bool comma = false;
-
-                for (j = 0; j < def->blkio.ndevices; j++) {
-                    if (!def->blkio.devices[j].rbps)
-                        continue;
-                    if (comma)
-                        virBufferAddChar(&buf, ',');
-                    else
-                        comma = true;
-                    virBufferAsprintf(&buf, "%s,%llu",
-                                      def->blkio.devices[j].path,
-                                      def->blkio.devices[j].rbps);
-                }
-                if (virBufferError(&buf)) {
-                    virReportOOMError();
-                    goto cleanup;
-                }
-                param->value.s = virBufferContentAndReset(&buf);
-            }
-            if (virTypedParameterAssign(param,
-                                        VIR_DOMAIN_BLKIO_DEVICE_READ_BPS,
-                                        VIR_TYPED_PARAM_STRING,
-                                        param->value.s) < 0)
-                goto cleanup;
-            break;
+    for (i = 0; i < def->blkio.ndevices; i++) {
+        if (!def->blkio.devices[i].rbps)
+            continue;
+        virBufferAsprintf(&buf, "%s,%llu,",
+                          def->blkio.devices[i].path,
+                          def->blkio.devices[i].rbps);
+    }
+    if (virBufferError(&buf)) {
+        virReportOOMError();
+        goto cleanup;
+    }
+    virBufferTrim(&buf, ",", -1);
+    str = virBufferContentAndReset(&buf);
+    if (virTypedParameterAssign(&params[4],
+                                VIR_DOMAIN_BLKIO_DEVICE_READ_BPS,
+                                VIR_TYPED_PARAM_STRING,
+                                str) < 0)
+        goto cleanup;
 
-        case 5: /* blkiotune.device_write_bps */
-            if (def->blkio.ndevices > 0) {
-                virBuffer buf = VIR_BUFFER_INITIALIZER;
-                bool comma = false;
-
-                for (j = 0; j < def->blkio.ndevices; j++) {
-                    if (!def->blkio.devices[j].wbps)
-                        continue;
-                    if (comma)
-                        virBufferAddChar(&buf, ',');
-                    else
-                        comma = true;
-                    virBufferAsprintf(&buf, "%s,%llu",
-                                      def->blkio.devices[j].path,
-                                      def->blkio.devices[j].wbps);
-                }
-                if (virBufferError(&buf)) {
-                    virReportOOMError();
-                    goto cleanup;
-                }
-                param->value.s = virBufferContentAndReset(&buf);
-            }
-            if (virTypedParameterAssign(param,
-                                        VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS,
-                                        VIR_TYPED_PARAM_STRING,
-                                        param->value.s) < 0)
-                goto cleanup;
-            break;
+    /* 5: blkiotune.device_write_bps */
+    if (*nparams <= 5)
+        goto success;
+
+    for (i = 0; i < def->blkio.ndevices; i++) {
+        if (!def->blkio.devices[i].wbps)
+            continue;
+        virBufferAsprintf(&buf, "%s,%llu,",
+                          def->blkio.devices[i].path,
+                          def->blkio.devices[i].wbps);
+        if (virBufferError(&buf)) {
+            virReportOOMError();
+            goto cleanup;
         }
+        virBufferTrim(&buf, ",", -1);
+        str = virBufferContentAndReset(&buf);
     }
+    if (virTypedParameterAssign(&params[5],
+                                VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS,
+                                VIR_TYPED_PARAM_STRING,
+                                str) < 0)
+        goto cleanup;
 
+ success:
     if (CONF_NB_BLKIO_PARAM < *nparams)
         *nparams = CONF_NB_BLKIO_PARAM;
     ret = 0;
-- 
1.8.5.5

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