From 1f25978eb4bcd36a75c0d0e03f427c7b4bf9ba24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Dec 2022 11:46:00 +0100 Subject: [PATCH 1/7] module/cpu: cleanup poll-interval MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * man page: spell out ‘milliseconds’ * use a ‘static const’ variable for min_poll_interval, instead of a macro --- doc/yambar-modules-cpu.5.scd | 4 ++-- modules/cpu.c | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/doc/yambar-modules-cpu.5.scd b/doc/yambar-modules-cpu.5.scd index bfd9357..400b2dd 100644 --- a/doc/yambar-modules-cpu.5.scd +++ b/doc/yambar-modules-cpu.5.scd @@ -31,8 +31,8 @@ total CPU usage. | poll-interval : int : no -: Refresh interval of the CPU usage stats in ms (default=500). Cannot - be less then 500 ms +: Refresh interval of the CPU usage stats in milliseconds + (default=500). Cannot be less then 500ms. # EXAMPLES diff --git a/modules/cpu.c b/modules/cpu.c index b63e926..a18cc9b 100644 --- a/modules/cpu.c +++ b/modules/cpu.c @@ -13,7 +13,6 @@ #define LOG_MODULE "cpu" #define LOG_ENABLE_DBG 0 -#define SMALLEST_INTERVAL 500 #include "../log.h" #include "../bar/bar.h" @@ -22,6 +21,8 @@ #include "../particles/dynlist.h" #include "../plugin.h" +static const long min_poll_interval = 500; + struct cpu_stats { uint32_t *prev_cores_idle; uint32_t *prev_cores_nidle; @@ -276,19 +277,19 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) const struct yml_node *c = yml_get_value(node, "content"); return cpu_new( - interval == NULL ? SMALLEST_INTERVAL : yml_value_as_int(interval), + interval == NULL ? min_poll_interval : yml_value_as_int(interval), conf_to_particle(c, inherited)); } static bool -conf_verify_interval(keychain_t *chain, const struct yml_node *node) +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) < SMALLEST_INTERVAL) { - LOG_ERR("%s: interval value cannot be less than %d ms", - conf_err_prefix(chain, node), SMALLEST_INTERVAL); + 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; } @@ -299,7 +300,7 @@ static bool verify_conf(keychain_t *chain, const struct yml_node *node) { static const struct attr_info attrs[] = { - {"poll-interval", false, &conf_verify_interval}, + {"poll-interval", false, &conf_verify_poll_interval}, MODULE_COMMON_ATTRS, }; From a18296a179ce4987d1d7edd9538f9ff24040fd95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Dec 2022 11:47:08 +0100 Subject: [PATCH 2/7] module/disk-io: cleanup poll-interval MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * man page: spell out ‘milliseconds’ * use a ‘static const’ variable for min_poll_interval, instead of a macro --- doc/yambar-modules-disk-io.5.scd | 4 ++-- modules/disk-io.c | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/doc/yambar-modules-disk-io.5.scd b/doc/yambar-modules-disk-io.5.scd index 77220d9..6f6c7dc 100644 --- a/doc/yambar-modules-disk-io.5.scd +++ b/doc/yambar-modules-disk-io.5.scd @@ -38,8 +38,8 @@ currently present in the machine. | poll-interval : int : no -: Refresh interval of disk's stats in ms (default=500). - Cannot be less then 500 ms +: Refresh interval of disk's stats in milliseconds (default=500). + Cannot be less then 500ms. # EXAMPLES diff --git a/modules/disk-io.c b/modules/disk-io.c index 8609c44..2142b56 100644 --- a/modules/disk-io.c +++ b/modules/disk-io.c @@ -7,16 +7,17 @@ #include -#include "../particles/dynlist.h" +#define LOG_MODULE "disk-io" +#define LOG_ENABLE_DBG 0 +#include "../log.h" + #include "../bar/bar.h" #include "../config-verify.h" #include "../config.h" -#include "../log.h" +#include "../particles/dynlist.h" #include "../plugin.h" -#define LOG_MODULE "disk-io" -#define LOG_ENABLE_DBG 0 -#define SMALLEST_INTERVAL 500 +static const long min_poll_interval = 500; struct device_stats { char *name; @@ -314,20 +315,20 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) const struct yml_node *c = yml_get_value(node, "content"); return disk_io_new( - interval == NULL ? SMALLEST_INTERVAL : yml_value_as_int(interval), + interval == NULL ? min_poll_interval : yml_value_as_int(interval), conf_to_particle(c, inherited)); } static bool -conf_verify_interval(keychain_t *chain, const struct yml_node *node) +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) < SMALLEST_INTERVAL) { + if (yml_value_as_int(node) < min_poll_interval) { LOG_ERR( - "%s: poll-interval value cannot be less than %d ms", - conf_err_prefix(chain, node), SMALLEST_INTERVAL); + "%s: poll-interval value cannot be less than %ldms", + conf_err_prefix(chain, node), min_poll_interval); return false; } @@ -338,7 +339,7 @@ static bool verify_conf(keychain_t *chain, const struct yml_node *node) { static const struct attr_info attrs[] = { - {"poll-interval", false, &conf_verify_interval}, + {"poll-interval", false, &conf_verify_poll_interval}, MODULE_COMMON_ATTRS, }; From ac8e45c3312690ff6b49dfc192c217284c569106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Dec 2022 11:47:15 +0100 Subject: [PATCH 3/7] module/mem: cleanup poll-interval MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * man page: spell out ‘milliseconds’ * use a ‘static const’ variable for min_poll_interval, instead of a macro --- doc/yambar-modules-mem.5.scd | 4 ++-- modules/mem.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/doc/yambar-modules-mem.5.scd b/doc/yambar-modules-mem.5.scd index 82710c3..7d72256 100644 --- a/doc/yambar-modules-mem.5.scd +++ b/doc/yambar-modules-mem.5.scd @@ -33,8 +33,8 @@ mem - This module provides the memory usage | poll-interval : string : no -: Refresh interval of the memory usage stats in ms - (default=500). Cannot be less then 500 ms. +: Refresh interval of the memory usage stats in milliseconds + (default=500). Cannot be less then 500ms. # EXAMPLES diff --git a/modules/mem.c b/modules/mem.c index 417c8df..54f7f83 100644 --- a/modules/mem.c +++ b/modules/mem.c @@ -12,13 +12,14 @@ #define LOG_MODULE "mem" #define LOG_ENABLE_DBG 0 -#define SMALLEST_INTERVAL 500 #include "../bar/bar.h" #include "../config-verify.h" #include "../config.h" #include "../log.h" #include "../plugin.h" +static const long min_poll_interval = 500; + struct private { struct particle *label; @@ -155,18 +156,19 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) const struct yml_node *c = yml_get_value(node, "content"); return mem_new( - interval == NULL ? SMALLEST_INTERVAL : yml_value_as_int(interval), + interval == NULL ? min_poll_interval : yml_value_as_int(interval), conf_to_particle(c, inherited)); } static bool -conf_verify_interval(keychain_t *chain, const struct yml_node *node) +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) < SMALLEST_INTERVAL) { - LOG_ERR("%s: interval value cannot be less than %d ms", conf_err_prefix(chain, node), SMALLEST_INTERVAL); + 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; } @@ -177,7 +179,7 @@ static bool verify_conf(keychain_t *chain, const struct yml_node *node) { static const struct attr_info attrs[] = { - {"poll-interval", false, &conf_verify_interval}, + {"poll-interval", false, &conf_verify_poll_interval}, MODULE_COMMON_ATTRS, }; From 8fbbce10a5e555adfe61ebc83b20437c52e3b0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Dec 2022 11:47:50 +0100 Subject: [PATCH 4/7] =?UTF-8?q?module/battery:=20poll-interval:=20convert?= =?UTF-8?q?=20value=20from=20=E2=80=98seconds=E2=80=99=20to=20=E2=80=98mil?= =?UTF-8?q?liseconds=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/yambar-modules-battery.5.scd | 7 +++++-- examples/configurations/laptop.conf | 2 +- modules/battery.c | 32 +++++++++++++++++++++++------ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/doc/yambar-modules-battery.5.scd b/doc/yambar-modules-battery.5.scd index 101c02b..045b3fa 100644 --- a/doc/yambar-modules-battery.5.scd +++ b/doc/yambar-modules-battery.5.scd @@ -57,7 +57,10 @@ the state *unknown* under other conditions. | poll-interval : int : no -: How often, in seconds, to poll for capacity changes (default=*60*). Set to `0` to disable polling (*warning*: many batteries do not support asynchronous reporting). +: How often, in milliseconds, to poll for capacity changes + (default=*60000*). Set to `0` to disable polling (*warning*: many + batteries do not support asynchronous reporting). Cannot be less + than 500ms. # EXAMPLES @@ -66,7 +69,7 @@ bar: left: - battery: name: BAT0 - poll-interval: 30 + poll-interval: 30000 content: string: {text: "BAT: {capacity}% {estimate}"} ``` diff --git a/examples/configurations/laptop.conf b/examples/configurations/laptop.conf index de04ed7..dd7fbcb 100644 --- a/examples/configurations/laptop.conf +++ b/examples/configurations/laptop.conf @@ -221,7 +221,7 @@ bar: content: [ string: {text: , font: *awesome}, string: {text: "{percent}%"}] - battery: name: BAT0 - poll-interval: 30 + poll-interval: 30000 anchors: discharging: &discharging list: diff --git a/modules/battery.c b/modules/battery.c index c4f949c..7d31aa3 100644 --- a/modules/battery.c +++ b/modules/battery.c @@ -20,12 +20,15 @@ #include "../config-verify.h" #include "../plugin.h" +static const long min_poll_interval = 500; +static const long default_poll_interval = 60 * 1000; + enum state { STATE_FULL, STATE_NOTCHARGING, STATE_CHARGING, STATE_DISCHARGING, STATE_UNKNOWN }; struct private { struct particle *label; - int poll_interval; + long poll_interval; char *battery; char *manufacturer; char *model; @@ -429,7 +432,7 @@ run(struct module *mod) {.fd = udev_monitor_get_fd(mon), .events = POLLIN}, }; if (poll(fds, sizeof(fds) / sizeof(fds[0]), - m->poll_interval > 0 ? m->poll_interval * 1000 : -1) < 0) + m->poll_interval > 0 ? m->poll_interval : -1) < 0) { if (errno == EINTR) continue; @@ -469,11 +472,11 @@ out: } static struct module * -battery_new(const char *battery, struct particle *label, int poll_interval_secs) +battery_new(const char *battery, struct particle *label, long poll_interval_msecs) { struct private *m = calloc(1, sizeof(*m)); m->label = label; - m->poll_interval = poll_interval_secs; + m->poll_interval = poll_interval_msecs; m->battery = strdup(battery); m->state = STATE_UNKNOWN; @@ -496,7 +499,24 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) return battery_new( yml_value_as_string(name), conf_to_particle(c, inherited), - poll_interval != NULL ? yml_value_as_int(poll_interval) : 60); + (poll_interval != NULL + ? yml_value_as_int(poll_interval) + : default_poll_interval)); +} + +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 @@ -504,7 +524,7 @@ 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, }; 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 5/7] =?UTF-8?q?module/network:=20poll-interval:=20convert?= =?UTF-8?q?=20value=20from=20=E2=80=98seconds=E2=80=99=20to=20=E2=80=98mil?= =?UTF-8?q?liseconds=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, }; From c4f820e486318dfebade944a77f3c554cf7d3f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Dec 2022 12:06:25 +0100 Subject: [PATCH 6/7] =?UTF-8?q?module/script:=20poll-interval:=20convert?= =?UTF-8?q?=20value=20from=20=E2=80=98seconds=E2=80=99=20to=20=E2=80=98mil?= =?UTF-8?q?liseconds=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/yambar-modules-script.5.scd | 4 ++-- modules/script.c | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/doc/yambar-modules-script.5.scd b/doc/yambar-modules-script.5.scd index 17ac34e..ec91c7e 100644 --- a/doc/yambar-modules-script.5.scd +++ b/doc/yambar-modules-script.5.scd @@ -78,8 +78,8 @@ User defined. | poll-interval : integer : no -: Number of seconds between each script run. If unset, continuous mode - is used. +: Number of milliseconds between each script run. If unset, or set to + 0, continuous mode is used. # EXAMPLES diff --git a/modules/script.c b/modules/script.c index 1c349a1..2b97590 100644 --- a/modules/script.c +++ b/modules/script.c @@ -22,6 +22,8 @@ #include "../module.h" #include "../plugin.h" +static const long min_poll_interval = 500; + struct private { char *path; size_t argc; @@ -574,7 +576,7 @@ run(struct module *mod) break; if (m->aborted) break; - if (m->poll_interval < 0) + if (m->poll_interval <= 0) break; struct timeval now; @@ -583,7 +585,10 @@ run(struct module *mod) break; } - struct timeval poll_interval = {.tv_sec = m->poll_interval}; + struct timeval poll_interval = { + .tv_sec = m->poll_interval / 1000, + .tv_usec = (m->poll_interval % 1000) * 1000, + }; struct timeval timeout; timeradd(&now, &poll_interval, &timeout); @@ -670,7 +675,7 @@ from_conf(const struct yml_node *node, struct conf_inherit inherited) return script_new( yml_value_as_string(path), argc, argv, - poll_interval != NULL ? yml_value_as_int(poll_interval) : -1, + poll_interval != NULL ? yml_value_as_int(poll_interval) : 0, conf_to_particle(c, inherited)); } @@ -695,13 +700,28 @@ conf_verify_args(keychain_t *chain, const struct yml_node *node) return conf_verify_list(chain, node, &conf_verify_string); } +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[] = { {"path", true, &conf_verify_path}, {"args", false, &conf_verify_args}, - {"poll-interval", false, &conf_verify_unsigned}, + {"poll-interval", false, &conf_verify_poll_interval}, MODULE_COMMON_ATTRS, }; From d26d3953f16f01d751ca2fc90fc1b398bbb118a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Dec 2022 12:09:28 +0100 Subject: [PATCH 7/7] changelog: batter/network/script: poll-interval: seconds -> milliseconds --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 196202a..bc6f849 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ * disk-io: `interval` renamed to `poll-interval` * mem: `interval` renamed to `poll-interval` +* battery/network/script: `poll-interval` unit changed from seconds to + milliseconds ([#244][244]). + +[244]: https://codeberg.org/dnkl/yambar/issues/244 ### Deprecated