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