From 1102aa16bfd72974d340a907caac2993a88a814a Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 27 Feb 2025 06:03:32 +0100 Subject: [PATCH] Return negative total CPU % if it cannot be calculated (*very* old kernel, older than 2.6.33 released 2010) Signed-off-by: DL6ER --- src/api/docs/content/specs/info.yaml | 2 +- src/daemon.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/api/docs/content/specs/info.yaml b/src/api/docs/content/specs/info.yaml index 1378984aa..75b6783b0 100644 --- a/src/api/docs/content/specs/info.yaml +++ b/src/api/docs/content/specs/info.yaml @@ -463,7 +463,7 @@ components: example: 8 "%cpu": type: number - description: Total CPU usage in percent + description: Total CPU usage in percent (may be higher than 100% on multi-core systems and negative if the value cannot be computed) example: 0.0 load: type: object diff --git a/src/daemon.c b/src/daemon.c index 8a27661ce..509a26184 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -462,6 +462,9 @@ void calc_cpu_usage(const unsigned int interval) static float last_ftl_cpu_time = 0.0f; ftl_cpu_usage = 100.0 * (ftl_cpu_time - last_ftl_cpu_time) / interval; + // Store the current time for the next call to this function + last_ftl_cpu_time = ftl_cpu_time; + // The number of clock ticks per second static long user_hz = 0; if(user_hz == 0) @@ -469,7 +472,11 @@ void calc_cpu_usage(const unsigned int interval) // Calculate the total CPU usage unsigned long total_total, total_idle; - parse_proc_stat(&total_total, &total_idle); + if(!parse_proc_stat(&total_total, &total_idle)) + { + total_cpu_usage = -1.0f; + return; + } // Calculate the CPU usage since the last call to this function static unsigned long last_total_total = 0, last_total_idle = 0; @@ -477,7 +484,6 @@ void calc_cpu_usage(const unsigned int interval) total_cpu_usage = 100.0 * (total_total - last_total_total - (total_idle - last_total_idle)) / (total_total - last_total_total); // Store the current time for the next call to this function - last_ftl_cpu_time = ftl_cpu_time; last_total_idle = total_idle; last_total_total = total_total; }