fix(environment): CPU usage from two reads a second apart
get_environment promised two samples one second apart but read /proc/stat once, so the figure was the average since boot: a box at 100 % for the last minute showed its lifetime average. It now reads the cpu line twice, a second apart, and uses the difference. Closes #10
This commit is contained in:
1 parent
c211ba0d9e
commit
097d9b9581
3 files changed
+96
-11
No files matched your search
@@ -24,6 +24,9 @@ recorded here; their history is the git log.
|
||||
as a success (#3).
|
||||
- `apply_updates()` reports a failed upgrade as failed, with the package manager's
|
||||
exit status in `error`; it used to report every upgrade as done (#11).
|
||||
- `get_environment()` reports the CPU usage of the last second, from two reads of
|
||||
`/proc/stat` one second apart; it used to report the average since boot, so a box
|
||||
busy for the last minute could show a low figure (#10).
|
||||
|
||||
## [0.7.0] - 2026-10-23
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@ _RC_MARKER = "__OWRT_RC="
|
||||
_RC_RE = re.compile(rf"^{_RC_MARKER}(\d+)\s*$", re.MULTILINE)
|
||||
#: What apk prints when its index is not in RAM (after a reboot).
|
||||
_APK_INDEX_MISSING = re.compile(r"opening from cache .*No such file or directory")
|
||||
#: The aggregate CPU line of ``/proc/stat``, read twice one second apart.
|
||||
_CPU_SAMPLES = "grep '^cpu ' /proc/stat; sleep 1; grep '^cpu ' /proc/stat"
|
||||
|
||||
|
||||
def _split_status(raw: str) -> tuple[str, int | None]:
|
||||
@@ -45,6 +47,36 @@ def _status_result(raw: str, command: str) -> dict[str, Any]:
|
||||
return {"success": True, "output": output}
|
||||
|
||||
|
||||
def _cpu_counters(line: str) -> tuple[int, int]:
|
||||
"""``(idle, total)`` jiffies of a ``cpu`` line.
|
||||
|
||||
The total counts user, nice, system, idle, iowait, irq and softirq.
|
||||
"""
|
||||
fields = [int(x) for x in line.split()[1:8]]
|
||||
if len(fields) < 7:
|
||||
raise ValueError(f"not a cpu line: {line!r}")
|
||||
return fields[3], sum(fields)
|
||||
|
||||
|
||||
def _cpu_usage_percent(output: str) -> float:
|
||||
"""Busy share of the time between the two ``cpu`` lines of :data:`_CPU_SAMPLES`.
|
||||
|
||||
0.0 when there are not two readable lines or the counters did not move.
|
||||
"""
|
||||
lines = [line for line in output.splitlines() if line.startswith("cpu ")]
|
||||
if len(lines) < 2:
|
||||
return 0.0
|
||||
try:
|
||||
idle1, total1 = _cpu_counters(lines[0])
|
||||
idle2, total2 = _cpu_counters(lines[1])
|
||||
except ValueError:
|
||||
return 0.0
|
||||
elapsed = total2 - total1
|
||||
if elapsed <= 0:
|
||||
return 0.0
|
||||
return (1 - (idle2 - idle1) / elapsed) * 100
|
||||
|
||||
|
||||
class OpenWrtSystemMixin:
|
||||
"""Mixin providing system-level NAPALM getters.
|
||||
|
||||
@@ -54,21 +86,13 @@ class OpenWrtSystemMixin:
|
||||
def get_environment(self) -> dict[str, Any]:
|
||||
"""Return device environment data (CPU, memory).
|
||||
|
||||
CPU usage from ``/proc/stat`` (two samples 1 second apart via ``awk``).
|
||||
CPU usage from ``/proc/stat``: the difference of two reads one second
|
||||
apart, so it is the last second, not the average since boot.
|
||||
Memory from ``/proc/meminfo``.
|
||||
"""
|
||||
cpu_out = self._send_command(
|
||||
"awk '/^cpu /{idle1=$5; total1=$2+$3+$4+$5+$6+$7+$8} "
|
||||
"END{print (1-(idle1/total1))*100}' /proc/stat"
|
||||
)
|
||||
cpu_pct = _cpu_usage_percent(self._send_command(_CPU_SAMPLES))
|
||||
mem_out = self._send_command("cat /proc/meminfo")
|
||||
|
||||
cpu_pct = 0.0
|
||||
try:
|
||||
cpu_pct = float(cpu_out.strip())
|
||||
except (ValueError, AttributeError):
|
||||
pass
|
||||
|
||||
mem_total = 0
|
||||
mem_available = 0
|
||||
for line in mem_out.splitlines():
|
||||
|
||||
@@ -2005,3 +2005,61 @@ class TestManageService:
|
||||
with pytest.raises(ValueError):
|
||||
driver.manage_service("x; reboot", "start")
|
||||
assert sent == []
|
||||
|
||||
|
||||
# Busy 15 % on average since boot, 90 % in the second between the two reads.
|
||||
PROC_STAT_CPU_TWICE = """\
|
||||
cpu 1000 0 500 8500 0 0 0 0 0 0
|
||||
cpu 1090 0 500 8510 0 0 0 0 0 0
|
||||
"""
|
||||
|
||||
MEMINFO = """\
|
||||
MemTotal: 124608 kB
|
||||
MemFree: 43204 kB
|
||||
MemAvailable: 62000 kB
|
||||
"""
|
||||
|
||||
|
||||
class TestGetEnvironmentCpu:
|
||||
"""CPU usage is the second between two reads, not the average since boot (#10)."""
|
||||
|
||||
def _answer(self, driver, cpu: str) -> list:
|
||||
sent: list = []
|
||||
|
||||
def send(cmd, **kw):
|
||||
sent.append(cmd)
|
||||
return MEMINFO if "meminfo" in cmd else cpu
|
||||
|
||||
driver._send_command = send
|
||||
return sent
|
||||
|
||||
def test_usage_is_the_difference_of_two_reads(self, driver):
|
||||
self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
assert driver.get_environment()["cpu"] == {0: {"%usage": 90.0}}
|
||||
|
||||
def test_the_two_reads_are_a_second_apart(self, driver):
|
||||
sent = self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
driver.get_environment()
|
||||
|
||||
command = next(cmd for cmd in sent if "/proc/stat" in cmd)
|
||||
assert command.count("/proc/stat") == 2
|
||||
assert "sleep 1" in command
|
||||
|
||||
def test_counters_that_did_not_move_are_zero(self, driver):
|
||||
self._answer(driver, "cpu 1000 0 500 8500 0 0 0\ncpu 1000 0 500 8500 0 0 0\n")
|
||||
|
||||
assert driver.get_environment()["cpu"][0]["%usage"] == 0.0
|
||||
|
||||
def test_a_single_read_is_not_taken_for_usage(self, driver):
|
||||
self._answer(driver, "cpu 1000 0 500 8500 0 0 0\n")
|
||||
|
||||
assert driver.get_environment()["cpu"][0]["%usage"] == 0.0
|
||||
|
||||
def test_memory_is_still_read(self, driver):
|
||||
self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
memory = driver.get_environment()["memory"]
|
||||
|
||||
assert memory == {"available_ram": 62000 * 1024, "used_ram": (124608 - 62000) * 1024}
|
||||
Reference in new issue
Block a user