From f472733f06a951ec69da96564a40603e327e767e Mon Sep 17 00:00:00 2001 From: Alastair Poole Date: Sat, 16 Nov 2019 15:58:16 +0000 Subject: [PATCH] CPU: Only use active CPU count for generic CPU progress. Some systems disable CPU cores. In the CPU tab this is fine, but for an accurate representation of CPU load we need to check for the number of active CPUs. E.g. OpenBSD disables HT on amd64 thus a 2-core system will only use 1 core. --- src/system.c | 20 ++++++++++++++++++++ src/system.h | 3 +++ src/ui.c | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/system.c b/src/system.c index 261d82d..42ff2d3 100644 --- a/src/system.c +++ b/src/system.c @@ -206,6 +206,26 @@ cpu_count(void) return cores; } +int +system_cpu_online_count_get(void) +{ +#if defined(__linux__) + return cpu_count(); +#endif + static int cores = 0; + + if (cores != 0) return cores; + + size_t len; + int mib[2] = { CTL_HW, HW_NCPUONLINE }; + + len = sizeof(cores); + if (sysctl(mib, 2, &cores, &len, NULL, 0) < 0) + return cpu_count(); + + return cores; +} + static void _cpu_state_get(cpu_core_t **cores, int ncpu) { diff --git a/src/system.h b/src/system.h index 3f98a70..e7d2527 100644 --- a/src/system.h +++ b/src/system.h @@ -74,4 +74,7 @@ int void system_power_state_get(power_t *power); +int + system_cpu_online_count_get(); + #endif diff --git a/src/ui.c b/src/ui.c index 83bc79f..5fdac0b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -434,7 +434,7 @@ _system_stats_feedback_cb(void *data, Ecore_Thread *thread, void *msg) free(results->cores[i]); } - cpu_usage = cpu_usage / results->cpu_count; + cpu_usage = cpu_usage / system_cpu_online_count_get(); _progressbar_value_force_set(ui->progress_cpu, (double)cpu_usage / 100);