From 62aaf6e3b136fefee1595ccc4f21e5fc83d7a57a Mon Sep 17 00:00:00 2001 From: Sniffleupagus Date: Wed, 19 Apr 2023 20:58:37 -0700 Subject: [PATCH] copied cpu_stat from pwnagotchi/__index__.py, and calculate load since previous call, instead of sampling 0.1s while updating On Pi0 instead of always seeing 100%, I see 40-80% typical, which seems more in line with what is happening on the system --- pwnagotchi/plugins/default/memtemp.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pwnagotchi/plugins/default/memtemp.py b/pwnagotchi/plugins/default/memtemp.py index 898df2f2..d9ac75ce 100644 --- a/pwnagotchi/plugins/default/memtemp.py +++ b/pwnagotchi/plugins/default/memtemp.py @@ -41,6 +41,7 @@ class MemTemp(plugins.Plugin): ALLOWED_FIELDS = { 'mem': 'mem_usage', 'cpu': 'cpu_load', + 'cpus': 'cpu_load_since', 'temp': 'cpu_temp', 'freq': 'cpu_freq' } @@ -50,6 +51,7 @@ class MemTemp(plugins.Plugin): FIELD_WIDTH = 4 def on_loaded(self): + self._last_cpu_load = self._cpu_stat() logging.info("memtemp plugin loaded.") def mem_usage(self): @@ -58,6 +60,28 @@ class MemTemp(plugins.Plugin): def cpu_load(self): return f"{int(pwnagotchi.cpu_load() * 100)}%" + def _cpu_stat(self): + """ + Returns the splitted first line of the /proc/stat file + """ + with open('/proc/stat', 'rt') as fp: + return list(map(int,fp.readline().split()[1:])) + + def cpu_load_since(self): + """ + Returns the % load, since last time called + """ + parts0 = self._cpu_stat() + parts1 = self._last_cpu_load + self._last_cpu_load = parts0 + + parts_diff = [p1 - p0 for (p0, p1) in zip(parts0, parts1)] + user, nice, sys, idle, iowait, irq, softirq, steal, _guest, _guest_nice = parts_diff + idle_sum = idle + iowait + non_idle_sum = user + nice + sys + irq + softirq + steal + total = idle_sum + non_idle_sum + return f"{int(non_idle_sum / total * 100)}%" + def cpu_temp(self): if self.options['scale'] == "fahrenheit": temp = (pwnagotchi.temperature() * 9 / 5) + 32