#!/usr/bin/env python3 """R-1338 (2026-10-28 night): the update test's memory figure counts only the memory the app still holds. Measured on the bench (felhom.eu audits/night-2026-10-28/r1338/): Wanderer's Meilisearch read `anon` 382 MB of its 384 MB limit for ten minutes, while 297 MB sat on the inactive FILE list with only 1 MB of real file cache — pages the allocator gave back (MADV_FREE), which the kernel reclaims first (0 kills). The mark read 95–96 % on every venue; the live figure is ~86 MB (22 %). These tests use those numbers. Run: python3 test_memory_live.py (pure Python; no Docker).""" import importlib.util import os import sys import unittest HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) from memfigure import anon_live # noqa: E402 MB = 1000 * 1000 def _load_upgrade_test(): spec = importlib.util.spec_from_file_location("upgrade_test_mod", os.path.join(HERE, "upgrade-test.py")) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod class AnonLive(unittest.TestCase): def test_the_measured_spike(self): # The cap run's sample at +112 s (bench, MB = 10^6 B): estimate (a) 86 MB, the anon lists (b) 99 MB → 99 MB. # RED-PROOF: return stat["anon"] → 382 MB → FAILS. self.assertEqual(anon_live({"anon": 382 * MB, "file": 1 * MB, "active_file": 0, "inactive_file": 297 * MB, "active_anon": 99 * MB, "inactive_anon": 0, "shmem": 0}), 99 * MB) # without the anon-list fields: estimate (a) alone self.assertEqual(anon_live({"anon": 382 * MB, "file": 1 * MB, "active_file": 0, "inactive_file": 297 * MB}), 86 * MB) def test_it_errs_high_never_above_anon(self): # RED-PROOF: drop the max(a, b) → 86 MB → FAILS; drop the cap → 69 MB above anon's 59 → FAILS. self.assertEqual(anon_live({"anon": 300 * MB, "file": 0, "active_file": 0, "inactive_file": 214 * MB, "active_anon": 100 * MB, "inactive_anon": 0, "shmem": 0}), 100 * MB) self.assertEqual(anon_live({"anon": 59 * MB, "file": 1 * MB, "active_file": 0, "inactive_file": 0, "active_anon": 69 * MB, "inactive_anon": 0, "shmem": 0}), 59 * MB) def test_before_the_spike_nothing_is_given_back(self): self.assertEqual(anon_live({"anon": 59 * MB, "file": 1 * MB, "active_file": 0, "inactive_file": 0}), 59 * MB) def test_real_file_cache_is_not_subtracted(self): # a page cache of 200 MB on the file lists (file == the lists): nothing is given-back anon self.assertEqual(anon_live({"anon": 300 * MB, "file": 200 * MB, "active_file": 120 * MB, "inactive_file": 80 * MB}), 300 * MB) def test_missing_anon_is_unknown(self): self.assertIsNone(anon_live({"file": 1})) class SoakSummary(unittest.TestCase): def test_the_mark_reads_the_live_figure(self): os.environ.setdefault("TARGET", "9202") import box_walk lim = 402653184 samples = [ {"c": "wanderer-search", "anon": 59 * MB, "file": 1 * MB, "active_file": 0, "inactive_file": 0, "max": str(lim), "peak": 76 * MB, "oom_kill": 0, "restarts": 0, "running": "true", "started": "s", "oomkilled": "false", "cg": "x"}, {"c": "wanderer-search", "anon": 382 * MB, "file": 1 * MB, "active_file": 0, "inactive_file": 297 * MB, "active_anon": 99 * MB, "inactive_anon": 0, "shmem": 0, "max": str(lim), "peak": lim, "oom_kill": 0, "restarts": 0, "running": "true", "started": "s", "oomkilled": "false", "cg": "x"}, ] r = box_walk.soak_summary(samples, 20, 10) c = r["containers"]["wanderer-search"] # RED-PROOF: let peaks take anon_peak_pct again → peak_pct 94.9 → FAILS. self.assertEqual(r["peak_pct"], round(100.0 * 99 * MB / lim, 1)) self.assertEqual(c["anon_peak_pct"], round(100.0 * 382 * MB / lim, 1)) self.assertEqual(r["peak_basis"], "anon_live") class TightMark(unittest.TestCase): def test_the_bench_mark_prefers_the_live_figure(self): ut = _load_upgrade_test() per = {"search": {"anon_live_peak_pct": 0.224, "anon_peak_pct": 0.995, "peak_pct": 1.0}, "old": {"anon_peak_pct": 0.95, "peak_pct": 1.0}, # a record without the live figure: decision 22 stands "small": {"anon_live_peak_pct": 0.10, "anon_peak_pct": 0.12, "peak_pct": 0.5}} # RED-PROOF: look up anon_peak_pct before anon_live_peak_pct → "search" is tight → FAILS. self.assertEqual(ut.tight_containers(per, 0.80), ["old"]) if __name__ == "__main__": unittest.main()