From d9c470cd308c5ffb94b88d1c5f6847da68eea394 Mon Sep 17 00:00:00 2001 From: aznohonza Date: Tue, 1 Dec 2026 08:25:46 +0100 Subject: [PATCH 1/3] game_run_summary: record the run cgroup's memory peak --- docs/linux_proton.md | 6 ++++-- scripts/run_game_wine.sh | 9 +++++++++ tests/test_game_run_summary.py | 13 +++++++++++-- tests/test_run_game_wine.py | 25 +++++++++++++++++++++++-- tools/game_run_summary.py | 15 +++++++++++---- 5 files changed, 58 insertions(+), 10 deletions(-) diff --git a/docs/linux_proton.md b/docs/linux_proton.md index 61627b69a9..34a6682632 100644 --- a/docs/linux_proton.md +++ b/docs/linux_proton.md @@ -101,8 +101,10 @@ What it does: folder. Every other `LEONIDA_*` variable reaches the runtime unchanged, so `LEONIDA_INPUT_SCRIPT=a@210,a@240` presses A as above. Arguments after `--` go to `gta6rt`. - **Summary:** `tools/game_run_summary.py` prints the run time, how it ended, exceptions by code, D3D12 calls by - outcome, presents, the most frequent failing calls and the `[rt]` lines by category with numbers masked. It never - prints a log line. `--screenshot-every S` captures the game window into the run folder (ImageMagick's `import` and + outcome, presents, the most frequent failing calls, the cgroup's peak memory and the `[rt]` lines by category + with numbers masked. The peak is `memory.peak` (cgroup v2, kernel 5.19+): the run's own systemd scope when it is + started in one (`systemd-run --user --scope`), else the caller's cgroup since it was created. It never prints a + log line. `--screenshot-every S` captures the game window into the run folder (ImageMagick's `import` and `xprop`). The run folder's `stderr.log`, `boot.jsonl` and screenshots can show or quote what the title prints, so they stay on diff --git a/scripts/run_game_wine.sh b/scripts/run_game_wine.sh index cc60d67670..b4b5b3cc65 100755 --- a/scripts/run_game_wine.sh +++ b/scripts/run_game_wine.sh @@ -225,6 +225,8 @@ else fi export QT_QPA_PLATFORM=offscreen DRM_SYSFS=${LEONIDA_DRM_SYSFS_DIR:-/sys/class/drm} +CGROUP_SELF=${LEONIDA_SELF_CGROUP_PATH:-/proc/self/cgroup} +CGROUP_ROOT=${LEONIDA_CGROUP_ROOT:-/sys/fs/cgroup} if [ -n "$DEVICE" ]; then export MESA_VK_DEVICE_SELECT="$DEVICE!" # kwin and Xwayland's glamor on the card that renders, so the present path stays on one device. @@ -305,6 +307,13 @@ wait "$WATCH" 2>/dev/null || true teardown; SESSION_PID= T1=$(date +%s) +# The cgroup's peak memory (cgroup v2, kernel 5.19+): the run's own systemd scope when it has one (the unit +# also carries MemoryMax), else the caller's cgroup since it was created. Absent on cgroup v1: no line then. +cg_rel=$(sed -n 's/^0:://p' "$CGROUP_SELF" 2>/dev/null || true) +if [ -n "$cg_rel" ] && [ -r "$CGROUP_ROOT$cg_rel/memory.peak" ]; then + cat "$CGROUP_ROOT$cg_rel/memory.peak" > "$RUN/memory_peak" 2>/dev/null || rm -f "$RUN/memory_peak" +fi + echo "== $(tr '\n' ';' < "$RUN/session.txt" 2>/dev/null | sed 's/;$//')" if [ ! -f "$RUN/exit_code" ]; then echo "the run did not start or the session ended early ($((T1 - T0)) s); see $RUN/session.log" >&2 diff --git a/tests/test_game_run_summary.py b/tests/test_game_run_summary.py index 6705f1bb83..ca18b7d177 100644 --- a/tests/test_game_run_summary.py +++ b/tests/test_game_run_summary.py @@ -53,7 +53,8 @@ class GameRunSummaryTest(unittest.TestCase): - def run_folder(self, stderr=STDERR, startup=STARTUP, exit_code=None, seconds=None, tail=""): + def run_folder(self, stderr=STDERR, startup=STARTUP, exit_code=None, seconds=None, memory_peak=None, + tail=""): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) run = tmp.name @@ -61,7 +62,7 @@ def run_folder(self, stderr=STDERR, startup=STARTUP, exit_code=None, seconds=Non f.write("\n".join(stderr) + "\n") with open(os.path.join(run, "boot.jsonl"), "w", encoding="utf-8", newline="\n") as f: f.write("".join(json.dumps(e) + "\n" for e in startup) + tail) - for name, value in (("exit_code", exit_code), ("seconds", seconds)): + for name, value in (("exit_code", exit_code), ("seconds", seconds), ("memory_peak", memory_peak)): if value is not None: with open(os.path.join(run, name), "w", encoding="utf-8", newline="\n") as f: f.write(f"{value}\n") @@ -91,6 +92,14 @@ def test_counts(self): self.assertEqual(s["startup_events"], {"exception": 2, "trace_open": 1, "debug_string": 1, "file_open_failed": 1}) + def test_memory_peak_is_printed_in_gib_and_absent_is_no_line(self): + run = self.run_folder(exit_code=0, memory_peak=25316786176) + self.assertEqual(summary.summarize(run)["memory_peak_bytes"], 25316786176) + self.assertIn("cgroup memory peak: 23.6 GiB", self.printed(run)) + run = self.run_folder(exit_code=0) + self.assertIsNone(summary.summarize(run)["memory_peak_bytes"]) + self.assertNotIn("memory peak", self.printed(run)) + def test_categories_mask_numbers_and_hide_free_text(self): s = summary.summarize(self.run_folder()) self.assertEqual(s["rt_lines"], 7) diff --git a/tests/test_run_game_wine.py b/tests/test_run_game_wine.py index 7dd976ffce..ce78da8c73 100644 --- a/tests/test_run_game_wine.py +++ b/tests/test_run_game_wine.py @@ -85,8 +85,8 @@ def inputs(self, **drop): flat += [flag, str(value)] return flat - def run_script(self, *args, display=":99"): - env = {"PATH": str(self.bin), "HOME": str(self.tmp), "STUB_LOG": str(self.log)} + def run_script(self, *args, display=":99", env=None): + env = {"PATH": str(self.bin), "HOME": str(self.tmp), "STUB_LOG": str(self.log), **(env or {})} if display: env["DISPLAY"] = display return subprocess.run([str(self.bin / "bash"), str(SCRIPT), *args], cwd=self.tmp, env=env, capture_output=True, text=True, @@ -193,6 +193,27 @@ def test_a_visible_run_against_stubs(self): self.assertEqual(p.returncode, 2) self.assertIn("already holds a run", p.stderr) + def test_records_the_cgroup_memory_peak(self): + proc = self.tmp / "cgroup" + proc.write_text("0::/test.scope\n", encoding="utf-8", newline="\n") + root = self.tmp / "sys_cgroup" + (root / "test.scope").mkdir(parents=True) + (root / "test.scope" / "memory.peak").write_text("25316786176\n", encoding="utf-8", newline="\n") + p = self.run_script(*self.inputs(), "--visible", "--seconds", "30", + env={"LEONIDA_SELF_CGROUP_PATH": str(proc), "LEONIDA_CGROUP_ROOT": str(root)}) + self.assertEqual(p.returncode, 0, p.stdout + p.stderr) + self.assertEqual((self.out / "memory_peak").read_text(encoding="utf-8").strip(), "25316786176") + self.assertIn("cgroup memory peak: 23.6 GiB", p.stdout) + + def test_no_cgroup_v2_peak_is_not_an_error(self): + proc = self.tmp / "cgroup" + proc.write_text("12:memory:/test.scope\n", encoding="utf-8", newline="\n") + p = self.run_script(*self.inputs(), "--visible", "--seconds", "30", + env={"LEONIDA_SELF_CGROUP_PATH": str(proc), "LEONIDA_CGROUP_ROOT": str(self.tmp)}) + self.assertEqual(p.returncode, 0, p.stdout + p.stderr) + self.assertFalse((self.out / "memory_peak").exists()) + self.assertNotIn("memory peak", p.stdout) + def test_without_a_device_it_warns(self): p = self.run_script(*self.inputs(), "--visible", "--seconds", "30") self.assertEqual(p.returncode, 0, p.stdout + p.stderr) diff --git a/tools/game_run_summary.py b/tools/game_run_summary.py index f99073884e..a97396c82c 100644 --- a/tools/game_run_summary.py +++ b/tools/game_run_summary.py @@ -6,12 +6,15 @@ * `stderr.log`: the `[d3d12-trace] # ` lines (`--trace-d3d12`) and the `[rt] ...` lines; * `boot.jsonl`: the startup trace (`--trace`, runtime/README.md "Startup trace"); -* `exit_code` and `seconds`: written by the launcher (the process exit code and the run time). +* `exit_code` and `seconds`: written by the launcher (the process exit code and the run time); +* `memory_peak`: the run cgroup's `memory.peak` in bytes, when the launcher found it (cgroup v2, kernel 5.19+): + the peak of the run's own systemd scope when it has one, else of the caller's cgroup. Prints the run time, how the run ended, the exceptions by code, the startup trace events by kind, the D3D12 calls by -outcome, the presents, the most frequent failing calls by method name, whether the runtime served its silent audio -endpoint (`LEONIDA_AUDIO=silent`), and the `[rt]` lines counted by category: the text before the line's first colon, -with every number masked as `#` (`slot 31` counts as `slot #`; a name such as `d3d12` stays) and anything that is not a short list of plain words counted as `(other)`. +outcome, the presents, the most frequent failing calls by method name, the cgroup's peak memory, whether the runtime +served its silent audio endpoint (`LEONIDA_AUDIO=silent`), and the `[rt]` lines counted by category: the text before +the line's first colon, with every number masked as `#` (`slot 31` counts as `slot #`; a name such as `d3d12` stays) +and anything that is not a short list of plain words counted as `(other)`. It never prints a log line. The logs and traces in the run folder can quote what the title prints (debug strings, file paths, the title's own messages), so they stay on the machine that made them: share this summary in issues and @@ -135,6 +138,7 @@ def summarize(run: str, limit_seconds: int | None = None, top: int = 12) -> dict code = _read_int(os.path.join(run, "exit_code")) seconds = _read_int(os.path.join(run, "seconds")) + memory_peak = _read_int(os.path.join(run, "memory_peak")) # GNU timeout also forwards a child's exit status and can report a SIGKILL before its deadline. finished_before_limit = limit_seconds is not None and seconds is not None and 0 <= seconds < limit_seconds if code is None: @@ -151,6 +155,7 @@ def summarize(run: str, limit_seconds: int | None = None, top: int = 12) -> dict return { "seconds": seconds, "exit_code": code, + "memory_peak_bytes": memory_peak, "end": end, "exceptions": sum(exception_codes.values()), "exception_codes": dict(exception_codes.most_common()), @@ -177,6 +182,8 @@ def _pairs(counts, sep=" "): def render(s: dict, top: int = 12) -> str: secs = "unknown" if s["seconds"] is None else f"{s['seconds']} s" out = [f"run time: {secs}; end: {s['end']}"] + if s["memory_peak_bytes"] is not None: + out.append(f"cgroup memory peak: {s['memory_peak_bytes'] / 2**30:.1f} GiB") exc = f"exceptions traced: {s['exceptions']}" if s["exception_codes"]: exc += f" (by code: {_pairs(s['exception_codes'])})" From ace83ae110db8fadcb7e8d00d9e27a829cc47247 Mon Sep 17 00:00:00 2001 From: aznohonza Date: Tue, 1 Dec 2026 17:56:00 +0100 Subject: [PATCH 2/3] game_run_summary: say whose peak the line is, and make the v2 path guard bite KelboLive's review of #2845: without a systemd scope the peak is the caller cgroup's lifetime peak, so the one-line summary now says so, which keeps a comparison table from reading it as the run's own. The `[ -n "$cg_rel" ]` guard was untested: removing it still passed, because the fake root had no memory.peak to pick up. The v1 test now has one at the root, so an empty v2 path reads nothing and dropping the guard fails it (checked: mutation gives 1 failure). AI-assisted: yes --- tests/test_game_run_summary.py | 3 ++- tests/test_run_game_wine.py | 4 +++- tools/game_run_summary.py | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_game_run_summary.py b/tests/test_game_run_summary.py index ca18b7d177..567a8711e1 100644 --- a/tests/test_game_run_summary.py +++ b/tests/test_game_run_summary.py @@ -95,7 +95,8 @@ def test_counts(self): def test_memory_peak_is_printed_in_gib_and_absent_is_no_line(self): run = self.run_folder(exit_code=0, memory_peak=25316786176) self.assertEqual(summary.summarize(run)["memory_peak_bytes"], 25316786176) - self.assertIn("cgroup memory peak: 23.6 GiB", self.printed(run)) + self.assertIn("cgroup memory peak: 23.6 GiB (the run's scope, else the caller's cgroup)", + self.printed(run)) run = self.run_folder(exit_code=0) self.assertIsNone(summary.summarize(run)["memory_peak_bytes"]) self.assertNotIn("memory peak", self.printed(run)) diff --git a/tests/test_run_game_wine.py b/tests/test_run_game_wine.py index 5a3cc28d79..513adee553 100644 --- a/tests/test_run_game_wine.py +++ b/tests/test_run_game_wine.py @@ -204,11 +204,13 @@ def test_records_the_cgroup_memory_peak(self): LEONIDA_SELF_CGROUP_PATH=str(proc), LEONIDA_CGROUP_ROOT=str(root)) self.assertEqual(p.returncode, 0, p.stdout + p.stderr) self.assertEqual((self.out / "memory_peak").read_text(encoding="utf-8").strip(), "25316786176") - self.assertIn("cgroup memory peak: 23.6 GiB", p.stdout) + self.assertIn("cgroup memory peak: 23.6 GiB (the run's scope, else the caller's cgroup)", p.stdout) def test_no_cgroup_v2_peak_is_not_an_error(self): proc = self.tmp / "cgroup" proc.write_text("12:memory:/test.scope\n", encoding="utf-8", newline="\n") + # A peak at the root itself: an empty v2 path must not read it (the cgroup file has no "0::" line). + (self.tmp / "memory.peak").write_text("999\n", encoding="utf-8", newline="\n") p = self.run_script(*self.inputs(), "--visible", "--seconds", "30", LEONIDA_SELF_CGROUP_PATH=str(proc), LEONIDA_CGROUP_ROOT=str(self.tmp)) self.assertEqual(p.returncode, 0, p.stdout + p.stderr) diff --git a/tools/game_run_summary.py b/tools/game_run_summary.py index a97396c82c..cd0080e7d4 100644 --- a/tools/game_run_summary.py +++ b/tools/game_run_summary.py @@ -183,7 +183,8 @@ def render(s: dict, top: int = 12) -> str: secs = "unknown" if s["seconds"] is None else f"{s['seconds']} s" out = [f"run time: {secs}; end: {s['end']}"] if s["memory_peak_bytes"] is not None: - out.append(f"cgroup memory peak: {s['memory_peak_bytes'] / 2**30:.1f} GiB") + out.append(f"cgroup memory peak: {s['memory_peak_bytes'] / 2**30:.1f} GiB " + f"(the run's scope, else the caller's cgroup)") exc = f"exceptions traced: {s['exceptions']}" if s["exception_codes"]: exc += f" (by code: {_pairs(s['exception_codes'])})" From cb661b0f0d31b4d59105ff1730548de1ede8814b Mon Sep 17 00:00:00 2001 From: aznohonza Date: Tue, 1 Dec 2026 23:28:45 +0100 Subject: [PATCH 3/3] game_run_summary: the caller-cgroup peak is a lifetime peak Without a systemd scope the saved value is the caller cgroup's lifetime peak, not the run's; the summary line says so now. AI-assisted. --- tests/test_game_run_summary.py | 2 +- tools/game_run_summary.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_game_run_summary.py b/tests/test_game_run_summary.py index 567a8711e1..937c7966fa 100644 --- a/tests/test_game_run_summary.py +++ b/tests/test_game_run_summary.py @@ -95,7 +95,7 @@ def test_counts(self): def test_memory_peak_is_printed_in_gib_and_absent_is_no_line(self): run = self.run_folder(exit_code=0, memory_peak=25316786176) self.assertEqual(summary.summarize(run)["memory_peak_bytes"], 25316786176) - self.assertIn("cgroup memory peak: 23.6 GiB (the run's scope, else the caller's cgroup)", + self.assertIn("cgroup memory peak: 23.6 GiB (the run's scope, else the caller cgroup's lifetime peak)", self.printed(run)) run = self.run_folder(exit_code=0) self.assertIsNone(summary.summarize(run)["memory_peak_bytes"]) diff --git a/tools/game_run_summary.py b/tools/game_run_summary.py index cd0080e7d4..9832d00f78 100644 --- a/tools/game_run_summary.py +++ b/tools/game_run_summary.py @@ -184,7 +184,7 @@ def render(s: dict, top: int = 12) -> str: out = [f"run time: {secs}; end: {s['end']}"] if s["memory_peak_bytes"] is not None: out.append(f"cgroup memory peak: {s['memory_peak_bytes'] / 2**30:.1f} GiB " - f"(the run's scope, else the caller's cgroup)") + f"(the run's scope, else the caller cgroup's lifetime peak)") exc = f"exceptions traced: {s['exceptions']}" if s["exception_codes"]: exc += f" (by code: {_pairs(s['exception_codes'])})"