module/network: poll-interval: convert value from ‘seconds’ to ‘milliseconds’

This commit is contained in:
Daniel Eklöf 2022-12-22 11:59:08 +01:00
parent 8fbbce10a5
commit 500b051fe4
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 38 additions and 16 deletions

View file

@ -72,8 +72,9 @@ address.
| poll-interval | poll-interval
: int : int
: no : no
: Periodically (in seconds) update the signal, rx+tx bitrate, and : Periodically (in milliseconds) update the signal, rx+tx bitrate, and
ul+dl speed tags. ul+dl speed tags. Setting it to 0 disables updates. Cannot be less
than 500ms.
# EXAMPLES # EXAMPLES

View file

@ -181,7 +181,7 @@ bar:
state == up && ipv4 != "": {string: {text: , font: *awesome}} state == up && ipv4 != "": {string: {text: , font: *awesome}}
- network: - network:
name: wlp2s0 name: wlp2s0
poll-interval: 1 poll-interval: 1000
content: content:
map: map:
default: {string: {text: , font: *awesome, foreground: ffffff66}} default: {string: {text: , font: *awesome, foreground: ffffff66}}

View file

@ -34,6 +34,8 @@
#define UNUSED __attribute__((unused)) #define UNUSED __attribute__((unused))
static const long min_poll_interval = 500;
struct rt_stats_msg { struct rt_stats_msg {
struct rtmsg rth; struct rtmsg rth;
struct rtnl_link_stats64 stats; struct rtnl_link_stats64 stats;
@ -79,10 +81,10 @@ struct private {
uint32_t rx_bitrate; uint32_t rx_bitrate;
uint32_t tx_bitrate; uint32_t tx_bitrate;
uint64_t ul_speed; double ul_speed;
uint64_t ul_bits; uint64_t ul_bits;
uint64_t dl_speed; double dl_speed;
uint64_t dl_bits; uint64_t dl_bits;
}; };
@ -1120,15 +1122,16 @@ static void
handle_stats(struct module *mod, struct rt_stats_msg *msg) handle_stats(struct module *mod, struct rt_stats_msg *msg)
{ {
struct private *m = mod->private; struct private *m = mod->private;
uint64_t ul_bits = msg->stats.tx_bytes*8; uint64_t ul_bits = msg->stats.tx_bytes * 8;
uint64_t dl_bits = msg->stats.rx_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->ul_bits = ul_bits;
m->dl_bits = dl_bits; m->dl_bits = dl_bits;
} }
@ -1336,9 +1339,12 @@ run(struct module *mod)
goto out; goto out;
} }
const long secs = m->poll_interval / 1000;
const long msecs = m->poll_interval % 1000;
struct itimerspec poll_time = { struct itimerspec poll_time = {
.it_value = {.tv_sec = m->poll_interval}, .it_value = {.tv_sec = secs, .tv_nsec = msecs * 1000000},
.it_interval = {.tv_sec = m->poll_interval}, .it_interval = {.tv_sec = secs, .tv_nsec = msecs * 1000000},
}; };
if (timerfd_settime(timer_fd, 0, &poll_time, NULL) < 0) { 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); 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 static bool
verify_conf(keychain_t *chain, const struct yml_node *node) verify_conf(keychain_t *chain, const struct yml_node *node)
{ {
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
{"name", true, &conf_verify_string}, {"name", true, &conf_verify_string},
{"poll-interval", false, &conf_verify_unsigned}, {"poll-interval", false, &conf_verify_poll_interval},
MODULE_COMMON_ATTRS, MODULE_COMMON_ATTRS,
}; };