[dev] [PATCH] slstatus: cpu_perc: make 100% equivalent of 1 cpu

From: Kurt Van Dijck <dev.kurt_AT_vandijck-laurijssen.be>
Date: Thu, 14 Sep 2017 23:59:15 +0200

This commit allows to specify (statically) the number of CPU's (ncpu).
This allows to show the cpu usage relative to 1 CPU.
So, when 1 cpu is busy, 100% is shown. 2 cpu's busy: 200%, and so on.
At this point, the configuration of ncpu is static.

When no number is given (the backward compatible option), then slstatus
thinks it only has 1 cpu and no scaling is done, like it used to be.

Signed-off-by: Kurt Van Dijck <dev.kurt_AT_vandijck-laurijssen.be>
---
 config.def.h | 6 ++++++
 slstatus.c   | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/config.def.h b/config.def.h
index 6370008..f099bd1 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -3,6 +3,12 @@
 /* interval between updates (in ms) */
 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 ...
+ */
+static int ncpu = 1;
+
 /* 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 cddc79a..ef79b03 100644
--- a/slstatus.c
+++ b/slstatus.c
_AT_@ -197,7 +197,7 @@ cpu_perc(void)
 	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]));
 
-	return bprintf("%d", perc);
+	return bprintf("%d", (ncpu?:1)*perc);
 }
 
 static const char *
_AT_@ -221,7 +221,7 @@ cpu_iowait(void)
 	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]));
 
-	return bprintf("%d", perc);
+	return bprintf("%d", (ncpu?:1)*perc);
 }
 
 static const char *
-- 
1.8.5.rc3
Received on Thu Sep 14 2017 - 23:59:15 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 15 2017 - 00:12:08 CEST