[hackers][slstatus][PATCH] Report average CPU frequency of cluster instead of single-core one

From: Platon Ryzhikov <ihummer63_AT_yandex.ru>
Date: Wed, 11 Nov 2020 14:05:45 +0300

This is especially useful for ARM SoCs with different cores

---
 components/cpu.c | 27 +++++++++++++++++++++------
 config.def.h     |  4 +++-
 2 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/components/cpu.c b/components/cpu.c
index 9e28003..c326297 100644
--- a/components/cpu.c
+++ b/components/cpu.c
_AT_@ -7,16 +7,31 @@
 
 #if defined(__linux__)
 	const char *
-	cpu_freq(void)
+	cpu_freq(const char *range)
 	{
-		uintmax_t freq;
-
-		/* in kHz */
-		if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/"
-		           "scaling_cur_freq", "%ju", &freq) != 1) {
+		uintmax_t freq, sum_freq;
+		static unsigned int i, cpu0, ncpus;
+
+		sum_freq = 0;
+		/* define first cpu in cluster and their number */
+		if (!range) {
+			cpu0 = 0;
+			ncpus = 1;
+		} else if (sscanf(range, "%u %u", &cpu0, &ncpus) != 2) {
 			return NULL;
 		}
 
+		for (i = cpu0; i < cpu0 + ncpus; i += 1) {
+			/* in kHz */
+			if (pscanf(bprintf("/sys/devices/system/cpu/cpu%d/cpufreq/"
+			           "scaling_cur_freq", i), "%ju", &freq) != 1) {
+				return NULL;
+			}
+			sum_freq += freq;
+		}
+
+		freq = (double)sum_freq/(double)ncpus;
+
 		return fmt_human(freq * 1000, 1000);
 	}
 
diff --git a/config.def.h b/config.def.h
index e06be66..6554bb3 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -19,7 +19,9 @@ static const char unknown_str[] = "n/a";
  * battery_remaining   battery remaining HH:MM         battery name (BAT0)
  *                                                     NULL on OpenBSD/FreeBSD
  * cpu_perc            cpu usage in percent            NULL
- * cpu_freq            cpu frequency in MHz            NULL
+ * cpu_freq            cpu frequency in MHz            string "first_cpu ncpus"
+ *                                                     "0 2" means cpu0+cpu1
+ *                                                     NULL on OpenBSD/FreeBSD
  * datetime            date and time                   format string (%F %T)
  * disk_free           free disk space in GB           mountpoint path (/)
  * disk_perc           disk usage in percent           mountpoint path (/)
-- 
2.29.2
Received on Wed Nov 11 2020 - 12:05:45 CET

This archive was generated by hypermail 2.3.0 : Wed Nov 11 2020 - 12:48:32 CET