From 0d41abea2ee24192647df9545ef210a99fd977b6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 8 Jun 2020 13:28:24 +0200 Subject: [PATCH] lldp: only have GHashTable instance for LLDP neighbors when running When the instance is not running (after creation or after stop), there is no need to keep the GHashTable around. Create it when needed (during start) and clear it during stop. This makes it slightly cheaper to keep a NMLldpListener instance around, if it's currently not running. NMDevice already keeps the NMLldpListener around, even after stopping it. It's not clear whether the instance will be started again, so also clear the GHashTable. Also, one effect is that if you initially were in a network with many LLDP neibors, after stop and start, the GHashTable now gets recreated and may not need to allocate a large internal array as before. --- src/devices/nm-device.c | 6 ++++-- src/devices/nm-lldp-listener.c | 21 ++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index ef4b5cf8d0..4ce27f76fc 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -3845,7 +3845,8 @@ nm_device_update_dynamic_ip_setup (NMDevice *self) /* FIXME: todo */ } - if (priv->lldp_listener && nm_lldp_listener_is_running (priv->lldp_listener)) { + if ( priv->lldp_listener + && nm_lldp_listener_is_running (priv->lldp_listener)) { nm_lldp_listener_stop (priv->lldp_listener); if (!nm_lldp_listener_start (priv->lldp_listener, nm_device_get_ifindex (self), &error)) { _LOGD (LOGD_DEVICE, "LLDP listener %p could not be restarted: %s", @@ -7157,7 +7158,8 @@ lldp_init (NMDevice *self, gboolean restart) gs_free_error GError *error = NULL; if (priv->lldp_listener) { - if (restart && nm_lldp_listener_is_running (priv->lldp_listener)) + if ( restart + && nm_lldp_listener_is_running (priv->lldp_listener)) nm_lldp_listener_stop (priv->lldp_listener); } else { priv->lldp_listener = nm_lldp_listener_new (); diff --git a/src/devices/nm-lldp-listener.c b/src/devices/nm-lldp-listener.c index ff18975724..ee443d002c 100644 --- a/src/devices/nm-lldp-listener.c +++ b/src/devices/nm-lldp-listener.c @@ -30,16 +30,15 @@ NM_GOBJECT_PROPERTIES_DEFINE (NMLldpListener, ); typedef struct { - char *iface; - int ifindex; sd_lldp *lldp_handle; GHashTable *lldp_neighbors; + GVariant *variant; /* the timestamp in nsec until which we delay updates. */ gint64 ratelimit_next_nsec; guint ratelimit_id; - GVariant *variant; + int ifindex; } NMLldpListenerPrivate; struct _NMLldpListener { @@ -724,6 +723,8 @@ process_lldp_neighbor (NMLldpListener *self, sd_lldp_neighbor *neighbor_sd, gboo g_return_if_fail (priv->lldp_handle); g_return_if_fail (neighbor_sd); + nm_assert (priv->lldp_neighbors); + p_parse_error = _LOGT_ENABLED () ? &parse_error : NULL; neigh = lldp_neighbor_new (neighbor_sd, p_parse_error); @@ -831,6 +832,10 @@ nm_lldp_listener_start (NMLldpListener *self, int ifindex, GError **error) goto err; } + priv->lldp_neighbors = g_hash_table_new_full (lldp_neighbor_id_hash, + lldp_neighbor_id_equal, + (GDestroyNotify) lldp_neighbor_free, NULL); + _LOGD ("start"); return TRUE; @@ -863,6 +868,7 @@ nm_lldp_listener_stop (NMLldpListener *self) size = g_hash_table_size (priv->lldp_neighbors); g_hash_table_remove_all (priv->lldp_neighbors); + nm_clear_pointer (&priv->lldp_neighbors, g_hash_table_unref); if ( size > 0 || priv->ratelimit_id != 0) changed = TRUE; @@ -897,8 +903,8 @@ nm_lldp_listener_get_neighbors (NMLldpListener *self) priv = NM_LLDP_LISTENER_GET_PRIVATE (self); if (G_UNLIKELY (!priv->variant)) { - GVariantBuilder array_builder; gs_free LldpNeighbor **neighbors = NULL; + GVariantBuilder array_builder; guint i, n; g_variant_builder_init (&array_builder, G_VARIANT_TYPE ("aa{sv}")); @@ -932,12 +938,6 @@ get_property (GObject *object, guint prop_id, static void nm_lldp_listener_init (NMLldpListener *self) { - NMLldpListenerPrivate *priv = NM_LLDP_LISTENER_GET_PRIVATE (self); - - priv->lldp_neighbors = g_hash_table_new_full (lldp_neighbor_id_hash, - lldp_neighbor_id_equal, - (GDestroyNotify) lldp_neighbor_free, NULL); - _LOGT ("lldp listener created"); } @@ -962,7 +962,6 @@ finalize (GObject *object) NMLldpListenerPrivate *priv = NM_LLDP_LISTENER_GET_PRIVATE (self); nm_lldp_listener_stop (self); - g_hash_table_unref (priv->lldp_neighbors); nm_clear_g_variant (&priv->variant);