From 7c8c36678f92caf211de7d6b96cbba244a15932c Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:39:23 -0500 Subject: [PATCH] test(steam): device benchmark harness and charts for the Steam Lite experiment --- tools/steam_lite_bench.py | 794 +++++++++++++++++++++++++++ tools/steam_lite_charts.py | 457 +++++++++++++++ tools/tests/test_steam_lite_bench.py | 54 ++ 3 files changed, 1305 insertions(+) create mode 100644 tools/steam_lite_bench.py create mode 100644 tools/steam_lite_charts.py create mode 100644 tools/tests/test_steam_lite_bench.py diff --git a/tools/steam_lite_bench.py b/tools/steam_lite_bench.py new file mode 100644 index 0000000000..08639c8fdc --- /dev/null +++ b/tools/steam_lite_bench.py @@ -0,0 +1,794 @@ +#!/usr/bin/env python3 +"""Benchmark the Steam Lite Runtime profiles on a device (docs/development/steam-lite-runtime.md). + + tools/steam_lite_bench.py info --out runs/thor + tools/steam_lite_bench.py cycle --app 1234 --profiles stock,1,2,3 --repeat 3 --out runs/thor + tools/steam_lite_bench.py report runs/thor + +Needs a DroidDeck build with debugging tools on, a Steam session started with DD_STEAM_LITE set +(any level: the profile under test is switched at runtime through agent/steam-lite.json), and +tools/droiddeckctl. Each cycle samples the client's process tree inside the guest (smaps_rollup +PSS and SwapPss, CPU ticks, MemAvailable, PSI) through idle, a game launch, the game in front, +an overlay round trip and the return to the library. +""" + +from __future__ import annotations + +import argparse +import base64 +import csv +import gzip +import json +import os +import random +import statistics +import subprocess +import sys +import time +from pathlib import Path + +TOOLS = Path(__file__).resolve().parent +CTL = TOOLS / "droiddeckctl" +PROFILES = { + "stock": {"level": "stock"}, + "1": {"level": "1"}, + "2": {"level": "2"}, + "3": {"level": "3"}, + "gc": {"level": "stock", "switches": "+gc"}, + "moderate": {"level": "stock", "switches": "+moderate"}, + "critical": {"level": "stock", "switches": "+critical"}, + "pause": {"level": "stock", "switches": "+pause"}, + "freeze": {"level": "stock", "switches": "+freeze"}, + "gc+critical": {"level": "stock", "switches": "+gc +critical"}, +} +WEB = ("browser", "renderer", "gpu-process", "zygote") +# The supervisor ("steam-lite") and the sampler measuring ("sampler") are not part of the client. +NOT_CLIENT = ("steam-lite", "sampler") +REMOTE = "/root/.cache/steam-lite-bench" +LITE = os.environ.get("DD_STEAM_LITE_BIN", "droiddeck-steam-lite") + + +class Device: + def __init__(self, serial: str | None): + self.serial = serial + + def ctl(self, *args: str, timeout: float = 120) -> dict: + cmd = [sys.executable, str(CTL)] + (["--serial", self.serial] if self.serial else []) + list(args) + done = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout + 60) + try: + body = json.loads(done.stdout or "{}") + except ValueError: + body = {"ok": False, "raw": done.stdout[-2000:]} + body.setdefault("exit", done.returncode) + if done.returncode and "error" not in body: + body["error"] = done.stderr[-2000:] + return body + + def guest(self, *argv: str, timeout: float = 60) -> dict: + return self.ctl("guest", "--timeout", str(timeout), "--", *argv, timeout=timeout) + + def sh(self, script: str, *args: str, timeout: float = 60) -> str: + reply = self.guest("sh", "-c", script, "sh", *args, timeout=timeout) + return (reply.get("stdout") or {}).get("text", "") + + def adb(self, *args: str, timeout: float = 30) -> str: + cmd = ["adb"] + (["-s", self.serial] if self.serial else []) + list(args) + return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout).stdout + + def profile(self, name: str) -> None: + body = json.dumps(PROFILES[name]) + self.sh('printf %s "$1" > "$DD_LAUNCH_DIR/agent/steam-lite.json"', body) + + def mark(self, text: str) -> None: + self.sh('printf %s "$1" > ' + REMOTE + "/mark", text) + + def status(self) -> dict: + try: + return json.loads(self.sh('cat "$DD_LAUNCH_DIR/agent/steam-lite-status.json"') or "{}") + except ValueError: + return {} + + def fetch(self, path: str) -> str: + text = self.sh("gzip -c \"$1\" | base64 -w0", path, timeout=120) + return gzip.decompress(base64.b64decode(text)).decode() if text else "" + + +def info(dev: Device, out: Path) -> dict: + props = {k: dev.adb("shell", "getprop", k).strip() for k in ( + "ro.product.model", "ro.product.manufacturer", "ro.soc.model", "ro.build.version.release", + "ro.build.fingerprint")} + mem = dev.adb("shell", "grep -E 'MemTotal|SwapTotal' /proc/meminfo") + package = dev.adb("shell", "dumpsys package com.droiddeck.launcher | grep -m2 -E 'versionName|versionCode'") + probe = dev.sh(LITE + " probe", timeout=60) + zram = dev.sh("cat /proc/swaps; ls /sys/block | grep zram; cat /sys/block/zram0/mm_stat 2>&1 | head -1;" + " cat /proc/pressure/memory 2>&1 | head -2; ls /sys/class/kgsl/kgsl/proc 2>&1 | head -3") + env = dev.sh("env | grep -E '^(DD_STEAM_LITE|DD_ZRAM|DD_WEBHELPER_ARGS|DD_STEAM_CLIENT_ARCH|DD_VKBRIDGE|" + "DD_STEAM_SOFT_COMPOSITING|DD_OPEN_DRIVER)=' | sort") + body = {"props": props, "meminfo": mem, "package": package, "env": env, "counters": zram} + try: + body["probe"] = json.loads(probe) + except ValueError: + body["probe"] = probe + out.mkdir(parents=True, exist_ok=True) + (out / "device.json").write_text(json.dumps(body, indent=1)) + return body + + +ABLATE = r""" +import json, subprocess, sys, time +lite, out, actions, waits = sys.argv[1], sys.argv[2], sys.argv[3].split(","), [float(w) for w in sys.argv[4].split(",")] +def run(*args): + return subprocess.run([lite] + list(args), capture_output=True, text=True).stdout.strip() +with open(out, "w") as stream: + for action in actions: + print(json.dumps({"label": "before " + action}), file=stream) + print(run("sample"), file=stream) + start = time.monotonic() + result = json.loads(run("apply", action) or "{}") + print(json.dumps({"label": "apply " + action, "result": result, + "ms": round((time.monotonic() - start) * 1000)}), file=stream) + for wait in waits: + time.sleep(max(0.0, start + wait - time.monotonic())) + print(json.dumps({"label": "after %s +%gs" % (action, wait)}), file=stream) + print(run("sample"), file=stream) + stream.flush() +""" + + +def ablate(dev: Device, actions: list[str], waits: list[float], out: Path, label: str) -> list[dict]: + """One action at a time: a sample before it, then one after each wait (seconds since the action).""" + total = len(actions) * (max(waits) + 20) + 30 + dev.sh("mkdir -p " + REMOTE) + reply = dev.ctl("guest", "--timeout", str(min(600, total)), "--stdin", ABLATE, "--", "python3", "-", LITE, + REMOTE + "/ablate.jsonl", ",".join(actions), ",".join(str(w) for w in waits), timeout=min(600, total)) + if reply.get("exitCode") != 0: + print(json.dumps({"guest": reply}), file=sys.stderr) + lines = [json.loads(x) for x in dev.fetch(REMOTE + "/ablate.jsonl").splitlines() if x.strip()] + out.mkdir(parents=True, exist_ok=True) + with open(out / (label + ".jsonl"), "w") as stream: + for item in lines: + print(json.dumps(item), file=stream) + rows, label_now, previous = [], None, None + for item in lines: + if "result" in item: + rows.append({"label": item["label"], "ms": item.get("ms"), "result": item["result"]}) + elif "label" in item and "processes" not in item: + label_now = item["label"] + elif "processes" in item: + by = {} + for r in item["processes"]: + if r["role"] not in NOT_CLIENT: + by[r["role"]] = by.get(r["role"], 0) + (r.get("Pss") or 0) + rows.append({"label": label_now, "total_mib": round(sum(by.values()) / 1024, 1), + **{k: round(v / 1024, 1) for k, v in sorted(by.items())}, + "swappss_mib": round(sum(r.get("SwapPss") or 0 for r in item["processes"]) / 1024, 1), + "gpu_mib": round(sum((item.get("gpu") or {}).values()) / 1048576, 1), + "avail_mib": round((item["memory"].get("MemAvailable") or 0) / 1024, 1), + "web_cpu_pct_since_last": round(cpu(previous, item, WEB) or 0, 1) if previous else None}) + previous = item + return rows + + +def wait_focus(dev: Device, want, timeout: float) -> float | None: + start = time.monotonic() + while time.monotonic() - start < timeout: + focus = dev.ctl("focus", timeout=40) + if want(focus.get("focusedApp")): + return time.monotonic() - start + time.sleep(0.5) + return None + + +def running(dev: Device) -> bool: + return bool((dev.ctl("state", timeout=40).get("session") or {}).get("running")) + + +def ensure_steam(dev: Device, fresh: bool = False) -> None: + if fresh and running(dev): + dev.ctl("stop", "--timeout", "90", timeout=100) + if not running(dev): + dev.adb("shell", "monkey -p com.droiddeck.launcher -c android.intent.category.LAUNCHER 1") + time.sleep(6) + dev.ctl("wake", timeout=30) + started = dev.ctl("start", "steam", "--wait", "--timeout", "300", timeout=320) + if not started.get("ok"): + raise RuntimeError("Steam did not start: %s" % json.dumps(started)[:400]) + time.sleep(5) + + +def last_exit(dev: Device) -> dict: + text = dev.adb("shell", "dumpsys activity exit-info com.droiddeck.launcher") + entry = {} + for line in text.splitlines(): + line = line.strip() + if line.startswith("timestamp=") and entry: + break + for part in line.split(" "): + key, sep, value = part.partition("=") + if sep and key in ("timestamp", "reason", "pss", "rss", "importance"): + entry.setdefault(key, value) + if line.startswith("timestamp="): + entry["timestamp"] = line.split("=", 1)[1].split(" pid=")[0] + return entry + + +def recorder(dev: Device, seconds: float, interval: float, name: str) -> subprocess.Popen: + return subprocess.Popen( + [sys.executable, str(CTL)] + (["--serial", dev.serial] if dev.serial else []) + + ["guest", "--timeout", str(seconds + 30), "--", LITE, "record", str(seconds), + str(interval), REMOTE + "/" + name, "--mark", REMOTE + "/mark"], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + +# A trial arm: the supervisor profile plus the session environment it needs (set before the session starts). +ARMS = { + "stock": ("stock", {"DD_STEAM_SOFT_COMPOSITING": "0"}), + "l1": ("1", {"DD_STEAM_SOFT_COMPOSITING": "0"}), + "l3": ("3", {"DD_STEAM_SOFT_COMPOSITING": "0"}), + "softcomp": ("stock", {"DD_STEAM_SOFT_COMPOSITING": "1"}), + "softcomp+l1": ("1", {"DD_STEAM_SOFT_COMPOSITING": "1"}), +} + + +def system_available(dev: Device) -> int | None: + text = dev.sh("grep MemAvailable /proc/meminfo") + parts = text.split() + return int(parts[1]) // 1024 if len(parts) >= 2 and parts[1].isdigit() else None + + +def oom_trial(dev: Device, app: str, profile: str, run_dir: Path, args) -> dict: + """Fresh Steam, launch, load the save, and see whether the session survives.""" + run_dir.mkdir(parents=True, exist_ok=True) + ensure_steam(dev, fresh=True) + dev.profile(profile) + name = "oom-%s.jsonl" % run_dir.name + dev.sh("mkdir -p " + REMOTE + " && rm -f " + REMOTE + "/" + name) + dev.mark("idle") + before_exit = last_exit(dev).get("timestamp") + total = args.idle + 60 + args.title + 10 + args.watch + 20 + rec = recorder(dev, total, args.interval, name) + events = {"profile": profile, "app": app, "started": time.time(), "kind": "oom", "env": args.env or [], + "arm": args.arm or profile} + time.sleep(args.idle) + dev.mark("launch") + t0 = time.monotonic() + launched = dev.ctl("launch", app, "--wait", "--timeout", "120", timeout=130) + events["launch_s"] = round(time.monotonic() - t0, 2) if launched.get("ok") else None + dev.mark("game") + time.sleep(args.title) + events["status_in_game"] = dev.status() + load_attempts = [] + base = system_available(dev) + for attempt in range(args.load_attempts): + dev.mark("load") + attempt_t = time.time() + for step in args.load_steps.split(";"): + kind, _, rest = step.partition(":") + if kind == "pad": + buttons, _, timing = rest.partition("@") + hold, _, gap = (timing or "200").partition("/") + dev.ctl("input", "gamepad", *buttons.split(","), "--hold", hold, "--gap", gap or "300", timeout=60) + elif kind == "key": + dev.ctl("input", "key", rest, timeout=30) + elif kind == "tap": + x, _, y = rest.partition(",") + dev.ctl("input", "tap", x, y, timeout=30) + elif kind == "sleep": + time.sleep(float(rest)) + started = time.monotonic() + lowest = base + died = False + while time.monotonic() - started < args.load_check: + if not running(dev): + died = True + break + now = system_available(dev) + if now is None: + break + lowest = min(lowest or now, now) + if base is not None and base - lowest >= 400: + break + time.sleep(1) + load_attempts.append({"attempt": attempt + 1, "t": attempt_t, "died": died, + "drop_mib": None if base is None or lowest is None else base - lowest}) + if died or base is None or lowest is None or base - lowest >= 400: + break + events["load_attempts"] = load_attempts + events["loaded"] = any(a["died"] or (a["drop_mib"] or 0) >= 400 for a in load_attempts) + load_t = load_attempts[-1]["t"] if load_attempts else time.time() + events["load_t"] = load_t + before_load = base + alive = running(dev) + if alive: + dev.ctl("screenshot", str(run_dir / "after-inputs.png"), timeout=60) + else: + events["died_after_s"] = round(time.time() - load_t, 1) + while alive and time.time() - load_t < args.watch: + time.sleep(5) + if not running(dev): + alive = False + events["died_after_s"] = round(time.time() - load_t, 1) + break + events["survived"] = alive + events["available_before_load_mib"] = before_load + exit_now = last_exit(dev) + events["exit"] = exit_now if exit_now.get("timestamp") != before_exit else None + if events["exit"]: + try: + killed = time.mktime(time.strptime(events["exit"]["timestamp"].split(".")[0], "%Y-%m-%d %H:%M:%S")) + events["died_after_s"] = round(killed - load_t, 1) + except (KeyError, ValueError): + pass + if alive: + events["status_after_load"] = dev.status() + dev.ctl("screenshot", str(run_dir / "loaded.png"), timeout=60) + dev.ctl("quit", app, "--timeout", "30", timeout=40) + rec.wait(timeout=total + 120) if alive else rec.kill() + if not alive: + ensure_steam(dev) + text = dev.fetch(REMOTE + "/" + name) + (run_dir / "samples.jsonl").write_text(text) + events["finished"] = time.time() + (run_dir / "events.json").write_text(json.dumps(events, indent=1)) + return events + + +FLAG_BASE = "--no-zygote --in-process-gpu -AudioServiceOutOfProcess" +# Every variant sets all of these, so one variant never inherits another's persistent setting. +FLAG_DEFAULTS = {"DD_WEBHELPER_ARGS": FLAG_BASE, "DD_STEAM_SOFT_COMPOSITING": "0", "DD_STEAM_CEF_GPU": "1"} +FLAGS = { + "default": {}, + "v8-small": {"DD_WEBHELPER_ARGS": FLAG_BASE + " --js-flags=--optimize-for-size,--max-semi-space-size=2"}, + "gpu-budget": {"DD_WEBHELPER_ARGS": FLAG_BASE + + " --force-gpu-mem-available-mb=96 --force-gpu-mem-discardable-limit-mb=32"}, + "soft-compositing": {"DD_STEAM_SOFT_COMPOSITING": "1"}, + "cef-software": {"DD_STEAM_CEF_GPU": "0"}, +} + + +def set_variant(dev: Device, variant: str) -> dict: + env = dict(FLAG_DEFAULTS, **FLAGS[variant]) + for key, value in env.items(): + dev.ctl("env", "set", "--persistent", "%s=%s" % (key, value), timeout=30) + return env + + +def flags_run(dev: Device, variant: str, run_dir: Path, args) -> dict: + """A fresh session with one web helper flag set, sampled in the library after it settles.""" + run_dir.mkdir(parents=True, exist_ok=True) + env = set_variant(dev, variant) + value = env["DD_WEBHELPER_ARGS"] + ensure_steam(dev, fresh=True) + cmdline = dev.sh("xargs -0 echo < /proc/$(pgrep -f 'steamwebhelper -nocrash' | head -1)/cmdline") + time.sleep(args.settle) + samples = [] + for _ in range(args.samples): + text = dev.sh(LITE + " sample") + try: + samples.append(json.loads(text)) + except ValueError: + pass + time.sleep(args.interval) + with open(run_dir / "samples.jsonl", "w") as stream: + for item in samples: + print(json.dumps(item), file=stream) + dev.ctl("screenshot", str(run_dir / "library.png"), timeout=60) + events = {"kind": "flags", "variant": variant, "env": env, "helper_cmdline": cmdline.strip()[:4000], + "helper_cmdline_has": [f for f in (value or "").split() if f.startswith("--") and f in cmdline], + "started": time.time()} + (run_dir / "events.json").write_text(json.dumps(events, indent=1)) + rows = [] + for item in samples: + roles = {} + for r in item["processes"]: + if r["role"] not in NOT_CLIENT: + roles[r["role"]] = roles.get(r["role"], 0) + (r.get("Pss") or 0) + rows.append({"total": sum(roles.values()) / 1024, "renderer": roles.get("renderer", 0) / 1024, + "browser": roles.get("browser", 0) / 1024, + "gpu": sum((item.get("gpu") or {}).values()) / 1048576}) + return {"variant": variant, "applied": events["helper_cmdline_has"], + **{k: round(statistics.median([r[k] for r in rows]), 1) for k in ("total", "renderer", "browser", "gpu")}} + + +def cycle(dev: Device, app: str, profile: str, run_dir: Path, args) -> dict: + run_dir.mkdir(parents=True, exist_ok=True) + ensure_steam(dev) + dev.profile(profile) + dev.sh("mkdir -p " + REMOTE + " && rm -f " + REMOTE + "/samples.jsonl") + dev.mark("idle") + total = args.idle + args.launch_timeout + args.game + args.overlay + args.back + 30 + cdp = ["--cdp"] if args.cdp else [] + recorder = subprocess.Popen( + [sys.executable, str(CTL)] + (["--serial", dev.serial] if dev.serial else []) + + ["guest", "--timeout", str(total + 30), "--", LITE, "record", str(total), + str(args.interval), REMOTE + "/samples.jsonl", "--mark", REMOTE + "/mark"] + cdp, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + events = {"profile": profile, "app": app, "started": time.time()} + try: + time.sleep(args.idle) + dev.mark("launch") + t0 = time.monotonic() + launched = dev.ctl("launch", app, "--wait", "--timeout", str(args.launch_timeout), timeout=args.launch_timeout) + events["launch_s"] = round(time.monotonic() - t0, 2) if launched.get("ok") else None + events["launch_error"] = None if launched.get("ok") else launched.get("error") or launched.get("message") + dev.mark("game") + time.sleep(max(0, args.game - args.pacing)) + pacing = dev.ctl("pacing", "--seconds", str(args.pacing), timeout=args.pacing + 30) + events["pacing"] = {k: pacing.get(k) for k in ("fpsOnScreen", "presentToPresentMs", "cadenceMs", + "offCadencePercent", "frames", "error")} + events["status_in_game"] = dev.status() + if args.overlay: + dev.mark("overlay") + opens = [] + for _ in range(args.overlay_rounds): + t1 = time.monotonic() + dev.ctl("input", "gamepad", "guide", timeout=20) + shown = wait_focus(dev, lambda a: a == 769, 10) + opens.append(None if shown is None else round(time.monotonic() - t1, 2)) + time.sleep(2) + dev.ctl("input", "gamepad", "guide", timeout=20) + wait_focus(dev, lambda a: a not in (None, 769), 10) + time.sleep(2) + events["overlay_open_s"] = opens + dev.mark("quit") + dev.ctl("quit", app, "--timeout", "30", timeout=40) + dev.mark("back") + time.sleep(args.back) + finally: + recorder.wait(timeout=total + 120) + text = dev.fetch(REMOTE + "/samples.jsonl") + (run_dir / "samples.jsonl").write_text(text) + events["finished"] = time.time() + events["status_after"] = dev.status() + (run_dir / "events.json").write_text(json.dumps(events, indent=1)) + return events + + +def fresh_session(dev: Device, env: dict) -> bool: + for key, value in env.items(): + dev.ctl("env", "set", "--persistent", "%s=%s" % (key, value), timeout=30) + dev.ctl("stop", "--timeout", "60", timeout=80) + dev.adb("shell", "monkey -p com.droiddeck.launcher -c android.intent.category.LAUNCHER 1") + time.sleep(5) + return bool(dev.ctl("start", "steam", "--wait", "--timeout", "300", timeout=320).get("ok")) + + +def snapshot(dev: Device, label: str) -> dict | None: + """The client tree's memory and the web helper's CPU ticks, printed as it is taken.""" + try: + d = json.loads(dev.sh(LITE + " sample")) + except ValueError: + print(time.strftime("%H:%M:%S"), label, "sample failed", flush=True) + return None + rows = [r for r in d["processes"] if r["role"] not in NOT_CLIENT] + out = {"label": label, "t": d["t"], "mono": d["mono"], "tck": d["clk_tck"], + "pss": sum(r.get("Pss") or 0 for r in rows) // 1024, + "renderer": sum(r.get("Pss") or 0 for r in rows if r["role"] == "renderer") // 1024, + "swappss": sum(r.get("SwapPss") or 0 for r in rows) // 1024, + "gpu": sum((d.get("gpu") or {}).values()) // 1048576, + "avail": d["memory"]["MemAvailable"] // 1024, + "ticks": {str(r["pid"]): r.get("ticks") or 0 for r in rows if r["role"] in WEB}, + "lite": [{k: r.get(k) for k in ("pid", "Pss", "ticks")} for r in d["processes"] if r["role"] == "steam-lite"]} + print(time.strftime("%H:%M:%S"), label, *("%s %s" % (k, out[k]) for k in + ("pss", "renderer", "swappss", "gpu", "avail")), flush=True) + return out + + +def web_cpu(a: dict | None, b: dict | None) -> float | None: + if not a or not b or b["mono"] <= a["mono"]: + return None + used = sum(b["ticks"][p] - a["ticks"][p] for p in b["ticks"] if p in a["ticks"]) + return round(100.0 * used / a["tck"] / (b["mono"] - a["mono"]), 1) + + +def wait_app(dev: Device, want: int, limit: float) -> float | None: + start = time.monotonic() + while time.monotonic() - start < limit: + if dev.ctl("focus", timeout=30).get("focusedApp") == want: + return round(time.monotonic() - start, 2) + time.sleep(0.2) + return None + + +def steady(dev: Device, level: str, app: str, out: Path) -> dict: + """One fresh session at one level: library, a game in front for 80 s, three overlay round trips, library.""" + out.mkdir(parents=True, exist_ok=True) + run = {"level": level, "app": app, "started": fresh_session(dev, {"DD_STEAM_LITE": level})} + time.sleep(30) + run["idle"] = snapshot(dev, "library idle") + t0 = time.monotonic() + run["launch_ok"] = bool(dev.ctl("launch", app, "--wait", "--timeout", "180", timeout=200).get("ok")) + run["launch_s"] = round(time.monotonic() - t0, 1) + time.sleep(60) + a = snapshot(dev, "game +60s") + time.sleep(20) + run["game"] = snapshot(dev, "game +80s") + run["web_cpu_pct"] = web_cpu(a, run["game"]) + run["status"] = dev.status() + opens, closes = [], [] + for i in range(3): + t1 = time.monotonic() + dev.ctl("input", "gamepad", "guide", "--hold", "150", timeout=30) + opens.append(None if wait_app(dev, 769, 10) is None else round(time.monotonic() - t1, 2)) + if i == 0: + dev.ctl("screenshot", str(out / "overlay.png"), timeout=60) + time.sleep(2) + t2 = time.monotonic() + dev.ctl("input", "gamepad", "guide", "--hold", "150", timeout=30) + closes.append(None if wait_app(dev, int(app), 10) is None else round(time.monotonic() - t2, 2)) + time.sleep(3) + run["overlay_open_s"], run["overlay_close_s"] = opens, closes + print(time.strftime("%H:%M:%S"), "web cpu", run["web_cpu_pct"], "overlay open", opens, "close", closes, flush=True) + run["after_overlay"] = snapshot(dev, "after overlay") + dev.ctl("quit", app, "--timeout", "30", timeout=40) + time.sleep(15) + run["back"] = snapshot(dev, "library after quit") + (out / "run.json").write_text(json.dumps(run, indent=1)) + return run + + +def library(dev: Device, soft: str, out: Path) -> dict: + """The settled library (90 s after start) with the web helper composited on the GPU or in software.""" + out.mkdir(parents=True, exist_ok=True) + run = {"soft": soft, "started": fresh_session(dev, {"DD_STEAM_SOFT_COMPOSITING": soft})} + time.sleep(90) + a = snapshot(dev, "library +90s") + time.sleep(20) + b = snapshot(dev, "library +110s") + run.update({"idle_cpu": web_cpu(a, b), "idle_pss": b and b["pss"], "idle_gpu": b and b["gpu"], + "idle_avail": b and b["avail"]}) + dev.ctl("screenshot", str(out / "library.png"), timeout=60) + (out / "run.json").write_text(json.dumps(run, indent=1)) + return run + + +# ---- analysis ----------------------------------------------------------------------------------- + +def load(run_dir: Path) -> tuple[list[dict], dict]: + samples = [json.loads(line) for line in (run_dir / "samples.jsonl").read_text().splitlines() if line.strip()] + events = json.loads((run_dir / "events.json").read_text()) + return samples, events + + +def tree(sample: dict, roles=None) -> dict: + rows = [r for r in sample["processes"] if r["role"] not in NOT_CLIENT and (roles is None or r["role"] in roles)] + pss = [r.get("Pss") for r in rows] + known = [p for p in pss if p is not None] + return {"pss": sum(known) if known else None, "swap": sum(r.get("SwapPss") or 0 for r in rows), + "unreadable": len(pss) - len(known), "ticks": {r["pid"]: r.get("ticks") or 0 for r in rows}} + + +def cpu(a: dict, b: dict, roles=None) -> float | None: + dt = b["mono"] - a["mono"] + if dt <= 0: + return None + ta, tb = tree(a, roles)["ticks"], tree(b, roles)["ticks"] + used = sum(tb[p] - ta[p] for p in tb if p in ta and tb[p] >= ta[p]) + return 100.0 * used / b["clk_tck"] / dt + + +def phases(samples: list[dict]) -> dict: + out: dict[str, list[dict]] = {} + for s in samples: + out.setdefault(s.get("mark") or "?", []).append(s) + return out + + +def summarize(samples: list[dict], events: dict) -> dict: + row = {"profile": events["profile"], "launch_s": events.get("launch_s")} + by = phases(samples) + for name, group in by.items(): + trees = [tree(s) for s in group] + web = [tree(s, WEB) for s in group] + pss = [t["pss"] for t in trees if t["pss"] is not None] + if pss: + tail = pss[len(pss) // 2:] + row[name + "_pss_mib"] = round(statistics.median(tail) / 1024, 1) + row[name + "_pss_peak_mib"] = round(max(pss) / 1024, 1) + row[name + "_web_pss_mib"] = round(statistics.median([t["pss"] for t in web[len(web) // 2:] + if t["pss"] is not None]) / 1024, 1) + row[name + "_swappss_mib"] = round(statistics.median([t["swap"] for t in trees[len(trees) // 2:]]) / 1024, 1) + avail = [s["memory"].get("MemAvailable") for s in group if s["memory"].get("MemAvailable")] + if avail: + row[name + "_avail_min_mib"] = round(min(avail) / 1024, 1) + if len(group) >= 2: + half = group[len(group) // 2:] if len(group) >= 4 else group + row[name + "_cpu_pct"] = round(cpu(half[0], half[-1]) or 0, 1) + row[name + "_web_cpu_pct"] = round(cpu(half[0], half[-1], WEB) or 0, 1) + row[name + "_lite_cpu_pct"] = round(_self_cpu(half[0], half[-1]), 2) + lite_pss = [r.get("Pss") for s in group for r in s["processes"] if r["role"] == "steam-lite" and r.get("Pss")] + if lite_pss: + row["lite_pss_mib"] = round(max(lite_pss) / 1024, 1) + psi = [s["memory"].get("psi_some_avg10") for s in group if s["memory"].get("psi_some_avg10") is not None] + if psi: + row[name + "_psi_some_max"] = max(psi) + pacing = events.get("pacing") or {} + row["fps"] = pacing.get("fpsOnScreen") + hist = {int(k): v for k, v in (pacing.get("presentToPresentMs") or {}).items()} + if hist: + ordered = sorted(hist.items()) + n = sum(hist.values()) + for pct in (95, 99): + seen = 0 + for ms, count in ordered: + seen += count + if seen >= n * pct / 100: + row["frame_p%d_ms" % pct] = ms + break + opens = [o for o in events.get("overlay_open_s") or [] if o is not None] + row["overlay_open_median_s"] = round(statistics.median(opens), 2) if opens else None + row["overlay_failures"] = sum(1 for o in events.get("overlay_open_s") or [] if o is None) + return row + + +def _self_cpu(a: dict, b: dict) -> float: + dt = b["mono"] - a["mono"] + ta = {r["pid"]: r.get("ticks") or 0 for r in a["processes"] if r["role"] == "steam-lite"} + tb = {r["pid"]: r.get("ticks") or 0 for r in b["processes"] if r["role"] == "steam-lite"} + used = sum(tb[p] - ta[p] for p in tb if p in ta) + return 100.0 * used / b["clk_tck"] / dt if dt > 0 else 0.0 + + +def report(root: Path) -> list[dict]: + rows = [] + for events_path in sorted(root.glob("**/events.json")): + try: + samples, events = load(events_path.parent) + except (OSError, ValueError): + continue + if not samples: + continue + row = summarize(samples, events) + row["run"] = str(events_path.parent.relative_to(root)) + rows.append(row) + if not rows: + return rows + keys = ["run", "profile"] + sorted({k for r in rows for k in r} - {"run", "profile"}) + with open(root / "runs.csv", "w", newline="") as stream: + writer = csv.DictWriter(stream, fieldnames=keys) + writer.writeheader() + writer.writerows(rows) + return rows + + +def main(argv=None) -> int: + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--serial", default=os.environ.get("ADB_SERIAL")) + sub = parser.add_subparsers(dest="command", required=True) + p_info = sub.add_parser("info") + p_info.add_argument("--out", type=Path, required=True) + p_cycle = sub.add_parser("cycle") + p_cycle.add_argument("--app", required=True) + p_cycle.add_argument("--profiles", default="stock,1,2,3") + p_cycle.add_argument("--repeat", type=int, default=3) + p_cycle.add_argument("--out", type=Path, required=True) + p_cycle.add_argument("--idle", type=float, default=40) + p_cycle.add_argument("--game", type=float, default=60) + p_cycle.add_argument("--pacing", type=float, default=20) + p_cycle.add_argument("--overlay", type=float, default=1) + p_cycle.add_argument("--overlay-rounds", type=int, default=3) + p_cycle.add_argument("--back", type=float, default=40) + p_cycle.add_argument("--interval", type=float, default=2) + p_cycle.add_argument("--launch-timeout", type=float, default=180) + p_cycle.add_argument("--cdp", action="store_true") + p_cycle.add_argument("--seed", type=int, default=1) + p_ablate = sub.add_parser("ablate") + p_ablate.add_argument("--actions", default="gc,moderate,critical") + p_ablate.add_argument("--waits", default="3,20,60") + p_ablate.add_argument("--out", type=Path, required=True) + p_ablate.add_argument("--label", required=True) + p_oom = sub.add_parser("oom") + p_oom.add_argument("--app", required=True) + p_oom.add_argument("--profiles", default="stock,1") + p_oom.add_argument("--arms", help="comma-separated arms from ARMS; replaces --profiles and --env") + p_oom.add_argument("--repeat", type=int, default=3) + p_oom.add_argument("--out", type=Path, required=True) + p_oom.add_argument("--idle", type=float, default=20) + p_oom.add_argument("--title", type=float, default=25) + p_oom.add_argument("--load-steps", default="pad:a@2000;key:ENTER;sleep:6;tap:600,363") + p_oom.add_argument("--load-attempts", type=int, default=4) + p_oom.add_argument("--load-check", type=float, default=30) + p_oom.add_argument("--watch", type=float, default=90) + p_oom.add_argument("--interval", type=float, default=1) + p_oom.add_argument("--seed", type=int, default=1) + p_oom.add_argument("--env", action="append", metavar="KEY=VALUE", + help="persistent session environment for these trials (set before the first one)") + p_oom.add_argument("--arm", help="name for this set of trials in charts (default: the profile)") + p_flags = sub.add_parser("flags") + p_flags.add_argument("--variants", default="default,v8-small,gpu-budget") + p_flags.add_argument("--repeat", type=int, default=2) + p_flags.add_argument("--settle", type=float, default=60) + p_flags.add_argument("--samples", type=int, default=5) + p_flags.add_argument("--interval", type=float, default=5) + p_flags.add_argument("--out", type=Path, required=True) + p_steady = sub.add_parser("steady") + p_steady.add_argument("--app", required=True) + p_steady.add_argument("--levels", default="stock,1,2,stock,1,2,stock,1,2", + help="one fresh session per entry, in this order") + p_steady.add_argument("--out", type=Path, required=True) + p_library = sub.add_parser("library") + p_library.add_argument("--order", default="1,0,1,0", help="DD_STEAM_SOFT_COMPOSITING per fresh session") + p_library.add_argument("--out", type=Path, required=True) + p_report = sub.add_parser("report") + p_report.add_argument("root", type=Path) + args = parser.parse_args(argv) + dev = Device(args.serial) + if args.command == "info": + print(json.dumps(info(dev, args.out), indent=1)) + return 0 + if args.command == "steady": + counts: dict[str, int] = {} + for level in args.levels.split(","): + counts[level] = counts.get(level, 0) + 1 + name = "%s-%d" % ("stock" if level == "stock" else "l" + level, counts[level]) + print("== steady", name, flush=True) + steady(dev, level, args.app, args.out / name) + return 0 + if args.command == "library": + counts = {} + for soft in args.order.split(","): + counts[soft] = counts.get(soft, 0) + 1 + name = "%s-%d" % ("softcomp" if soft == "1" else "default", counts[soft]) + print("== library", name, flush=True) + library(dev, soft, args.out / name) + return 0 + if args.command == "flags": + try: + for i in range(args.repeat): + for variant in args.variants.split(","): + print("== %s run %d" % (variant, i + 1), flush=True) + try: + print(json.dumps(flags_run(dev, variant, args.out / ("flags-%s-r%d" % (variant, i + 1)), args)), + flush=True) + except Exception as error: + print("run failed: %r" % error, flush=True) + finally: + set_variant(dev, "default") + return 0 + if args.command in ("cycle", "oom"): + for item in getattr(args, "env", None) or []: + dev.ctl("env", "set", "--persistent", item, timeout=30) + arms = (getattr(args, "arms", None) or "").split(",") if getattr(args, "arms", None) else None + names = arms or args.profiles.split(",") + unknown = [n for n in names if n not in (ARMS if arms else PROFILES)] + if unknown: + parser.error("unknown profiles: %s" % ", ".join(unknown)) + order = [] + rng = random.Random(args.seed) + for i in range(args.repeat): + block = list(names) + rng.shuffle(block) + order += [(i, n) for n in block] + for i, name in order: + run_dir = args.out / ("%s-%s-r%d" % (args.app, name, i + 1)) + print("== %s run %d" % (name, i + 1), flush=True) + try: + if args.command == "oom": + profile = name + if arms: + profile, env = ARMS[name] + for key, value in env.items(): + dev.ctl("env", "set", "--persistent", "%s=%s" % (key, value), timeout=30) + args.arm, args.env = name, ["%s=%s" % kv for kv in env.items()] + run_dir = args.out / ("oom-%s-%s-r%d" % (args.app, args.arm or name, i + 1)) + events = oom_trial(dev, args.app, profile, run_dir, args) + print(json.dumps({k: events.get(k) for k in ("arm", "profile", "loaded", "survived", + "died_after_s", "exit", "launch_s")}), flush=True) + else: + cycle(dev, args.app, name, run_dir, args) + print(json.dumps(summarize(*load(run_dir))), flush=True) + except Exception as error: + print("run failed: %r" % error, flush=True) + return 0 + if args.command == "ablate": + rows = ablate(dev, args.actions.split(","), [float(w) for w in args.waits.split(",")], args.out, args.label) + for row in rows: + print(json.dumps(row)) + return 0 + rows = report(args.root) + print(json.dumps(rows, indent=1)) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/steam_lite_charts.py b/tools/steam_lite_charts.py new file mode 100644 index 0000000000..2db658e8f1 --- /dev/null +++ b/tools/steam_lite_charts.py @@ -0,0 +1,457 @@ +#!/usr/bin/env python3 +"""Charts for docs/development/steam-lite-runtime.md from tools/steam_lite_bench.py output. + + tools/steam_lite_charts.py RESULTS_DIR OUT_DIR + +RESULTS_DIR holds one folder per device (device.json, cycle runs with samples.jsonl and events.json, +ablation *.jsonl). Every chart shows each run as a point beside the median bar; axes start at zero. +""" + +from __future__ import annotations + +import json +import statistics +import sys +from pathlib import Path + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt # noqa: E402 + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import steam_lite_bench as bench # noqa: E402 + +NOT_CLIENT = bench.NOT_CLIENT + +ORDER = ["stock", "1", "2", "3", "l1", "softcomp", "softcomp+l1", "gc", "moderate", "critical", "pause", "freeze", + "gc+critical"] +NAMES = {"stock": "stock", "1": "L1 conservative", "2": "L2 balanced", "3": "L3 aggressive", "l1": "L1", + "softcomp": "soft compositing", "softcomp+l1": "soft compositing + L1"} + + +def runs(device_dir: Path) -> list[dict]: + rows = [] + for events in sorted(device_dir.glob("*/events.json")): + if json.loads(events.read_text()).get("kind") in ("oom", "flags"): + continue + try: + samples, ev = bench.load(events.parent) + except (OSError, ValueError): + continue + if samples: + row = bench.summarize(samples, ev) + row["run"] = events.parent.name + row["app"] = ev.get("app") + rows.append(row) + return rows + + +def grouped(ax, rows, keys, labels, unit, order=None): + profiles = [p for p in (order or ORDER) if any(r["profile"] == p for r in rows)] + width = 0.8 / len(keys) + for k, (key, label) in enumerate(zip(keys, labels)): + xs, med = [], [] + for i, p in enumerate(profiles): + values = [r[key] for r in rows if r["profile"] == p and r.get(key) is not None] + x = i + (k - (len(keys) - 1) / 2) * width + if values: + med.append(statistics.median(values)) + xs.append(x) + ax.scatter([x] * len(values), values, s=12, color="black", zorder=3) + ax.bar(xs, med, width * 0.9, label=label, zorder=2) + ax.set_xticks(range(len(profiles))) + ax.set_xticklabels([NAMES.get(p, p) for p in profiles], fontsize=8) + ax.set_ylim(bottom=0) + ax.set_ylabel(unit) + ax.grid(axis="y", alpha=0.3, zorder=0) + ax.legend(fontsize=7) + + +def chart_cycles(device: str, rows: list[dict], out: Path) -> list[Path]: + made = [] + for app in sorted({r["app"] for r in rows}): + mine = [r for r in rows if r["app"] == app] + n = len(mine) + fig, axes = plt.subplots(2, 3, figsize=(15, 8)) + fig.suptitle("%s, app %s: stock vs profiles (%d runs; bars = median, dots = runs)" % (device, app, n)) + grouped(axes[0][0], mine, ["idle_pss_mib", "game_pss_mib", "back_pss_mib"], + ["library idle", "game in front", "back in library"], "client tree PSS (MiB)") + axes[0][0].set_title("1. Steam process-tree PSS", fontsize=9) + grouped(axes[0][1], mine, ["launch_pss_peak_mib", "game_swappss_mib"], ["peak during launch", "SwapPss in game"], + "MiB") + axes[0][1].set_title("2. launch peak PSS and SwapPss", fontsize=9) + grouped(axes[0][2], mine, ["idle_web_cpu_pct", "game_web_cpu_pct", "game_lite_cpu_pct"], + ["web helper, library idle", "web helper, game in front", "supervisor, game in front"], + "% of one core") + axes[0][2].set_title("3. idle and hidden CPU", fontsize=9) + grouped(axes[1][0], mine, ["fps"], ["on-screen FPS"], "FPS") + axes[1][0].set_title("4a. game FPS (SurfaceFlinger)", fontsize=9) + grouped(axes[1][1], mine, ["frame_p95_ms", "frame_p99_ms"], ["p95", "p99"], "present-to-present (ms)") + axes[1][1].set_title("4b. frame time", fontsize=9) + grouped(axes[1][2], mine, ["launch_s", "overlay_open_median_s"], ["game launch", "overlay open (median)"], + "seconds") + axes[1][2].set_title("5. launch and overlay latency", fontsize=9) + fig.tight_layout() + path = out / ("%s-%s-cycles.svg" % (device, app)) + fig.savefig(path) + plt.close(fig) + made.append(path) + return made + + +def chart_ablation(device: str, device_dir: Path, out: Path) -> list[Path]: + made = [] + for path in sorted(device_dir.glob("ablate-*.jsonl")): + items = [json.loads(x) for x in path.read_text().splitlines() if x.strip()] + rows, label = [], None + for item in items: + if "processes" in item: + total = sum(r.get("Pss") or 0 for r in item["processes"] if r["role"] not in NOT_CLIENT) + renderer = sum(r.get("Pss") or 0 for r in item["processes"] if r["role"] == "renderer") + rows.append((label, total / 1024, renderer / 1024, sum((item.get("gpu") or {}).values()) / 1048576)) + elif "label" in item and "result" not in item: + label = item["label"] + actions, deltas = [], {} + for i, (lab, total, renderer, gpu) in enumerate(rows): + if lab and lab.startswith("before "): + action = lab[7:] + after = [r for r in rows[i + 1:] if r[0] and r[0].startswith("after %s +" % action)] + if after: + last = after[-1] + actions.append(action) + deltas.setdefault(action, []).append((last[1] - total, last[2] - renderer, last[3] - gpu)) + if not actions: + continue + uniq = list(dict.fromkeys(actions)) + fig, ax = plt.subplots(figsize=(8, 4)) + width = 0.27 + for k, (idx, name) in enumerate(((0, "client tree PSS"), (1, "renderer PSS"), (2, "KGSL GPU"))): + meds = [statistics.median([d[idx] for d in deltas[a]]) for a in uniq] + xs = [i + (k - 1) * width for i in range(len(uniq))] + ax.bar(xs, meds, width * 0.9, label=name) + for x, a in zip(xs, uniq): + ax.scatter([x] * len(deltas[a]), [d[idx] for d in deltas[a]], s=10, color="black", zorder=3) + ax.axhline(0, color="black", linewidth=0.8) + ax.set_xticks(range(len(uniq))) + ax.set_xticklabels(uniq) + ax.set_ylabel("change after the last wait (MiB; negative = freed)") + ax.set_title("6. %s %s: each intervention alone" % (device, path.stem[7:]), fontsize=9) + ax.legend(fontsize=7) + ax.grid(axis="y", alpha=0.3) + fig.tight_layout() + target = out / ("%s-%s.svg" % (device, path.stem)) + fig.savefig(target) + plt.close(fig) + made.append(target) + return made + + +COLORS = {"stock": "#1f77b4", "1": "#ff7f0e", "2": "#2ca02c", "3": "#d62728", "softcomp": "#9467bd", + "softcomp+1": "#8c564b", "pageout": "#e377c2", "zramfix": "#17becf"} + + +def chart_oom(device: str, device_dir: Path, out: Path) -> list[Path]: + trials = [] + for events_path in sorted(device_dir.glob("oom-*/events.json")): + events = json.loads(events_path.read_text()) + events["profile"] = events.get("arm") or events["profile"] + try: + samples, _ = bench.load(events_path.parent) + except (OSError, ValueError): + samples = [] + trials.append((events, samples)) + if not trials: + return [] + by_app = {} + for events, samples in trials: + by_app.setdefault(events["app"], []).append((events, samples)) + made = [] + for app, group in by_app.items(): + fig, (ax, bx) = plt.subplots(1, 2, figsize=(14, 5), gridspec_kw={"width_ratios": [3, 1]}) + for events, samples in group: + load = events.get("load_t") + if load is None or not samples or not events.get("loaded"): + continue + xs = [s["t"] - load for s in samples] + ys = [(s["memory"].get("MemAvailable") or 0) / 1024 for s in samples] + color = COLORS.get(events["profile"], "gray") + ax.plot(xs, ys, color=color, alpha=0.8, linewidth=1, + label=NAMES.get(events["profile"], events["profile"])) + if not events.get("survived"): + ax.scatter([xs[-1]], [ys[-1]], marker="x", s=60, color=color, zorder=3) + handles, labels = ax.get_legend_handles_labels() + unique = dict(zip(labels, handles)) + ax.legend(unique.values(), unique.keys(), fontsize=7) + ax.axvline(0, color="black", linewidth=0.8) + ax.set_ylim(bottom=0) + ax.set_xlim(-60, 45) + ax.set_xlabel("seconds from pressing Continue (x = last sample before the session was killed)") + ax.set_ylabel("MemAvailable (MiB)") + ax.set_title("%s, app %s: memory through a save load" % (device, app), fontsize=9) + profiles = list(dict.fromkeys([p for p in ORDER if any(e["profile"] == p for e, _ in group)] + + [e["profile"] for e, _ in group])) + valid = [(e, x) for e, x in group if e.get("loaded")] + survived = [sum(1 for e, _ in valid if e["profile"] == p and e.get("survived")) for p in profiles] + totals = [sum(1 for e, _ in valid if e["profile"] == p) for p in profiles] + bx.bar(range(len(profiles)), totals, color="#dddddd", label="trials that loaded the save") + bx.bar(range(len(profiles)), survived, color="#2ca02c", label="survived 90 s") + bx.set_xticks(range(len(profiles))) + bx.set_xticklabels([NAMES.get(p, p) for p in profiles], fontsize=7) + bx.set_ylabel("trials") + bx.legend(fontsize=7) + bx.set_title("session survival", fontsize=9) + fig.tight_layout() + target = out / ("%s-%s-oom.svg" % (device, app)) + fig.savefig(target) + plt.close(fig) + made.append(target) + return made + + +def chart_flags(device: str, device_dir: Path, out: Path) -> list[Path]: + rows = [] + for events_path in sorted(device_dir.glob("**/flags-*/events.json")): + events = json.loads(events_path.read_text()) + lines = (events_path.parent / "samples.jsonl").read_text().splitlines() + for line in lines: + item = json.loads(line) + roles = {} + for r in item["processes"]: + if r["role"] not in NOT_CLIENT: + roles[r["role"]] = roles.get(r["role"], 0) + (r.get("Pss") or 0) + rows.append({"profile": events["variant"], "total": sum(roles.values()) / 1024, + "renderer": roles.get("renderer", 0) / 1024, "browser": roles.get("browser", 0) / 1024, + "gpu": sum((item.get("gpu") or {}).values()) / 1048576, + "run": events_path.parent.name}) + if not rows: + return [] + per_run = {} + for r in rows: + per_run.setdefault(r["run"], []).append(r) + medians = [{"profile": rs[0]["profile"], **{k: statistics.median(x[k] for x in rs) + for k in ("total", "renderer", "browser", "gpu")}} + for rs in per_run.values()] + variants = list(dict.fromkeys(r["profile"] for r in medians)) + fig, ax = plt.subplots(figsize=(10, 4.5)) + grouped(ax, medians, ["total", "renderer", "browser", "gpu"], + ["client tree PSS", "renderer PSS", "browser PSS", "KGSL GPU (not in PSS)"], "MiB", order=variants) + ax.set_title("7. %s: library idle, web helper flag variants (bars = median of runs, dots = runs)" % device, + fontsize=9) + fig.tight_layout() + target = out / ("%s-flags.svg" % device) + fig.savefig(target) + plt.close(fig) + return [target] + + +def steady_runs(device_dir: Path) -> list[dict]: + rows = [] + for path in sorted(device_dir.glob("steady/*/run.json")): + run = json.loads(path.read_text()) + game = run.get("game") or {} + opens = [o for o in run.get("overlay_open_s") or [] if o is not None] + rows.append({"profile": "stock" if run["level"] == "stock" else run["level"], "run": path.parent.name, + "resident": game.get("pss"), "swapped": game.get("swappss"), + "footprint": (game.get("pss") or 0) + (game.get("swappss") or 0) if game else None, + "renderer": game.get("renderer"), "gpu": game.get("gpu"), "avail": game.get("avail"), + "idle_gpu": (run.get("idle") or {}).get("gpu"), "cpu": run.get("web_cpu_pct"), + "overlay": statistics.median(opens) if opens else None, + "passes": [h.get("outcome") for h in (run.get("status") or {}).get("history", [])]}) + return rows + + +def chart_steady(device: str, device_dir: Path, out: Path) -> list[Path]: + rows = steady_runs(device_dir) + if not rows: + return [] + fig, axes = plt.subplots(1, 3, figsize=(15, 4.5)) + grouped(axes[0], rows, ["resident", "footprint", "renderer"], + ["resident PSS", "resident + SwapPss", "renderer PSS"], "MiB") + axes[0].set_title("Steam tree with the game in front (+80 s)", fontsize=9) + grouped(axes[1], rows, ["gpu", "avail"], ["KGSL GPU (not in PSS)", "MemAvailable"], "MiB") + axes[1].set_title("GPU memory and MemAvailable in game", fontsize=9) + grouped(axes[2], rows, ["cpu", "overlay"], ["web helper CPU (% of one core)", "overlay open (s, tool-bound)"], "") + axes[2].set_title("hidden UI CPU and overlay latency", fontsize=9) + fig.suptitle("%s: Bully in front, one fresh session per run (bars = median, dots = runs)" % device, fontsize=10) + fig.tight_layout() + target = out / ("%s-steady.svg" % device) + fig.savefig(target) + plt.close(fig) + return [target] + + +def library_runs(device_dir: Path) -> list[dict]: + rows = [] + for path in sorted(device_dir.glob("ui/*/run.json")): + run = json.loads(path.read_text()) + rows.append({"profile": "soft compositing" if run["soft"] == "1" else "GPU compositing", + "cpu": run.get("idle_cpu"), "pss": run.get("idle_pss"), "gpu": run.get("idle_gpu"), + "run": path.parent.name}) + return rows + + +def chart_burst(device: str, device_dir: Path, out: Path) -> list[Path]: + made = [] + for path in sorted(device_dir.glob("burst-*.jsonl")): + rows = [json.loads(x) for x in path.read_text().splitlines() if x.strip()] + if not rows: + continue + t0 = rows[0]["t"] + xs = [r["t"] - t0 for r in rows] + game = [] + steam = [] + for r in rows: + game.append(max((p[4] for p in r["top"] if p[1].lower().startswith("arcade")), default=0)) + steam.append(sum(p[4] for p in r["top"] if p[1].startswith("steamweb"))) + fig, ax = plt.subplots(figsize=(10, 4.5)) + ax.plot(xs, game, label="game KGSL (GPU memory)") + ax.plot(xs, steam, label="Steam web helper KGSL") + ax.plot(xs, [r["mem"]["Unevictable"] for r in rows], label="Unevictable (system)") + ax.plot(xs, [r["mem"]["MemAvailable"] for r in rows], label="MemAvailable") + ax.scatter([xs[-1]], [rows[-1]["mem"]["MemAvailable"]], marker="x", s=70, color="black", zorder=3, + label="last sample before lmkd killed the session") + ax.set_ylim(bottom=0) + ax.set_xlabel("seconds from pressing Continue") + ax.set_ylabel("MiB") + ax.grid(alpha=0.3) + ax.legend(fontsize=7) + ax.set_title("%s %s: what fills memory while the save loads (0.5 s samples)" % (device, path.stem[6:]), + fontsize=9) + fig.tight_layout() + target = out / ("%s-%s.svg" % (device, path.stem)) + fig.savefig(target) + plt.close(fig) + made.append(target) + return made + + +def more_tables(device: str, device_dir: Path) -> str: + out = [] + rows = steady_runs(device_dir) + if rows: + out.append("**%s: Bully in front, steady state** (one fresh session per run; game +80 s; MiB unless noted)\n" + % device) + out.append("| Run | Resident PSS | SwapPss | Renderer | KGSL | MemAvailable | Web helper CPU % | " + "Overlay open s (median, tool-bound) | Supervisor pass |") + out.append("| --- | --- | --- | --- | --- | --- | --- | --- | --- |") + for r in rows: + passes = "; ".join(", ".join(sorted(k for k in o if k != "ms")) for o in r["passes"]) or "none (stock)" + out.append("| %s | %s | %s | %s | %s | %s | %s | %s | %s |" % ( + r["run"], r["resident"], r["swapped"], r["renderer"], r["gpu"], r["avail"], r["cpu"], + r["overlay"], passes)) + out.append("") + rows = library_runs(device_dir) + if rows: + out.append("**%s: settled library (+90 to +110 s after start)**\n" % device) + out.append("| Run | Compositing | Web helper CPU % | Client tree PSS (MiB) | KGSL (MiB) |") + out.append("| --- | --- | --- | --- | --- |") + for r in rows: + out.append("| %s | %s | %s | %s | %s |" % (r["run"], r["profile"], r["cpu"], r["pss"], r["gpu"])) + out.append("") + return "\n".join(out) + + +def fmt(values: list[float]) -> str: + if not values: + return "n/a" + return "%.0f (%.0f-%.0f)" % (statistics.median(values), min(values), max(values)) + + +def tables(device: str, device_dir: Path) -> str: + """Markdown tables of every measurement in device_dir, medians with the full range.""" + out = [] + flags = {} + for events_path in sorted(device_dir.glob("**/flags-*/events.json")): + events = json.loads(events_path.read_text()) + rows = [json.loads(x) for x in (events_path.parent / "samples.jsonl").read_text().splitlines() if x.strip()] + if not rows: + continue + def med(key): + vals = [] + for item in rows: + if key == "gpu": + vals.append(sum((item.get("gpu") or {}).values()) / 1048576) + else: + vals.append(sum(r.get("Pss") or 0 for r in item["processes"] if r["role"] not in NOT_CLIENT + and (key == "total" or r["role"] == key)) / 1024) + return statistics.median(vals) + flags.setdefault(events["variant"], []).append({k: med(k) for k in ("total", "renderer", "browser", "gpu")}) + if flags: + out.append("**%s: library idle per web helper variant** (one fresh session per run, median of its samples; " + "cells are median (min-max) over runs, MiB)\n" % device) + out.append("| Variant | Runs | Client tree PSS | Renderer PSS | Browser PSS | KGSL GPU (not in PSS) |") + out.append("| --- | --- | --- | --- | --- | --- |") + for variant, runs_ in flags.items(): + out.append("| %s | %d | %s | %s | %s | %s |" % (variant, len(runs_), *[fmt([r[k] for r in runs_]) + for k in ("total", "renderer", "browser", "gpu")])) + out.append("") + trials = {} + for events_path in sorted(device_dir.glob("oom-*/events.json")): + events = json.loads(events_path.read_text()) + trials.setdefault(events.get("arm") or events["profile"], []).append(events) + if trials: + out.append("**%s: save load in app %s** (fresh session per trial; a trial counts once the load started)\n" + % (device, next(iter(trials.values()))[0]["app"])) + out.append("| Arm | Trials loaded | Survived 90 s | Killed (lmkd) | Seconds from Continue to kill |") + out.append("| --- | --- | --- | --- | --- |") + for arm, group in trials.items(): + valid = [e for e in group if e.get("loaded")] + killed = [e for e in valid if not e.get("survived")] + out.append("| %s | %d of %d | %d | %d | %s |" % ( + arm, len(valid), len(group), sum(1 for e in valid if e.get("survived")), len(killed), + fmt([e["died_after_s"] for e in killed if e.get("died_after_s") is not None]))) + out.append("") + for path in sorted(device_dir.glob("ablate-*.jsonl")): + items = [json.loads(x) for x in path.read_text().splitlines() if x.strip()] + label, before, rows = None, {}, [] + for item in items: + if "processes" in item: + total = sum(r.get("Pss") or 0 for r in item["processes"] if r["role"] not in NOT_CLIENT) / 1024 + renderer = sum(r.get("Pss") or 0 for r in item["processes"] if r["role"] == "renderer") / 1024 + gpu = sum((item.get("gpu") or {}).values()) / 1048576 + if label and label.startswith("before "): + before[label[7:]] = (total, renderer, gpu) + elif label and label.startswith("after "): + action, wait = label[6:].rsplit(" +", 1) + if action in before: + b = before[action] + rows.append((action, wait, total - b[0], renderer - b[1], gpu - b[2])) + elif "label" in item and "result" not in item: + label = item["label"] + if rows: + out.append("**%s %s: one intervention at a time** (change from the sample just before it, MiB; " + "negative = freed)\n" % (device, path.stem[7:])) + out.append("| Action | After | Client tree PSS | Renderer PSS | KGSL GPU |") + out.append("| --- | --- | --- | --- | --- |") + for action, wait, t, r, g in rows: + out.append("| %s | %s | %+.1f | %+.1f | %+.1f |" % (action, wait, t, r, g)) + out.append("") + return "\n".join(out) + + +def main(argv: list[str]) -> int: + if len(argv) != 2: + print(__doc__.strip(), file=sys.stderr) + return 2 + root, out = Path(argv[0]), Path(argv[1]) + out.mkdir(parents=True, exist_ok=True) + for device_dir in sorted(p for p in root.iterdir() if p.is_dir()): + rows = runs(device_dir) + made = chart_cycles(device_dir.name, rows, out) if rows else [] + made += chart_ablation(device_dir.name, device_dir, out) + made += chart_oom(device_dir.name, device_dir, out) + made += chart_flags(device_dir.name, device_dir, out) + made += chart_steady(device_dir.name, device_dir, out) + made += chart_burst(device_dir.name, device_dir, out) + for path in made: + print(path) + text = "\n".join(t for t in (tables(device_dir.name, device_dir), more_tables(device_dir.name, device_dir)) if t) + if text: + (out / ("%s-tables.md" % device_dir.name)).write_text(text + "\n") + print(out / ("%s-tables.md" % device_dir.name)) + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tools/tests/test_steam_lite_bench.py b/tools/tests/test_steam_lite_bench.py new file mode 100644 index 0000000000..2dec8bb417 --- /dev/null +++ b/tools/tests/test_steam_lite_bench.py @@ -0,0 +1,54 @@ +import importlib.util +from pathlib import Path +import unittest + +PATH = Path(__file__).resolve().parents[1] / 'steam_lite_bench.py' +SPEC = importlib.util.spec_from_file_location('steam_lite_bench', PATH) +bench = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(bench) + + +def sample(mono, mark, rows, available=1_000_000): + return {'mono': mono, 'clk_tck': 100, 'mark': mark, 'processes': rows, + 'memory': {'MemAvailable': available, 'psi_some_avg10': 0.5}, 'gpu': {}} + + +def row(pid, role, pss, ticks, swap=0): + return {'pid': pid, 'role': role, 'Pss': pss, 'SwapPss': swap, 'ticks': ticks} + + +class AccountingTest(unittest.TestCase): + def test_tree_sums_pss_and_leaves_out_the_supervisor(self): + s = sample(0, 'idle', [row(1, 'client', 100, 0), row(2, 'renderer', 300, 0, swap=20), + row(3, 'steam-lite', 9000, 0)]) + self.assertEqual(bench.tree(s)['pss'], 400) + self.assertEqual(bench.tree(s)['swap'], 20) + self.assertEqual(bench.tree(s, bench.WEB)['pss'], 300) + + def test_unreadable_pss_is_not_zero(self): + s = sample(0, 'idle', [row(1, 'client', None, 0)]) + self.assertIsNone(bench.tree(s)['pss']) + self.assertEqual(bench.tree(s)['unreadable'], 1) + + def test_cpu_is_percent_of_one_core_and_ignores_new_processes(self): + a = sample(10, 'game', [row(1, 'renderer', 1, 100)]) + b = sample(20, 'game', [row(1, 'renderer', 1, 150), row(2, 'renderer', 1, 999)]) + self.assertAlmostEqual(bench.cpu(a, b), 5.0) + + def test_summary_per_phase(self): + samples = [sample(t, 'idle', [row(1, 'renderer', 1024 * (300 + t), 10 * t)]) for t in range(4)] + samples += [sample(10 + t, 'game', [row(1, 'renderer', 1024 * 250, 100)], available=500_000) + for t in range(4)] + events = {'profile': '1', 'launch_s': 12.5, 'pacing': {'fpsOnScreen': 60, 'presentToPresentMs': + {'16': 90, '33': 9, '50': 1}}, 'overlay_open_s': [0.5, None, 0.7]} + out = bench.summarize(samples, events) + self.assertEqual(out['idle_pss_peak_mib'], 303.0) + self.assertEqual(out['game_pss_mib'], 250.0) + self.assertEqual(out['game_avail_min_mib'], 488.3) + self.assertEqual((out['frame_p95_ms'], out['frame_p99_ms']), (33, 33)) + self.assertEqual((out['overlay_open_median_s'], out['overlay_failures']), (0.6, 1)) + self.assertEqual(out['idle_web_cpu_pct'], 10.0) + + +if __name__ == '__main__': + unittest.main()