From 53c4a434a8587a531c288e98875ab76c180d376a Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Thu, 12 Nov 2026 19:07:10 +0100 Subject: feat(stats): show GPU usage on the dashboard chip xe reports no busy percentage, but its DRM fdinfo carries per-engine cycle counters. The 2s fdinfo pass that sums VRAM now also sums drm-cycles-rcs/ccs per client, and gpu.util is the busier of render (games) and compute (local LLMs) between two passes; a sum could pass 100% with both running. The GPU chip now matches the CPU chip: usage on a trend, temperature on the small line. VRAM and fan move to keystone's performance page. Without usage the temperature keeps the big value. Co-Authored-By: Claude Opus 5.5 --- deckit/modules/stats.py | 54 +++++++++++++++++++++++++++++++++++-------------- static/dashboard.js | 16 ++++++++++----- tests/test_stats.py | 18 ++++++++++++++--- 3 files changed, 65 insertions(+), 23 deletions(-) diff --git a/deckit/modules/stats.py b/deckit/modules/stats.py index 947b334..121572e 100644 --- a/deckit/modules/stats.py +++ b/deckit/modules/stats.py @@ -17,7 +17,7 @@ """System stats: CPU, memory, GPU (Intel xe), network, uptime, pending updates. The Arc driver exposes temperature and fan through hwmon and no VRAM capacity; -VRAM in use comes from the DRM fdinfo scan, as quickshell's perf panel does. +VRAM in use and engine busy cycles come from the DRM fdinfo scan, as quickshell's perf panel does. """ import json @@ -52,9 +52,15 @@ def _parse_bytes(value): return int(float(m[1]) * UNITS[m[2].upper()]) -def _intel_vram_used(root=Path("/proc")): - """Sum drm-resident-vram0 over xe clients, counting each client id once.""" - seen, total = set(), 0 +ENGINES = ("rcs", "ccs") # render (games) and compute (local LLMs) + + +def _intel_fdinfo(root=Path("/proc")): + """One pass over xe clients, each client id counted once. + + Returns (VRAM bytes, {engine: busy cycles summed}, {engine: GPU cycles}). + """ + seen, total, busy, clock = set(), 0, dict.fromkeys(ENGINES, 0), {} for fdinfo in root.glob("[0-9]*/fdinfo/*"): try: text = fdinfo.read_text() @@ -65,15 +71,32 @@ def _intel_vram_used(root=Path("/proc")): key, sep, value = line.partition(":") if sep and key.startswith("drm-"): fields[key] = value.strip() - client, resident = fields.get("drm-client-id"), fields.get("drm-resident-vram0") - if fields.get("drm-driver") != "xe" or not client or client in seen or not resident: - continue - m = re.match(r"(\d+)\s*([KMGT]?)(?:i?B)?", resident) - if not m: + client = fields.get("drm-client-id") + if fields.get("drm-driver") != "xe" or not client or client in seen: continue seen.add(client) - total += int(m[1]) * UNITS[m[2].upper()] - return total + for e in ENGINES: + if fields.get(f"drm-cycles-{e}", "").isdigit(): + busy[e] += int(fields[f"drm-cycles-{e}"]) + clock[e] = max(clock.get(e, 0), int(fields.get(f"drm-total-cycles-{e}", "0") or 0)) + m = re.match(r"(\d+)\s*([KMGT]?)(?:i?B)?", fields.get("drm-resident-vram0", "")) + if m: + total += int(m[1]) * UNITS[m[2].upper()] + return total, busy, clock + + +def _gpu_util(before, after): + """Busiest engine's percentage between two _intel_fdinfo samples, or None.""" + if not before: + return None + pcts = [] + for e in ENGINES: + dt = after[2].get(e, 0) - before[2].get(e, 0) + if dt > 0: + # ponytail: a client exiting mid-sample takes its cycles with it, + # so the delta can dip below zero; clamp rather than track clients. + pcts.append(min(100, max(0, 100 * (after[1][e] - before[1][e]) / dt))) + return round(max(pcts)) if pcts else None def _cpu_sensor(hw): @@ -182,7 +205,7 @@ def make(cfg: dict) -> Module: cpu_name = _cpu_name(_read("/proc/cpuinfo")) or "CPU" os_info = {**_os(_read("/etc/os-release")), "kernel": platform.release()} gpu_total = _parse_bytes(hw.get("gpu_mem_total", "")) if gpu else None - prev = {"cpu": None, "net": None, "t": None, "gpu_t": 0, "gpu_v": None} + prev = {"cpu": None, "net": None, "t": None, "gpu_t": 0, "gpu_v": None, "gpu_s": None} def cpu_percent(): fields = list(map(int, _read("/proc/stat").splitlines()[0].split()[1:])) @@ -213,17 +236,18 @@ def make(cfg: dict) -> Module: # The fdinfo scan touches every process, so keep it to once every 2 s. if time.time() - prev["gpu_t"] < 2: return prev["gpu_v"] + sample = _intel_fdinfo() temp = _read(gpu["hwmon"] / "temp2_input") fan = _read(gpu["hwmon"] / "fan1_input") v = { "name": gpu["name"], - "util": None, + "util": _gpu_util(prev["gpu_s"], sample), "temp": round(int(temp) / 1000) if temp else None, "fan": int(fan) if fan else None, - "memUsed": _intel_vram_used(), + "memUsed": sample[0], "memTotal": gpu_total, } - prev.update(gpu_t=time.time(), gpu_v=v) + prev.update(gpu_t=time.time(), gpu_v=v, gpu_s=sample) return v def state(): diff --git a/static/dashboard.js b/static/dashboard.js index 70fdd6c..adf690e 100644 --- a/static/dashboard.js +++ b/static/dashboard.js @@ -117,7 +117,7 @@ const SKELETON = `
${G.cpu}
${G.mem}
-
+
${G.gpu}
${G.net}
@@ -162,7 +162,7 @@ export default { $(".c-bat").hidden = $(".c-wifi").hidden = !on.has("tablet"); $(".dash-music").hidden = !on.has("music"); - const hist = { cpu: [], down: [], up: [] }; + const hist = { cpu: [], gpu: [], down: [], up: [] }; if (on.has("stats")) poll("stats", 1, (st) => { $(".logo").textContent = DISTRO[st.os?.id] ?? G.tux; $(".os").textContent = st.os?.name ?? ""; @@ -182,9 +182,15 @@ export default { $(".c-gpu").hidden = !g; if (g) { $(".c-gpu .n").textContent = g.name ?? ""; - chip(".c-gpu", g.temp != null ? `${g.temp}°C` : "—", - g.memTotal ? `${gib(g.memUsed)} / ${Math.round(g.memTotal / 2 ** 30)} GiB` : `${gib(g.memUsed)} GiB`, - g.memTotal ? (100 * g.memUsed) / g.memTotal : 0, g.temp >= 85); + // Like the CPU chip: usage on a trend, temperature beside it. VRAM + // and fan are on keystone's performance page. Without usage (an + // older kernel) the temperature takes the big value. + if (g.util != null) { + push(hist.gpu, g.util); + $(".c-gpu path").setAttribute("d", area(hist.gpu, Math.max(25, ...hist.gpu))); + } + const t = g.temp != null ? `${g.temp}°C` : "—"; + chip(".c-gpu", g.util != null ? `${g.util}%` : t, g.util != null ? t : "", null, g.temp >= 85); } const max = Math.max(1, ...hist.down, ...hist.up); const [down, up] = $(".c-net").querySelectorAll("path"); diff --git a/tests/test_stats.py b/tests/test_stats.py index f45a0cd..0a16cbe 100644 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -75,8 +75,10 @@ class Intel(unittest.TestCase): import tempfile root = Path(tempfile.mkdtemp()) rows = [ - ("100", "3", "drm-driver:\txe\ndrm-client-id:\t7\ndrm-resident-vram0:\t1024 MiB\n"), - ("100", "4", "drm-driver:\txe\ndrm-client-id:\t7\ndrm-resident-vram0:\t1024 MiB\n"), + ("100", "3", "drm-driver:\txe\ndrm-client-id:\t7\ndrm-resident-vram0:\t1024 MiB\n" + "drm-cycles-rcs:\t30\ndrm-total-cycles-rcs:\t1000\n"), + ("100", "4", "drm-driver:\txe\ndrm-client-id:\t7\ndrm-resident-vram0:\t1024 MiB\n" + "drm-cycles-rcs:\t30\ndrm-total-cycles-rcs:\t1000\n"), ("200", "5", "drm-driver:\txe\ndrm-client-id:\t8\ndrm-resident-vram0:\t512 MiB\n"), ("300", "6", "drm-driver:\ti915\ndrm-client-id:\t9\ndrm-resident-vram0:\t999 MiB\n"), ] @@ -84,7 +86,17 @@ class Intel(unittest.TestCase): d = root / pid / "fdinfo" d.mkdir(parents=True, exist_ok=True) (d / fd).write_text(body) - self.assertEqual(stats._intel_vram_used(root), (1024 + 512) * 1024 ** 2) + vram, busy, clock = stats._intel_fdinfo(root) + self.assertEqual(vram, (1024 + 512) * 1024 ** 2) + self.assertEqual((busy["rcs"], clock["rcs"]), (30, 1000)) + + def test_gpu_util_is_busiest_engine(self): + before = (0, {"rcs": 0, "ccs": 0}, {"rcs": 1000, "ccs": 1000}) + after = (0, {"rcs": 200, "ccs": 900}, {"rcs": 2000, "ccs": 2000}) + self.assertEqual(stats._gpu_util(before, after), 90) + self.assertIsNone(stats._gpu_util(None, after)) + exited = (0, {"rcs": 0, "ccs": 0}, {"rcs": 3000, "ccs": 3000}) + self.assertEqual(stats._gpu_util(after, exited), 0) if __name__ == "__main__": -- cgit v1.2.3