render / rpms / libvirt

Forked from rpms/libvirt 7 months ago
Clone
c480ed
From dbcd08b082f3e96cf09328041549c0f1dc4e1335 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <dbcd08b082f3e96cf09328041549c0f1dc4e1335@dist-git>
c480ed
From: Michal Privoznik <mprivozn@redhat.com>
c480ed
Date: Tue, 30 Jul 2019 15:30:51 +0200
c480ed
Subject: [PATCH] virNetDevOpenvswitchInterfaceStats: Optimize for speed
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
We run 'ovs-vsctl' nine times (first to find if interface is
c480ed
there and then eight times = for each stats member separately).
c480ed
This is very inefficient. I've found a way to run it once and
c480ed
with a bit of help from virJSON module we can parse out stats
c480ed
we need.
c480ed
c480ed
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
(cherry picked from commit c297eab52599c91a4cb26b66dbdfe9d07c3142d3)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1721434
c480ed
c480ed
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c480ed
Message-Id: <ba02a9b373ff7014e1edfc98da4edf11abe34a9e.1564493409.git.mprivozn@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/virnetdevopenvswitch.c | 111 +++++++++++++++++++++-----------
c480ed
 1 file changed, 74 insertions(+), 37 deletions(-)
c480ed
c480ed
diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
c480ed
index cb403aaf2e..dbd832053b 100644
c480ed
--- a/src/util/virnetdevopenvswitch.c
c480ed
+++ b/src/util/virnetdevopenvswitch.c
c480ed
@@ -35,6 +35,7 @@
c480ed
 #include "virmacaddr.h"
c480ed
 #include "virstring.h"
c480ed
 #include "virlog.h"
c480ed
+#include "virjson.h"
c480ed
 
c480ed
 #define VIR_FROM_THIS VIR_FROM_NONE
c480ed
 
c480ed
@@ -318,58 +319,94 @@ int
c480ed
 virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
                                    virDomainInterfaceStatsPtr stats)
c480ed
 {
c480ed
-    char *tmp;
c480ed
-    bool gotStats = false;
c480ed
     VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
     VIR_AUTOFREE(char *) output = NULL;
c480ed
+    VIR_AUTOPTR(virJSONValue) jsonStats = NULL;
c480ed
+    virJSONValuePtr jsonMap = NULL;
c480ed
+    size_t i;
c480ed
 
c480ed
-    /* Just ensure the interface exists in ovs */
c480ed
     cmd = virCommandNew(OVSVSCTL);
c480ed
     virNetDevOpenvswitchAddTimeout(cmd);
c480ed
-    virCommandAddArgList(cmd, "get", "Interface", ifname, "name", NULL);
c480ed
+    virCommandAddArgList(cmd, "--if-exists", "--format=list", "--data=json",
c480ed
+                         "--no-headings", "--columns=statistics", "list",
c480ed
+                         "Interface", ifname, NULL);
c480ed
     virCommandSetOutputBuffer(cmd, &output);
c480ed
 
c480ed
-    if (virCommandRun(cmd, NULL) < 0) {
c480ed
+    /* The above command returns either:
c480ed
+     * 1) empty string if @ifname doesn't exist, or
c480ed
+     * 2) a JSON array, for instance:
c480ed
+     *    ["map",[["collisions",0],["rx_bytes",0],["rx_crc_err",0],["rx_dropped",0],
c480ed
+     *    ["rx_errors",0],["rx_frame_err",0],["rx_over_err",0],["rx_packets",0],
c480ed
+     *    ["tx_bytes",12406],["tx_dropped",0],["tx_errors",0],["tx_packets",173]]]
c480ed
+     */
c480ed
+
c480ed
+    if (virCommandRun(cmd, NULL) < 0 ||
c480ed
+        STREQ_NULLABLE(output, "")) {
c480ed
         /* no ovs-vsctl or interface 'ifname' doesn't exists in ovs */
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
                        _("Interface not found"));
c480ed
         return -1;
c480ed
     }
c480ed
 
c480ed
-#define GET_STAT(name, member) \
c480ed
-    do { \
c480ed
-        VIR_FREE(output); \
c480ed
-        virCommandFree(cmd); \
c480ed
-        cmd = virCommandNew(OVSVSCTL); \
c480ed
-        virNetDevOpenvswitchAddTimeout(cmd); \
c480ed
-        virCommandAddArgList(cmd, "--if-exists", "get", "Interface", \
c480ed
-                             ifname, "statistics:" name, NULL); \
c480ed
-        virCommandSetOutputBuffer(cmd, &output); \
c480ed
-        if (virCommandRun(cmd, NULL) < 0 || !output || !*output || *output == '\n') { \
c480ed
-            stats->member = -1; \
c480ed
-        } else { \
c480ed
-            if (virStrToLong_ll(output, &tmp, 10, &stats->member) < 0 || \
c480ed
-                *tmp != '\n') { \
c480ed
-                virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
c480ed
-                               _("Fail to parse ovs-vsctl output")); \
c480ed
-                return -1; \
c480ed
-            } \
c480ed
-            gotStats = true; \
c480ed
-        } \
c480ed
-    } while (0)
c480ed
+    if (!(jsonStats = virJSONValueFromString(output)) ||
c480ed
+        !virJSONValueIsArray(jsonStats) ||
c480ed
+        !(jsonMap = virJSONValueArrayGet(jsonStats, 1))) {
c480ed
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
+                       _("Unable to parse ovs-vsctl output"));
c480ed
+        return -1;
c480ed
+    }
c480ed
 
c480ed
-    /* The TX/RX fields appear to be swapped here
c480ed
-     * because this is the host view. */
c480ed
-    GET_STAT("rx_bytes", tx_bytes);
c480ed
-    GET_STAT("rx_packets", tx_packets);
c480ed
-    GET_STAT("rx_errors", tx_errs);
c480ed
-    GET_STAT("rx_dropped", tx_drop);
c480ed
-    GET_STAT("tx_bytes", rx_bytes);
c480ed
-    GET_STAT("tx_packets", rx_packets);
c480ed
-    GET_STAT("tx_errors", rx_errs);
c480ed
-    GET_STAT("tx_dropped", rx_drop);
c480ed
+    stats->rx_bytes = stats->rx_packets = stats->rx_errs = stats->rx_drop = -1;
c480ed
+    stats->tx_bytes = stats->tx_packets = stats->tx_errs = stats->tx_drop = -1;
c480ed
 
c480ed
-    if (!gotStats) {
c480ed
+    for (i = 0; i < virJSONValueArraySize(jsonMap); i++) {
c480ed
+        virJSONValuePtr item = virJSONValueArrayGet(jsonMap, i);
c480ed
+        virJSONValuePtr jsonKey;
c480ed
+        virJSONValuePtr jsonVal;
c480ed
+        const char *key;
c480ed
+        long long val;
c480ed
+
c480ed
+        if (!item ||
c480ed
+            (!(jsonKey = virJSONValueArrayGet(item, 0))) ||
c480ed
+            (!(jsonVal = virJSONValueArrayGet(item, 1))) ||
c480ed
+            (!(key = virJSONValueGetString(jsonKey))) ||
c480ed
+            (virJSONValueGetNumberLong(jsonVal, &val) < 0)) {
c480ed
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
+                           _("Malformed ovs-vsctl output"));
c480ed
+            return -1;
c480ed
+        }
c480ed
+
c480ed
+        /* The TX/RX fields appear to be swapped here
c480ed
+         * because this is the host view. */
c480ed
+        if (STREQ(key, "rx_bytes")) {
c480ed
+            stats->tx_bytes = val;
c480ed
+        } else if (STREQ(key, "rx_packets")) {
c480ed
+            stats->tx_packets = val;
c480ed
+        } else if (STREQ(key, "rx_errors")) {
c480ed
+            stats->tx_errs = val;
c480ed
+        } else if (STREQ(key, "rx_dropped")) {
c480ed
+            stats->tx_drop = val;
c480ed
+        } else if (STREQ(key, "tx_bytes")) {
c480ed
+            stats->rx_bytes = val;
c480ed
+        } else if (STREQ(key, "tx_packets")) {
c480ed
+            stats->rx_packets = val;
c480ed
+        } else if (STREQ(key, "tx_errors")) {
c480ed
+            stats->rx_errs = val;
c480ed
+        } else if (STREQ(key, "tx_dropped")) {
c480ed
+            stats->rx_drop = val;
c480ed
+        } else {
c480ed
+            VIR_DEBUG("Unused ovs-vsctl stat key=%s val=%lld", key, val);
c480ed
+        }
c480ed
+    }
c480ed
+
c480ed
+    if (stats->rx_bytes == -1 &&
c480ed
+        stats->rx_packets == -1 &&
c480ed
+        stats->rx_errs == -1 &&
c480ed
+        stats->rx_drop == -1 &&
c480ed
+        stats->tx_bytes == -1 &&
c480ed
+        stats->tx_packets == -1 &&
c480ed
+        stats->tx_errs == -1 &&
c480ed
+        stats->tx_drop == -1) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
                        _("Interface doesn't have any statistics"));
c480ed
         return -1;
c480ed
-- 
c480ed
2.22.0
c480ed