[ovs-dev] [PATCH v4 ovn 2/3] controller: add memory accounting for if_status_mgr module

Lorenzo Bianconi lorenzo.bianconi at redhat.com
Thu Oct 14 15:17:00 UTC 2021


Introduce memory accounting for data structures in ovn-controller
if_status_mgr module.

Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi at redhat.com>
---
 controller/if-status.c      | 33 +++++++++++++++++++++++++++++++++
 controller/if-status.h      |  3 +++
 controller/ovn-controller.c |  1 +
 3 files changed, 37 insertions(+)

diff --git a/controller/if-status.c b/controller/if-status.c
index 00f826c50..fa4c8bd94 100644
--- a/controller/if-status.c
+++ b/controller/if-status.c
@@ -18,6 +18,7 @@
 #include "binding.h"
 #include "if-status.h"
 #include "ofctrl-seqno.h"
+#include "simap.h"
 
 #include "lib/hmapx.h"
 #include "lib/util.h"
@@ -86,6 +87,8 @@ struct ovs_iface {
                              */
 };
 
+static uint64_t ifaces_usage;
+
 /* State machine manager for all local OVS interfaces. */
 struct if_status_mgr {
     /* All local interfaces, mapping from 'iface-id' to 'struct ovs_iface'. */
@@ -336,6 +339,18 @@ if_status_mgr_run(struct if_status_mgr *mgr,
                                   ovs_readonly);
 }
 
+static void
+ovs_iface_account_mem(const char *iface_id, bool erase)
+{
+    uint32_t size = (strlen(iface_id) + sizeof(struct ovs_iface) +
+                     sizeof(struct shash_node));
+    if (erase) {
+        ifaces_usage -= size;
+    } else {
+        ifaces_usage += size;
+    }
+}
+
 static struct ovs_iface *
 ovs_iface_create(struct if_status_mgr *mgr, const char *iface_id,
                  enum if_state state)
@@ -346,6 +361,7 @@ ovs_iface_create(struct if_status_mgr *mgr, const char *iface_id,
     iface->id = xstrdup(iface_id);
     shash_add_nocopy(&mgr->ifaces, iface->id, iface);
     ovs_iface_set_state(mgr, iface, state);
+    ovs_iface_account_mem(iface_id, false);
     return iface;
 }
 
@@ -359,6 +375,7 @@ ovs_iface_destroy(struct if_status_mgr *mgr, struct ovs_iface *iface)
     if (node) {
         shash_steal(&mgr->ifaces, node);
     }
+    ovs_iface_account_mem(iface->id, true);
     free(iface->id);
     free(iface);
 }
@@ -420,3 +437,19 @@ if_status_mgr_update_bindings(struct if_status_mgr *mgr,
         local_binding_set_down(bindings, iface->id, sb_readonly, ovs_readonly);
     }
 }
+
+void
+if_status_mgr_get_memory_usage(struct if_status_mgr *mgr,
+                               struct simap *usage)
+{
+    uint64_t ifaces_state_usage = 0;
+    for (size_t i = 0; i < ARRAY_SIZE(mgr->ifaces_per_state); i++) {
+        ifaces_state_usage += sizeof(struct hmapx_node) *
+                              hmapx_count(&mgr->ifaces_per_state[i]);
+    }
+
+    simap_increase(usage, "if_status_mgr_ifaces_usage-KB",
+                   ROUND_UP(ifaces_usage, 1024) / 1024);
+    simap_increase(usage, "if_status_mgr_ifaces_state_usage-KB",
+                   ROUND_UP(ifaces_state_usage, 1024) / 1024);
+}
diff --git a/controller/if-status.h b/controller/if-status.h
index 51fe7c684..ff4aa760e 100644
--- a/controller/if-status.h
+++ b/controller/if-status.h
@@ -21,6 +21,7 @@
 #include "binding.h"
 
 struct if_status_mgr;
+struct simap;
 
 struct if_status_mgr *if_status_mgr_create(void);
 void if_status_mgr_clear(struct if_status_mgr *);
@@ -33,5 +34,7 @@ void if_status_mgr_delete_iface(struct if_status_mgr *, const char *iface_id);
 void if_status_mgr_update(struct if_status_mgr *, struct local_binding_data *);
 void if_status_mgr_run(struct if_status_mgr *mgr, struct local_binding_data *,
                        bool sb_readonly, bool ovs_readonly);
+void if_status_mgr_get_memory_usage(struct if_status_mgr *mgr,
+                                    struct simap *usage);
 
 # endif /* controller/if-status.h */
diff --git a/controller/ovn-controller.c b/controller/ovn-controller.c
index 4202f32cc..e0fb0988f 100644
--- a/controller/ovn-controller.c
+++ b/controller/ovn-controller.c
@@ -3463,6 +3463,7 @@ main(int argc, char *argv[])
 
             lflow_cache_get_memory_usage(ctrl_engine_ctx.lflow_cache, &usage);
             ofctrl_get_memory_usage(&usage);
+            if_status_mgr_get_memory_usage(if_mgr, &usage);
             memory_report(&usage);
             simap_destroy(&usage);
         }
-- 
2.31.1



More information about the dev mailing list