aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--deckit/modules/stats.py34
-rw-r--r--static/modules/stats.js6
-rw-r--r--tests/test_stats.py16
3 files changed, 52 insertions, 4 deletions
diff --git a/deckit/modules/stats.py b/deckit/modules/stats.py
index c190ec8..a764246 100644
--- a/deckit/modules/stats.py
+++ b/deckit/modules/stats.py
@@ -21,7 +21,9 @@ VRAM in use comes from the DRM fdinfo scan, as quickshell's perf panel does.
"""
import re
+import shlex
import socket
+import subprocess
import time
from pathlib import Path
@@ -89,12 +91,40 @@ def _battery(hw):
return None
+def _cpu_name(cpuinfo):
+ """'AMD Ryzen 7 9700X 8-Core Processor' -> 'AMD Ryzen 7 9700X'."""
+ for line in cpuinfo.splitlines():
+ if line.startswith("model name"):
+ name = re.sub(r"\((R|TM)\)|\bCPU\b|@.*|\d+-Core|Processor", "", line.partition(":")[2])
+ return " ".join(name.split()) or None
+ return None
+
+
+def _gpu_name(lspci):
+ """lspci -mm's line -> 'Intel Arc B580': the vendor's first word, then the
+ marketing name in brackets, else the whole device name."""
+ try:
+ f = shlex.split(lspci)
+ except ValueError:
+ return None
+ if len(f) < 4:
+ return None
+ m = re.search(r"\[(.+?)\]", f[3])
+ return f"{f[2].split()[0]} {m[1] if m else f[3]}"
+
+
def _gpu(hw):
if hw.get("gpu", "auto") == "none":
return None
for name in Path("/sys/class/hwmon").glob("hwmon*/name"):
if _read(name) == "xe":
- return {"hwmon": name.parent, "name": "Intel Arc"}
+ slot = (name.parent / "device").resolve().name
+ try:
+ out = subprocess.run(["lspci", "-mm", "-s", slot], capture_output=True,
+ text=True, timeout=2).stdout
+ except (OSError, subprocess.TimeoutExpired):
+ out = ""
+ return {"hwmon": name.parent, "name": _gpu_name(out) or "GPU"}
return None
@@ -113,6 +143,7 @@ def make(cfg: dict) -> Module:
cpu_sensor = _cpu_sensor(hw)
battery = _battery(hw)
gpu = _gpu(hw)
+ cpu_name = _cpu_name(_read("/proc/cpuinfo")) or "CPU"
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}
@@ -167,6 +198,7 @@ def make(cfg: dict) -> Module:
"host": socket.gethostname(),
"uptime": int(float(_read("/proc/uptime", "0").split()[0])),
"cpu": cpu_percent(),
+ "cpuName": cpu_name,
"cpuTemp": round(int(t) / 1000) if t else None,
"memUsed": mem["MemTotal"] - mem["MemAvailable"],
"memTotal": mem["MemTotal"],
diff --git a/static/modules/stats.js b/static/modules/stats.js
index c76e889..e280e47 100644
--- a/static/modules/stats.js
+++ b/static/modules/stats.js
@@ -52,11 +52,11 @@ export default {
box.textContent = "";
const g = st.gpu;
box.append(
- tile("CPU", `${st.cpu}%`, st.cpuTemp != null ? `${st.cpuTemp}°C` : "—"),
+ tile(st.cpuName ?? "CPU", `${st.cpu}%`, st.cpuTemp != null ? `${st.cpuTemp}°C` : "—"),
tile("Memory", gib(st.memUsed), `/ ${gib(st.memTotal)} GiB`),
- tile("GPU", g?.temp != null ? `${g.temp}°` : "—",
+ tile(g?.name ?? "GPU", g?.temp != null ? `${g.temp}°` : "—",
g ? `${bytes(g.memUsed)} / ${bytes(g.memTotal)} GiB` : "no GPU"),
- tile("Network", `↓${rate(st.net.down)}`, `↑${rate(st.net.up)}`),
+ tile("Network", `↓${rate(st.net.down)} ↑${rate(st.net.up)}`),
);
const foot = document.createElement("div");
foot.className = "mfoot";
diff --git a/tests/test_stats.py b/tests/test_stats.py
index 1a28ae8..f32bfac 100644
--- a/tests/test_stats.py
+++ b/tests/test_stats.py
@@ -37,6 +37,22 @@ class Stats(unittest.TestCase):
self.assertIn("stats", names)
+class Names(unittest.TestCase):
+ def test_cpu_name(self):
+ self.assertEqual(stats._cpu_name("model name\t: AMD Ryzen 7 9700X 8-Core Processor\n"),
+ "AMD Ryzen 7 9700X")
+ self.assertEqual(stats._cpu_name("model name : Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz"),
+ "Intel Core i7-8550U")
+ self.assertIsNone(stats._cpu_name(""))
+
+ def test_gpu_name(self):
+ line = ('03:00.0 "VGA compatible controller" "Intel Corporation" '
+ '"Battlemage G21 [Arc B580]" -p00 "Vendor" "Device 4215"')
+ self.assertEqual(stats._gpu_name(line), "Intel Arc B580")
+ self.assertEqual(stats._gpu_name('03:00.0 "VGA" "Intel Corporation" "DG2"'), "Intel DG2")
+ self.assertIsNone(stats._gpu_name(""))
+
+
class Intel(unittest.TestCase):
def test_parse_bytes(self):
self.assertEqual(stats._parse_bytes("12G"), 12 * 1024 ** 3)