testsAndMisc/linux_configuration/tests/test_usage_report_pmon_names.py
Krzysztof kuhy Rudnicki 20d5d1f89b fix(usage_report): stop charging atop's HZ field as CPU; bundle since-last-report mode
atop's `-P PRC` output inserts the clock-tick rate (HZ=100) between the
`state` and `utime` columns. Both the Python parser and the native C
aggregator read that constant as utime for every record, charging a flat
1 CPU-second per record — so cpu_seconds collapsed to pid_count and
short-lived fork-storm commands (xset, dd, chronyc) topped the CPU table
(xset showed 67h). The old test fixtures lacked the HZ field, so code and
tests agreed on the bug.

- _parse_prc / atop_agg.c: read utime/stime past the HZ field (after+2/+3,
  tokens[10]/[11]); bump the length guards accordingly
- restore C/atop_agg (deleted in 89b4f59) under linux_configuration/C/,
  where the build path resolves; corrected test fixtures to include HZ
- _atop_agg_binary: fall back to the Python parser when the C source tree
  is gone instead of trusting an orphaned cached binary
- add regression tests proving HZ is not summed as CPU
- bundle the in-progress since-last-report multi-day aggregation (segments,
  -b/-e bounding, persisted state, window merging) and its tests/conftest
- meta: gate linux_configuration/tests in pytest_changed_packages.py

Verified by running usage_report.py --date 20260604: Top CPU now led by
SkyrimSE; xset/dd/chronyc fall to ~0. C unit tests + full pytest suite green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-04 18:13:47 +02:00

77 lines
1.9 KiB
Python

"""Regression tests for pmon process-name normalization in usage_report."""
from __future__ import annotations
from typing import TYPE_CHECKING
import _usage_report_parsing as parsing
if TYPE_CHECKING:
import pytest
def test_normalize_pmon_command_prefers_first_executable_token() -> None:
"""The parser should keep executable-like token, not trailing args."""
tokens = ["code-insiders", "--type=", "gpu-process", "Not"]
assert parsing._normalize_pmon_command(tokens) == "code-insiders"
def test_normalize_pmon_command_skips_leading_option_tokens() -> None:
"""If the first token is an option, use the next non-option token."""
tokens = ["--type=", "code-insiders", "--flag"]
assert parsing._normalize_pmon_command(tokens) == "code-insiders"
def test_ingest_pmon_row_uses_command_field_start_not_last_token() -> None:
"""Rows with command args should aggregate under process name, not args."""
row = [
"20260507",
"12:00:00",
"0",
"123",
"C",
"10",
"5",
"0",
"0",
"0",
"0",
"code-insiders",
"--type=",
"gpu-process",
]
agg: dict[str, object] = {}
consumed = parsing._ingest_pmon_row(row, agg)
assert consumed == 1
assert "code-insiders" in agg
def test_ingest_pmon_row_falls_back_to_proc_comm_on_unknown(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""When command field is empty, parser can recover with /proc/<pid>/comm."""
row = [
"20260507",
"12:00:00",
"0",
"999",
"C",
"30",
"10",
"0",
"0",
"0",
"0",
]
agg: dict[str, object] = {}
monkeypatch.setattr(parsing, "_pid_comm_name", lambda _pid: "python")
consumed = parsing._ingest_pmon_row(row, agg)
assert consumed == 1
assert "python" in agg