diff options
author | Thomas Haller <thaller@redhat.com> | 2016-08-16 12:10:23 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2016-08-17 16:08:21 +0200 |
commit | c16e14c71c8cc34ac9550cb5e45469c42b9c352a (patch) | |
tree | 9f4b3520ec272547741dda1e3c6815db830436b9 | |
parent | 3d9d91b2beaff21b14edf444cb0077ea49a89cac (diff) | |
download | NetworkManager-c16e14c71c8cc34ac9550cb5e45469c42b9c352a.tar.gz |
device: drop nm-device-statistics.c and refactor tracking device statistics
Originally, "nm-device-statistics.c" contained code to fetch the device
counters via netlink. As now the netlink part is handled by NMPlatform,
the code can be simplified by merging it back to NMDevice.
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/devices/nm-device-private.h | 3 | ||||
-rw-r--r-- | src/devices/nm-device-statistics.c | 96 | ||||
-rw-r--r-- | src/devices/nm-device-statistics.h | 31 | ||||
-rw-r--r-- | src/devices/nm-device.c | 162 |
5 files changed, 107 insertions, 187 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 35bcf93ffa..c460caff67 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -331,8 +331,6 @@ libNetworkManager_la_SOURCES = \ devices/nm-device-generic.h \ devices/nm-device-logging.h \ devices/nm-device-private.h \ - devices/nm-device-statistics.c \ - devices/nm-device-statistics.h \ \ dhcp-manager/nm-dhcp-client.c \ dhcp-manager/nm-dhcp-client.h \ diff --git a/src/devices/nm-device-private.h b/src/devices/nm-device-private.h index 58216e175a..7381fd930f 100644 --- a/src/devices/nm-device-private.h +++ b/src/devices/nm-device-private.h @@ -114,9 +114,6 @@ void nm_device_ip_method_failed (NMDevice *self, int family, NMDeviceStateReason gboolean nm_device_ipv6_sysctl_set (NMDevice *self, const char *property, const char *value); -void nm_device_set_tx_bytes (NMDevice *self, guint64 tx_bytes); -void nm_device_set_rx_bytes (NMDevice *self, guint64 rx_bytes); - #define NM_DEVICE_CLASS_DECLARE_TYPES(klass, conn_type, ...) \ NM_DEVICE_CLASS (klass)->connection_type = conn_type; \ { \ diff --git a/src/devices/nm-device-statistics.c b/src/devices/nm-device-statistics.c deleted file mode 100644 index 4b2bbdd08b..0000000000 --- a/src/devices/nm-device-statistics.c +++ /dev/null @@ -1,96 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2016 Canonical Ltd - * - */ - -#include "nm-default.h" - -#include <inttypes.h> - -#include "nm-device-statistics.h" -#include "nm-device-private.h" -#include "nm-utils.h" -#include "nm-platform.h" - -#define _NMLOG_DOMAIN LOGD_DEVICE -#define _NMLOG(level, ...) \ - nm_log_obj ((level), _NMLOG_DOMAIN, (self->device), "device-stats", \ - "(%s): " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ - nm_device_get_iface (self->device) ?: "(none)" \ - _NM_UTILS_MACRO_REST(__VA_ARGS__)) - -struct _NMDeviceStatistics { - NMDevice *device; - guint stats_update_id; -}; - -static gboolean -update_stats (gpointer user_data) -{ - NMDeviceStatistics *self = user_data; - int ifindex; - const NMPlatformLink *pllink; - - ifindex = nm_device_get_ip_ifindex (self->device); - - nm_platform_link_refresh (NM_PLATFORM_GET, ifindex); - - pllink = nm_platform_link_get (NM_PLATFORM_GET, ifindex); - if (pllink) { - _LOGT ("ifindex %d: {RX} %"PRIu64" packets %"PRIu64" bytes {TX} %"PRIu64" packets %"PRIu64" bytes", - ifindex, pllink->rx_packets, pllink->rx_bytes, pllink->tx_packets, pllink->tx_bytes); - - nm_device_set_tx_bytes (self->device, pllink->tx_bytes); - nm_device_set_rx_bytes (self->device, pllink->rx_bytes); - } else { - _LOGT ("error no stats available for ifindex %d", ifindex); - nm_device_set_tx_bytes (self->device, 0); - nm_device_set_rx_bytes (self->device, 0); - } - - return TRUE; -} - -/********************************************/ - -NMDeviceStatistics * -nm_device_statistics_new (NMDevice *device, unsigned rate_ms) -{ - NMDeviceStatistics *self; - - self = g_malloc0 (sizeof (*self)); - self->device = device; - - self->stats_update_id = g_timeout_add (rate_ms, update_stats, self); - - return self; -} - -void -nm_device_statistics_unref (NMDeviceStatistics *self) -{ - g_source_remove (self->stats_update_id); - g_free (self); -} - -void -nm_device_statistics_change_rate (NMDeviceStatistics *self, unsigned rate_ms) -{ - g_source_remove (self->stats_update_id); - - self->stats_update_id = g_timeout_add (rate_ms, update_stats, self); -} diff --git a/src/devices/nm-device-statistics.h b/src/devices/nm-device-statistics.h deleted file mode 100644 index 17ff19f484..0000000000 --- a/src/devices/nm-device-statistics.h +++ /dev/null @@ -1,31 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2016 Canonical Ltd - */ - -#ifndef __NETWORKMANAGER_DEVICE_STATISTICS_H__ -#define __NETWORKMANAGER_DEVICE_STATISTICS_H__ - -typedef struct _NMDeviceStatistics NMDeviceStatistics; - -NMDeviceStatistics * -nm_device_statistics_new (NMDevice *device, unsigned rate_ms); - -void nm_device_statistics_unref (NMDeviceStatistics *self); - -void nm_device_statistics_change_rate (NMDeviceStatistics *self, unsigned rate_ms); - -#endif /* __NETWORKMANAGER_DEVICE_STATISTICS_H__ */ diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index cd6e280cbd..de7ec70db4 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -66,7 +66,6 @@ #include "sd-ipv4ll.h" #include "nm-audit-manager.h" #include "nm-arping-manager.h" -#include "nm-device-statistics.h" #include "nm-device-logging.h" _LOG_DECLARE_SELF (NMDevice); @@ -414,11 +413,10 @@ typedef struct _NMDevicePrivate { guint check_delete_unrealized_id; struct { + guint timeout_id; guint refresh_rate_ms; guint64 tx_bytes; guint64 rx_bytes; - - NMDeviceStatistics *statistics; } stats; } NMDevicePrivate; @@ -783,36 +781,112 @@ nm_device_set_ip_iface (NMDevice *self, const char *iface) g_free (old_ip_iface); } -void -nm_device_set_tx_bytes (NMDevice *self, guint64 tx_bytes) +/*****************************************************************************/ + +static void +_stats_update_counters (NMDevice *self, + guint64 tx_bytes, + guint64 rx_bytes) { NMDevicePrivate *priv; - g_return_if_fail (NM_IS_DEVICE (self)); - priv = NM_DEVICE_GET_PRIVATE (self); - if (tx_bytes == priv->stats.tx_bytes) - return; - priv->stats.tx_bytes = tx_bytes; - _notify (self, PROP_TX_BYTES); + if (priv->stats.tx_bytes != tx_bytes) { + priv->stats.tx_bytes = tx_bytes; + _notify (self, PROP_TX_BYTES); + } + if (priv->stats.rx_bytes != rx_bytes) { + priv->stats.rx_bytes = rx_bytes; + _notify (self, PROP_RX_BYTES); + } } -void -nm_device_set_rx_bytes (NMDevice *self, guint64 rx_bytes) +static void +_stats_refresh (NMDevice *self) { - NMDevicePrivate *priv; + const NMPlatformLink *pllink; + int ifindex; - g_return_if_fail (NM_IS_DEVICE (self)); + ifindex = nm_device_get_ip_ifindex (self); + if (ifindex > 0) { + nm_platform_link_refresh (NM_PLATFORM_GET, ifindex); + pllink = nm_platform_link_get (NM_PLATFORM_GET, ifindex); + if (pllink) { + _stats_update_counters (self, pllink->tx_bytes, pllink->rx_bytes); + return; + } + } + _stats_update_counters (self, 0, 0); +} + +static gboolean +_stats_timeout_cb (gpointer user_data) +{ + NMDevice *self = user_data; + + _LOGT (LOGD_DEVICE, "stats: refresh"); + _stats_refresh (self); + return G_SOURCE_CONTINUE; +} + +static guint +_stats_refresh_rate_real (guint refresh_rate_ms) +{ + const guint STATS_REFRESH_RATE_MS_MIN = 200; + + if (refresh_rate_ms == 0) + return 0; + + if (refresh_rate_ms < STATS_REFRESH_RATE_MS_MIN) { + /* you cannot set the refresh-rate arbitrarly small. E.g. + * setting to 1ms is just killing. Have a lowest number. */ + return STATS_REFRESH_RATE_MS_MIN; + } + + return refresh_rate_ms; +} + +static void +_stats_set_refresh_rate (NMDevice *self, guint refresh_rate_ms) +{ + NMDevicePrivate *priv; + guint old_rate; priv = NM_DEVICE_GET_PRIVATE (self); - if (rx_bytes == priv->stats.rx_bytes) + + if (priv->stats.refresh_rate_ms == refresh_rate_ms) + return; + + old_rate = priv->stats.refresh_rate_ms; + priv->stats.refresh_rate_ms = refresh_rate_ms; + _notify (self, PROP_REFRESH_RATE_MS); + + _LOGD (LOGD_DEVICE, "stats: set refresh to %u ms", priv->stats.refresh_rate_ms); + + if (!nm_device_is_real (self)) return; - priv->stats.rx_bytes = rx_bytes; - _notify (self, PROP_RX_BYTES); + refresh_rate_ms = _stats_refresh_rate_real (refresh_rate_ms); + if (_stats_refresh_rate_real (old_rate) == refresh_rate_ms) + return; + + if (!refresh_rate_ms) { + nm_clear_g_source (&priv->stats.timeout_id); + _stats_update_counters (self, 0, 0); + return; + } + + nm_clear_g_source (&priv->stats.timeout_id); + + /* trigger an inital refresh of the data when the refresh-rate changes */ + _stats_refresh (self); + + priv->stats.timeout_id = g_timeout_add (refresh_rate_ms, _stats_timeout_cb, self); } +/*****************************************************************************/ + static gboolean get_ip_iface_identifier (NMDevice *self, NMUtilsIPv6IfaceId *out_iid) { @@ -1894,6 +1968,7 @@ device_ip_link_changed (NMDevice *self) _notify (self, PROP_IP_IFACE); nm_device_update_dynamic_ip_setup (self); } + return G_SOURCE_REMOVE; } @@ -2152,6 +2227,7 @@ realize_start_setup (NMDevice *self, const NMPlatformLink *plink) static guint32 id = 0; NMDeviceCapabilities capabilities = 0; NMConfig *config; + guint real_rate; g_return_if_fail (NM_IS_DEVICE (self)); @@ -2243,9 +2319,12 @@ realize_start_setup (NMDevice *self, const NMPlatformLink *plink) priv->carrier = TRUE; } - if (priv->stats.refresh_rate_ms && !priv->stats.statistics) { - priv->stats.statistics = nm_device_statistics_new (self, - priv->stats.refresh_rate_ms); + nm_assert (!priv->stats.timeout_id); + real_rate = _stats_refresh_rate_real (priv->stats.refresh_rate_ms); + if (real_rate) { + if (plink) + _stats_update_counters (self, plink->tx_bytes, plink->rx_bytes); + priv->stats.timeout_id = g_timeout_add (real_rate, _stats_timeout_cb, self); } klass->realize_start_notify (self, plink); @@ -2419,14 +2498,9 @@ nm_device_unrealize (NMDevice *self, gboolean remove_resources, GError **error) g_clear_pointer (&priv->physical_port_id, g_free); _notify (self, PROP_PHYSICAL_PORT_ID); } - if (priv->stats.statistics) { - nm_device_statistics_unref (priv->stats.statistics); - priv->stats.statistics = NULL; - priv->stats.tx_bytes = 0; - priv->stats.tx_bytes = 0; - _notify (self, PROP_TX_BYTES); - _notify (self, PROP_RX_BYTES); - } + + nm_clear_g_source (&priv->stats.timeout_id); + _stats_update_counters (self, 0, 0); priv->hw_addr_type = HW_ADDR_TYPE_UNSET; g_clear_pointer (&priv->hw_addr_perm, g_free); @@ -12143,6 +12217,8 @@ dispose (GObject *object) nm_clear_g_source (&priv->check_delete_unrealized_id); + nm_clear_g_source (&priv->stats.timeout_id); + link_disconnect_action_cancel (self); if (priv->settings) { @@ -12168,11 +12244,6 @@ dispose (GObject *object) g_clear_object (&priv->lldp_listener); } - if (priv->stats.statistics) { - nm_device_statistics_unref (priv->stats.statistics); - priv->stats.statistics = NULL; - } - G_OBJECT_CLASS (nm_device_parent_class)->dispose (object); if (nm_clear_g_source (&priv->queued_state.id)) { @@ -12305,28 +12376,9 @@ set_property (GObject *object, guint prop_id, /* construct only */ priv->hw_addr_perm = g_value_dup_string (value); break; - case PROP_REFRESH_RATE_MS: { - guint refresh_rate_ms; - - refresh_rate_ms = g_value_get_uint (value); - if (priv->stats.refresh_rate_ms == refresh_rate_ms) - break; - - priv->stats.refresh_rate_ms = refresh_rate_ms; - _LOGI (LOGD_DEVICE, "statistics refresh rate set to %u ms", priv->stats.refresh_rate_ms); - - if (priv->stats.refresh_rate_ms) { - if (priv->stats.statistics) - nm_device_statistics_change_rate (priv->stats.statistics, priv->stats.refresh_rate_ms); - else - priv->stats.statistics = - nm_device_statistics_new (self, priv->stats.refresh_rate_ms); - } else { - nm_device_statistics_unref (priv->stats.statistics); - priv->stats.statistics = NULL; - } + case PROP_REFRESH_RATE_MS: + _stats_set_refresh_rate (self, g_value_get_uint (value)); break; - } default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; |