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
This commit is contained in:
Sniffleupagus
2023-04-19 20:58:37 -07:00
parent 81f371f809
commit 62aaf6e3b1

View File

@ -41,6 +41,7 @@ class MemTemp(plugins.Plugin):
ALLOWED_FIELDS = { ALLOWED_FIELDS = {
'mem': 'mem_usage', 'mem': 'mem_usage',
'cpu': 'cpu_load', 'cpu': 'cpu_load',
'cpus': 'cpu_load_since',
'temp': 'cpu_temp', 'temp': 'cpu_temp',
'freq': 'cpu_freq' 'freq': 'cpu_freq'
} }
@ -50,6 +51,7 @@ class MemTemp(plugins.Plugin):
FIELD_WIDTH = 4 FIELD_WIDTH = 4
def on_loaded(self): def on_loaded(self):
self._last_cpu_load = self._cpu_stat()
logging.info("memtemp plugin loaded.") logging.info("memtemp plugin loaded.")
def mem_usage(self): def mem_usage(self):
@ -58,6 +60,28 @@ class MemTemp(plugins.Plugin):
def cpu_load(self): def cpu_load(self):
return f"{int(pwnagotchi.cpu_load() * 100)}%" 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): def cpu_temp(self):
if self.options['scale'] == "fahrenheit": if self.options['scale'] == "fahrenheit":
temp = (pwnagotchi.temperature() * 9 / 5) + 32 temp = (pwnagotchi.temperature() * 9 / 5) + 32