diff --git a/daemon/main.c b/daemon/main.c index 29ed3103f..42395f005 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -1351,8 +1351,8 @@ int main(int argc, char **argv) { rtpe_config.idle_priority, "release closed sockets", 1000000); /* separate thread for update of running min/max call counters */ - thread_create_detach_prio(call_rate_stats_updater, NULL, rtpe_config.idle_scheduling, - rtpe_config.idle_priority, "call rate stats"); + thread_create_looper(call_rate_stats_updater, rtpe_config.idle_scheduling, + rtpe_config.idle_priority, "call rate stats", 1000000); /* separate thread for ports iterations (stats update from the kernel) functionality */ thread_create_looper(kernel_stats_updater, rtpe_config.idle_scheduling, diff --git a/daemon/statistics.c b/daemon/statistics.c index bb3d12872..7c97d1042 100644 --- a/daemon/statistics.c +++ b/daemon/statistics.c @@ -1022,27 +1022,17 @@ const char *statistics_ng(bencode_item_t *input, bencode_item_t *output) { /** * Separate thread for update of running min/max call counters. */ -void call_rate_stats_updater(void * dummy) { - bool first_run = true; - struct timeval last_run = (struct timeval) {0,0}; +enum thread_looper_action call_rate_stats_updater() { + static struct timeval last_run; - while (!rtpe_shutdown) { - struct timeval tv_start; - long long run_diff_us; + stats_rate_min_max(&rtpe_rate_graphite_min_max, &rtpe_stats_rate); - gettimeofday(&tv_start, NULL); /* current run */ - run_diff_us = timeval_diff(&tv_start, &last_run); /* TODO: do we need `run_diff_us` at all? */ - last_run = tv_start; /* for the next cycle */ - - stats_rate_min_max(&rtpe_rate_graphite_min_max, &rtpe_stats_rate); - - if (!first_run) /* `stats_counters_calc_rate()` shouldn't be called on the very first cycle */ - stats_counters_calc_rate(&rtpe_stats, run_diff_us, &rtpe_stats_intv, &rtpe_stats_rate); + if (!last_run.tv_sec) { /* `stats_counters_calc_rate()` shouldn't be called on the very first cycle */ + long long run_diff_us = timeval_diff(&rtpe_now, &last_run); + stats_counters_calc_rate(&rtpe_stats, run_diff_us, &rtpe_stats_intv, &rtpe_stats_rate); + } - thread_cancel_enable(); - usleep(1000000); /* sleep for 1 second in each iteration */ - thread_cancel_disable(); + last_run = rtpe_now; - first_run = false; - } + return TLA_CONTINUE; } diff --git a/include/statistics.h b/include/statistics.h index 2a109c13b..23c301589 100644 --- a/include/statistics.h +++ b/include/statistics.h @@ -198,7 +198,7 @@ void statistics_update_foreignown_inc(struct call* c); GQueue *statistics_gather_metrics(struct interface_sampled_rate_stats *); void statistics_free_metrics(GQueue **); const char *statistics_ng(bencode_item_t *input, bencode_item_t *output); -void call_rate_stats_updater(void * dummy); +enum thread_looper_action call_rate_stats_updater(void); /** * Calculation of the call rate counters.