Merge branch 'poll-interval-milliseconds'

Closes #244
This commit is contained in:
Daniel Eklöf 2022-12-23 11:06:14 +01:00
commit 2283647fc7
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
14 changed files with 134 additions and 61 deletions

View file

@ -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

View file

@ -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}"}
```

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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}}
@ -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:

View file

@ -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,
};

View file

@ -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,
};

View file

@ -7,16 +7,17 @@
#include <tllist.h>
#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,
};

View file

@ -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,
};

View file

@ -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,
};

View file

@ -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,
};