Change cpu_load to take a tag, and compute over interval since last call with same tag, instead of a 0.1s sampling

This commit is contained in:
Sniffleupagus
2023-05-19 10:58:20 -07:00
parent c90855840c
commit 3f9e78af73

View File

@ -9,7 +9,7 @@ from pwnagotchi._version import __version__
_name = None
config = None
_cpu_stats = {}
def set_name(new_name):
if new_name is None:
@ -83,13 +83,18 @@ def _cpu_stat():
return list(map(int,fp.readline().split()[1:]))
def cpu_load():
def cpu_load(tag=None):
"""
Returns the current cpuload
"""
if tag and tag in _cpu_stats.keys():
parts0 = _cpu_stats[tag]
else:
parts0 = _cpu_stat()
time.sleep(0.1)
parts1 = _cpu_stat()
if tag: _cpu_stats[tag] = parts1
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