render / rpms / libvirt

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