From a6ecfb8a802bf5157af547f8c37e764dd0effd01 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Thu, 25 May 2023 04:42:36 -0400 Subject: [PATCH] MT#55283 refactor load monitor thread Use the looper thread helper functions. Change-Id: Ib272ce706484f393bd42f291231c82ff6ea5212f --- daemon/load.c | 66 ++++++++++++++++++++++++-------------------------- daemon/main.c | 4 +-- include/load.h | 2 +- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/daemon/load.c b/daemon/load.c index b12e72128..61056f136 100644 --- a/daemon/load.c +++ b/daemon/load.c @@ -14,46 +14,42 @@ int cpu_usage; // percent times 100 (0 - 9999) static long used_last, idle_last; -void load_thread(void *dummy) { +enum thread_looper_action load_thread() { // anything to do? if (!rtpe_config.load_limit && !rtpe_config.cpu_limit) - return; + return TLA_BREAK; - while (!rtpe_shutdown) { - if (rtpe_config.load_limit) { - double loadavg; - if (getloadavg(&loadavg, 1) >= 1) - g_atomic_int_set(&load_average, (int) (loadavg * 100.0)); - else - ilog(LOG_WARN, "Failed to obtain load average: %s", strerror(errno)); - } + if (rtpe_config.load_limit) { + double loadavg; + if (getloadavg(&loadavg, 1) >= 1) + g_atomic_int_set(&load_average, (int) (loadavg * 100.0)); + else + ilog(LOG_WARN, "Failed to obtain load average: %s", strerror(errno)); + } - if (rtpe_config.cpu_limit) { - FILE *f; - f = fopen("/proc/stat", "r"); - if (f) { - long user_now, nice_now, system_now, idle_now; - if (fscanf(f, "cpu %li %li %li %li", - &user_now, &nice_now, &system_now, &idle_now) == 4) - { - long used_now = user_now + nice_now + system_now; - long used_secs = used_now - used_last; - long idle_secs = idle_now - idle_last; - long total_secs = used_secs + idle_secs; - if (total_secs > 0 && used_last && idle_last) - g_atomic_int_set(&cpu_usage, (int) (used_secs - * 10000 / total_secs)); - used_last = used_now; - idle_last = idle_now; - } - else - ilog(LOG_WARN, "Failed to obtain CPU usage"); - fclose(f); + if (rtpe_config.cpu_limit) { + FILE *f; + f = fopen("/proc/stat", "r"); + if (f) { + long user_now, nice_now, system_now, idle_now; + if (fscanf(f, "cpu %li %li %li %li", + &user_now, &nice_now, &system_now, &idle_now) == 4) + { + long used_now = user_now + nice_now + system_now; + long used_secs = used_now - used_last; + long idle_secs = idle_now - idle_last; + long total_secs = used_secs + idle_secs; + if (total_secs > 0 && used_last && idle_last) + g_atomic_int_set(&cpu_usage, (int) (used_secs + * 10000 / total_secs)); + used_last = used_now; + idle_last = idle_now; } + else + ilog(LOG_WARN, "Failed to obtain CPU usage"); + fclose(f); } - - thread_cancel_enable(); - usleep(500000); - thread_cancel_disable(); } + + return TLA_CONTINUE; } diff --git a/daemon/main.c b/daemon/main.c index 5db5b927b..29ed3103f 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -1343,8 +1343,8 @@ int main(int argc, char **argv) { rtpe_config.idle_priority, "poller timer"); /* load monitoring thread */ - thread_create_detach_prio(load_thread, NULL, rtpe_config.idle_scheduling, - rtpe_config.idle_priority, "load monitor"); + thread_create_looper(load_thread, rtpe_config.idle_scheduling, + rtpe_config.idle_priority, "load monitor", 500000); /* separate thread for releasing ports (sockets), which are scheduled for clearing */ thread_create_looper(release_closed_sockets, rtpe_config.idle_scheduling, diff --git a/include/load.h b/include/load.h index d11be7756..d0dcee0bc 100644 --- a/include/load.h +++ b/include/load.h @@ -4,6 +4,6 @@ extern int load_average; // times 100 extern int cpu_usage; // times 100 -void load_thread(void *); +enum thread_looper_action load_thread(void); #endif