From f253cc169062d65a941e7d9a55ab1bcc2ca14e5f Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 29 Sep 2024 13:55:11 -0700 Subject: charts: compute a "sample duration" for current sample This turns the time passed since the last sample taken into a "sample duration". Ideally this would always be 1, and up until now in the main use case, vwm, it's been assumed to generally be 1 and drops in the timeline treated benign/fleeting because of the live viewing. But with the introduction of --headless and increasing use on my servers / embedded interests, this has become more problematic. In this commit the duration is only being maintained, but not applied. Subsequent commits will have to repeat the current sample in the graphs (this_sample_duration - 1) times. --- src/charts.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/charts.c b/src/charts.c index b4916df..4bb87e2 100644 --- a/src/charts.c +++ b/src/charts.c @@ -54,6 +54,7 @@ typedef struct _vwm_charts_t { /* libvmon */ struct timeval maybe_sample, last_sample, this_sample; + unsigned this_sample_duration; typeof(((vmon_sys_stat_t *)0)->user) last_user_cpu; typeof(((vmon_sys_stat_t *)0)->system) last_system_cpu; unsigned long long last_total, this_total, total_delta; @@ -1272,13 +1273,23 @@ int vwm_charts_update(vwm_charts_t *charts, int *desired_delay_us) charts->sampling_interval <= charts->prev_sampling_interval && this_delta >= (charts->sampling_interval * 1.5)) { + /* adjust charts->this_sample_duration as needed since we've missed our deadline. + * This is more of an issue in headless mode, especially when run on slower/embedded + * devices, even worse when periodically snapshoting costly PNGs that may take + * several seconds during which no sampling occurs. + */ + charts->this_sample_duration = (this_delta / charts->sampling_interval) + .5f /* rounded to int */; + /* require > 1 contiguous drops before lowering the rate, tolerates spurious one-off stalls */ if (++charts->contiguous_drops > 2) vwm_charts_rate_decrease(charts); } else { charts->contiguous_drops = 0; + charts->this_sample_duration = 1; /* ideally always 1, but > 1 when sample deadline missed (repeat sample) */ } + VWM_TRACE("sample_duration=%u", charts->this_sample_duration); + /* age the sys-wide sample data into "last" variables, before the new sample overwrites them. */ charts->last_sample = charts->this_sample; charts->this_sample = charts->maybe_sample; -- cgit v1.2.3