[PATCH 4/4] qemu: json monitor: reduce duplicated code

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

 



This patch replaces repetitive blocks of code with a couple
of macros for the sake of clarity.
There are no changes in behaviour.
---
 src/qemu/qemu_monitor_json.c | 129 ++++++++++++++++++-------------------------
 1 file changed, 53 insertions(+), 76 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 4810bd3..4fa72c9 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -1775,6 +1775,25 @@ int qemuMonitorJSONGetBlockStatsInfo(qemuMonitorPtr mon,
 }
 
 
+#define QEMU_MONITOR_JSON_MALFORMED_ENTRY(kind) do { \
+    if (report) \
+        virReportError(VIR_ERR_INTERNAL_ERROR, \
+                       _("blockstats %s entry was not " \
+                         "in expected format"), \
+                        kind); \
+    goto cleanup; \
+} while (0)
+
+
+#define QEMU_MONITOR_JSON_MISSING_STAT(statistic) do { \
+    if (report) \
+        virReportError(VIR_ERR_INTERNAL_ERROR, \
+                        _("cannot read %s statistic"), \
+                        statistic); \
+    goto cleanup; \
+} while (0)
+
+
 int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
                                         const char *dev_name,
                                         qemuBlockStatsPtr bstats,
@@ -1814,26 +1833,16 @@ int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
     for (i = 0; i < virJSONValueArraySize(devices) && count < nstats; i++) {
         virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
         virJSONValuePtr stats;
-        if (!dev || dev->type != VIR_JSON_TYPE_OBJECT) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                               _("blockstats device entry was not "
-                                 "in expected format"));
-            goto cleanup;
-        }
+        if (!dev || dev->type != VIR_JSON_TYPE_OBJECT)
+            QEMU_MONITOR_JSON_MALFORMED_ENTRY("device");
 
         /* If dev_name is specified, we are looking for a specific device,
          * so we must be stricter.
          */
         if (dev_name) {
             const char *thisdev = virJSONValueObjectGetString(dev, "device");
-            if (!thisdev) {
-                if (report)
-                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                                   _("blockstats device entry was not "
-                                     "in expected format"));
-                goto cleanup;
-            }
+            if (!thisdev)
+                QEMU_MONITOR_JSON_MALFORMED_ENTRY("device");
 
             /* New QEMU has separate names for host & guest side of the disk
              * and libvirt gives the host side a 'drive-' prefix. The passed
@@ -1847,81 +1856,44 @@ int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
         }
 
         if ((stats = virJSONValueObjectGet(dev, "stats")) == NULL ||
-            stats->type != VIR_JSON_TYPE_OBJECT) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                               _("blockstats stats entry was not "
-                                 "in expected format"));
-            goto cleanup;
-        }
+            stats->type != VIR_JSON_TYPE_OBJECT)
+            QEMU_MONITOR_JSON_MALFORMED_ENTRY("stats");
 
         if (virJSONValueObjectGetNumberLong(stats, "rd_bytes",
-                                            &bstats->rd_bytes) < 0) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("cannot read %s statistic"),
-                               "rd_bytes");
-            goto cleanup;
-        }
+                                            &bstats->rd_bytes) < 0)
+            QEMU_MONITOR_JSON_MISSING_STAT("rd_bytes");
+
         if (virJSONValueObjectGetNumberLong(stats, "rd_operations",
-                                            &bstats->rd_req) < 0) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("cannot read %s statistic"),
-                                "rd_operations");
-            goto cleanup;
-        }
+                                            &bstats->rd_req) < 0)
+            QEMU_MONITOR_JSON_MISSING_STAT("rd_operations");
+
         if (virJSONValueObjectHasKey(stats, "rd_total_time_ns") &&
             (virJSONValueObjectGetNumberLong(stats, "rd_total_time_ns",
-                                             &bstats->rd_total_times) < 0)) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("cannot read %s statistic"),
-                               "rd_total_time_ns");
-            goto cleanup;
-        }
+                                             &bstats->rd_total_times) < 0))
+            QEMU_MONITOR_JSON_MISSING_STAT("rd_total_time_ns");
+
         if (virJSONValueObjectGetNumberLong(stats, "wr_bytes",
-                                            &bstats->wr_bytes) < 0) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("cannot read %s statistic"),
-                               "wr_bytes");
-            goto cleanup;
-        }
+                                            &bstats->wr_bytes) < 0)
+            QEMU_MONITOR_JSON_MISSING_STAT("wr_bytes");
+
         if (virJSONValueObjectGetNumberLong(stats, "wr_operations",
-                                            &bstats->wr_req) < 0) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("cannot read %s statistic"),
-                               "wr_operations");
-            goto cleanup;
-        }
+                                            &bstats->wr_req) < 0)
+            QEMU_MONITOR_JSON_MISSING_STAT("wr_operations");
+
         if (virJSONValueObjectHasKey(stats, "wr_total_time_ns") &&
             (virJSONValueObjectGetNumberLong(stats, "wr_total_time_ns",
-                                             &bstats->wr_total_times) < 0)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("cannot read %s statistic"),
-                           "wr_total_time_ns");
-            goto cleanup;
-        }
+                                             &bstats->wr_total_times) < 0))
+            QEMU_MONITOR_JSON_MISSING_STAT("wr_total_time_ns");
+
         if (virJSONValueObjectHasKey(stats, "flush_operations") &&
             (virJSONValueObjectGetNumberLong(stats, "flush_operations",
-                                             &bstats->flush_req) < 0)) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("cannot read %s statistic"),
-                               "flush_operations");
-            goto cleanup;
-        }
+                                             &bstats->flush_req) < 0))
+            QEMU_MONITOR_JSON_MISSING_STAT("flush_operations");
+
         if (virJSONValueObjectHasKey(stats, "flush_total_time_ns") &&
             (virJSONValueObjectGetNumberLong(stats, "flush_total_time_ns",
-                                             &bstats->flush_total_times) < 0)) {
-            if (report)
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("cannot read %s statistic"),
-                               "flush_total_time_ns");
-            goto cleanup;
-        }
+                                             &bstats->flush_total_times) < 0))
+            QEMU_MONITOR_JSON_MISSING_STAT("flush_total_time_ns");
 
         count++;
         bstats++;
@@ -1947,6 +1919,11 @@ int qemuMonitorJSONGetAllBlockStatsInfo(qemuMonitorPtr mon,
 }
 
 
+#undef QEMU_MONITOR_JSON_MISSING_STAT
+
+#undef QEMU_MONITOR_JSON_MALFORMED_ENTRY
+
+
 int qemuMonitorJSONGetBlockStatsParamsNumber(qemuMonitorPtr mon,
                                              int *nparams)
 {
-- 
1.9.3

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