From 500b051fe4b3ed34445325f1caf0f347edd73dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Dec 2022 11:59:08 +0100 Subject: [PATCH] =?UTF-8?q?module/network:=20poll-interval:=20convert=20va?= =?UTF-8?q?lue=20from=20=E2=80=98seconds=E2=80=99=20to=20=E2=80=98millisec?= =?UTF-8?q?onds=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/yambar-modules-network.5.scd | 5 +-- examples/configurations/laptop.conf | 2 +- modules/network.c | 47 +++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/doc/yambar-modules-network.5.scd b/doc/yambar-modules-network.5.scd index 7bb9afc..a07a87f 100644 --- a/doc/yambar-modules-network.5.scd +++ b/doc/yambar-modules-network.5.scd @@ -72,8 +72,9 @@ address. | poll-interval : int : no -: Periodically (in seconds) update the signal, rx+tx bitrate, and - ul+dl speed tags. +: Periodically (in milliseconds) update the signal, rx+tx bitrate, and + ul+dl speed tags. Setting it to 0 disables updates. Cannot be less + than 500ms. # EXAMPLES diff --git a/examples/configurations/laptop.conf b/examples/configurations/laptop.conf index dd7fbcb..87f3d1c 100644 --- a/examples/configurations/laptop.conf +++ b/examples/configurations/laptop.conf @@ -181,7 +181,7 @@ bar: state == up && ipv4 != "": {string: {text: , font: *awesome}} - network: name: wlp2s0 - poll-interval: 1 + poll-interval: 1000 content: map: default: {string: {text: , font: *awesome, foreground: ffffff66}} diff --git a/modules/network.c b/modules/network.c index 17358a6..423f1b9 100644 --- a/modules/network.c +++ b/modules/network.c @@ -34,6 +34,8 @@ #define UNUSED __attribute__((unused)) +static const long min_poll_interval = 500; + struct rt_stats_msg { struct rtmsg rth; struct rtnl_link_stats64 stats; @@ -79,10 +81,10 @@ struct private { uint32_t rx_bitrate; uint32_t tx_bitrate; - uint64_t ul_speed; + double ul_speed; uint64_t ul_bits; - uint64_t dl_speed; + double dl_speed; uint64_t dl_bits; }; @@ -1120,15 +1122,16 @@ static void handle_stats(struct module *mod, struct rt_stats_msg *msg) { struct private *m = mod->private; - uint64_t ul_bits = msg->stats.tx_bytes*8; - uint64_t dl_bits = msg->stats.rx_bytes*8; + uint64_t ul_bits = msg->stats.tx_bytes * 8; + uint64_t dl_bits = msg->stats.rx_bytes * 8; + + const double poll_interval_secs = (double)m->poll_interval / 1000.; + + if (m->ul_bits != 0) + m->ul_speed = (double)(ul_bits - m->ul_bits) / poll_interval_secs; + if (m->dl_bits != 0) + m->dl_speed = (double)(dl_bits - m->dl_bits) / poll_interval_secs; - if (m->ul_bits != 0) { - m->ul_speed = (ul_bits - m->ul_bits) / m->poll_interval; - } - if (m->dl_bits != 0) { - m->dl_speed = (dl_bits - m->dl_bits) / m->poll_interval; - } m->ul_bits = ul_bits; m->dl_bits = dl_bits; } @@ -1336,9 +1339,12 @@ run(struct module *mod) goto out; } + const long secs = m->poll_interval / 1000; + const long msecs = m->poll_interval % 1000; + struct itimerspec poll_time = { - .it_value = {.tv_sec = m->poll_interval}, - .it_interval = {.tv_sec = m->poll_interval}, + .it_value = {.tv_sec = secs, .tv_nsec = msecs * 1000000}, + .it_interval = {.tv_sec = secs, .tv_nsec = msecs * 1000000}, }; if (timerfd_settime(timer_fd, 0, &poll_time, NULL) < 0) { @@ -1485,12 +1491,27 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) poll != NULL ? yml_value_as_int(poll) : 0); } +static bool +conf_verify_poll_interval(keychain_t *chain, const struct yml_node *node) +{ + if (!conf_verify_unsigned(chain, node)) + return false; + + if (yml_value_as_int(node) < min_poll_interval) { + LOG_ERR("%s: interval value cannot be less than %ldms", + conf_err_prefix(chain, node), min_poll_interval); + return false; + } + + return true; +} + static bool verify_conf(keychain_t *chain, const struct yml_node *node) { static const struct attr_info attrs[] = { {"name", true, &conf_verify_string}, - {"poll-interval", false, &conf_verify_unsigned}, + {"poll-interval", false, &conf_verify_poll_interval}, MODULE_COMMON_ATTRS, };