[dev] [PATCH 2/3] slstatus: add cpu_iowait

From: Kurt Van Dijck <dev.kurt_AT_vandijck-laurijssen.be>
Date: Mon, 11 Sep 2017 20:05:53 +0200

This commit introduces the cpu_iowait item, this is the percentage
of cpu time spent waiting on disks. High numbers typically indicate
that your system is not responsive due to disk IO

Signed-off-by: Kurt Van Dijck <dev.kurt_AT_vandijck-laurijssen.be>
---
 config.def.h |  1 +
 slstatus.c   | 37 +++++++++++++++++++++++++++++++------
 2 files changed, 32 insertions(+), 6 deletions(-)
diff --git a/config.def.h b/config.def.h
index 0e28d1e..6370008 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -16,6 +16,7 @@ static const char unknown_str[] = "n/a";
  * battery_power        battery power usage             battery name
  * battery_state        battery charging state          battery name
  * cpu_perc             cpu usage in percent            NULL
+ * cpu_iowait           cpu iowait in percent           NULL
  * cpu_freq             cpu frequency in MHz            NULL
  * datetime             date and time                   format string
  * disk_free            free disk space in GB           mountpoint path
diff --git a/slstatus.c b/slstatus.c
index e1f0e00..cddc79a 100644
--- a/slstatus.c
+++ b/slstatus.c
_AT_@ -41,6 +41,7 @@ static const char *battery_power(const char *bat);
 static const char *battery_state(const char *bat);
 static const char *cpu_freq(void);
 static const char *cpu_perc(void);
+static const char *cpu_iowait(void);
 static const char *datetime(const char *fmt);
 static const char *disk_free(const char *mnt);
 static const char *disk_perc(const char *mnt);
_AT_@ -179,13 +180,13 @@ static const char *
 cpu_perc(void)
 {
 	int perc;
-	static long double a[4];
+	static long double a[7];
 	static int valid;
-	long double b[4];
+	long double b[7];
 
 	memcpy(b, a, sizeof(b));
-	if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
-	           &a[3]) != 4) {
+	if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
+	           &a[3], &a[4], &a[5], &a[6]) != 7) {
 		return unknown_str;
 	}
 	if (!valid) {
_AT_@ -193,8 +194,32 @@ cpu_perc(void)
 		return unknown_str;
 	}
 
-	perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) /
-	       ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
+	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);
+}
+
+static const char *
+cpu_iowait(void)
+{
+	int perc;
+	static int valid;
+	static long double a[7];
+	long double b[7];
+
+	memcpy(b, a, sizeof(b));
+	if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
+	           &a[3], &a[4], &a[5], &a[6]) != 7) {
+		return unknown_str;
+	}
+	if (!valid) {
+		valid = 1;
+		return unknown_str;
+	}
+
+	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);
 }
-- 
1.8.5.rc3
Received on Mon Sep 11 2017 - 20:05:53 CEST

This archive was generated by hypermail 2.3.0 : Mon Sep 11 2017 - 20:12:33 CEST