[dev] [PATCH 2/2] slstatus: determine the number of cpu's in runtime

From: Kurt Van Dijck <dev.kurt_AT_vandijck-laurijssen.be>
Date: Fri, 15 Sep 2017 19:50:48 +0200

This runtime probe is only used when cpu usage is reported relative to 1cpu.
The default case reports cpu usage relative to 100%, and not runtime
cpu counting is required.

Signed-off-by: Kurt Van Dijck <dev.kurt_AT_vandijck-laurijssen.be>
---
 config.def.h |  7 ++++---
 slstatus.c   | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/config.def.h b/config.def.h
index f099bd1..71880f6 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -4,10 +4,11 @@
 static const int interval = 1000;
 
 /* number of CPU's to scale CPU usage for
- * ideally, this number is detected repeatedly in runtime
- * since it may even change ...
+ * When ncpu=0, cpu usage is relative to 100%.
+ * When ncpu!=0, cpu usage is relative to 1cpu and the number of cpu's
+ * is determined on each measurement.
  */
-static int ncpu = 1;
+static int ncpu = 0;
 
 /* text to show if no value can be retrieved */
 static const char unknown_str[] = "n/a";
diff --git a/slstatus.c b/slstatus.c
index 79faeb3..ef44499 100644
--- a/slstatus.c
+++ b/slstatus.c
_AT_@ -179,6 +179,19 @@ cpu_freq(void)
 	       bprintf("%d", (freq + 500) / 1000) : unknown_str;
 }
 
+static int
+cpu_count(void)
+{
+	char cbuf[4];
+	int cpucnt, id;
+
+	pscanf(NULL, "%*[^\n]");
+	for (cpucnt = 0; ; ++cpucnt)
+		if (pscanf(NULL, "cpu%i %*[^\n]", &id) != 1)
+			break;
+	return cpucnt;
+}
+
 static const char *
 cpu_perc(void)
 {
_AT_@ -196,6 +209,8 @@ cpu_perc(void)
 		valid = 1;
 		return unknown_str;
 	}
+	if (ncpu)
+		ncpu = cpu_count();
 
 	perc = 100 * ((b[0]+b[1]+b[2]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[5]+a[6])) /
 	       ((b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]));
_AT_@ -220,6 +235,8 @@ cpu_iowait(void)
 		valid = 1;
 		return unknown_str;
 	}
+	if (ncpu)
+		ncpu = cpu_count();
 
 	perc = 100 * ((b[4]) - (a[4])) /
 	       ((b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]));
-- 
1.8.5.rc3
Received on Fri Sep 15 2017 - 19:50:48 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 15 2017 - 20:01:00 CEST