[PATCH 2/3] Reuse GetBlkioParameters code for live and persistent XML

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

 



Most of the values are read from the XML and the only difference
is that one of them open-codes virTypedParameterAssign.
---
 src/conf/domain_conf.c | 435 ++++++++++++++++---------------------------------
 1 file changed, 140 insertions(+), 295 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 4fe7cb6..99ab232 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -19782,7 +19782,7 @@ virDomainObjSetMetadata(virDomainObjPtr vm,
 
 int
 virDomainGetBlkioParametersInternal(virCgroupPtr cgroup,
-                                    virDomainDefPtr def,
+                                    virDomainDefPtr liveDef,
                                     virDomainDefPtr persistentDef,
                                     virTypedParameterPtr params,
                                     int *nparams,
@@ -19791,6 +19791,12 @@ virDomainGetBlkioParametersInternal(virCgroupPtr cgroup,
     int ret = -1;
     size_t i, j;
     unsigned int val;
+    virDomainDefPtr def;
+
+    if (flags & VIR_DOMAIN_AFFECT_LIVE)
+        def = liveDef;
+    else
+        def = persistentDef;
 
     if ((*nparams) == 0) {
         /* Current number of blkio parameters supported by cgroups */
@@ -19818,151 +19824,6 @@ virDomainGetBlkioParametersInternal(virCgroupPtr cgroup,
                                             VIR_TYPED_PARAM_UINT, val) < 0)
                     goto cleanup;
                 break;
-
-            case 1: /* blkiotune.device_weight */
-                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].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;
-
-            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;
-
-            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;
-
-             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;
-
-             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;
             }
         }
     } else if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
@@ -19982,175 +19843,159 @@ virDomainGetBlkioParametersInternal(virCgroupPtr cgroup,
                 }
                 param->value.ui = persistentDef->blkio.weight;
                 break;
+            }
+        }
+    }
 
-            case 1: /* blkiotune.device_weight */
-                if (persistentDef->blkio.ndevices > 0) {
-                    virBuffer buf = VIR_BUFFER_INITIALIZER;
-                    bool comma = false;
+    for (i = 1; i < *nparams && i < CONF_NB_BLKIO_PARAM; i++) {
+        virTypedParameterPtr param = &params[i];
+        val = 0;
 
-                    for (j = 0; j < persistentDef->blkio.ndevices; j++) {
-                        if (!persistentDef->blkio.devices[j].weight)
-                            continue;
-                        if (comma)
-                            virBufferAddChar(&buf, ',');
-                        else
-                            comma = true;
-                        virBufferAsprintf(&buf, "%s,%u",
-                                          persistentDef->blkio.devices[j].path,
-                                          persistentDef->blkio.devices[j].weight);
-                    }
-                    if (virBufferError(&buf)) {
-                        virReportOOMError();
-                        goto cleanup;
-                    }
-                    param->value.s = virBufferContentAndReset(&buf);
+        switch (i) {
+        case 1: /* blkiotune.device_weight */
+            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].weight)
+                        continue;
+                    if (comma)
+                        virBufferAddChar(&buf, ',');
+                    else
+                        comma = true;
+                    virBufferAsprintf(&buf, "%s,%u",
+                                      def->blkio.devices[j].path,
+                                      def->blkio.devices[j].weight);
                 }
-                if (!param->value.s && VIR_STRDUP(param->value.s, "") < 0)
-                    goto cleanup;
-                param->type = VIR_TYPED_PARAM_STRING;
-                if (virStrcpyStatic(param->field,
-                                    VIR_DOMAIN_BLKIO_DEVICE_WEIGHT) == NULL) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("Field name '%s' too long"),
-                                   VIR_DOMAIN_BLKIO_DEVICE_WEIGHT);
+                if (virBufferError(&buf)) {
+                    virReportOOMError();
                     goto cleanup;
                 }
-                break;
-
-            case 2: /* blkiotune.device_read_iops */
-                if (persistentDef->blkio.ndevices > 0) {
-                    virBuffer buf = VIR_BUFFER_INITIALIZER;
-                    bool comma = false;
+                param->value.s = virBufferContentAndReset(&buf);
+            }
+            if (virTypedParameterAssign(param,
+                                        VIR_DOMAIN_BLKIO_DEVICE_WEIGHT,
+                                        VIR_TYPED_PARAM_STRING,
+                                        param->value.s) < 0)
+                goto cleanup;
+            break;
 
-                    for (j = 0; j < persistentDef->blkio.ndevices; j++) {
-                        if (!persistentDef->blkio.devices[j].riops)
-                            continue;
-                        if (comma)
-                            virBufferAddChar(&buf, ',');
-                        else
-                            comma = true;
-                        virBufferAsprintf(&buf, "%s,%u",
-                                          persistentDef->blkio.devices[j].path,
-                                          persistentDef->blkio.devices[j].riops);
-                    }
-                    if (virBufferError(&buf)) {
-                        virReportOOMError();
-                        goto cleanup;
-                    }
-                    param->value.s = virBufferContentAndReset(&buf);
+        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 (!param->value.s && VIR_STRDUP(param->value.s, "") < 0)
-                    goto cleanup;
-                param->type = VIR_TYPED_PARAM_STRING;
-                if (virStrcpyStatic(param->field,
-                                    VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS) == NULL) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("Field name '%s' too long"),
-                                   VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS);
+                if (virBufferError(&buf)) {
+                    virReportOOMError();
                     goto cleanup;
                 }
-                break;
-            case 3: /* blkiotune.device_write_iops */
-                if (persistentDef->blkio.ndevices > 0) {
-                    virBuffer buf = VIR_BUFFER_INITIALIZER;
-                    bool comma = false;
-
-                    for (j = 0; j < persistentDef->blkio.ndevices; j++) {
-                        if (!persistentDef->blkio.devices[j].wiops)
-                            continue;
-                        if (comma)
-                            virBufferAddChar(&buf, ',');
-                        else
-                            comma = true;
-                        virBufferAsprintf(&buf, "%s,%u",
-                                          persistentDef->blkio.devices[j].path,
-                                          persistentDef->blkio.devices[j].wiops);
-                    }
-                    if (virBufferError(&buf)) {
-                        virReportOOMError();
-                        goto cleanup;
-                    }
-                    param->value.s = virBufferContentAndReset(&buf);
+                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;
+
+        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 (!param->value.s && VIR_STRDUP(param->value.s, "") < 0)
-                    goto cleanup;
-                param->type = VIR_TYPED_PARAM_STRING;
-                if (virStrcpyStatic(param->field,
-                                    VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS) == NULL) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("Field name '%s' too long"),
-                                   VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS);
+                if (virBufferError(&buf)) {
+                    virReportOOMError();
                     goto cleanup;
                 }
-                break;
-            case 4: /* blkiotune.device_read_bps */
-                if (persistentDef->blkio.ndevices > 0) {
-                    virBuffer buf = VIR_BUFFER_INITIALIZER;
-                    bool comma = false;
-
-                    for (j = 0; j < persistentDef->blkio.ndevices; j++) {
-                        if (!persistentDef->blkio.devices[j].rbps)
-                            continue;
-                        if (comma)
-                            virBufferAddChar(&buf, ',');
-                        else
-                            comma = true;
-                        virBufferAsprintf(&buf, "%s,%llu",
-                                          persistentDef->blkio.devices[j].path,
-                                          persistentDef->blkio.devices[j].rbps);
-                    }
-                    if (virBufferError(&buf)) {
-                        virReportOOMError();
-                        goto cleanup;
-                    }
-                    param->value.s = virBufferContentAndReset(&buf);
+                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;
+
+        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 (!param->value.s && VIR_STRDUP(param->value.s, "") < 0)
-                    goto cleanup;
-                param->type = VIR_TYPED_PARAM_STRING;
-                if (virStrcpyStatic(param->field,
-                                    VIR_DOMAIN_BLKIO_DEVICE_READ_BPS) == NULL) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("Field name '%s' too long"),
-                                   VIR_DOMAIN_BLKIO_DEVICE_READ_BPS);
+                if (virBufferError(&buf)) {
+                    virReportOOMError();
                     goto cleanup;
                 }
-                break;
-
-            case 5: /* blkiotune.device_write_bps */
-                if (persistentDef->blkio.ndevices > 0) {
-                    virBuffer buf = VIR_BUFFER_INITIALIZER;
-                    bool comma = false;
+                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 (j = 0; j < persistentDef->blkio.ndevices; j++) {
-                        if (!persistentDef->blkio.devices[j].wbps)
-                            continue;
-                        if (comma)
-                            virBufferAddChar(&buf, ',');
-                        else
-                            comma = true;
-                        virBufferAsprintf(&buf, "%s,%llu",
-                                          persistentDef->blkio.devices[j].path,
-                                          persistentDef->blkio.devices[j].wbps);
-                    }
-                    if (virBufferError(&buf)) {
-                        virReportOOMError();
-                        goto cleanup;
-                    }
-                    param->value.s = virBufferContentAndReset(&buf);
+        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 (!param->value.s && VIR_STRDUP(param->value.s, "") < 0)
-                    goto cleanup;
-                param->type = VIR_TYPED_PARAM_STRING;
-                if (virStrcpyStatic(param->field,
-                                    VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS) == NULL) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("Field name '%s' too long"),
-                                   VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS);
+                if (virBufferError(&buf)) {
+                    virReportOOMError();
                     goto cleanup;
                 }
-                break;
+                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;
         }
     }
 
-- 
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]