mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
Merge pull request #2967 from aznohonza/tools/cgroup-memory-peak
game_run_summary: record the run cgroup's memory peak
This commit is contained in:
@ -111,8 +111,10 @@ What it does:
|
||||
with a local change that leaves this trace off (`LEONIDA_NO_TRACE_D3D12=1`); `main`'s script always passes
|
||||
`--trace-d3d12`.
|
||||
- **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
|
||||
|
||||
@ -245,6 +245,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.
|
||||
@ -325,6 +327,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
|
||||
|
||||
@ -53,7 +53,8 @@ STARTUP = [
|
||||
|
||||
|
||||
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 @@ class GameRunSummaryTest(unittest.TestCase):
|
||||
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,15 @@ class GameRunSummaryTest(unittest.TestCase):
|
||||
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 (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"])
|
||||
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)
|
||||
|
||||
@ -194,6 +194,29 @@ class RunGameWineTest(unittest.TestCase):
|
||||
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",
|
||||
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 (the run's scope, else the caller cgroup's lifetime peak)", 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)
|
||||
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)
|
||||
|
||||
@ -6,12 +6,15 @@ Reads, in the run folder:
|
||||
|
||||
* `stderr.log`: the `[d3d12-trace] #<seq> <method> <outcome>` 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,9 @@ 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 "
|
||||
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'])})"
|
||||
|
||||
Reference in New Issue
Block a user