code: plan9front

Download patch

ref: 6a78bb2e4f00581d85de1c5f9949782de3dd8933
parent: 1047b53efc370ad77546337a221171777b6face1
author: Sigrid <ftrvxmtrx@gmail.com>
date: Fri Jan 22 05:15:36 EST 2021

stats: don't query battery and temp as often when using acpi

Querying battery (or temperature) using ACPI takes quite some
resources, which makes the battery discharge faster.  It doesn't make
much sense to have it queried as often either.  So, when using ACPI:

1) set battery query period to 10s minimum
2) set temperature query period to 5s minimum

--- a/sys/src/cmd/stats.c
+++ b/sys/src/cmd/stats.c
@@ -219,7 +219,9 @@
 double	scale = 1.0;
 int	logscale = 0;
 int	ylabels = 0;
-int 	sleeptime = 1000;
+int	sleeptime = 1000;
+int	batteryperiod = 1000;
+int	tempperiod = 1000;
 
 char	*procnames[NPROC] = {"main", "input"};
 
@@ -625,6 +627,7 @@
 	}
 	m->bitsybatfd = -1;
 	if(m->batteryfd >= 0){
+		batteryperiod = 10000;
 		if(loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
 			memmove(m->batterystats, a, sizeof(m->batterystats));
 	}else{
@@ -636,6 +639,7 @@
 	snprint(buf, sizeof buf, "%s/dev/cputemp", mpt);
 	m->tempfd = open(buf, OREAD);
 	if(m->tempfd < 0){
+		tempperiod = 5000;
 		snprint(buf, sizeof buf, "%s/mnt/acpi/cputemp", mpt);
 		m->tempfd = open(buf, OREAD);
 	}
@@ -679,7 +683,14 @@
 int
 needbattery(int init)
 {
-	return init | present[Mbattery];
+	static uint step = 0;
+
+	if(++step*sleeptime >= batteryperiod){
+		step = 0;
+		return init | present[Mbattery];
+	}
+
+	return 0;
 }
 
 int
@@ -691,7 +702,14 @@
 int
 needtemp(int init)
 {
-	return init | present[Mtemp];
+	static uint step = 0;
+
+	if(++step*sleeptime >= tempperiod){
+		step = 0;
+		return init | present[Mtemp];
+	}
+
+	return 0;
 }
 
 void
@@ -737,10 +755,12 @@
 	if(needsignal(init) && loadbuf(m, &m->ifstatsfd) && strncmp(m->buf, "Signal: ", 8)==0 && readnums(m, nelem(m->netetherifstats), a, 1)){
 		memmove(m->netetherifstats, a, sizeof m->netetherifstats);
 	}
-	if(needbattery(init) && loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
-		memmove(m->batterystats, a, sizeof(m->batterystats));
-	if(needbattery(init) && loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
-		memmove(m->batterystats, a, sizeof(m->batterystats));
+	if(needbattery(init)){
+		if(loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
+			memmove(m->batterystats, a, sizeof(m->batterystats));
+		else if(loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
+			memmove(m->batterystats, a, sizeof(m->batterystats));
+	}
 	if(needtemp(init) && loadbuf(m, &m->tempfd))
 		for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
 			 m->temp[n] = a[0];